Module: Datadog::OpenTelemetry::API::Context::SingletonClass

Defined in:
lib/datadog/opentelemetry/api/context.rb

Overview

Singleton class methods for Datadog::OpenTelemetry::API::Context

Instance Method Summary collapse

Instance Method Details

#attach(context) ⇒ Object

Associates a Context with the caller’s current Fiber. Every call to this operation should be paired with a corresponding call to detach.

Returns a token to be used with the matching call to detach

Parameters:

  • context (Context)

    The new context

Returns:

  • (Object)

    A token to be used when detaching



124
125
126
127
128
129
130
# File 'lib/datadog/opentelemetry/api/context.rb', line 124

def attach(context)
  previous_trace = Tracing.active_trace
  continue_trace!(context)

  stack.push(previous_trace && previous_trace.otel_context || ::OpenTelemetry::Context::ROOT)
  stack.size
end

#clearObject

Part of the OpenTelemetry public API for Datadog::OpenTelemetry::API::Context.



156
157
158
159
160
# File 'lib/datadog/opentelemetry/api/context.rb', line 156

def clear
  super
  tracer = Tracing.send(:tracer)
  tracer.send(:call_context).activate!(nil)
end

#currentContext

Returns current context, which is never nil

Returns:



110
111
112
113
114
115
# File 'lib/datadog/opentelemetry/api/context.rb', line 110

def current
  trace = Tracing.active_trace
  return ::OpenTelemetry::Context::ROOT unless trace

  trace.otel_context ||= ::OpenTelemetry::Context.from_trace(trace)
end

#detach(token) ⇒ Boolean

Restores the previous Context associated with the current Fiber. The supplied token is used to check if the call to detach is balanced with a corresponding attach call. A warning is logged if the calls are unbalanced.

Parameters:

  • token (Object)

    The token provided by the matching call to attach

Returns:

  • (Boolean)

    True if the calls matched, false otherwise



139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
# File 'lib/datadog/opentelemetry/api/context.rb', line 139

def detach(token)
  s = stack
  calls_matched = (token == s.size)
  unless calls_matched
    ::OpenTelemetry.handle_error(
      exception: ::OpenTelemetry::Context::DetachError.new(
        'calls to detach should match corresponding calls to attach.'
      )
    )
  end

  previous_context = s.pop
  continue_trace!(previous_context)
  calls_matched
end

#from_trace(trace) ⇒ Object

Creates a new Datadog::OpenTelemetry::API::Context associated with a TraceOperation.



163
164
165
# File 'lib/datadog/opentelemetry/api/context.rb', line 163

def from_trace(trace)
  new({}, trace: trace)
end