opentelemetry-propagator-b3 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.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 13556a8ad4843308103d63e92356c269c87be42826bf0a026131b90eae0c0792
4
- data.tar.gz: 9f7949588ae2a6b554b0bf3406acc05ce0c47008b06e83d3c0d04a9c23928ace
3
+ metadata.gz: d8bb36f87a6d784d8889edf5d733410b48567f33d4a4c41d40f750e5a36ac989
4
+ data.tar.gz: ebd953a02b5045be4a44bb18c5d6094cf3198523b8b905b86023bb6732c0e695
5
5
  SHA512:
6
- metadata.gz: 8ff23eb11e5434e66be786ff15af0138f8d5c1a16cd7c852ce2f1a64262a2a7e561bd21201e72d3824b7a9ff6f691cd74fb41f6a349a5d50b9b8a9f03b072b3f
7
- data.tar.gz: ba6f63d852685d0f651e0bb8512fea83ad357db2a5f925832e940acac773362f498eae5f39709a7aa1afe262e0279d5edf104f18df4846ba1d229c283de16990
6
+ metadata.gz: 2ddfc1c88bd19aa81b049d9ef832de04bc321dd0219a44b01937a656f6d6b14061b0a7b452067b9844dea537c9d73ba5e26ad5301870262cb82af05cafcbbeec
7
+ data.tar.gz: 31e80b987d02727a2daba0fd489605daed15881890726eec64422d77842700e7dc8ba00d49d0318bddc95427a0855366a72bdcfc2bea1fbb41615e13d1da2a04
data/CHANGELOG.md CHANGED
@@ -1,5 +1,13 @@
1
1
  # Release History: opentelemetry-propagator-b3
2
2
 
3
+ ### v0.17.0 / 2021-04-22
4
+
5
+ * BREAKING CHANGE: Replace TextMapInjector/TextMapExtractor pairs with a TextMapPropagator.
6
+
7
+ [Check the propagator documentation](https://open-telemetry.github.io/opentelemetry-ruby/) for the new usage.
8
+
9
+ * FIXED: Refactor propagators to add #fields
10
+
3
11
  ### v0.16.0 / 2021-03-17
4
12
 
5
13
  * DOCS: Replace Gitter with GitHub Discussions
@@ -31,28 +31,11 @@ module OpenTelemetry
31
31
  def debug?(context)
32
32
  context.value(DEBUG_CONTEXT_KEY)
33
33
  end
34
-
35
- # @api private
36
- # Convert an id from a hex encoded string to byte array, optionally left
37
- # padding to the correct length. Assumes the input id has already been
38
- # validated to be 16 or 32 characters in length.
39
- def to_trace_id(hex_id)
40
- if hex_id.length == 32
41
- Array(hex_id).pack('H*')
42
- else
43
- [0, hex_id].pack('qH*')
44
- end
45
- end
46
-
47
- # @api private
48
- # Convert an id from a hex encoded string to byte array.
49
- def to_span_id(hex_id)
50
- Array(hex_id).pack('H*')
51
- end
52
34
  end
53
35
  end
54
36
  end
55
37
 
56
38
  require_relative './b3/version'
39
+ require_relative './b3/text_map_extractor'
57
40
  require_relative './b3/multi'
58
41
  require_relative './b3/single'
@@ -4,8 +4,7 @@
4
4
  #
5
5
  # SPDX-License-Identifier: Apache-2.0
6
6
 
7
- require_relative './multi/text_map_extractor'
8
- require_relative './multi/text_map_injector'
7
+ require_relative './multi/text_map_propagator'
9
8
 
10
9
  # OpenTelemetry is an open source observability framework, providing a
11
10
  # general-purpose API, SDK, and related tools required for the instrumentation
@@ -18,30 +17,18 @@ module OpenTelemetry
18
17
  module Propagator
19
18
  # Namespace for OpenTelemetry B3 propagation
20
19
  module B3
21
- # Namespace for OpenTelemetry b3 multi header encoding
20
+ # Namespace for OpenTelemetry B3 multi header encoding
22
21
  module Multi
23
22
  extend self
24
23
 
25
- B3_TRACE_ID_KEY = 'X-B3-TraceId'
26
- B3_SPAN_ID_KEY = 'X-B3-SpanId'
27
- B3_SAMPLED_KEY = 'X-B3-Sampled'
28
- B3_FLAGS_KEY = 'X-B3-Flags'
29
- TEXT_MAP_EXTRACTOR = TextMapExtractor.new
30
- TEXT_MAP_INJECTOR = TextMapInjector.new
24
+ TEXT_MAP_PROPAGATOR = TextMapPropagator.new
31
25
 
32
- private_constant :B3_TRACE_ID_KEY, :B3_SPAN_ID_KEY, :B3_SAMPLED_KEY,
33
- :B3_FLAGS_KEY, :TEXT_MAP_INJECTOR, :TEXT_MAP_EXTRACTOR
26
+ private_constant :TEXT_MAP_PROPAGATOR
34
27
 
35
- # Returns an extractor that extracts context in the B3 multi header
36
- # format
37
- def text_map_injector
38
- TEXT_MAP_INJECTOR
39
- end
40
-
41
- # Returns an injector that injects context in the B3 multi header
42
- # format
43
- def text_map_extractor
44
- TEXT_MAP_EXTRACTOR
28
+ # Returns a text map propagator that propagates context using the
29
+ # B3 multi header format.
30
+ def text_map_propagator
31
+ TEXT_MAP_PROPAGATOR
45
32
  end
46
33
  end
47
34
  end
@@ -0,0 +1,64 @@
1
+ # frozen_string_literal: true
2
+
3
+ # Copyright The OpenTelemetry Authors
4
+ #
5
+ # SPDX-License-Identifier: Apache-2.0
6
+
7
+ # OpenTelemetry is an open source observability framework, providing a
8
+ # general-purpose API, SDK, and related tools required for the instrumentation
9
+ # of cloud-native software, frameworks, and libraries.
10
+ #
11
+ # The OpenTelemetry module provides global accessors for telemetry objects.
12
+ # See the documentation for the `opentelemetry-api` gem for details.
13
+ module OpenTelemetry
14
+ # Namespace for OpenTelemetry propagator extension libraries
15
+ module Propagator
16
+ # Namespace for OpenTelemetry B3 propagation
17
+ module B3
18
+ # Namespace for OpenTelemetry B3 multi header encoding
19
+ module Multi
20
+ # Propagates trace context using the B3 multi header format
21
+ class TextMapPropagator
22
+ include TextMapExtractor
23
+
24
+ FIELDS = [B3_TRACE_ID_KEY, B3_SPAN_ID_KEY, B3_SAMPLED_KEY, B3_FLAGS_KEY].freeze
25
+
26
+ private_constant :FIELDS
27
+
28
+ # Inject trace context into the supplied carrier.
29
+ #
30
+ # @param [Carrier] carrier The mutable carrier to inject trace context into
31
+ # @param [Context] context The context to read trace context from
32
+ # @param [optional Setter] setter If the optional setter is provided, it
33
+ # will be used to write context into the carrier, otherwise the default
34
+ # text map setter will be used.
35
+ def inject(carrier, context: Context.current, setter: Context::Propagation.text_map_setter)
36
+ span_context = Trace.current_span(context).context
37
+ return unless span_context.valid?
38
+
39
+ setter.set(carrier, B3_TRACE_ID_KEY, span_context.hex_trace_id)
40
+ setter.set(carrier, B3_SPAN_ID_KEY, span_context.hex_span_id)
41
+
42
+ if B3.debug?(context)
43
+ setter.set(carrier, B3_FLAGS_KEY, '1')
44
+ elsif span_context.trace_flags.sampled?
45
+ setter.set(carrier, B3_SAMPLED_KEY, '1')
46
+ else
47
+ setter.set(carrier, B3_SAMPLED_KEY, '0')
48
+ end
49
+
50
+ nil
51
+ end
52
+
53
+ # Returns the predefined propagation fields. If your carrier is reused, you
54
+ # should delete the fields returned by this method before calling +inject+.
55
+ #
56
+ # @return [Array<String>] a list of fields that will be used by this propagator.
57
+ def fields
58
+ FIELDS
59
+ end
60
+ end
61
+ end
62
+ end
63
+ end
64
+ end
@@ -4,8 +4,7 @@
4
4
  #
5
5
  # SPDX-License-Identifier: Apache-2.0
6
6
 
7
- require_relative './single/text_map_extractor'
8
- require_relative './single/text_map_injector'
7
+ require_relative './single/text_map_propagator'
9
8
 
10
9
  # OpenTelemetry is an open source observability framework, providing a
11
10
  # general-purpose API, SDK, and related tools required for the instrumentation
@@ -18,26 +17,18 @@ module OpenTelemetry
18
17
  module Propagator
19
18
  # Namespace for OpenTelemetry B3 propagation
20
19
  module B3
21
- # Namespace for OpenTelemetry b3 single header encoding
20
+ # Namespace for OpenTelemetry B3 single header encoding
22
21
  module Single
23
22
  extend self
24
23
 
25
- B3_CONTEXT_KEY = 'b3'
26
- TEXT_MAP_EXTRACTOR = TextMapExtractor.new
27
- TEXT_MAP_INJECTOR = TextMapInjector.new
24
+ TEXT_MAP_PROPAGATOR = TextMapPropagator.new
28
25
 
29
- private_constant :B3_CONTEXT_KEY, :TEXT_MAP_INJECTOR, :TEXT_MAP_EXTRACTOR
26
+ private_constant :TEXT_MAP_PROPAGATOR
30
27
 
31
- # Returns an extractor that extracts context in the B3 single header
32
- # format
33
- def text_map_injector
34
- TEXT_MAP_INJECTOR
35
- end
36
-
37
- # Returns an injector that injects context in the B3 single header
38
- # format
39
- def text_map_extractor
40
- TEXT_MAP_EXTRACTOR
28
+ # Returns a text map propagator that propagates context using the
29
+ # B3 single header format.
30
+ def text_map_propagator
31
+ TEXT_MAP_PROPAGATOR
41
32
  end
42
33
  end
43
34
  end
@@ -0,0 +1,64 @@
1
+ # frozen_string_literal: true
2
+
3
+ # Copyright The OpenTelemetry Authors
4
+ #
5
+ # SPDX-License-Identifier: Apache-2.0
6
+
7
+ # OpenTelemetry is an open source observability framework, providing a
8
+ # general-purpose API, SDK, and related tools required for the instrumentation
9
+ # of cloud-native software, frameworks, and libraries.
10
+ #
11
+ # The OpenTelemetry module provides global accessors for telemetry objects.
12
+ # See the documentation for the `opentelemetry-api` gem for details.
13
+ module OpenTelemetry
14
+ # Namespace for OpenTelemetry propagator extension libraries
15
+ module Propagator
16
+ # Namespace for OpenTelemetry B3 propagation
17
+ module B3
18
+ # Namespace for OpenTelemetry b3 single header encoding
19
+ module Single
20
+ # Propagates trace context using the b3 single header format
21
+ class TextMapPropagator
22
+ include TextMapExtractor
23
+
24
+ FIELDS = [B3_CONTEXT_KEY].freeze
25
+
26
+ private_constant :FIELDS
27
+
28
+ # Inject trace context into the supplied carrier.
29
+ #
30
+ # @param [Carrier] carrier The mutable carrier to inject trace context into
31
+ # @param [Context] context The context to read trace context from
32
+ # @param [optional Setter] setter If the optional setter is provided, it
33
+ # will be used to write context into the carrier, otherwise the default
34
+ # text map setter will be used.
35
+ def inject(carrier, context: Context.current, setter: Context::Propagation.text_map_setter)
36
+ span_context = Trace.current_span(context).context
37
+ return unless span_context.valid?
38
+
39
+ sampling_state = if B3.debug?(context)
40
+ 'd'
41
+ elsif span_context.trace_flags.sampled?
42
+ '1'
43
+ else
44
+ '0'
45
+ end
46
+
47
+ b3_value = "#{span_context.hex_trace_id}-#{span_context.hex_span_id}-#{sampling_state}"
48
+
49
+ setter.set(carrier, B3_CONTEXT_KEY, b3_value)
50
+ nil
51
+ end
52
+
53
+ # Returns the predefined propagation fields. If your carrier is reused, you
54
+ # should delete the fields returned by this method before calling +inject+.
55
+ #
56
+ # @return [Array<String>] a list of fields that will be used by this propagator.
57
+ def fields
58
+ FIELDS
59
+ end
60
+ end
61
+ end
62
+ end
63
+ end
64
+ end
@@ -0,0 +1,116 @@
1
+ # frozen_string_literal: true
2
+
3
+ # Copyright The OpenTelemetry Authors
4
+ #
5
+ # SPDX-License-Identifier: Apache-2.0
6
+
7
+ # OpenTelemetry is an open source observability framework, providing a
8
+ # general-purpose API, SDK, and related tools required for the instrumentation
9
+ # of cloud-native software, frameworks, and libraries.
10
+ #
11
+ # The OpenTelemetry module provides global accessors for telemetry objects.
12
+ # See the documentation for the `opentelemetry-api` gem for details.
13
+ module OpenTelemetry
14
+ # Namespace for OpenTelemetry propagator extension libraries
15
+ module Propagator
16
+ # Namespace for OpenTelemetry B3 propagation
17
+ module B3
18
+ # Extracts trace context using the b3 single or multi header formats, favouring b3 single header.
19
+ module TextMapExtractor
20
+ B3_CONTEXT_REGEX = /\A(?<trace_id>(?:[0-9a-f]{16}){1,2})-(?<span_id>[0-9a-f]{16})(?:-(?<sampling_state>[01d](?![0-9a-f])))?(?:-(?<parent_span_id>[0-9a-f]{16}))?\z/.freeze
21
+ B3_TRACE_ID_REGEX = /\A(?:[0-9a-f]{16}){1,2}\z/.freeze
22
+ B3_SPAN_ID_REGEX = /\A[0-9a-f]{16}\z/.freeze
23
+ SAMPLED_VALUES = %w[1 true].freeze
24
+
25
+ B3_CONTEXT_KEY = 'b3'
26
+ B3_TRACE_ID_KEY = 'X-B3-TraceId'
27
+ B3_SPAN_ID_KEY = 'X-B3-SpanId'
28
+ B3_SAMPLED_KEY = 'X-B3-Sampled'
29
+ B3_FLAGS_KEY = 'X-B3-Flags'
30
+
31
+ private_constant :B3_CONTEXT_REGEX, :B3_TRACE_ID_REGEX, :B3_SPAN_ID_REGEX, :SAMPLED_VALUES
32
+ private_constant :B3_CONTEXT_KEY, :B3_TRACE_ID_KEY, :B3_SPAN_ID_KEY, :B3_SAMPLED_KEY, :B3_FLAGS_KEY
33
+
34
+ # Extract trace context from the supplied carrier. The b3 single header takes
35
+ # precedence over the multi-header format.
36
+ # If extraction fails, the original context will be returned.
37
+ #
38
+ # @param [Carrier] carrier The carrier to get the header from
39
+ # @param [optional Context] context Context to be updated with the trace context
40
+ # extracted from the carrier. Defaults to +Context.current+.
41
+ # @param [optional Getter] getter If the optional getter is provided, it
42
+ # will be used to read the header from the carrier, otherwise the default
43
+ # text map getter will be used.
44
+ #
45
+ # @return [Context] context updated with extracted baggage, or the original context
46
+ # if extraction fails
47
+ def extract(carrier, context: Context.current, getter: Context::Propagation.text_map_getter)
48
+ extract_b3_single_header(carrier, context, getter) || extract_b3_multi_header(carrier, context, getter) || context
49
+ end
50
+
51
+ private
52
+
53
+ def extract_b3_single_header(carrier, context, getter)
54
+ match = getter.get(carrier, B3_CONTEXT_KEY)&.match(B3_CONTEXT_REGEX)
55
+ return unless match
56
+
57
+ debug = match['sampling_state'] == 'd'
58
+ sampled = debug || match['sampling_state'] == '1'
59
+ extracted_context(match['trace_id'], match['span_id'], sampled, debug, context)
60
+ end
61
+
62
+ def extract_b3_multi_header(carrier, context, getter)
63
+ trace_id_hex = getter.get(carrier, B3_TRACE_ID_KEY)
64
+ return unless B3_TRACE_ID_REGEX.match?(trace_id_hex)
65
+
66
+ span_id_hex = getter.get(carrier, B3_SPAN_ID_KEY)
67
+ return unless B3_SPAN_ID_REGEX.match?(span_id_hex)
68
+
69
+ sampled = getter.get(carrier, B3_SAMPLED_KEY)
70
+ flags = getter.get(carrier, B3_FLAGS_KEY)
71
+
72
+ debug = flags == '1'
73
+ sampled = debug || SAMPLED_VALUES.include?(sampled)
74
+ extracted_context(trace_id_hex, span_id_hex, sampled, debug, context)
75
+ end
76
+
77
+ def extracted_context(trace_id_hex, span_id_hex, sampled, debug, context)
78
+ span_context = Trace::SpanContext.new(
79
+ trace_id: to_trace_id(trace_id_hex),
80
+ span_id: to_span_id(span_id_hex),
81
+ trace_flags: to_trace_flags(sampled),
82
+ remote: true
83
+ )
84
+
85
+ span = Trace::Span.new(span_context: span_context)
86
+ context = B3.context_with_debug(context) if debug
87
+ Trace.context_with_span(span, parent_context: context)
88
+ end
89
+
90
+ def to_trace_flags(sampled)
91
+ if sampled
92
+ Trace::TraceFlags::SAMPLED
93
+ else
94
+ Trace::TraceFlags::DEFAULT
95
+ end
96
+ end
97
+
98
+ # Convert an id from a hex encoded string to byte array, optionally left
99
+ # padding to the correct length. Assumes the input id has already been
100
+ # validated to be 16 or 32 characters in length.
101
+ def to_trace_id(hex_id)
102
+ if hex_id.length == 32
103
+ Array(hex_id).pack('H*')
104
+ else
105
+ [0, hex_id].pack('qH*')
106
+ end
107
+ end
108
+
109
+ # Convert an id from a hex encoded string to byte array.
110
+ def to_span_id(hex_id)
111
+ Array(hex_id).pack('H*')
112
+ end
113
+ end
114
+ end
115
+ end
116
+ end
@@ -7,7 +7,7 @@
7
7
  module OpenTelemetry
8
8
  module Propagator
9
9
  module B3
10
- VERSION = '0.16.0'
10
+ VERSION = '0.17.0'
11
11
  end
12
12
  end
13
13
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: opentelemetry-propagator-b3
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.16.0
4
+ version: 0.17.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - OpenTelemetry Authors
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2021-03-22 00:00:00.000000000 Z
11
+ date: 2021-04-23 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: opentelemetry-api
@@ -16,14 +16,14 @@ dependencies:
16
16
  requirements:
17
17
  - - "~>"
18
18
  - !ruby/object:Gem::Version
19
- version: 0.16.0
19
+ version: 0.17.0
20
20
  type: :runtime
21
21
  prerelease: false
22
22
  version_requirements: !ruby/object:Gem::Requirement
23
23
  requirements:
24
24
  - - "~>"
25
25
  - !ruby/object:Gem::Version
26
- version: 0.16.0
26
+ version: 0.17.0
27
27
  - !ruby/object:Gem::Dependency
28
28
  name: bundler
29
29
  requirement: !ruby/object:Gem::Requirement
@@ -136,20 +136,19 @@ files:
136
136
  - lib/opentelemetry-propagator-b3.rb
137
137
  - lib/opentelemetry/propagator/b3.rb
138
138
  - lib/opentelemetry/propagator/b3/multi.rb
139
- - lib/opentelemetry/propagator/b3/multi/text_map_extractor.rb
140
- - lib/opentelemetry/propagator/b3/multi/text_map_injector.rb
139
+ - lib/opentelemetry/propagator/b3/multi/text_map_propagator.rb
141
140
  - lib/opentelemetry/propagator/b3/single.rb
142
- - lib/opentelemetry/propagator/b3/single/text_map_extractor.rb
143
- - lib/opentelemetry/propagator/b3/single/text_map_injector.rb
141
+ - lib/opentelemetry/propagator/b3/single/text_map_propagator.rb
142
+ - lib/opentelemetry/propagator/b3/text_map_extractor.rb
144
143
  - lib/opentelemetry/propagator/b3/version.rb
145
144
  homepage: https://github.com/open-telemetry/opentelemetry-ruby
146
145
  licenses:
147
146
  - Apache-2.0
148
147
  metadata:
149
- changelog_uri: https://open-telemetry.github.io/opentelemetry-ruby/opentelemetry-propagator-b3/v0.16.0/file.CHANGELOG.html
148
+ changelog_uri: https://open-telemetry.github.io/opentelemetry-ruby/opentelemetry-propagator-b3/v0.17.0/file.CHANGELOG.html
150
149
  source_code_uri: https://github.com/open-telemetry/opentelemetry-ruby/tree/main/propagator/b3
151
150
  bug_tracker_uri: https://github.com/open-telemetry/opentelemetry-ruby/issues
152
- documentation_uri: https://open-telemetry.github.io/opentelemetry-ruby/opentelemetry-propagator-b3/v0.16.0
151
+ documentation_uri: https://open-telemetry.github.io/opentelemetry-ruby/opentelemetry-propagator-b3/v0.17.0
153
152
  post_install_message:
154
153
  rdoc_options: []
155
154
  require_paths:
@@ -165,7 +164,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
165
164
  - !ruby/object:Gem::Version
166
165
  version: '0'
167
166
  requirements: []
168
- rubygems_version: 3.1.4
167
+ rubygems_version: 3.1.6
169
168
  signing_key:
170
169
  specification_version: 4
171
170
  summary: B3 Context Propagation Extension for the OpenTelemetry framework
@@ -1,98 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- # Copyright The OpenTelemetry Authors
4
- #
5
- # SPDX-License-Identifier: Apache-2.0
6
-
7
- # OpenTelemetry is an open source observability framework, providing a
8
- # general-purpose API, SDK, and related tools required for the instrumentation
9
- # of cloud-native software, frameworks, and libraries.
10
- #
11
- # The OpenTelemetry module provides global accessors for telemetry objects.
12
- # See the documentation for the `opentelemetry-api` gem for details.
13
- module OpenTelemetry
14
- # Namespace for OpenTelemetry propagator extension libraries
15
- module Propagator
16
- # Namespace for OpenTelemetry B3 propagation
17
- module B3
18
- # Namespace for OpenTelemetry b3 multi header encoding
19
- module Multi
20
- # Extracts context from carriers in the b3 single header format
21
- class TextMapExtractor
22
- B3_TRACE_ID_REGEX = /\A(?:[0-9a-f]{16}){1,2}\z/.freeze
23
- B3_SPAN_ID_REGEX = /\A[0-9a-f]{16}\z/.freeze
24
- SAMPLED_VALUES = %w[1 true].freeze
25
- DEBUG_FLAG = '1'
26
- private_constant :B3_TRACE_ID_REGEX, :B3_SPAN_ID_REGEX, :SAMPLED_VALUES, :DEBUG_FLAG
27
-
28
- # Returns a new TextMapExtractor that extracts b3 context using the
29
- # specified getter
30
- #
31
- # @param [optional Getter] default_getter The default getter used to read
32
- # headers from a carrier during extract. Defaults to a
33
- # {OpenTelemetry::Context:Propagation::TextMapGetter} instance.
34
- # @return [TextMapExtractor]
35
- def initialize(default_getter = Context::Propagation.text_map_getter)
36
- @default_getter = default_getter
37
- end
38
-
39
- # Extract b3 context from the supplied carrier and set the active span
40
- # in the given context. The original context will be returned if b3
41
- # cannot be extracted from the carrier.
42
- #
43
- # @param [Carrier] carrier The carrier to get the header from.
44
- # @param [Context] context The context to be updated with extracted context
45
- # @param [optional Getter] getter If the optional getter is provided, it
46
- # will be used to read the header from the carrier, otherwise the default
47
- # getter will be used.
48
- # @return [Context] Updated context with active span derived from the header, or the original
49
- # context if parsing fails.
50
- def extract(carrier, context, getter = nil)
51
- getter ||= @default_getter
52
-
53
- trace_id_hex = getter.get(carrier, B3_TRACE_ID_KEY)
54
- return context unless valid_trace_id?(trace_id_hex)
55
-
56
- span_id_hex = getter.get(carrier, B3_SPAN_ID_KEY)
57
- return context unless valid_span_id?(span_id_hex)
58
-
59
- sampled = getter.get(carrier, B3_SAMPLED_KEY)
60
- flags = getter.get(carrier, B3_FLAGS_KEY)
61
-
62
- context = B3.context_with_debug(context) if flags == DEBUG_FLAG
63
-
64
- span_context = Trace::SpanContext.new(
65
- trace_id: B3.to_trace_id(trace_id_hex),
66
- span_id: B3.to_span_id(span_id_hex),
67
- trace_flags: to_trace_flags(sampled, flags),
68
- remote: true
69
- )
70
-
71
- span = Trace::Span.new(span_context: span_context)
72
- Trace.context_with_span(span, parent_context: context)
73
- rescue OpenTelemetry::Error
74
- context
75
- end
76
-
77
- private
78
-
79
- def to_trace_flags(sampled, b3_flags)
80
- if b3_flags == DEBUG_FLAG || SAMPLED_VALUES.include?(sampled)
81
- Trace::TraceFlags::SAMPLED
82
- else
83
- Trace::TraceFlags::DEFAULT
84
- end
85
- end
86
-
87
- def valid_trace_id?(trace_id)
88
- B3_TRACE_ID_REGEX.match?(trace_id)
89
- end
90
-
91
- def valid_span_id?(span_id)
92
- B3_SPAN_ID_REGEX.match?(span_id)
93
- end
94
- end
95
- end
96
- end
97
- end
98
- end
@@ -1,55 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- # Copyright The OpenTelemetry Authors
4
- #
5
- # SPDX-License-Identifier: Apache-2.0
6
- module OpenTelemetry
7
- # Namespace for OpenTelemetry propagator extension libraries
8
- module Propagator
9
- # Namespace for OpenTelemetry B3 propagation
10
- module B3
11
- # Namespace for OpenTelemetry b3 single header encoding
12
- module Multi
13
- # Injects context into carriers using the b3 single header format
14
- class TextMapInjector
15
- # Returns a new TextMapInjector that injects b3 context using the
16
- # specified setter
17
- #
18
- # @param [optional Setter] default_setter The default setter used to
19
- # write context into a carrier during inject. Defaults to a
20
- # {OpenTelemetry::Context:Propagation::TextMapSetter} instance.
21
- # @return [TextMapInjector]
22
- def initialize(default_setter = Context::Propagation.text_map_setter)
23
- @default_setter = default_setter
24
- end
25
-
26
- # Set the span context on the supplied carrier.
27
- #
28
- # @param [Context] context The active Context.
29
- # @param [optional Setter] setter If the optional setter is provided, it
30
- # will be used to write context into the carrier, otherwise the default
31
- # setter will be used.
32
- # @return [Object] the carrier with context injected
33
- def inject(carrier, context, setter = nil)
34
- span_context = Trace.current_span(context).context
35
- return unless span_context.valid?
36
-
37
- setter ||= @default_setter
38
- setter.set(carrier, B3_TRACE_ID_KEY, span_context.hex_trace_id)
39
- setter.set(carrier, B3_SPAN_ID_KEY, span_context.hex_span_id)
40
-
41
- if B3.debug?(context)
42
- setter.set(carrier, B3_FLAGS_KEY, '1')
43
- elsif span_context.trace_flags.sampled?
44
- setter.set(carrier, B3_SAMPLED_KEY, '1')
45
- else
46
- setter.set(carrier, B3_SAMPLED_KEY, '0')
47
- end
48
-
49
- carrier
50
- end
51
- end
52
- end
53
- end
54
- end
55
- end
@@ -1,79 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- # Copyright The OpenTelemetry Authors
4
- #
5
- # SPDX-License-Identifier: Apache-2.0
6
-
7
- # OpenTelemetry is an open source observability framework, providing a
8
- # general-purpose API, SDK, and related tools required for the instrumentation
9
- # of cloud-native software, frameworks, and libraries.
10
- #
11
- # The OpenTelemetry module provides global accessors for telemetry objects.
12
- # See the documentation for the `opentelemetry-api` gem for details.
13
- module OpenTelemetry
14
- # Namespace for OpenTelemetry propagator extension libraries
15
- module Propagator
16
- # Namespace for OpenTelemetry B3 propagation
17
- module B3
18
- # Namespace for OpenTelemetry b3 single header encoding
19
- module Single
20
- # Extracts context from carriers in the b3 single header format
21
- class TextMapExtractor
22
- B3_CONTEXT_REGEX = /\A(?<trace_id>(?:[0-9a-f]{16}){1,2})-(?<span_id>[0-9a-f]{16})(?:-(?<sampling_state>[01d](?![0-9a-f])))?(?:-(?<parent_span_id>[0-9a-f]{16}))?\z/.freeze
23
- SAMPLED_VALUES = %w[1 d].freeze
24
-
25
- # Returns a new TextMapExtractor that extracts b3 context using the
26
- # specified getter
27
- #
28
- # @param [optional Getter] default_getter The default getter used to read
29
- # headers from a carrier during extract. Defaults to a
30
- # {OpenTelemetry::Context:Propagation::TextMapGetter} instance.
31
- # @return [TextMapExtractor]
32
- def initialize(default_getter = Context::Propagation.text_map_getter)
33
- @default_getter = default_getter
34
- end
35
-
36
- # Extract b3 context from the supplied carrier and set the active span
37
- # in the given context. The original context will be returned if b3
38
- # cannot be extracted from the carrier.
39
- #
40
- # @param [Carrier] carrier The carrier to get the header from.
41
- # @param [Context] context The context to be updated with extracted context
42
- # @param [optional Getter] getter If the optional getter is provided, it
43
- # will be used to read the header from the carrier, otherwise the default
44
- # getter will be used.
45
- # @return [Context] Updated context with active span derived from the header, or the original
46
- # context if parsing fails.
47
- def extract(carrier, context, getter = nil)
48
- getter ||= @default_getter
49
- header = getter.get(carrier, B3_CONTEXT_KEY)
50
- return context unless (match = header.match(B3_CONTEXT_REGEX))
51
-
52
- span_context = Trace::SpanContext.new(
53
- trace_id: B3.to_trace_id(match['trace_id']),
54
- span_id: B3.to_span_id(match['span_id']),
55
- trace_flags: to_trace_flags(match['sampling_state']),
56
- remote: true
57
- )
58
-
59
- span = Trace::Span.new(span_context: span_context)
60
- context = B3.context_with_debug(context) if match['sampling_state'] == 'd'
61
- Trace.context_with_span(span, parent_context: context)
62
- rescue OpenTelemetry::Error
63
- context
64
- end
65
-
66
- private
67
-
68
- def to_trace_flags(sampling_state)
69
- if SAMPLED_VALUES.include?(sampling_state)
70
- Trace::TraceFlags::SAMPLED
71
- else
72
- Trace::TraceFlags::DEFAULT
73
- end
74
- end
75
- end
76
- end
77
- end
78
- end
79
- end
@@ -1,55 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- # Copyright The OpenTelemetry Authors
4
- #
5
- # SPDX-License-Identifier: Apache-2.0
6
- module OpenTelemetry
7
- # Namespace for OpenTelemetry propagator extension libraries
8
- module Propagator
9
- # Namespace for OpenTelemetry B3 propagation
10
- module B3
11
- # Namespace for OpenTelemetry b3 single header encoding
12
- module Single
13
- # Injects context into carriers using the b3 single header format
14
- class TextMapInjector
15
- # Returns a new TextMapInjector that injects b3 context using the
16
- # specified setter
17
- #
18
- # @param [optional Setter] default_setter The default setter used to
19
- # write context into a carrier during inject. Defaults to a
20
- # {OpenTelemetry::Context:Propagation::TextMapSetter} instance.
21
- # @return [TextMapInjector]
22
- def initialize(default_setter = Context::Propagation.text_map_setter)
23
- @default_setter = default_setter
24
- end
25
-
26
- # Set the span context on the supplied carrier.
27
- #
28
- # @param [Context] context The active Context.
29
- # @param [optional Setter] setter If the optional setter is provided, it
30
- # will be used to write context into the carrier, otherwise the default
31
- # setter will be used.
32
- # @return [Object] the carrier with context injected
33
- def inject(carrier, context, setter = nil)
34
- span_context = Trace.current_span(context).context
35
- return unless span_context.valid?
36
-
37
- sampling_state = if B3.debug?(context)
38
- 'd'
39
- elsif span_context.trace_flags.sampled?
40
- '1'
41
- else
42
- '0'
43
- end
44
-
45
- b3_value = "#{span_context.hex_trace_id}-#{span_context.hex_span_id}-#{sampling_state}"
46
-
47
- setter ||= @default_setter
48
- setter.set(carrier, B3_CONTEXT_KEY, b3_value)
49
- carrier
50
- end
51
- end
52
- end
53
- end
54
- end
55
- end