opentelemetry-propagator-b3 0.13.0 → 0.18.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 +4 -4
- data/CHANGELOG.md +27 -1
- data/README.md +2 -2
- data/lib/opentelemetry/propagator/b3.rb +1 -18
- data/lib/opentelemetry/propagator/b3/multi.rb +8 -39
- data/lib/opentelemetry/propagator/b3/multi/text_map_propagator.rb +64 -0
- data/lib/opentelemetry/propagator/b3/single.rb +8 -37
- data/lib/opentelemetry/propagator/b3/single/text_map_propagator.rb +64 -0
- data/lib/opentelemetry/propagator/b3/text_map_extractor.rb +116 -0
- data/lib/opentelemetry/propagator/b3/version.rb +1 -1
- metadata +10 -11
- data/lib/opentelemetry/propagator/b3/multi/text_map_extractor.rb +0 -109
- data/lib/opentelemetry/propagator/b3/multi/text_map_injector.rb +0 -66
- data/lib/opentelemetry/propagator/b3/single/text_map_extractor.rb +0 -81
- data/lib/opentelemetry/propagator/b3/single/text_map_injector.rb +0 -57
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: f45eb322f2e12362885f84237c00e8b789ee8f8f690fb2881fcb41b3ebe7f4bf
|
4
|
+
data.tar.gz: 17b80c35c2c1e9a4245f1cc1ff8f2694d4a3c21571653335af4ec2987aec519c
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 9f2cf1a10dad43d79c576860a0af40c49bcbaacf68e1ef09f518bf2c828c651bb9c9333c57c461fea1a8bccb3edaa1f897baab022c95dec4b9024b6f371fcb9b
|
7
|
+
data.tar.gz: d25666c4e62a9372773edd582d68c656278e9f75f63130208ccc6e7f5af182adc657d50eaf0a18dfd25d605f7a6ad45c15a8bc4bea6e6b0c9f828c8ac6fd70bb
|
data/CHANGELOG.md
CHANGED
@@ -1,5 +1,31 @@
|
|
1
1
|
# Release History: opentelemetry-propagator-b3
|
2
2
|
|
3
|
+
### v0.18.0 / 2021-05-21
|
4
|
+
|
5
|
+
* ADDED: Updated API depedency for 1.0.0.rc1
|
6
|
+
|
7
|
+
### v0.17.0 / 2021-04-22
|
8
|
+
|
9
|
+
* BREAKING CHANGE: Replace TextMapInjector/TextMapExtractor pairs with a TextMapPropagator.
|
10
|
+
|
11
|
+
[Check the propagator documentation](https://open-telemetry.github.io/opentelemetry-ruby/) for the new usage.
|
12
|
+
|
13
|
+
* FIXED: Refactor propagators to add #fields
|
14
|
+
|
15
|
+
### v0.16.0 / 2021-03-17
|
16
|
+
|
17
|
+
* DOCS: Replace Gitter with GitHub Discussions
|
18
|
+
|
19
|
+
### v0.15.0 / 2021-02-18
|
20
|
+
|
21
|
+
* (No significant changes)
|
22
|
+
|
23
|
+
### v0.14.0 / 2021-02-03
|
24
|
+
|
25
|
+
* BREAKING CHANGE: Replace getter and setter callables and remove rack specific propagators
|
26
|
+
|
27
|
+
* ADDED: Replace getter and setter callables and remove rack specific propagators
|
28
|
+
|
3
29
|
### v0.13.0 / 2021-01-29
|
4
30
|
|
5
31
|
* (No significant changes)
|
@@ -10,7 +36,7 @@
|
|
10
36
|
|
11
37
|
### v0.11.0 / 2020-12-11
|
12
38
|
|
13
|
-
* FIXED: Copyright comments to not reference year
|
39
|
+
* FIXED: Copyright comments to not reference year
|
14
40
|
|
15
41
|
### v0.10.0 / 2020-12-03
|
16
42
|
|
data/README.md
CHANGED
@@ -30,7 +30,7 @@ Or, if you use [bundler][bundler-home], include `opentelemetry-propagator-b3` in
|
|
30
30
|
|
31
31
|
The `opentelemetry-propagator-b3` gem source is [on github][repo-github], along with related gems including `opentelemetry-api` and `opentelemetry-sdk`.
|
32
32
|
|
33
|
-
The OpenTelemetry Ruby gems are maintained by the OpenTelemetry-Ruby special interest group (SIG). You can get involved by joining us
|
33
|
+
The OpenTelemetry Ruby gems are maintained by the OpenTelemetry-Ruby special interest group (SIG). You can get involved by joining us in [GitHub Discussions][discussions-url] or attending our weekly meeting. See the [meeting calendar][community-meetings] for dates and times. For more information on this and other language SIGs, see the OpenTelemetry [community page][ruby-sig].
|
34
34
|
|
35
35
|
## License
|
36
36
|
|
@@ -42,5 +42,5 @@ The `opentelemetry-propagator-b3` gem is distributed under the Apache 2.0 licens
|
|
42
42
|
[license-github]: https://github.com/open-telemetry/opentelemetry-ruby/blob/main/LICENSE
|
43
43
|
[ruby-sig]: https://github.com/open-telemetry/community#ruby-sig
|
44
44
|
[community-meetings]: https://github.com/open-telemetry/community#community-meetings
|
45
|
-
[
|
45
|
+
[discussions-url]: https://github.com/open-telemetry/opentelemetry-ruby/discussions
|
46
46
|
[b3-spec]: https://github.com/openzipkin/b3-propagation
|
@@ -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/
|
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,48 +17,18 @@ module OpenTelemetry
|
|
18
17
|
module Propagator
|
19
18
|
# Namespace for OpenTelemetry B3 propagation
|
20
19
|
module B3
|
21
|
-
# Namespace for OpenTelemetry
|
20
|
+
# Namespace for OpenTelemetry B3 multi header encoding
|
22
21
|
module Multi
|
23
22
|
extend self
|
24
23
|
|
25
|
-
|
26
|
-
TEXT_MAP_INJECTOR = TextMapInjector.new
|
27
|
-
RACK_KEYS = {
|
28
|
-
b3_trace_id_key: 'HTTP_X_B3_TRACEID',
|
29
|
-
b3_span_id_key: 'HTTP_X_B3_SPANID',
|
30
|
-
b3_sampled_key: 'HTTP_X_B3_SAMPLED',
|
31
|
-
b3_flags_key: 'HTTP_X_B3_FLAGS'
|
32
|
-
}.freeze
|
33
|
-
RACK_EXTRACTOR = TextMapExtractor.new(**RACK_KEYS)
|
34
|
-
RACK_INJECTOR = TextMapInjector.new(**RACK_KEYS)
|
24
|
+
TEXT_MAP_PROPAGATOR = TextMapPropagator.new
|
35
25
|
|
36
|
-
private_constant :
|
37
|
-
:RACK_EXTRACTOR, :RACK_KEYS
|
26
|
+
private_constant :TEXT_MAP_PROPAGATOR
|
38
27
|
|
39
|
-
# Returns
|
40
|
-
# format
|
41
|
-
def
|
42
|
-
|
43
|
-
end
|
44
|
-
|
45
|
-
# Returns an injector that injects context in the B3 multi header
|
46
|
-
# format
|
47
|
-
def text_map_extractor
|
48
|
-
TEXT_MAP_EXTRACTOR
|
49
|
-
end
|
50
|
-
|
51
|
-
# Returns an extractor that extracts context in the B3 multi header
|
52
|
-
# format with Rack normalized keys (upcased and prefixed with
|
53
|
-
# HTTP_)
|
54
|
-
def rack_injector
|
55
|
-
RACK_INJECTOR
|
56
|
-
end
|
57
|
-
|
58
|
-
# Returns an injector that injects context in the B3 multi header
|
59
|
-
# format with Rack normalized keys (upcased and prefixed with
|
60
|
-
# HTTP_)
|
61
|
-
def rack_extractor
|
62
|
-
RACK_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
|
63
32
|
end
|
64
33
|
end
|
65
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/
|
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,46 +17,18 @@ module OpenTelemetry
|
|
18
17
|
module Propagator
|
19
18
|
# Namespace for OpenTelemetry B3 propagation
|
20
19
|
module B3
|
21
|
-
# Namespace for OpenTelemetry
|
20
|
+
# Namespace for OpenTelemetry B3 single header encoding
|
22
21
|
module Single
|
23
22
|
extend self
|
24
23
|
|
25
|
-
|
26
|
-
TEXT_MAP_INJECTOR = TextMapInjector.new
|
27
|
-
RACK_EXTRACTOR = TextMapExtractor.new(
|
28
|
-
b3_key: 'HTTP_B3'
|
29
|
-
)
|
30
|
-
RACK_INJECTOR = TextMapInjector.new(
|
31
|
-
b3_key: 'HTTP_B3'
|
32
|
-
)
|
24
|
+
TEXT_MAP_PROPAGATOR = TextMapPropagator.new
|
33
25
|
|
34
|
-
private_constant :
|
35
|
-
:RACK_EXTRACTOR
|
26
|
+
private_constant :TEXT_MAP_PROPAGATOR
|
36
27
|
|
37
|
-
# Returns
|
38
|
-
# format
|
39
|
-
def
|
40
|
-
|
41
|
-
end
|
42
|
-
|
43
|
-
# Returns an injector that injects context in the B3 single header
|
44
|
-
# format
|
45
|
-
def text_map_extractor
|
46
|
-
TEXT_MAP_EXTRACTOR
|
47
|
-
end
|
48
|
-
|
49
|
-
# Returns an extractor that extracts context in the B3 single header
|
50
|
-
# format with Rack normalized keys (upcased and prefixed with
|
51
|
-
# HTTP_)
|
52
|
-
def rack_injector
|
53
|
-
RACK_INJECTOR
|
54
|
-
end
|
55
|
-
|
56
|
-
# Returns an injector that injects context in the B3 single header
|
57
|
-
# format with Rack normalized keys (upcased and prefixed with
|
58
|
-
# HTTP_)
|
59
|
-
def rack_extractor
|
60
|
-
RACK_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
|
61
32
|
end
|
62
33
|
end
|
63
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
|
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.
|
4
|
+
version: 0.18.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-
|
11
|
+
date: 2021-05-21 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.
|
19
|
+
version: 1.0.0.rc1
|
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.
|
26
|
+
version: 1.0.0.rc1
|
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/
|
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/
|
143
|
-
- lib/opentelemetry/propagator/b3/
|
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.
|
148
|
+
changelog_uri: https://open-telemetry.github.io/opentelemetry-ruby/opentelemetry-propagator-b3/v0.18.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.
|
151
|
+
documentation_uri: https://open-telemetry.github.io/opentelemetry-ruby/opentelemetry-propagator-b3/v0.18.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.
|
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,109 +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
|
-
include Context::Propagation::DefaultGetter
|
23
|
-
|
24
|
-
B3_TRACE_ID_REGEX = /\A(?:[0-9a-f]{16}){1,2}\z/.freeze
|
25
|
-
B3_SPAN_ID_REGEX = /\A[0-9a-f]{16}\z/.freeze
|
26
|
-
SAMPLED_VALUES = %w[1 true].freeze
|
27
|
-
DEBUG_FLAG = '1'
|
28
|
-
private_constant :B3_TRACE_ID_REGEX, :B3_SPAN_ID_REGEX, :SAMPLED_VALUES, :DEBUG_FLAG
|
29
|
-
|
30
|
-
# Returns a new TextMapExtractor that extracts b3 context using the
|
31
|
-
# specified header keys
|
32
|
-
#
|
33
|
-
# @param [String] b3_trace_id_key The b3 trace id key used in the carrier
|
34
|
-
# @param [String] b3_span_id_key The b3 span id key used in the carrier
|
35
|
-
# @param [String] b3_sampled_key The b3 sampled key used in the carrier
|
36
|
-
# @param [String] b3_flags_key The b3 flags key used in the carrier
|
37
|
-
# @return [TextMapExtractor]
|
38
|
-
def initialize(b3_trace_id_key: 'X-B3-TraceId',
|
39
|
-
b3_span_id_key: 'X-B3-SpanId',
|
40
|
-
b3_sampled_key: 'X-B3-Sampled',
|
41
|
-
b3_flags_key: 'X-B3-Flags')
|
42
|
-
@b3_trace_id_key = b3_trace_id_key
|
43
|
-
@b3_span_id_key = b3_span_id_key
|
44
|
-
@b3_sampled_key = b3_sampled_key
|
45
|
-
@b3_flags_key = b3_flags_key
|
46
|
-
end
|
47
|
-
|
48
|
-
# Extract b3 context from the supplied carrier and set the active span
|
49
|
-
# in the given context. The original context will be returned if b3
|
50
|
-
# cannot be extracted from the carrier.
|
51
|
-
#
|
52
|
-
# @param [Carrier] carrier The carrier to get the header from.
|
53
|
-
# @param [Context] context The context to be updated with extracted context
|
54
|
-
# @param [optional Callable] getter An optional callable that takes a carrier and a key and
|
55
|
-
# returns the value associated with the key. If omitted the default getter will be used
|
56
|
-
# which expects the carrier to respond to [] and []=.
|
57
|
-
# @yield [Carrier, String] if an optional getter is provided, extract will yield the carrier
|
58
|
-
# and the header key to the getter.
|
59
|
-
# @return [Context] Updated context with active span derived from the header, or the original
|
60
|
-
# context if parsing fails.
|
61
|
-
def extract(carrier, context, &getter)
|
62
|
-
getter ||= default_getter
|
63
|
-
|
64
|
-
trace_id_hex = getter.call(carrier, @b3_trace_id_key)
|
65
|
-
return context unless valid_trace_id?(trace_id_hex)
|
66
|
-
|
67
|
-
span_id_hex = getter.call(carrier, @b3_span_id_key)
|
68
|
-
return context unless valid_span_id?(span_id_hex)
|
69
|
-
|
70
|
-
sampled = getter.call(carrier, @b3_sampled_key)
|
71
|
-
flags = getter.call(carrier, @b3_flags_key)
|
72
|
-
|
73
|
-
context = B3.context_with_debug(context) if flags == DEBUG_FLAG
|
74
|
-
|
75
|
-
span_context = Trace::SpanContext.new(
|
76
|
-
trace_id: B3.to_trace_id(trace_id_hex),
|
77
|
-
span_id: B3.to_span_id(span_id_hex),
|
78
|
-
trace_flags: to_trace_flags(sampled, flags),
|
79
|
-
remote: true
|
80
|
-
)
|
81
|
-
|
82
|
-
span = Trace::Span.new(span_context: span_context)
|
83
|
-
Trace.context_with_span(span, parent_context: context)
|
84
|
-
rescue OpenTelemetry::Error
|
85
|
-
context
|
86
|
-
end
|
87
|
-
|
88
|
-
private
|
89
|
-
|
90
|
-
def to_trace_flags(sampled, b3_flags)
|
91
|
-
if b3_flags == DEBUG_FLAG || SAMPLED_VALUES.include?(sampled)
|
92
|
-
Trace::TraceFlags::SAMPLED
|
93
|
-
else
|
94
|
-
Trace::TraceFlags::DEFAULT
|
95
|
-
end
|
96
|
-
end
|
97
|
-
|
98
|
-
def valid_trace_id?(trace_id)
|
99
|
-
B3_TRACE_ID_REGEX.match?(trace_id)
|
100
|
-
end
|
101
|
-
|
102
|
-
def valid_span_id?(span_id)
|
103
|
-
B3_SPAN_ID_REGEX.match?(span_id)
|
104
|
-
end
|
105
|
-
end
|
106
|
-
end
|
107
|
-
end
|
108
|
-
end
|
109
|
-
end
|
@@ -1,66 +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
|
-
include Context::Propagation::DefaultSetter
|
16
|
-
|
17
|
-
# Returns a new TextMapInjector that extracts b3 context using the
|
18
|
-
# specified header keys
|
19
|
-
#
|
20
|
-
# @param [String] b3_trace_id_key The b3 trace id key used in the carrier
|
21
|
-
# @param [String] b3_span_id_key The b3 span id key used in the carrier
|
22
|
-
# @param [String] b3_sampled_key The b3 sampled key used in the carrier
|
23
|
-
# @param [String] b3_flags_key The b3 flags key used in the carrier
|
24
|
-
# @return [TextMapInjector]
|
25
|
-
def initialize(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
|
-
@b3_trace_id_key = b3_trace_id_key
|
30
|
-
@b3_span_id_key = b3_span_id_key
|
31
|
-
@b3_sampled_key = b3_sampled_key
|
32
|
-
@b3_flags_key = b3_flags_key
|
33
|
-
end
|
34
|
-
|
35
|
-
# Set the span context on the supplied carrier.
|
36
|
-
#
|
37
|
-
# @param [Context] context The active Context.
|
38
|
-
# @param [optional Callable] setter An optional callable that takes a carrier and a key and
|
39
|
-
# a value and assigns the key-value pair in the carrier. If omitted the default setter
|
40
|
-
# will be used which expects the carrier to respond to [] and []=.
|
41
|
-
# @yield [Carrier, String, String] if an optional setter is provided, inject will yield
|
42
|
-
# carrier, header key, header value to the setter.
|
43
|
-
# @return [Object] the carrier with context injected
|
44
|
-
def inject(carrier, context, &setter)
|
45
|
-
span_context = Trace.current_span(context).context
|
46
|
-
return unless span_context.valid?
|
47
|
-
|
48
|
-
setter ||= default_setter
|
49
|
-
setter.call(carrier, @b3_trace_id_key, span_context.hex_trace_id)
|
50
|
-
setter.call(carrier, @b3_span_id_key, span_context.hex_span_id)
|
51
|
-
|
52
|
-
if B3.debug?(context)
|
53
|
-
setter.call(carrier, @b3_flags_key, '1')
|
54
|
-
elsif span_context.trace_flags.sampled?
|
55
|
-
setter.call(carrier, @b3_sampled_key, '1')
|
56
|
-
else
|
57
|
-
setter.call(carrier, @b3_sampled_key, '0')
|
58
|
-
end
|
59
|
-
|
60
|
-
carrier
|
61
|
-
end
|
62
|
-
end
|
63
|
-
end
|
64
|
-
end
|
65
|
-
end
|
66
|
-
end
|
@@ -1,81 +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
|
-
include Context::Propagation::DefaultGetter
|
23
|
-
|
24
|
-
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
|
25
|
-
SAMPLED_VALUES = %w[1 d].freeze
|
26
|
-
|
27
|
-
# Returns a new TextMapExtractor that extracts b3 context using the
|
28
|
-
# specified header keys
|
29
|
-
#
|
30
|
-
# @param [String] b3_key The b3 header key used in the carrier
|
31
|
-
# @return [TextMapExtractor]
|
32
|
-
def initialize(b3_key: 'b3')
|
33
|
-
@b3_key = b3_key
|
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 Callable] getter An optional callable that takes a carrier and a key and
|
43
|
-
# returns the value associated with the key. If omitted the default getter will be used
|
44
|
-
# which expects the carrier to respond to [] and []=.
|
45
|
-
# @yield [Carrier, String] if an optional getter is provided, extract will yield the carrier
|
46
|
-
# and the header key to the getter.
|
47
|
-
# @return [Context] Updated context with active span derived from the header, or the original
|
48
|
-
# context if parsing fails.
|
49
|
-
def extract(carrier, context, &getter)
|
50
|
-
getter ||= default_getter
|
51
|
-
header = getter.call(carrier, @b3_key)
|
52
|
-
return context unless (match = header.match(B3_CONTEXT_REGEX))
|
53
|
-
|
54
|
-
span_context = Trace::SpanContext.new(
|
55
|
-
trace_id: B3.to_trace_id(match['trace_id']),
|
56
|
-
span_id: B3.to_span_id(match['span_id']),
|
57
|
-
trace_flags: to_trace_flags(match['sampling_state']),
|
58
|
-
remote: true
|
59
|
-
)
|
60
|
-
|
61
|
-
span = Trace::Span.new(span_context: span_context)
|
62
|
-
context = B3.context_with_debug(context) if match['sampling_state'] == 'd'
|
63
|
-
Trace.context_with_span(span, parent_context: context)
|
64
|
-
rescue OpenTelemetry::Error
|
65
|
-
context
|
66
|
-
end
|
67
|
-
|
68
|
-
private
|
69
|
-
|
70
|
-
def to_trace_flags(sampling_state)
|
71
|
-
if SAMPLED_VALUES.include?(sampling_state)
|
72
|
-
Trace::TraceFlags::SAMPLED
|
73
|
-
else
|
74
|
-
Trace::TraceFlags::DEFAULT
|
75
|
-
end
|
76
|
-
end
|
77
|
-
end
|
78
|
-
end
|
79
|
-
end
|
80
|
-
end
|
81
|
-
end
|
@@ -1,57 +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
|
-
include Context::Propagation::DefaultSetter
|
16
|
-
|
17
|
-
# Returns a new TextMapInjector that extracts b3 context using the
|
18
|
-
# specified header keys
|
19
|
-
#
|
20
|
-
# @param [String] b3_key The b3 header key used in the carrier
|
21
|
-
# @return [TextMapInjector]
|
22
|
-
def initialize(b3_key: 'b3')
|
23
|
-
@b3_key = b3_key
|
24
|
-
end
|
25
|
-
|
26
|
-
# Set the span context on the supplied carrier.
|
27
|
-
#
|
28
|
-
# @param [Context] context The active Context.
|
29
|
-
# @param [optional Callable] setter An optional callable that takes a carrier and a key and
|
30
|
-
# a value and assigns the key-value pair in the carrier. If omitted the default setter
|
31
|
-
# will be used which expects the carrier to respond to [] and []=.
|
32
|
-
# @yield [Carrier, String, String] if an optional setter is provided, inject will yield
|
33
|
-
# carrier, header key, header value to the setter.
|
34
|
-
# @return [Object] the carrier with context injected
|
35
|
-
def inject(carrier, context, &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 ||= default_setter
|
50
|
-
setter.call(carrier, @b3_key, b3_value)
|
51
|
-
carrier
|
52
|
-
end
|
53
|
-
end
|
54
|
-
end
|
55
|
-
end
|
56
|
-
end
|
57
|
-
end
|