opentelemetry-api 0.14.0 → 1.0.0.rc2
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 +51 -0
- data/README.md +2 -2
- data/lib/opentelemetry.rb +21 -28
- data/lib/opentelemetry/baggage.rb +95 -2
- data/lib/opentelemetry/baggage/builder.rb +30 -4
- data/lib/opentelemetry/baggage/entry.rb +20 -0
- data/lib/opentelemetry/baggage/propagation.rb +9 -18
- data/lib/opentelemetry/baggage/propagation/text_map_propagator.rb +110 -0
- data/lib/opentelemetry/context.rb +48 -33
- data/lib/opentelemetry/context/propagation.rb +35 -5
- data/lib/opentelemetry/context/propagation/composite_text_map_propagator.rb +105 -0
- data/lib/opentelemetry/context/propagation/noop_text_map_propagator.rb +51 -0
- data/lib/opentelemetry/context/propagation/rack_env_getter.rb +3 -1
- data/lib/opentelemetry/context/propagation/{propagator.rb → text_map_propagator.rb} +23 -16
- data/lib/opentelemetry/internal.rb +17 -0
- data/lib/opentelemetry/internal/proxy_tracer.rb +38 -0
- data/lib/opentelemetry/internal/proxy_tracer_provider.rb +59 -0
- data/lib/opentelemetry/trace.rb +10 -0
- data/lib/opentelemetry/trace/propagation/trace_context.rb +7 -18
- data/lib/opentelemetry/trace/propagation/trace_context/text_map_propagator.rb +73 -0
- data/lib/opentelemetry/trace/span.rb +20 -0
- data/lib/opentelemetry/trace/status.rb +34 -5
- data/lib/opentelemetry/trace/tracer.rb +8 -9
- data/lib/opentelemetry/version.rb +1 -1
- metadata +16 -25
- data/lib/opentelemetry/baggage/manager.rb +0 -41
- data/lib/opentelemetry/baggage/propagation/text_map_extractor.rb +0 -57
- data/lib/opentelemetry/baggage/propagation/text_map_injector.rb +0 -52
- data/lib/opentelemetry/context/propagation/composite_propagator.rb +0 -72
- data/lib/opentelemetry/context/propagation/noop_extractor.rb +0 -26
- data/lib/opentelemetry/context/propagation/noop_injector.rb +0 -26
- data/lib/opentelemetry/instrumentation.rb +0 -15
- data/lib/opentelemetry/instrumentation/base.rb +0 -245
- data/lib/opentelemetry/instrumentation/registry.rb +0 -86
- data/lib/opentelemetry/metrics.rb +0 -16
- data/lib/opentelemetry/metrics/handles.rb +0 -44
- data/lib/opentelemetry/metrics/instruments.rb +0 -105
- data/lib/opentelemetry/metrics/meter.rb +0 -72
- data/lib/opentelemetry/metrics/meter_provider.rb +0 -22
- data/lib/opentelemetry/trace/propagation/trace_context/text_map_extractor.rb +0 -52
- data/lib/opentelemetry/trace/propagation/trace_context/text_map_injector.rb +0 -49
- data/lib/opentelemetry/trace/util/http_to_status.rb +0 -28
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 87cc02bff4251a383a6bd5ea88b50f0c47b6aeb206d2585a6ddccbc9811aa503
|
4
|
+
data.tar.gz: 1ce534ee66ea666a853e2625563c515f7a7439c791401997c3a35c7515849fcd
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 7ea3d7eca27eda27dd1954cd9056ba3567a3d68ccafda1a55f9683acc381ef18e37ef319d12124ef11fe4b75af528c7d4157b8b7ed15d7dbde81487e36565c70
|
7
|
+
data.tar.gz: 878d7960e8b69e8d02a5e9354b8737250f7abacd9656b09b3ccd72d493abcdbd20aae4a76e5ff63d9dd8050a6714b410d948ad81cb489c5af8a44856316f4e35
|
data/CHANGELOG.md
CHANGED
@@ -1,5 +1,56 @@
|
|
1
1
|
# Release History: opentelemetry-api
|
2
2
|
|
3
|
+
### v1.0.0.rc2 / 2021-06-23
|
4
|
+
|
5
|
+
* BREAKING CHANGE: Remove optional parent_context from in_span [729](https://github.com/open-telemetry/opentelemetry-ruby/pull/729)
|
6
|
+
* BREAKING CHANGE: Refactor Baggage to remove Noop* [800](https://github.com/open-telemetry/opentelemetry-ruby/pull/800)
|
7
|
+
- The noop baggage manger has been removed.
|
8
|
+
- The baggage management methods are now available through OpenTelemetry::Baggage#method, previously OpenTelemetry.baggage#method
|
9
|
+
* BREAKING CHANGE: Total order constraint on span.status= [805](https://github.com/open-telemetry/opentelemetry-ruby/pull/805)
|
10
|
+
- The OpenTelemetry::Trace::Util::HttpToStatus module has been removed as it was incorrectly setting the span status to OK for codes codes in the range 100..399
|
11
|
+
- The HttpToStatus module can be replaced inline as follows `span.status = OpenTelemetry::Trace::Status.error unless (100..399).include?(response_code.to_i)`
|
12
|
+
- The `Status.new(code, description:)` initializer has been hidden in favour of simpler constructors for each status code: `Status.ok`, `Status.error` and `Status.unset`. Each constructor takes an optional description.
|
13
|
+
|
14
|
+
* ADDED: Add Tracer.non_recording_span to API [799](https://github.com/open-telemetry/opentelemetry-ruby/pull/799)
|
15
|
+
* FIXED: Reduce span allocation in API [795](https://github.com/open-telemetry/opentelemetry-ruby/pull/795)
|
16
|
+
* FIXED: Return early if carrier is nil [835](https://github.com/open-telemetry/opentelemetry-ruby/pull/835)
|
17
|
+
* FIXED: Update context to match spec [807](https://github.com/open-telemetry/opentelemetry-ruby/pull/807)
|
18
|
+
- The `Context.current` setter has been removed and the previously private attach/detach methods are now available as class methods on the context module.
|
19
|
+
|
20
|
+
### v1.0.0.rc1 / 2021-05-21
|
21
|
+
|
22
|
+
* BREAKING CHANGE: Remove optional parent_context from in_span
|
23
|
+
|
24
|
+
* FIXED: Remove optional parent_context from in_span
|
25
|
+
|
26
|
+
### v0.17.0 / 2021-04-22
|
27
|
+
|
28
|
+
* BREAKING CHANGE: Replace TextMapInjector/TextMapExtractor pairs with a TextMapPropagator.
|
29
|
+
|
30
|
+
[Check the propagator documentation](https://open-telemetry.github.io/opentelemetry-ruby/) for the new usage.
|
31
|
+
* BREAKING CHANGE: Remove metrics API.
|
32
|
+
|
33
|
+
`OpenTelemetry::Metrics` and all of its behavior removed until spec stabilizes.
|
34
|
+
* BREAKING CHANGE: Extract instrumentation base from api (#698).
|
35
|
+
|
36
|
+
To take advantage of a base instrumentation class to create your own auto-instrumentation, require and use the `opentelemetry-instrumentation-base` gem.
|
37
|
+
|
38
|
+
* ADDED: Default noop tracer for instrumentation
|
39
|
+
* FIXED: Refactor propagators to add #fields
|
40
|
+
* FIXED: Remove metrics API
|
41
|
+
* FIXED: Dynamically upgrade global tracer provider
|
42
|
+
|
43
|
+
### v0.16.0 / 2021-03-17
|
44
|
+
|
45
|
+
* ADDED: Span#add_attributes
|
46
|
+
* FIXED: Handle rack env getter edge cases
|
47
|
+
* DOCS: Replace Gitter with GitHub Discussions
|
48
|
+
|
49
|
+
### v0.15.0 / 2021-02-18
|
50
|
+
|
51
|
+
* ADDED: Add instrumentation config validation
|
52
|
+
* DOCS: Clarify nil attribute values not allowed
|
53
|
+
|
3
54
|
### v0.14.0 / 2021-02-03
|
4
55
|
|
5
56
|
* BREAKING CHANGE: Replace getter and setter callables and remove rack specific propagators
|
data/README.md
CHANGED
@@ -49,7 +49,7 @@ For additional examples, see the [examples on github][examples-github].
|
|
49
49
|
|
50
50
|
The `opentelemetry-api` gem source is [on github][repo-github], along with related gems including `opentelemetry-sdk`.
|
51
51
|
|
52
|
-
The OpenTelemetry Ruby gems are maintained by the OpenTelemetry-Ruby special interest group (SIG). You can get involved by joining us
|
52
|
+
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].
|
53
53
|
|
54
54
|
## License
|
55
55
|
|
@@ -63,4 +63,4 @@ The `opentelemetry-api` gem is distributed under the Apache 2.0 license. See [LI
|
|
63
63
|
[examples-github]: https://github.com/open-telemetry/opentelemetry-ruby/tree/main/examples
|
64
64
|
[ruby-sig]: https://github.com/open-telemetry/community#ruby-sig
|
65
65
|
[community-meetings]: https://github.com/open-telemetry/community#community-meetings
|
66
|
-
[
|
66
|
+
[discussions-url]: https://github.com/open-telemetry/opentelemetry-ruby/discussions
|
data/lib/opentelemetry.rb
CHANGED
@@ -9,9 +9,8 @@ require 'logger'
|
|
9
9
|
require 'opentelemetry/error'
|
10
10
|
require 'opentelemetry/context'
|
11
11
|
require 'opentelemetry/baggage'
|
12
|
-
require_relative './opentelemetry/instrumentation'
|
13
|
-
require 'opentelemetry/metrics'
|
14
12
|
require 'opentelemetry/trace'
|
13
|
+
require 'opentelemetry/internal'
|
15
14
|
require 'opentelemetry/version'
|
16
15
|
|
17
16
|
# OpenTelemetry is an open source observability framework, providing a
|
@@ -22,8 +21,10 @@ require 'opentelemetry/version'
|
|
22
21
|
module OpenTelemetry
|
23
22
|
extend self
|
24
23
|
|
25
|
-
|
26
|
-
|
24
|
+
@mutex = Mutex.new
|
25
|
+
@tracer_provider = Internal::ProxyTracerProvider.new
|
26
|
+
|
27
|
+
attr_writer :propagation, :logger, :error_handler
|
27
28
|
|
28
29
|
# @return [Object, Logger] configured Logger or a default STDOUT Logger.
|
29
30
|
def logger
|
@@ -44,36 +45,28 @@ module OpenTelemetry
|
|
44
45
|
error_handler.call(exception: exception, message: message)
|
45
46
|
end
|
46
47
|
|
48
|
+
# Register the global tracer provider.
|
49
|
+
#
|
50
|
+
# @param [TracerProvider] provider A tracer provider to register as the
|
51
|
+
# global instance.
|
52
|
+
def tracer_provider=(provider)
|
53
|
+
@mutex.synchronize do
|
54
|
+
if @tracer_provider.instance_of? Internal::ProxyTracerProvider
|
55
|
+
logger.debug("Upgrading default proxy tracer provider to #{provider.class}")
|
56
|
+
@tracer_provider.delegate = provider
|
57
|
+
end
|
58
|
+
@tracer_provider = provider
|
59
|
+
end
|
60
|
+
end
|
61
|
+
|
47
62
|
# @return [Object, Trace::TracerProvider] registered tracer provider or a
|
48
63
|
# default no-op implementation of the tracer provider.
|
49
64
|
def tracer_provider
|
50
|
-
@tracer_provider
|
51
|
-
end
|
52
|
-
|
53
|
-
# @return [Object, Metrics::MeterProvider] registered meter provider or a
|
54
|
-
# default no-op implementation of the meter provider.
|
55
|
-
def meter_provider
|
56
|
-
@meter_provider ||= Metrics::MeterProvider.new
|
57
|
-
end
|
58
|
-
|
59
|
-
# @return [Instrumentation::Registry] registry containing all known
|
60
|
-
# instrumentation
|
61
|
-
def instrumentation_registry
|
62
|
-
@instrumentation_registry ||= Instrumentation::Registry.new
|
63
|
-
end
|
64
|
-
|
65
|
-
# @return [Object, Baggage::Manager] registered
|
66
|
-
# baggage manager or a default no-op implementation of the
|
67
|
-
# manager.
|
68
|
-
def baggage
|
69
|
-
@baggage ||= Baggage::Manager.new
|
65
|
+
@mutex.synchronize { @tracer_provider }
|
70
66
|
end
|
71
67
|
|
72
68
|
# @return [Context::Propagation::Propagator] a propagator instance
|
73
69
|
def propagation
|
74
|
-
@propagation ||= Context::Propagation::
|
75
|
-
Context::Propagation::NoopInjector.new,
|
76
|
-
Context::Propagation::NoopExtractor.new
|
77
|
-
)
|
70
|
+
@propagation ||= Context::Propagation::NoopTextMapPropagator.new
|
78
71
|
end
|
79
72
|
end
|
@@ -4,13 +4,106 @@
|
|
4
4
|
#
|
5
5
|
# SPDX-License-Identifier: Apache-2.0
|
6
6
|
|
7
|
-
require 'opentelemetry/baggage/builder'
|
8
|
-
require 'opentelemetry/baggage/manager'
|
9
7
|
require 'opentelemetry/baggage/propagation'
|
8
|
+
require 'opentelemetry/baggage/builder'
|
9
|
+
require 'opentelemetry/baggage/entry'
|
10
10
|
|
11
11
|
module OpenTelemetry
|
12
12
|
# The Baggage module provides functionality to record and propagate
|
13
13
|
# baggage in a distributed trace
|
14
14
|
module Baggage
|
15
|
+
extend self
|
16
|
+
|
17
|
+
BAGGAGE_KEY = OpenTelemetry::Baggage::Propagation::ContextKeys.baggage_key
|
18
|
+
EMPTY_BAGGAGE = {}.freeze
|
19
|
+
private_constant(:BAGGAGE_KEY, :EMPTY_BAGGAGE)
|
20
|
+
|
21
|
+
# Used to chain modifications to baggage. The result is a
|
22
|
+
# context with an updated baggage. If only a single
|
23
|
+
# modification is being made to baggage, use the other
|
24
|
+
# methods on +Baggage+, if multiple modifications are being made, use
|
25
|
+
# this one.
|
26
|
+
#
|
27
|
+
# @param [optional Context] context The context to update with with new
|
28
|
+
# modified baggage. Defaults to +Context.current+
|
29
|
+
# @return [Context]
|
30
|
+
def build(context: Context.current)
|
31
|
+
builder = Builder.new(baggage_for(context).dup)
|
32
|
+
yield builder
|
33
|
+
context.set_value(BAGGAGE_KEY, builder.entries)
|
34
|
+
end
|
35
|
+
|
36
|
+
# Returns a new context with empty baggage
|
37
|
+
#
|
38
|
+
# @param [optional Context] context Context to clear baggage from. Defaults
|
39
|
+
# to +Context.current+
|
40
|
+
# @return [Context]
|
41
|
+
def clear(context: Context.current)
|
42
|
+
context.set_value(BAGGAGE_KEY, EMPTY_BAGGAGE)
|
43
|
+
end
|
44
|
+
|
45
|
+
# Returns the corresponding baggage.entry (or nil) for key
|
46
|
+
#
|
47
|
+
# @param [String] key The lookup key
|
48
|
+
# @param [optional Context] context The context from which to retrieve
|
49
|
+
# the key.
|
50
|
+
# Defaults to +Context.current+
|
51
|
+
# @return [String]
|
52
|
+
def value(key, context: Context.current)
|
53
|
+
baggage_for(context)[key]&.value
|
54
|
+
end
|
55
|
+
|
56
|
+
# Returns the baggage
|
57
|
+
#
|
58
|
+
# @param [optional Context] context The context from which to retrieve
|
59
|
+
# the baggage.
|
60
|
+
# Defaults to +Context.current+
|
61
|
+
# @return [Hash]
|
62
|
+
def values(context: Context.current)
|
63
|
+
baggage_for(context).transform_values(&:value)
|
64
|
+
end
|
65
|
+
|
66
|
+
# @api private
|
67
|
+
def raw_entries(context: Context.current)
|
68
|
+
baggage_for(context).dup.freeze
|
69
|
+
end
|
70
|
+
|
71
|
+
# Returns a new context with new key-value pair
|
72
|
+
#
|
73
|
+
# @param [String] key The key to store this value under
|
74
|
+
# @param [String] value String value to be stored under key
|
75
|
+
# @param [optional String] metadata This is here to store properties
|
76
|
+
# received from other W3C Baggage impelmentations but is not exposed in
|
77
|
+
# OpenTelemetry. This is condsidered private API and not for use by
|
78
|
+
# end-users.
|
79
|
+
# @param [optional Context] context The context to update with new
|
80
|
+
# value. Defaults to +Context.current+
|
81
|
+
# @return [Context]
|
82
|
+
def set_value(key, value, metadata: nil, context: Context.current)
|
83
|
+
new_baggage = baggage_for(context).dup
|
84
|
+
new_baggage[key] = Entry.new(value, metadata)
|
85
|
+
context.set_value(BAGGAGE_KEY, new_baggage)
|
86
|
+
end
|
87
|
+
|
88
|
+
# Returns a new context with value at key removed
|
89
|
+
#
|
90
|
+
# @param [String] key The key to remove
|
91
|
+
# @param [optional Context] context The context to remove baggage
|
92
|
+
# from. Defaults to +Context.current+
|
93
|
+
# @return [Context]
|
94
|
+
def remove_value(key, context: Context.current)
|
95
|
+
baggage = baggage_for(context)
|
96
|
+
return context unless baggage.key?(key)
|
97
|
+
|
98
|
+
new_baggage = baggage.dup
|
99
|
+
new_baggage.delete(key)
|
100
|
+
context.set_value(BAGGAGE_KEY, new_baggage)
|
101
|
+
end
|
102
|
+
|
103
|
+
private
|
104
|
+
|
105
|
+
def baggage_for(context)
|
106
|
+
context.value(BAGGAGE_KEY) || EMPTY_BAGGAGE
|
107
|
+
end
|
15
108
|
end
|
16
109
|
end
|
@@ -6,13 +6,39 @@
|
|
6
6
|
|
7
7
|
module OpenTelemetry
|
8
8
|
module Baggage
|
9
|
-
#
|
9
|
+
# Operational implementation of Baggage::Builder
|
10
10
|
class Builder
|
11
|
-
|
11
|
+
# @api private
|
12
|
+
attr_reader :entries
|
12
13
|
|
13
|
-
|
14
|
+
# @api private
|
15
|
+
def initialize(entries)
|
16
|
+
@entries = entries
|
17
|
+
end
|
14
18
|
|
15
|
-
|
19
|
+
# Set key-value in the to-be-created baggage
|
20
|
+
#
|
21
|
+
# @param [String] key The key to store this value under
|
22
|
+
# @param [String] value String value to be stored under key
|
23
|
+
# @param [optional String] metadata This is here to store properties
|
24
|
+
# received from other W3C Baggage impelmentations but is not exposed in
|
25
|
+
# OpenTelemetry. This is condsidered private API and not for use by
|
26
|
+
# end-users.
|
27
|
+
def set_value(key, value, metadata: nil)
|
28
|
+
@entries[key] = OpenTelemetry::Baggage::Entry.new(value, metadata)
|
29
|
+
end
|
30
|
+
|
31
|
+
# Removes key from the to-be-created baggage
|
32
|
+
#
|
33
|
+
# @param [String] key The key to remove
|
34
|
+
def remove_value(key)
|
35
|
+
@entries.delete(key)
|
36
|
+
end
|
37
|
+
|
38
|
+
# Clears all baggage from the to-be-created baggage
|
39
|
+
def clear
|
40
|
+
@entries.clear
|
41
|
+
end
|
16
42
|
end
|
17
43
|
end
|
18
44
|
end
|
@@ -0,0 +1,20 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
# Copyright The OpenTelemetry Authors
|
4
|
+
#
|
5
|
+
# SPDX-License-Identifier: Apache-2.0
|
6
|
+
|
7
|
+
module OpenTelemetry
|
8
|
+
# OpenTelemetry Baggage Implementation
|
9
|
+
module Baggage
|
10
|
+
# Read-only representation of a baggage entry
|
11
|
+
class Entry
|
12
|
+
attr_reader :value, :metadata
|
13
|
+
|
14
|
+
def initialize(value, metadata = nil)
|
15
|
+
@value = value
|
16
|
+
@metadata = metadata
|
17
|
+
end
|
18
|
+
end
|
19
|
+
end
|
20
|
+
end
|
@@ -5,32 +5,23 @@
|
|
5
5
|
# SPDX-License-Identifier: Apache-2.0
|
6
6
|
|
7
7
|
require 'opentelemetry/baggage/propagation/context_keys'
|
8
|
-
require 'opentelemetry/baggage/propagation/
|
9
|
-
require 'opentelemetry/baggage/propagation/text_map_extractor'
|
8
|
+
require 'opentelemetry/baggage/propagation/text_map_propagator'
|
10
9
|
|
11
10
|
module OpenTelemetry
|
12
11
|
module Baggage
|
13
|
-
# The Baggage::Propagation module contains
|
14
|
-
#
|
12
|
+
# The Baggage::Propagation module contains a text map propagator for
|
13
|
+
# sending and receiving baggage over the wire.
|
15
14
|
module Propagation
|
16
15
|
extend self
|
17
16
|
|
18
|
-
|
19
|
-
TEXT_MAP_EXTRACTOR = TextMapExtractor.new
|
20
|
-
TEXT_MAP_INJECTOR = TextMapInjector.new
|
17
|
+
TEXT_MAP_PROPAGATOR = TextMapPropagator.new
|
21
18
|
|
22
|
-
private_constant :
|
19
|
+
private_constant :TEXT_MAP_PROPAGATOR
|
23
20
|
|
24
|
-
# Returns
|
25
|
-
# format
|
26
|
-
def
|
27
|
-
|
28
|
-
end
|
29
|
-
|
30
|
-
# Returns an injector that injects context using the W3C Baggage
|
31
|
-
# format
|
32
|
-
def text_map_extractor
|
33
|
-
TEXT_MAP_EXTRACTOR
|
21
|
+
# Returns a text map propagator that propagates context using the
|
22
|
+
# W3C Baggage format.
|
23
|
+
def text_map_propagator
|
24
|
+
TEXT_MAP_PROPAGATOR
|
34
25
|
end
|
35
26
|
end
|
36
27
|
end
|
@@ -0,0 +1,110 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
# Copyright The OpenTelemetry Authors
|
4
|
+
#
|
5
|
+
# SPDX-License-Identifier: Apache-2.0
|
6
|
+
|
7
|
+
require 'cgi'
|
8
|
+
|
9
|
+
module OpenTelemetry
|
10
|
+
module Baggage
|
11
|
+
module Propagation
|
12
|
+
# Propagates baggage using the W3C Baggage format
|
13
|
+
class TextMapPropagator
|
14
|
+
# Maximums according to W3C Baggage spec
|
15
|
+
MAX_ENTRIES = 180
|
16
|
+
MAX_ENTRY_LENGTH = 4096
|
17
|
+
MAX_TOTAL_LENGTH = 8192
|
18
|
+
|
19
|
+
BAGGAGE_KEY = 'baggage'
|
20
|
+
FIELDS = [BAGGAGE_KEY].freeze
|
21
|
+
|
22
|
+
private_constant :BAGGAGE_KEY, :FIELDS, :MAX_ENTRIES, :MAX_ENTRY_LENGTH, :MAX_TOTAL_LENGTH
|
23
|
+
|
24
|
+
# Inject in-process baggage into the supplied carrier.
|
25
|
+
#
|
26
|
+
# @param [Carrier] carrier The mutable carrier to inject baggage into
|
27
|
+
# @param [Context] context The context to read baggage from
|
28
|
+
# @param [optional Setter] setter If the optional setter is provided, it
|
29
|
+
# will be used to write context into the carrier, otherwise the default
|
30
|
+
# text map setter will be used.
|
31
|
+
def inject(carrier, context: Context.current, setter: Context::Propagation.text_map_setter)
|
32
|
+
baggage = OpenTelemetry::Baggage.raw_entries(context: context)
|
33
|
+
|
34
|
+
return if baggage.nil? || baggage.empty?
|
35
|
+
|
36
|
+
encoded_baggage = encode(baggage)
|
37
|
+
setter.set(carrier, BAGGAGE_KEY, encoded_baggage) unless encoded_baggage&.empty?
|
38
|
+
nil
|
39
|
+
end
|
40
|
+
|
41
|
+
# Extract remote baggage from the supplied carrier.
|
42
|
+
# If extraction fails, the original context will be returned
|
43
|
+
#
|
44
|
+
# @param [Carrier] carrier The carrier to get the header from
|
45
|
+
# @param [optional Context] context Context to be updated with the baggage
|
46
|
+
# extracted from the carrier. Defaults to +Context.current+.
|
47
|
+
# @param [optional Getter] getter If the optional getter is provided, it
|
48
|
+
# will be used to read the header from the carrier, otherwise the default
|
49
|
+
# text map getter will be used.
|
50
|
+
#
|
51
|
+
# @return [Context] context updated with extracted baggage, or the original context
|
52
|
+
# if extraction fails
|
53
|
+
def extract(carrier, context: Context.current, getter: Context::Propagation.text_map_getter)
|
54
|
+
header = getter.get(carrier, BAGGAGE_KEY)
|
55
|
+
return context unless header
|
56
|
+
|
57
|
+
entries = header.gsub(/\s/, '').split(',')
|
58
|
+
|
59
|
+
OpenTelemetry::Baggage.build(context: context) do |builder|
|
60
|
+
entries.each do |entry|
|
61
|
+
# Note metadata is currently unused in OpenTelemetry, but is part
|
62
|
+
# the W3C spec where it's referred to as properties. We preserve
|
63
|
+
# the properties (as-is) so that they can be propagated elsewhere.
|
64
|
+
kv, meta = entry.split(';', 2)
|
65
|
+
k, v = kv.split('=').map!(&CGI.method(:unescape))
|
66
|
+
builder.set_value(k, v, metadata: meta)
|
67
|
+
end
|
68
|
+
end
|
69
|
+
rescue StandardError => e
|
70
|
+
OpenTelemetry.logger.debug "Error extracting W3C baggage: #{e.message}"
|
71
|
+
context
|
72
|
+
end
|
73
|
+
|
74
|
+
# Returns the predefined propagation fields. If your carrier is reused, you
|
75
|
+
# should delete the fields returned by this method before calling +inject+.
|
76
|
+
#
|
77
|
+
# @return [Array<String>] a list of fields that will be used by this propagator.
|
78
|
+
def fields
|
79
|
+
FIELDS
|
80
|
+
end
|
81
|
+
|
82
|
+
private
|
83
|
+
|
84
|
+
def encode(baggage)
|
85
|
+
result = +''
|
86
|
+
encoded_count = 0
|
87
|
+
baggage.each_pair do |key, entry|
|
88
|
+
break unless encoded_count < MAX_ENTRIES
|
89
|
+
|
90
|
+
encoded_entry = encode_value(key, entry)
|
91
|
+
next unless encoded_entry.size <= MAX_ENTRY_LENGTH &&
|
92
|
+
encoded_entry.size + result.size <= MAX_TOTAL_LENGTH
|
93
|
+
|
94
|
+
result << encoded_entry << ','
|
95
|
+
encoded_count += 1
|
96
|
+
end
|
97
|
+
result.chop!
|
98
|
+
end
|
99
|
+
|
100
|
+
def encode_value(key, entry)
|
101
|
+
result = +"#{CGI.escape(key.to_s)}=#{CGI.escape(entry.value.to_s)}"
|
102
|
+
# We preserve metadata recieved on extract and assume it's already formatted
|
103
|
+
# for transport. It's sent as-is without further processing.
|
104
|
+
result << ";#{entry.metadata}" if entry.metadata
|
105
|
+
result
|
106
|
+
end
|
107
|
+
end
|
108
|
+
end
|
109
|
+
end
|
110
|
+
end
|