opentelemetry-api 0.16.0 → 0.17.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (37) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +17 -0
  3. data/lib/opentelemetry.rb +23 -23
  4. data/lib/opentelemetry/baggage.rb +4 -1
  5. data/lib/opentelemetry/baggage/builder.rb +30 -4
  6. data/lib/opentelemetry/baggage/entry.rb +20 -0
  7. data/lib/opentelemetry/baggage/manager.rb +76 -13
  8. data/lib/opentelemetry/baggage/noop_builder.rb +18 -0
  9. data/lib/opentelemetry/baggage/noop_manager.rb +45 -0
  10. data/lib/opentelemetry/baggage/propagation.rb +9 -18
  11. data/lib/opentelemetry/baggage/propagation/text_map_propagator.rb +109 -0
  12. data/lib/opentelemetry/context/propagation.rb +35 -5
  13. data/lib/opentelemetry/context/propagation/composite_text_map_propagator.rb +105 -0
  14. data/lib/opentelemetry/context/propagation/noop_text_map_propagator.rb +51 -0
  15. data/lib/opentelemetry/context/propagation/{propagator.rb → text_map_propagator.rb} +23 -16
  16. data/lib/opentelemetry/internal.rb +17 -0
  17. data/lib/opentelemetry/internal/proxy_tracer.rb +38 -0
  18. data/lib/opentelemetry/internal/proxy_tracer_provider.rb +59 -0
  19. data/lib/opentelemetry/trace/propagation/trace_context.rb +7 -18
  20. data/lib/opentelemetry/trace/propagation/trace_context/text_map_propagator.rb +73 -0
  21. data/lib/opentelemetry/version.rb +1 -1
  22. metadata +16 -21
  23. data/lib/opentelemetry/baggage/propagation/text_map_extractor.rb +0 -57
  24. data/lib/opentelemetry/baggage/propagation/text_map_injector.rb +0 -52
  25. data/lib/opentelemetry/context/propagation/composite_propagator.rb +0 -72
  26. data/lib/opentelemetry/context/propagation/noop_extractor.rb +0 -26
  27. data/lib/opentelemetry/context/propagation/noop_injector.rb +0 -26
  28. data/lib/opentelemetry/instrumentation.rb +0 -15
  29. data/lib/opentelemetry/instrumentation/base.rb +0 -307
  30. data/lib/opentelemetry/instrumentation/registry.rb +0 -86
  31. data/lib/opentelemetry/metrics.rb +0 -16
  32. data/lib/opentelemetry/metrics/handles.rb +0 -44
  33. data/lib/opentelemetry/metrics/instruments.rb +0 -105
  34. data/lib/opentelemetry/metrics/meter.rb +0 -72
  35. data/lib/opentelemetry/metrics/meter_provider.rb +0 -22
  36. data/lib/opentelemetry/trace/propagation/trace_context/text_map_extractor.rb +0 -52
  37. data/lib/opentelemetry/trace/propagation/trace_context/text_map_injector.rb +0 -49
@@ -4,18 +4,48 @@
4
4
  #
5
5
  # SPDX-License-Identifier: Apache-2.0
6
6
 
7
- require 'opentelemetry/context/propagation/composite_propagator'
8
- require 'opentelemetry/context/propagation/noop_extractor'
9
- require 'opentelemetry/context/propagation/noop_injector'
10
- require 'opentelemetry/context/propagation/propagator'
7
+ require 'opentelemetry/context/propagation/composite_text_map_propagator'
8
+ require 'opentelemetry/context/propagation/noop_text_map_propagator'
9
+ require 'opentelemetry/context/propagation/rack_env_getter'
11
10
  require 'opentelemetry/context/propagation/text_map_getter'
11
+ require 'opentelemetry/context/propagation/text_map_propagator'
12
12
  require 'opentelemetry/context/propagation/text_map_setter'
13
- require 'opentelemetry/context/propagation/rack_env_getter'
14
13
 
15
14
  module OpenTelemetry
16
15
  class Context
17
16
  # The propagation module contains APIs and utilities to interact with context
18
17
  # and propagate across process boundaries.
18
+ #
19
+ # The API implicitly defines 3 interfaces: TextMapPropagator, TextMapInjector
20
+ # and TextMapExtractor. Concrete implementations of TextMapPropagator are
21
+ # provided. Custom text map propagators can leverage these implementations
22
+ # or simply implement the expected interface. The interfaces are described
23
+ # below.
24
+ #
25
+ # The TextMapPropagator interface:
26
+ #
27
+ # inject(carrier, context:, setter:)
28
+ # extract(carrier, context:, getter:) -> Context
29
+ # fields -> Array<String>
30
+ #
31
+ # The TextMapInjector interface:
32
+ #
33
+ # inject(carrier, context:, setter:)
34
+ # fields -> Array<String>
35
+ #
36
+ # The TextMapExtractor interface:
37
+ #
38
+ # extract(carrier, context:, getter:) -> Context
39
+ #
40
+ # The API provides 3 TextMapPropagator implementations:
41
+ # - A default NoopTextMapPropagator that implements +inject+ and +extract+
42
+ # methods as no-ops. Its +fields+ method returns an empty list.
43
+ # - A TextMapPropagator that composes an Injector and an Extractor. Its
44
+ # +fields+ method delegates to the provided Injector.
45
+ # - A CompositeTextMapPropagator that wraps either a list of text map
46
+ # propagators or a list of Injectors and a list of Extractors. Its
47
+ # +fields+ method returns the union of fields returned by the Injectors
48
+ # it wraps.
19
49
  module Propagation
20
50
  extend self
21
51
 
@@ -0,0 +1,105 @@
1
+ # frozen_string_literal: true
2
+
3
+ # Copyright The OpenTelemetry Authors
4
+ #
5
+ # SPDX-License-Identifier: Apache-2.0
6
+
7
+ module OpenTelemetry
8
+ class Context
9
+ module Propagation
10
+ # A composite text map propagator either composes a list of injectors and a
11
+ # list of extractors, or wraps a list of propagators, into a single interface
12
+ # exposing inject and extract methods. Injection and extraction will preserve
13
+ # the order of the injectors and extractors (or propagators) passed in during
14
+ # initialization.
15
+ class CompositeTextMapPropagator
16
+ class << self
17
+ private :new # rubocop:disable Style/AccessModifierDeclarations
18
+
19
+ # Returns a Propagator that extracts using the provided extractors
20
+ # and injectors.
21
+ #
22
+ # @param [Array<#inject, #fields>] injectors An array of text map injectors
23
+ # @param [Array<#extract>] extractors An array of text map extractors
24
+ def compose(injectors:, extractors:)
25
+ raise ArgumentError, 'injectors and extractors must both be non-nil arrays' unless injectors.is_a?(Array) && extractors.is_a?(Array)
26
+
27
+ new(injectors: injectors, extractors: extractors)
28
+ end
29
+
30
+ # Returns a Propagator that extracts using the provided propagators.
31
+ #
32
+ # @param [Array<#inject, #extract, #fields>] propagators An array of
33
+ # text map propagators
34
+ def compose_propagators(propagators)
35
+ raise ArgumentError, 'propagators must be a non-nil array' unless propagators.is_a?(Array)
36
+ return NoopTextMapPropagator.new if propagators.empty?
37
+ return propagators.first if propagators.size == 1
38
+
39
+ new(propagators: propagators)
40
+ end
41
+ end
42
+
43
+ # @api private
44
+ def initialize(injectors: nil, extractors: nil, propagators: nil)
45
+ @injectors = injectors
46
+ @extractors = extractors
47
+ @propagators = propagators
48
+ end
49
+
50
+ # Runs injectors or propagators in order. If an injection fails
51
+ # a warning will be logged and remaining injectors will be executed.
52
+ #
53
+ # @param [Object] carrier A mutable carrier to inject context into.
54
+ # @param [optional Context] context Context to be injected into carrier. Defaults
55
+ # to +Context.current+.
56
+ # @param [optional Setter] setter If the optional setter is provided, it
57
+ # will be used to write context into the carrier, otherwise the default
58
+ # setter will be used.
59
+ def inject(carrier, context: Context.current, setter: Context::Propagation.text_map_setter)
60
+ injectors = @injectors || @propagators
61
+ injectors.each do |injector|
62
+ injector.inject(carrier, context: context, setter: setter)
63
+ rescue StandardError => e
64
+ OpenTelemetry.logger.warn "Error in CompositePropagator#inject #{e.message}"
65
+ end
66
+ nil
67
+ end
68
+
69
+ # Runs extractors or propagators in order and returns a Context updated
70
+ # with the results of each extraction. If an extraction fails, a warning
71
+ # will be logged and remaining extractors will continue to be executed. Always
72
+ # returns a valid context.
73
+ #
74
+ # @param [Object] carrier The carrier to extract context from.
75
+ # @param [optional Context] context Context to be updated with the state
76
+ # extracted from the carrier. Defaults to +Context.current+.
77
+ # @param [optional Getter] getter If the optional getter is provided, it
78
+ # will be used to read the header from the carrier, otherwise the default
79
+ # getter will be used.
80
+ #
81
+ # @return [Context] a new context updated with state extracted from the
82
+ # carrier
83
+ def extract(carrier, context: Context.current, getter: Context::Propagation.text_map_getter)
84
+ extractors = @extractors || @propagators
85
+ extractors.inject(context) do |ctx, extractor|
86
+ extractor.extract(carrier, context: ctx, getter: getter)
87
+ rescue StandardError => e
88
+ OpenTelemetry.logger.warn "Error in CompositePropagator#extract #{e.message}"
89
+ ctx
90
+ end
91
+ end
92
+
93
+ # Returns the union of the propagation fields returned by the composed injectors
94
+ # or propagators. If your carrier is reused, you should delete the fields returned
95
+ # by this method before calling +inject+.
96
+ #
97
+ # @return [Array<String>] a list of fields that will be used by this propagator.
98
+ def fields
99
+ injectors = @injectors || @propagators
100
+ injectors.flat_map(&fields).uniq
101
+ end
102
+ end
103
+ end
104
+ end
105
+ end
@@ -0,0 +1,51 @@
1
+ # frozen_string_literal: true
2
+
3
+ # Copyright The OpenTelemetry Authors
4
+ #
5
+ # SPDX-License-Identifier: Apache-2.0
6
+
7
+ module OpenTelemetry
8
+ class Context
9
+ module Propagation
10
+ # A no-op text map propagator implementation
11
+ class NoopTextMapPropagator
12
+ EMPTY_LIST = [].freeze
13
+ private_constant(:EMPTY_LIST)
14
+
15
+ # Injects the provided context into a carrier.
16
+ #
17
+ # @param [Object] carrier A mutable carrier to inject context into.
18
+ # @param [optional Context] context Context to be injected into carrier. Defaults
19
+ # to +Context.current+.
20
+ # @param [optional Setter] setter If the optional setter is provided, it
21
+ # will be used to write context into the carrier, otherwise the default
22
+ # setter will be used.
23
+ def inject(carrier, context: Context.current, setter: Context::Propagation.text_map_setter); end
24
+
25
+ # Extracts and returns context from a carrier. Returns the provided
26
+ # context and logs a warning if an error if extraction fails.
27
+ #
28
+ # @param [Object] carrier The carrier to extract context from.
29
+ # @param [optional Context] context Context to be updated with the state
30
+ # extracted from the carrier. Defaults to +Context.current+.
31
+ # @param [optional Getter] getter If the optional getter is provided, it
32
+ # will be used to read the header from the carrier, otherwise the default
33
+ # getter will be used.
34
+ #
35
+ # @return [Context] a new context updated with state extracted from the
36
+ # carrier
37
+ def extract(carrier, context: Context.current, getter: Context::Propagation.text_map_getter)
38
+ context
39
+ end
40
+
41
+ # Returns the predefined propagation fields. If your carrier is reused, you
42
+ # should delete the fields returned by this method before calling +inject+.
43
+ #
44
+ # @return [Array<String>] a list of fields that will be used by this propagator.
45
+ def fields
46
+ EMPTY_LIST
47
+ end
48
+ end
49
+ end
50
+ end
51
+ end
@@ -7,45 +7,44 @@
7
7
  module OpenTelemetry
8
8
  class Context
9
9
  module Propagation
10
- # A propagator composes an extractor and injector into a single interface
11
- # exposing inject and extract methods
12
- class Propagator
10
+ # A text map propagator that composes an extractor and injector into a
11
+ # single interface exposing inject and extract methods.
12
+ class TextMapPropagator
13
13
  # Returns a Propagator that delegates inject and extract to the provided
14
14
  # injector and extractor
15
15
  #
16
16
  # @param [#inject] injector
17
17
  # @param [#extract] extractor
18
18
  def initialize(injector, extractor)
19
+ raise ArgumentError, 'injector and extractor must both be non-nil' if injector.nil? || extractor.nil?
20
+
19
21
  @injector = injector
20
22
  @extractor = extractor
21
23
  end
22
24
 
23
- # Returns a carrier with the provided context injected according the
24
- # underlying injector. Returns the carrier and logs a warning if
25
- # injection fails.
25
+ # Injects the provided context into a carrier using the underlying
26
+ # injector. Logs a warning if injection fails.
26
27
  #
27
- # @param [Object] carrier A carrier to inject context into
28
- # context into
28
+ # @param [Object] carrier A mutable carrier to inject context into.
29
29
  # @param [optional Context] context Context to be injected into carrier. Defaults
30
- # to +Context.current+
30
+ # to +Context.current+.
31
31
  # @param [optional Setter] setter If the optional setter is provided, it
32
32
  # will be used to write context into the carrier, otherwise the default
33
33
  # setter will be used.
34
- #
35
- # @return [Object] carrier
36
34
  def inject(carrier, context: Context.current, setter: Context::Propagation.text_map_setter)
37
35
  @injector.inject(carrier, context, setter)
38
- rescue => e # rubocop:disable Style/RescueStandardError
36
+ nil
37
+ rescue StandardError => e
39
38
  OpenTelemetry.logger.warn "Error in Propagator#inject #{e.message}"
40
- carrier
39
+ nil
41
40
  end
42
41
 
43
42
  # Extracts and returns context from a carrier. Returns the provided
44
43
  # context and logs a warning if an error if extraction fails.
45
44
  #
46
- # @param [Object] carrier The carrier to extract context from
45
+ # @param [Object] carrier The carrier to extract context from.
47
46
  # @param [optional Context] context Context to be updated with the state
48
- # extracted from the carrier. Defaults to +Context.current+
47
+ # extracted from the carrier. Defaults to +Context.current+.
49
48
  # @param [optional Getter] getter If the optional getter is provided, it
50
49
  # will be used to read the header from the carrier, otherwise the default
51
50
  # getter will be used.
@@ -54,10 +53,18 @@ module OpenTelemetry
54
53
  # carrier
55
54
  def extract(carrier, context: Context.current, getter: Context::Propagation.text_map_getter)
56
55
  @extractor.extract(carrier, context, getter)
57
- rescue => e # rubocop:disable Style/RescueStandardError
56
+ rescue StandardError => e
58
57
  OpenTelemetry.logger.warn "Error in Propagator#extract #{e.message}"
59
58
  context
60
59
  end
60
+
61
+ # Returns the predefined propagation fields. If your carrier is reused, you
62
+ # should delete the fields returned by this method before calling +inject+.
63
+ #
64
+ # @return [Array<String>] a list of fields that will be used by this propagator.
65
+ def fields
66
+ @injector.fields
67
+ end
61
68
  end
62
69
  end
63
70
  end
@@ -0,0 +1,17 @@
1
+ # frozen_string_literal: true
2
+
3
+ # Copyright The OpenTelemetry Authors
4
+ #
5
+ # SPDX-License-Identifier: Apache-2.0
6
+
7
+ require 'opentelemetry/internal/proxy_tracer'
8
+ require 'opentelemetry/internal/proxy_tracer_provider'
9
+
10
+ module OpenTelemetry
11
+ # @api private
12
+ #
13
+ # The Internal module provides API internal functionality that is not a part of the
14
+ # public API.
15
+ module Internal
16
+ end
17
+ end
@@ -0,0 +1,38 @@
1
+ # frozen_string_literal: true
2
+
3
+ # Copyright The OpenTelemetry Authors
4
+ #
5
+ # SPDX-License-Identifier: Apache-2.0
6
+
7
+ module OpenTelemetry
8
+ module Internal
9
+ # @api private
10
+ #
11
+ # {ProxyTracer} is an implementation of {OpenTelemetry::Trace::Tracer}. It is returned from
12
+ # the ProxyTracerProvider until a delegate tracer provider is installed. After the delegate
13
+ # tracer provider is installed, the ProxyTracer will delegate to the corresponding "real"
14
+ # tracer.
15
+ class ProxyTracer < Trace::Tracer
16
+ attr_writer :delegate
17
+
18
+ # Returns a new {ProxyTracer} instance.
19
+ #
20
+ # @return [ProxyTracer]
21
+ def initialize
22
+ @delegate = nil
23
+ end
24
+
25
+ def start_root_span(name, attributes: nil, links: nil, start_timestamp: nil, kind: nil)
26
+ return @delegate.start_root_span(name, attributes: attributes, links: links, start_timestamp: start_timestamp, kind: kind) unless @delegate.nil?
27
+
28
+ super
29
+ end
30
+
31
+ def start_span(name, with_parent: nil, attributes: nil, links: nil, start_timestamp: nil, kind: nil)
32
+ return @delegate.start_span(name, with_parent: with_parent, attributes: attributes, links: links, start_timestamp: start_timestamp, kind: kind) unless @delegate.nil?
33
+
34
+ super
35
+ end
36
+ end
37
+ end
38
+ end
@@ -0,0 +1,59 @@
1
+ # frozen_string_literal: true
2
+
3
+ # Copyright The OpenTelemetry Authors
4
+ #
5
+ # SPDX-License-Identifier: Apache-2.0
6
+
7
+ module OpenTelemetry
8
+ module Internal
9
+ # @api private
10
+ #
11
+ # {ProxyTracerProvider} is an implementation of {OpenTelemetry::Trace::TracerProvider}.
12
+ # It is the default global tracer provider returned by OpenTelemetry.tracer_provider.
13
+ # It delegates to a "real" TracerProvider after the global tracer provider is registered.
14
+ # It returns {ProxyTracer} instances until the delegate is installed.
15
+ class ProxyTracerProvider < Trace::TracerProvider
16
+ Key = Struct.new(:name, :version)
17
+ private_constant(:Key)
18
+
19
+ # Returns a new {ProxyTracerProvider} instance.
20
+ #
21
+ # @return [ProxyTracerProvider]
22
+ def initialize
23
+ @mutex = Mutex.new
24
+ @registry = {}
25
+ @delegate = nil
26
+ end
27
+
28
+ # Set the delegate tracer provider. If this is called more than once, a warning will
29
+ # be logged and superfluous calls will be ignored.
30
+ #
31
+ # @param [TracerProvider] provider The tracer provider to delegate to
32
+ def delegate=(provider)
33
+ unless @delegate.nil?
34
+ OpenTelemetry.logger.warn 'Attempt to reset delegate in ProxyTracerProvider ignored.'
35
+ return
36
+ end
37
+
38
+ @mutex.synchronize do
39
+ @delegate = provider
40
+ @registry.each { |key, tracer| tracer.delegate = provider.tracer(key.name, key.version) }
41
+ end
42
+ end
43
+
44
+ # Returns a {Tracer} instance.
45
+ #
46
+ # @param [optional String] name Instrumentation package name
47
+ # @param [optional String] version Instrumentation package version
48
+ #
49
+ # @return [Tracer]
50
+ def tracer(name = nil, version = nil)
51
+ @mutex.synchronize do
52
+ return @delegate.tracer(name, version) unless @delegate.nil?
53
+
54
+ @registry[Key.new(name, version)] ||= ProxyTracer.new
55
+ end
56
+ end
57
+ end
58
+ end
59
+ end
@@ -5,8 +5,7 @@
5
5
  # SPDX-License-Identifier: Apache-2.0
6
6
 
7
7
  require 'opentelemetry/trace/propagation/trace_context/trace_parent'
8
- require 'opentelemetry/trace/propagation/trace_context/text_map_extractor'
9
- require 'opentelemetry/trace/propagation/trace_context/text_map_injector'
8
+ require 'opentelemetry/trace/propagation/trace_context/text_map_propagator'
10
9
 
11
10
  module OpenTelemetry
12
11
  module Trace
@@ -15,24 +14,14 @@ module OpenTelemetry
15
14
  # for context propagation in the W3C Trace Context format.
16
15
  module TraceContext
17
16
  extend self
18
- TRACEPARENT_KEY = 'traceparent'
19
- TRACESTATE_KEY = 'tracestate'
20
- TEXT_MAP_EXTRACTOR = TextMapExtractor.new
21
- TEXT_MAP_INJECTOR = TextMapInjector.new
17
+ TEXT_MAP_PROPAGATOR = TextMapPropagator.new
22
18
 
23
- private_constant :TRACEPARENT_KEY, :TRACESTATE_KEY,
24
- :TEXT_MAP_INJECTOR, :TEXT_MAP_EXTRACTOR
19
+ private_constant :TEXT_MAP_PROPAGATOR
25
20
 
26
- # Returns an extractor that extracts context using the W3C Trace Context
27
- # format
28
- def text_map_extractor
29
- TEXT_MAP_EXTRACTOR
30
- end
31
-
32
- # Returns an injector that injects context using the W3C Trace Context
33
- # format
34
- def text_map_injector
35
- TEXT_MAP_INJECTOR
21
+ # Returns a text map propagator that propagates context using the
22
+ # W3C Trace Context format.
23
+ def text_map_propagator
24
+ TEXT_MAP_PROPAGATOR
36
25
  end
37
26
  end
38
27
  end