Module: Datadog::OpenTelemetry::API::Context
- Defined in:
- lib/datadog/opentelemetry/api/context.rb
Overview
The OpenTelemetry Context contains a key-value store that can be attached to a trace.
It loosely matches our ‘TraceOperations#tags`, except for the following:
-
Context can store arbitrary objects as values. One example is for the key ‘Context::Key.new(’current-span’)‘, which is associated with a `Span` object. In contrast, `TraceOperations#tags` only stores string values.
-
Context is how spans know who their parent span is. The parenting operation happens on every span created. Parenting is not directly tied to the active Fiber or Thread.
-
Context is immutable: changing a value creates a copy of Context.
-
Context is not bound to a specific trace: it can be reused an arbitrary number of times.
Defined Under Namespace
Modules: SingletonClass
Class Method Summary collapse
Instance Method Summary collapse
-
#ensure_trace ⇒ Object
Because Context can be reused, we have to make sure we have a valid ‘TraceOperation` on every invocation.
- #initialize(entries, trace: nil) ⇒ Object
-
#set_value(key, value) ⇒ Context
Returns a new Context where entries contains the newly added key and value.
-
#set_values(values) ⇒ Context
Returns a new Context with the current context’s entries merged with the new entries.
-
#trace ⇒ Object
The Datadog TraceOperation associated with this Context.
-
#value(key) ⇒ Object
(also: #[])
Returns the corresponding value (or nil) for key.
Class Method Details
.prepended(base) ⇒ Object
158 159 160 |
# File 'lib/datadog/opentelemetry/api/context.rb', line 158 def self.prepended(base) base.singleton_class.prepend(SingletonClass) end |
Instance Method Details
#ensure_trace ⇒ Object
Because Context can be reused, we have to make sure we have a valid ‘TraceOperation` on every invocation.
32 33 34 35 36 37 38 |
# File 'lib/datadog/opentelemetry/api/context.rb', line 32 def ensure_trace return nil unless @trace # The Context can be reused after the root span has finished. @trace.send(:reset) if @trace.finished? @trace end |
#initialize(entries, trace: nil) ⇒ Object
24 25 26 27 28 |
# File 'lib/datadog/opentelemetry/api/context.rb', line 24 def initialize(entries, trace: nil) @trace = trace || ::Datadog::Tracing.send(:tracer).send(:start_trace) @trace.otel_values.merge!(entries) if entries @trace.otel_context ||= self end |
#set_value(key, value) ⇒ Context
Returns a new Context where entries contains the newly added key and value
57 58 59 |
# File 'lib/datadog/opentelemetry/api/context.rb', line 57 def set_value(key, value) set_values(key => value) end |
#set_values(values) ⇒ Context
Returns a new Context with the current context’s entries merged with the
new entries
68 69 70 71 72 73 74 75 76 77 |
# File 'lib/datadog/opentelemetry/api/context.rb', line 68 def set_values(values) trace = nil if (current_span = values[CURRENT_SPAN_KEY]) trace = current_span.datadog_trace end existing_values = @trace && @trace.otel_values || {} ::OpenTelemetry::Context.new(existing_values.merge(values), trace: trace) end |
#trace ⇒ Object
The Datadog TraceOperation associated with this Datadog::OpenTelemetry::API::Context.
80 81 82 |
# File 'lib/datadog/opentelemetry/api/context.rb', line 80 def trace @trace end |
#value(key) ⇒ Object Also known as: []
Returns the corresponding value (or nil) for key
44 45 46 47 48 |
# File 'lib/datadog/opentelemetry/api/context.rb', line 44 def value(key) return nil unless @trace @trace.otel_value(key) end |