Module: OpenTelemetry::Baggage

Extended by:
Baggage
Included in:
Baggage
Defined in:
lib/opentelemetry/baggage.rb,
lib/opentelemetry/baggage/entry.rb,
lib/opentelemetry/baggage/builder.rb,
lib/opentelemetry/baggage/propagation.rb,
lib/opentelemetry/baggage/propagation/context_keys.rb,
lib/opentelemetry/baggage/propagation/text_map_propagator.rb

Overview

OpenTelemetry Baggage Implementation

Defined Under Namespace

Modules: Propagation Classes: Builder, Entry

Instance Method Summary collapse

Instance Method Details

#build(context: Context.current) {|builder| ... } ⇒ Context

Used to chain modifications to baggage. The result is a context with an updated baggage. If only a single modification is being made to baggage, use the other methods on Baggage, if multiple modifications are being made, use this one.

Parameters:

  • context (optional Context) (defaults to: Context.current)

    The context to update with with new modified baggage. Defaults to Context.current

Yields:

  • (builder)

Returns:



30
31
32
33
34
# File 'lib/opentelemetry/baggage.rb', line 30

def build(context: Context.current)
  builder = Builder.new(baggage_for(context).dup)
  yield builder
  context.set_value(BAGGAGE_KEY, builder.entries)
end

#clear(context: Context.current) ⇒ Context

Returns a new context with empty baggage

Parameters:

  • context (optional Context) (defaults to: Context.current)

    Context to clear baggage from. Defaults to Context.current

Returns:



41
42
43
# File 'lib/opentelemetry/baggage.rb', line 41

def clear(context: Context.current)
  context.set_value(BAGGAGE_KEY, EMPTY_BAGGAGE)
end

#raw_entries(context: Context.current) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



67
68
69
# File 'lib/opentelemetry/baggage.rb', line 67

def raw_entries(context: Context.current)
  baggage_for(context).dup.freeze
end

#remove_value(key, context: Context.current) ⇒ Context

Returns a new context with value at key removed

Parameters:

  • key (String)

    The key to remove

  • context (optional Context) (defaults to: Context.current)

    The context to remove baggage from. Defaults to Context.current

Returns:



94
95
96
97
98
99
100
101
# File 'lib/opentelemetry/baggage.rb', line 94

def remove_value(key, context: Context.current)
  baggage = baggage_for(context)
  return context unless baggage.key?(key)

  new_baggage = baggage.dup
  new_baggage.delete(key)
  context.set_value(BAGGAGE_KEY, new_baggage)
end

#set_value(key, value, metadata: nil, context: Context.current) ⇒ Context

Returns a new context with new key-value pair

Parameters:

  • key (String)

    The key to store this value under

  • value (String)

    String value to be stored under key

  • metadata (optional String) (defaults to: nil)

    This is here to store properties received from other W3C Baggage implementations but is not exposed in OpenTelemetry. This is condsidered private API and not for use by end-users.

  • context (optional Context) (defaults to: Context.current)

    The context to update with new value. Defaults to Context.current

Returns:



82
83
84
85
86
# File 'lib/opentelemetry/baggage.rb', line 82

def set_value(key, value, metadata: nil, context: Context.current)
  new_baggage = baggage_for(context).dup
  new_baggage[key] = Entry.new(value, )
  context.set_value(BAGGAGE_KEY, new_baggage)
end

#value(key, context: Context.current) ⇒ String

Returns the corresponding baggage.entry (or nil) for key

Parameters:

  • key (String)

    The lookup key

  • context (optional Context) (defaults to: Context.current)

    The context from which to retrieve the key. Defaults to Context.current

Returns:

  • (String)


52
53
54
# File 'lib/opentelemetry/baggage.rb', line 52

def value(key, context: Context.current)
  baggage_for(context)[key]&.value
end

#values(context: Context.current) ⇒ Hash

Returns the baggage

Parameters:

  • context (optional Context) (defaults to: Context.current)

    The context from which to retrieve the baggage. Defaults to Context.current

Returns:

  • (Hash)


62
63
64
# File 'lib/opentelemetry/baggage.rb', line 62

def values(context: Context.current)
  baggage_for(context).transform_values(&:value)
end