opentelemetry-propagator-ottrace 0.16.0 → 0.19.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/CHANGELOG.md +24 -0
- data/lib/opentelemetry/propagator/ottrace.rb +7 -18
- data/lib/opentelemetry/propagator/ottrace/text_map_propagator.rb +142 -0
- data/lib/opentelemetry/propagator/ottrace/version.rb +1 -1
- metadata +10 -11
- data/lib/opentelemetry/propagator/ottrace/text_map_extractor.rb +0 -103
- data/lib/opentelemetry/propagator/ottrace/text_map_injector.rb +0 -74
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 5b5cf12e061f413b78abecc045bdca50df6da9290f0a8e3122b75294f06844d8
|
4
|
+
data.tar.gz: d932aab774e78dfc9a3b41952f10b074a7a3a11add0f31d5b17bbdb0690fe99f
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 93bd9f07eae87a8815d6d9fee2c28bc31099f3699c26740f62f81ff3b6e2330a293242749b6dbb5afd17bd55f74a4c414050917fcfd6ee8f568c708d99abc4b3
|
7
|
+
data.tar.gz: 26db4c6a5a39cb81a872aa6ccfecc5736e59f71cd84a41d6805bb1a59dbc938e75d066254c98f4319fbf09a74d51bbdd3534c7d66e009bee79a83fa21a9e3d32
|
data/CHANGELOG.md
CHANGED
@@ -1,5 +1,29 @@
|
|
1
1
|
# Release History: opentelemetry-propagator-ottrace
|
2
2
|
|
3
|
+
### v0.19.1 / 2021-08-12
|
4
|
+
|
5
|
+
* (No significant changes)
|
6
|
+
|
7
|
+
### v0.19.0 / 2021-06-23
|
8
|
+
|
9
|
+
* BREAKING CHANGE: Refactor Baggage to remove Noop*
|
10
|
+
|
11
|
+
* ADDED: Add Tracer.non_recording_span to API
|
12
|
+
* FIXED: Support Case Insensitive Trace and Span IDs
|
13
|
+
* FIXED: Refactor Baggage to remove Noop*
|
14
|
+
|
15
|
+
### v0.18.0 / 2021-05-21
|
16
|
+
|
17
|
+
* ADDED: Updated API depedency for 1.0.0.rc1
|
18
|
+
|
19
|
+
### v0.17.0 / 2021-04-22
|
20
|
+
|
21
|
+
* BREAKING CHANGE: Replace TextMapInjector/TextMapExtractor pairs with a TextMapPropagator.
|
22
|
+
|
23
|
+
[Check the propagator documentation](https://open-telemetry.github.io/opentelemetry-ruby/) for the new usage.
|
24
|
+
|
25
|
+
* FIXED: Refactor propagators to add #fields
|
26
|
+
|
3
27
|
### v0.16.0 / 2021-03-17
|
4
28
|
|
5
29
|
* Initial release.
|
@@ -6,6 +6,7 @@
|
|
6
6
|
|
7
7
|
require 'opentelemetry-api'
|
8
8
|
require 'opentelemetry/propagator/ottrace/version'
|
9
|
+
require 'opentelemetry/propagator/ottrace/text_map_propagator'
|
9
10
|
|
10
11
|
# OpenTelemetry is an open source observability framework, providing a
|
11
12
|
# general-purpose API, SDK, and related tools required for the instrumentation
|
@@ -20,27 +21,15 @@ module OpenTelemetry
|
|
20
21
|
module OTTrace
|
21
22
|
extend self
|
22
23
|
|
23
|
-
|
24
|
-
SPAN_ID_HEADER = 'ot-tracer-spanid'
|
25
|
-
SAMPLED_HEADER = 'ot-tracer-sampled'
|
26
|
-
BAGGAGE_HEADER_PREFIX = 'ot-baggage-'
|
24
|
+
TEXT_MAP_PROPAGATOR = TextMapPropagator.new
|
27
25
|
|
28
|
-
|
29
|
-
VALID_BAGGAGE_HEADER_NAME_CHARS = /^[\^_`a-zA-Z\-0-9!#$%&'*+.|~]+$/.freeze
|
30
|
-
INVALID_BAGGAGE_HEADER_VALUE_CHARS = /[^\t\u0020-\u007E\u0080-\u00FF]/.freeze
|
26
|
+
private_constant :TEXT_MAP_PROPAGATOR
|
31
27
|
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
## Returns an injector that injects context into a carrier
|
38
|
-
def text_map_injector
|
39
|
-
TextMapInjector.new
|
28
|
+
# Returns a text map propagator that propagates context using the
|
29
|
+
# OTTrace format.
|
30
|
+
def text_map_propagator
|
31
|
+
TEXT_MAP_PROPAGATOR
|
40
32
|
end
|
41
33
|
end
|
42
34
|
end
|
43
35
|
end
|
44
|
-
|
45
|
-
require_relative './ottrace/text_map_injector'
|
46
|
-
require_relative './ottrace/text_map_extractor'
|
@@ -0,0 +1,142 @@
|
|
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 OTTrace propagation
|
17
|
+
module OTTrace
|
18
|
+
# Propagates context using OTTrace header format
|
19
|
+
class TextMapPropagator
|
20
|
+
PADDING = '0' * 16
|
21
|
+
VALID_TRACE_ID_REGEX = /^[0-9a-f]{32}$/i.freeze
|
22
|
+
VALID_SPAN_ID_REGEX = /^[0-9a-f]{16}$/i.freeze
|
23
|
+
TRACE_ID_64_BIT_WIDTH = 64 / 4
|
24
|
+
TRACE_ID_HEADER = 'ot-tracer-traceid'
|
25
|
+
SPAN_ID_HEADER = 'ot-tracer-spanid'
|
26
|
+
SAMPLED_HEADER = 'ot-tracer-sampled'
|
27
|
+
BAGGAGE_HEADER_PREFIX = 'ot-baggage-'
|
28
|
+
FIELDS = [TRACE_ID_HEADER, SPAN_ID_HEADER, SAMPLED_HEADER].freeze
|
29
|
+
|
30
|
+
# https://github.com/open-telemetry/opentelemetry-specification/blob/14d123c121b6caa53bffd011292c42a181c9ca26/specification/context/api-propagators.md#textmap-propagator0
|
31
|
+
VALID_BAGGAGE_HEADER_NAME_CHARS = /^[\^_`a-zA-Z\-0-9!#$%&'*+.|~]+$/.freeze
|
32
|
+
INVALID_BAGGAGE_HEADER_VALUE_CHARS = /[^\t\u0020-\u007E\u0080-\u00FF]/.freeze
|
33
|
+
|
34
|
+
private_constant :PADDING, :VALID_TRACE_ID_REGEX, :VALID_SPAN_ID_REGEX, :TRACE_ID_64_BIT_WIDTH, :TRACE_ID_HEADER,
|
35
|
+
:SPAN_ID_HEADER, :SAMPLED_HEADER, :BAGGAGE_HEADER_PREFIX, :FIELDS, :VALID_BAGGAGE_HEADER_NAME_CHARS,
|
36
|
+
:INVALID_BAGGAGE_HEADER_VALUE_CHARS
|
37
|
+
|
38
|
+
# Extract OTTrace context from the supplied carrier and set the active span
|
39
|
+
# in the given context. The original context will be returned if OTTrace
|
40
|
+
# cannot be extracted from the carrier.
|
41
|
+
#
|
42
|
+
# @param [Carrier] carrier The carrier to get the header from.
|
43
|
+
# @param [optional Context] context The context to be updated with extracted context
|
44
|
+
# @param [optional Getter] getter If the optional getter is provided, it
|
45
|
+
# will be used to read the header from the carrier, otherwise the default
|
46
|
+
# getter will be used.
|
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: Context.current, getter: Context::Propagation.text_map_getter)
|
50
|
+
trace_id = optionally_pad_trace_id(getter.get(carrier, TRACE_ID_HEADER))
|
51
|
+
span_id = getter.get(carrier, SPAN_ID_HEADER)
|
52
|
+
sampled = getter.get(carrier, SAMPLED_HEADER)
|
53
|
+
|
54
|
+
return context unless valid?(trace_id: trace_id, span_id: span_id)
|
55
|
+
|
56
|
+
span_context = Trace::SpanContext.new(
|
57
|
+
trace_id: Array(trace_id).pack('H*'),
|
58
|
+
span_id: Array(span_id).pack('H*'),
|
59
|
+
trace_flags: sampled == 'true' ? Trace::TraceFlags::SAMPLED : Trace::TraceFlags::DEFAULT,
|
60
|
+
remote: true
|
61
|
+
)
|
62
|
+
|
63
|
+
span = OpenTelemetry::Trace.non_recording_span(span_context)
|
64
|
+
Trace.context_with_span(span, parent_context: set_baggage(carrier: carrier, context: context, getter: getter))
|
65
|
+
end
|
66
|
+
|
67
|
+
# @param [Object] carrier to update with context.
|
68
|
+
# @param [optional Context] context The active Context.
|
69
|
+
# @param [optional Setter] setter If the optional setter is provided, it
|
70
|
+
# will be used to write context into the carrier, otherwise the default
|
71
|
+
# setter will be used.
|
72
|
+
def inject(carrier, context: Context.current, setter: Context::Propagation.text_map_setter)
|
73
|
+
span_context = Trace.current_span(context).context
|
74
|
+
return unless span_context.valid?
|
75
|
+
|
76
|
+
inject_span_context(span_context: span_context, carrier: carrier, setter: setter)
|
77
|
+
inject_baggage(context: context, carrier: carrier, setter: setter)
|
78
|
+
|
79
|
+
nil
|
80
|
+
end
|
81
|
+
|
82
|
+
# Returns the predefined propagation fields. If your carrier is reused, you
|
83
|
+
# should delete the fields returned by this method before calling +inject+.
|
84
|
+
#
|
85
|
+
# @return [Array<String>] a list of fields that will be used by this propagator.
|
86
|
+
def fields
|
87
|
+
FIELDS
|
88
|
+
end
|
89
|
+
|
90
|
+
private
|
91
|
+
|
92
|
+
def valid?(trace_id:, span_id:)
|
93
|
+
!(VALID_TRACE_ID_REGEX !~ trace_id || VALID_SPAN_ID_REGEX !~ span_id)
|
94
|
+
end
|
95
|
+
|
96
|
+
def optionally_pad_trace_id(trace_id)
|
97
|
+
if trace_id&.length == 16
|
98
|
+
"#{PADDING}#{trace_id}"
|
99
|
+
else
|
100
|
+
trace_id
|
101
|
+
end
|
102
|
+
end
|
103
|
+
|
104
|
+
def set_baggage(carrier:, context:, getter:)
|
105
|
+
baggage.build(context: context) do |builder|
|
106
|
+
prefix = BAGGAGE_HEADER_PREFIX
|
107
|
+
getter.keys(carrier).each do |carrier_key|
|
108
|
+
baggage_key = carrier_key.start_with?(prefix) && carrier_key[prefix.length..-1]
|
109
|
+
next unless baggage_key
|
110
|
+
next unless VALID_BAGGAGE_HEADER_NAME_CHARS =~ baggage_key
|
111
|
+
|
112
|
+
value = getter.get(carrier, carrier_key)
|
113
|
+
next unless INVALID_BAGGAGE_HEADER_VALUE_CHARS !~ value
|
114
|
+
|
115
|
+
builder.set_value(baggage_key, value)
|
116
|
+
end
|
117
|
+
end
|
118
|
+
end
|
119
|
+
|
120
|
+
def baggage
|
121
|
+
OpenTelemetry::Baggage
|
122
|
+
end
|
123
|
+
|
124
|
+
def inject_span_context(span_context:, carrier:, setter:)
|
125
|
+
setter.set(carrier, TRACE_ID_HEADER, span_context.hex_trace_id[TRACE_ID_64_BIT_WIDTH, TRACE_ID_64_BIT_WIDTH])
|
126
|
+
setter.set(carrier, SPAN_ID_HEADER, span_context.hex_span_id)
|
127
|
+
setter.set(carrier, SAMPLED_HEADER, span_context.trace_flags.sampled?.to_s)
|
128
|
+
end
|
129
|
+
|
130
|
+
def inject_baggage(context:, carrier:, setter:)
|
131
|
+
baggage.values(context: context)
|
132
|
+
.select { |key, value| valid_baggage_entry?(key, value) }
|
133
|
+
.each { |key, value| setter.set(carrier, "#{BAGGAGE_HEADER_PREFIX}#{key}", value) }
|
134
|
+
end
|
135
|
+
|
136
|
+
def valid_baggage_entry?(key, value)
|
137
|
+
VALID_BAGGAGE_HEADER_NAME_CHARS =~ key && INVALID_BAGGAGE_HEADER_VALUE_CHARS !~ value
|
138
|
+
end
|
139
|
+
end
|
140
|
+
end
|
141
|
+
end
|
142
|
+
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: opentelemetry-propagator-ottrace
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.19.1
|
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-08-16 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.rc3
|
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.rc3
|
27
27
|
- !ruby/object:Gem::Dependency
|
28
28
|
name: bundler
|
29
29
|
requirement: !ruby/object:Gem::Requirement
|
@@ -58,14 +58,14 @@ dependencies:
|
|
58
58
|
requirements:
|
59
59
|
- - "~>"
|
60
60
|
- !ruby/object:Gem::Version
|
61
|
-
version: 0.
|
61
|
+
version: 1.0.0.rc1
|
62
62
|
type: :development
|
63
63
|
prerelease: false
|
64
64
|
version_requirements: !ruby/object:Gem::Requirement
|
65
65
|
requirements:
|
66
66
|
- - "~>"
|
67
67
|
- !ruby/object:Gem::Version
|
68
|
-
version: 0.
|
68
|
+
version: 1.0.0.rc1
|
69
69
|
- !ruby/object:Gem::Dependency
|
70
70
|
name: rake
|
71
71
|
requirement: !ruby/object:Gem::Requirement
|
@@ -149,17 +149,16 @@ files:
|
|
149
149
|
- README.md
|
150
150
|
- lib/opentelemetry-propagator-ottrace.rb
|
151
151
|
- lib/opentelemetry/propagator/ottrace.rb
|
152
|
-
- lib/opentelemetry/propagator/ottrace/
|
153
|
-
- lib/opentelemetry/propagator/ottrace/text_map_injector.rb
|
152
|
+
- lib/opentelemetry/propagator/ottrace/text_map_propagator.rb
|
154
153
|
- lib/opentelemetry/propagator/ottrace/version.rb
|
155
154
|
homepage: https://github.com/open-telemetry/opentelemetry-ruby
|
156
155
|
licenses:
|
157
156
|
- Apache-2.0
|
158
157
|
metadata:
|
159
|
-
changelog_uri: https://open-telemetry.github.io/opentelemetry-ruby/opentelemetry-propagator-ottrace/v0.
|
158
|
+
changelog_uri: https://open-telemetry.github.io/opentelemetry-ruby/opentelemetry-propagator-ottrace/v0.19.1/file.CHANGELOG.html
|
160
159
|
source_code_uri: https://github.com/open-telemetry/opentelemetry-ruby/tree/main/propagator/ottrace
|
161
160
|
bug_tracker_uri: https://github.com/open-telemetry/opentelemetry-ruby/issues
|
162
|
-
documentation_uri: https://open-telemetry.github.io/opentelemetry-ruby/opentelemetry-propagator-ottrace/v0.
|
161
|
+
documentation_uri: https://open-telemetry.github.io/opentelemetry-ruby/opentelemetry-propagator-ottrace/v0.19.1
|
163
162
|
post_install_message:
|
164
163
|
rdoc_options: []
|
165
164
|
require_paths:
|
@@ -175,7 +174,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
175
174
|
- !ruby/object:Gem::Version
|
176
175
|
version: '0'
|
177
176
|
requirements: []
|
178
|
-
rubygems_version: 3.1.
|
177
|
+
rubygems_version: 3.1.6
|
179
178
|
signing_key:
|
180
179
|
specification_version: 4
|
181
180
|
summary: OTTrace Context Propagation Extension for the OpenTelemetry framework
|
@@ -1,103 +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 OTTrace propagation
|
17
|
-
module OTTrace
|
18
|
-
# Extracts context from carriers using OTTrace header format
|
19
|
-
class TextMapExtractor
|
20
|
-
PADDING = '0' * 16
|
21
|
-
VALID_TRACE_ID_REGEX = /^[0-9a-f]{32}$/.freeze
|
22
|
-
VALID_SPAN_ID_REGEX = /^[0-9a-f]{16}$/.freeze
|
23
|
-
|
24
|
-
# Returns a new TextMapExtractor that extracts OTTrace context using the
|
25
|
-
# specified getter
|
26
|
-
#
|
27
|
-
# @param [optional Getter] default_getter The default getter used to read
|
28
|
-
# headers from a carrier during extract. Defaults to a
|
29
|
-
# {OpenTelemetry::Context:Propagation::TextMapGetter} instance.
|
30
|
-
# @return [TextMapExtractor]
|
31
|
-
def initialize(default_getter: Context::Propagation.text_map_getter)
|
32
|
-
@default_getter = default_getter
|
33
|
-
end
|
34
|
-
|
35
|
-
# Extract OTTrace context from the supplied carrier and set the active span
|
36
|
-
# in the given context. The original context will be returned if OTTrace
|
37
|
-
# cannot be extracted from the carrier.
|
38
|
-
#
|
39
|
-
# @param [Carrier] carrier The carrier to get the header from.
|
40
|
-
# @param [Context] context The context to be updated with extracted context
|
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
|
-
# getter will be used.
|
44
|
-
# @return [Context] Updated context with active span derived from the header, or the original
|
45
|
-
# context if parsing fails.
|
46
|
-
def extract(carrier, context, getter = nil)
|
47
|
-
getter ||= default_getter
|
48
|
-
trace_id = optionally_pad_trace_id(getter.get(carrier, TRACE_ID_HEADER))
|
49
|
-
span_id = getter.get(carrier, SPAN_ID_HEADER)
|
50
|
-
sampled = getter.get(carrier, SAMPLED_HEADER)
|
51
|
-
|
52
|
-
return context unless valid?(trace_id: trace_id, span_id: span_id)
|
53
|
-
|
54
|
-
span_context = Trace::SpanContext.new(
|
55
|
-
trace_id: Array(trace_id).pack('H*'),
|
56
|
-
span_id: Array(span_id).pack('H*'),
|
57
|
-
trace_flags: sampled == 'true' ? Trace::TraceFlags::SAMPLED : Trace::TraceFlags::DEFAULT,
|
58
|
-
remote: true
|
59
|
-
)
|
60
|
-
|
61
|
-
span = Trace::Span.new(span_context: span_context)
|
62
|
-
Trace.context_with_span(span, parent_context: set_baggage(carrier: carrier, context: context, getter: getter))
|
63
|
-
end
|
64
|
-
|
65
|
-
private
|
66
|
-
|
67
|
-
attr_reader :default_getter
|
68
|
-
|
69
|
-
def valid?(trace_id:, span_id:)
|
70
|
-
!(VALID_TRACE_ID_REGEX !~ trace_id || VALID_SPAN_ID_REGEX !~ span_id)
|
71
|
-
end
|
72
|
-
|
73
|
-
def optionally_pad_trace_id(trace_id)
|
74
|
-
if trace_id&.length == 16
|
75
|
-
"#{PADDING}#{trace_id}"
|
76
|
-
else
|
77
|
-
trace_id
|
78
|
-
end
|
79
|
-
end
|
80
|
-
|
81
|
-
def set_baggage(carrier:, context:, getter:)
|
82
|
-
baggage.build(context: context) do |builder|
|
83
|
-
prefix = OTTrace::BAGGAGE_HEADER_PREFIX
|
84
|
-
getter.keys(carrier).each do |carrier_key|
|
85
|
-
baggage_key = carrier_key.start_with?(prefix) && carrier_key[prefix.length..-1]
|
86
|
-
next unless baggage_key
|
87
|
-
next unless VALID_BAGGAGE_HEADER_NAME_CHARS =~ baggage_key
|
88
|
-
|
89
|
-
value = getter.get(carrier, carrier_key)
|
90
|
-
next unless INVALID_BAGGAGE_HEADER_VALUE_CHARS !~ value
|
91
|
-
|
92
|
-
builder.set_value(baggage_key, value)
|
93
|
-
end
|
94
|
-
end
|
95
|
-
end
|
96
|
-
|
97
|
-
def baggage
|
98
|
-
OpenTelemetry.baggage
|
99
|
-
end
|
100
|
-
end
|
101
|
-
end
|
102
|
-
end
|
103
|
-
end
|
@@ -1,74 +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 OTTrace propagation
|
17
|
-
module OTTrace
|
18
|
-
# Injects context into carriers using the OTTrace format
|
19
|
-
class TextMapInjector
|
20
|
-
TRACE_ID_64_BIT_WIDTH = 64 / 4
|
21
|
-
|
22
|
-
# Returns a new TextMapInjector that injects context using the specified setter
|
23
|
-
#
|
24
|
-
# @param [optional Setter] default_setter The default setter used to
|
25
|
-
# write context into a carrier during inject. Defaults to a
|
26
|
-
# {OpenTelemetry::Context:Propagation::TextMapSetter} instance.
|
27
|
-
# @return [TextMapInjector]
|
28
|
-
def initialize(default_setter: Context::Propagation.text_map_setter)
|
29
|
-
@default_setter = default_setter
|
30
|
-
end
|
31
|
-
|
32
|
-
# @param [Object] carrier to update with context.
|
33
|
-
# @param [Context] context The active Context.
|
34
|
-
# @param [optional Setter] setter If the optional setter is provided, it
|
35
|
-
# will be used to write context into the carrier, otherwise the default
|
36
|
-
# setter will be used.
|
37
|
-
def inject(carrier, context, setter = nil)
|
38
|
-
setter ||= default_setter
|
39
|
-
span_context = Trace.current_span(context).context
|
40
|
-
return unless span_context.valid?
|
41
|
-
|
42
|
-
inject_span_context(span_context: span_context, carrier: carrier, setter: setter)
|
43
|
-
inject_baggage(context: context, carrier: carrier, setter: setter)
|
44
|
-
|
45
|
-
nil
|
46
|
-
end
|
47
|
-
|
48
|
-
private
|
49
|
-
|
50
|
-
attr_reader :default_setter
|
51
|
-
|
52
|
-
def inject_span_context(span_context:, carrier:, setter:)
|
53
|
-
setter.set(carrier, TRACE_ID_HEADER, span_context.hex_trace_id[TRACE_ID_64_BIT_WIDTH, TRACE_ID_64_BIT_WIDTH])
|
54
|
-
setter.set(carrier, SPAN_ID_HEADER, span_context.hex_span_id)
|
55
|
-
setter.set(carrier, SAMPLED_HEADER, span_context.trace_flags.sampled?.to_s)
|
56
|
-
end
|
57
|
-
|
58
|
-
def inject_baggage(context:, carrier:, setter:)
|
59
|
-
baggage.values(context: context)
|
60
|
-
.select { |key, value| valid_baggage_entry?(key, value) }
|
61
|
-
.each { |key, value| setter.set(carrier, "#{BAGGAGE_HEADER_PREFIX}#{key}", value) }
|
62
|
-
end
|
63
|
-
|
64
|
-
def valid_baggage_entry?(key, value)
|
65
|
-
VALID_BAGGAGE_HEADER_NAME_CHARS =~ key && INVALID_BAGGAGE_HEADER_VALUE_CHARS !~ value
|
66
|
-
end
|
67
|
-
|
68
|
-
def baggage
|
69
|
-
OpenTelemetry.baggage
|
70
|
-
end
|
71
|
-
end
|
72
|
-
end
|
73
|
-
end
|
74
|
-
end
|