Skip to content

Commit

Permalink
fix!: refactor Baggage to remove Noop* (#800)
Browse files Browse the repository at this point in the history
* fix!: refactor Baggage to remove Noop*

* Update api/lib/opentelemetry/baggage.rb

Co-authored-by: Andrew Hayworth <[email protected]>

Co-authored-by: Andrew Hayworth <[email protected]>
  • Loading branch information
fbogsany and ahayworth authored Jun 8, 2021
1 parent 3177da3 commit a9bcbda
Show file tree
Hide file tree
Showing 15 changed files with 125 additions and 273 deletions.
9 changes: 1 addition & 8 deletions api/lib/opentelemetry.rb
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ module OpenTelemetry
@mutex = Mutex.new
@tracer_provider = Internal::ProxyTracerProvider.new

attr_writer :propagation, :baggage, :logger, :error_handler
attr_writer :propagation, :logger, :error_handler

# @return [Object, Logger] configured Logger or a default STDOUT Logger.
def logger
Expand Down Expand Up @@ -65,13 +65,6 @@ def tracer_provider
@mutex.synchronize { @tracer_provider }
end

# @return [Object, Baggage::NoopManager] registered
# baggage manager or a default no-op implementation of the
# manager.
def baggage
@baggage ||= Baggage::NoopManager.new
end

# @return [Context::Propagation::Propagator] a propagator instance
def propagation
@propagation ||= Context::Propagation::NoopTextMapPropagator.new
Expand Down
96 changes: 93 additions & 3 deletions api/lib/opentelemetry/baggage.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,103 @@
require 'opentelemetry/baggage/propagation'
require 'opentelemetry/baggage/builder'
require 'opentelemetry/baggage/entry'
require 'opentelemetry/baggage/manager'
require 'opentelemetry/baggage/noop_builder'
require 'opentelemetry/baggage/noop_manager'

module OpenTelemetry
# The Baggage module provides functionality to record and propagate
# baggage in a distributed trace
module Baggage
extend self

BAGGAGE_KEY = OpenTelemetry::Baggage::Propagation::ContextKeys.baggage_key
EMPTY_BAGGAGE = {}.freeze
private_constant(:BAGGAGE_KEY, :EMPTY_BAGGAGE)

# 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.
#
# @param [optional Context] context The context to update with with new
# modified baggage. Defaults to +Context.current+
# @return [Context]
def build(context: Context.current)
builder = Builder.new(baggage_for(context).dup)
yield builder
context.set_value(BAGGAGE_KEY, builder.entries)
end

# Returns a new context with empty baggage
#
# @param [optional Context] context Context to clear baggage from. Defaults
# to +Context.current+
# @return [Context]
def clear(context: Context.current)
context.set_value(BAGGAGE_KEY, EMPTY_BAGGAGE)
end

# Returns the corresponding baggage.entry (or nil) for key
#
# @param [String] key The lookup key
# @param [optional Context] context The context from which to retrieve
# the key.
# Defaults to +Context.current+
# @return [String]
def value(key, context: Context.current)
baggage_for(context)[key]&.value
end

# Returns the baggage
#
# @param [optional Context] context The context from which to retrieve
# the baggage.
# Defaults to +Context.current+
# @return [Hash]
def values(context: Context.current)
baggage_for(context).transform_values(&:value)
end

# @api private
def raw_entries(context: Context.current)
baggage_for(context).dup.freeze
end

# Returns a new context with new key-value pair
#
# @param [String] key The key to store this value under
# @param [String] value String value to be stored under key
# @param [optional String] metadata This is here to store properties
# received from other W3C Baggage impelmentations but is not exposed in
# OpenTelemetry. This is condsidered private API and not for use by
# end-users.
# @param [optional Context] context The context to update with new
# value. Defaults to +Context.current+
# @return [Context]
def set_value(key, value, metadata: nil, context: Context.current)
new_baggage = baggage_for(context).dup
new_baggage[key] = Entry.new(value, metadata)
context.set_value(BAGGAGE_KEY, new_baggage)
end

# Returns a new context with value at key removed
#
# @param [String] key The key to remove
# @param [optional Context] context The context to remove baggage
# from. Defaults to +Context.current+
# @return [Context]
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

private

def baggage_for(context)
context.value(BAGGAGE_KEY) || EMPTY_BAGGAGE
end
end
end
104 changes: 0 additions & 104 deletions api/lib/opentelemetry/baggage/manager.rb

This file was deleted.

18 changes: 0 additions & 18 deletions api/lib/opentelemetry/baggage/noop_builder.rb

This file was deleted.

45 changes: 0 additions & 45 deletions api/lib/opentelemetry/baggage/noop_manager.rb

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ class TextMapPropagator
# will be used to write context into the carrier, otherwise the default
# text map setter will be used.
def inject(carrier, context: Context.current, setter: Context::Propagation.text_map_setter)
baggage = OpenTelemetry.baggage.raw_entries(context: context)
baggage = OpenTelemetry::Baggage.raw_entries(context: context)

return if baggage.nil? || baggage.empty?

Expand All @@ -55,7 +55,7 @@ def extract(carrier, context: Context.current, getter: Context::Propagation.text

entries = header.gsub(/\s/, '').split(',')

OpenTelemetry.baggage.build(context: context) do |builder|
OpenTelemetry::Baggage.build(context: context) do |builder|
entries.each do |entry|
# Note metadata is currently unused in OpenTelemetry, but is part
# the W3C spec where it's referred to as properties. We preserve
Expand Down
Loading

0 comments on commit a9bcbda

Please sign in to comment.