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



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

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.



142
143
144
145
146
# File 'lib/datadog/opentelemetry/api/context.rb', line 142

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

#currentContext

Returns current context, which is never nil

Returns:



96
97
98
99
100
101
# File 'lib/datadog/opentelemetry/api/context.rb', line 96

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



125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
# File 'lib/datadog/opentelemetry/api/context.rb', line 125

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.



149
150
151
# File 'lib/datadog/opentelemetry/api/context.rb', line 149

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