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
-
#attach(context) ⇒ Object
Associates a Context with the caller’s current Fiber.
-
#clear ⇒ Object
Part of the OpenTelemetry public API for Datadog::OpenTelemetry::API::Context.
-
#current ⇒ Context
Returns current context, which is never nil.
-
#detach(token) ⇒ Boolean
Restores the previous Context associated with the current Fiber.
-
#from_trace(trace) ⇒ Object
Creates a new Datadog::OpenTelemetry::API::Context associated with a TraceOperation.
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
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 |
#clear ⇒ Object
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 |
#current ⇒ Context
Returns current context, which is never nil
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.
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 |