Method: Datadog::OpenTelemetry::API::Context#set_values

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

#set_values(values) ⇒ Context

Returns a new Context with the current context’s entries merged with the

new entries

Parameters:

  • values (Hash)

    The values to be merged with the current context’s entries.

  • value (Object)

    Object to be stored under key

Returns:



71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
# File 'lib/datadog/opentelemetry/api/context.rb', line 71

def set_values(values)
  if (current_span = values[CURRENT_SPAN_KEY]) && current_span.datadog_trace
    # Because `#set_value` returns new context, we have to create
    # a new copy of the active trace to ensure there's no conflict with
    # other users of the active trace.
    # It effectively becomes an internal trace propagation.
    trace = Datadog::OpenTelemetry::Trace.start_trace_copy(
      current_span.datadog_trace,
      parent_span: current_span.datadog_span
    )
  end

  existing_values = @trace && @trace.otel_values || {}
  existing_baggage = @trace && @trace.baggage || {}

  # Retrieve the baggage removal sentinel and remove it from the values hash
  existing_baggage.delete(values[BAGGAGE_REMOVE_KEY]) if values.key?(BAGGAGE_REMOVE_KEY)

  # If the values hash contains a BAGGAGE_KEY, merge its contents with existing baggage
  # Otherwise, keep the existing baggage unchanged
  new_baggage = if values.key?(::OpenTelemetry::Baggage.const_get(:BAGGAGE_KEY))
                  existing_baggage.merge(values[::OpenTelemetry::Baggage.const_get(:BAGGAGE_KEY)])
                else
                  existing_baggage
                end

  ::OpenTelemetry::Context.new(existing_values.merge(values), trace: trace, baggage: new_baggage)
end