opentelemetry-processor-baggage 0.1.0 → 0.2.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: 1509e0dd86e9b4fcc4f84bc6e73c60867d9bc6df48d19c38fe3dd64df4445de0
4
- data.tar.gz: f29cb30b4945cf44f20a5ec9bb5e5ab61a9aacdd205485d8cabd64359b09b5e4
3
+ metadata.gz: 1fb1ce58d1b494c0573136d6d55edef712d80f3b0b579d4c96e917ad8b535fa3
4
+ data.tar.gz: 56cc2609787dea49e53a457f3ed25e655a06a8233ac63ae96f08f277b4f66736
5
5
  SHA512:
6
- metadata.gz: 2ce85503cd0be5803f9bc0ed9368653491f2b27e236876f9339c2ab6be83ab780536f44fdc0407b5d7f2ca916b5668983ac04ee63af8b71f03f8ee8de5c7118d
7
- data.tar.gz: 41474ef39a46c0e33402c6c8b89698071520814e8470a9813b95ed49551b0503c9b815d76caa3c07d03f3d358d2afc7602b83fa1a7ac9ada093fa82fd0d55dd3
6
+ metadata.gz: 87523ad79243273ff81768e00e3c48b22f91aa54467a443a8e07c83859bac44f4cd30ed55cfddd4845b27b2a5af7ed4335342ba3a758211f37e9bf31180f3e37
7
+ data.tar.gz: bf6f8546824177bef4502f1c163c2a7256660040cea1a078f37f0b96bc68078b7ce0a3edf5e9874c8d0b6eec22711db532e1fcf4846c1f46058cb6bc4ab64970
data/CHANGELOG.md CHANGED
@@ -1,5 +1,11 @@
1
1
  # Release History: opentelemetry-processor-baggage
2
2
 
3
+ ### v0.2.0 / 2024-06-18
4
+
5
+ * BREAKING CHANGE: Add baggage key predicate func to baggage span processor
6
+
7
+ * ADDED: Add baggage key predicate func to baggage span processor
8
+
3
9
  ### v0.1.0 / 2024-04-18
4
10
 
5
11
  * Initial release.
data/README.md CHANGED
@@ -2,7 +2,7 @@
2
2
 
3
3
  This is an OpenTelemetry [span processor](https://opentelemetry.io/docs/specs/otel/trace/sdk/#span-processor) that reads key/values stored in [Baggage](https://opentelemetry.io/docs/specs/otel/baggage/api/) in the starting span's parent context and adds them as attributes to the span.
4
4
 
5
- Keys and values added to Baggage will appear on all subsequent child spans for a trace within this service *and* will be propagated to external services via propagation headers.
5
+ Keys and values added to Baggage will appear on all subsequent child spans, not the current active span, for a trace within this service *and* will be propagated to external services via propagation headers.
6
6
  If the external services also have a Baggage span processor, the keys and values will appear in those child spans as well.
7
7
 
8
8
  ⚠️ Waning ⚠️
@@ -31,7 +31,7 @@ To install the instrumentation, add the gem to your Gemfile:
31
31
  gem 'opentelemetry-processor-baggage'
32
32
  ```
33
33
 
34
- Then add the processor to an SDK's configuration:
34
+ Then configure the span processor to copy all baggage entries:
35
35
 
36
36
  ```ruby
37
37
  require 'rubygems'
@@ -40,8 +40,11 @@ require 'bundler/setup'
40
40
  Bundler.require
41
41
 
42
42
  OpenTelemetry::SDK.configure do |c|
43
- # Add the BaggageSpanProcessor to the collection of span processors
44
- c.add_span_processor(OpenTelemetry::Processor::Baggage::BaggageSpanProcessor.new)
43
+ # Add the BaggageSpanProcessor to the collection of span processors and
44
+ # copy all baggage entries
45
+ c.add_span_processor(OpenTelemetry::Processor::Baggage::BaggageSpanProcessor.new(
46
+ OpenTelemetry::Processor::Baggage::ALLOW_ALL_BAGGAGE_KEYS
47
+ ))
45
48
 
46
49
  # Because the span processor list is no longer empty, the SDK will not use the
47
50
  # values in OTEL_TRACES_EXPORTER to instantiate exporters.
@@ -57,6 +60,27 @@ OpenTelemetry::SDK.configure do |c|
57
60
  end
58
61
  ```
59
62
 
63
+ Alternatively, you can provide a custom baggage key predicate to select which baggage keys you want to copy.
64
+
65
+ For example, to only copy baggage entries that start with `myapp.`:
66
+
67
+ ```ruby
68
+ OUR_BAGGAGE_KEY_PREFIX = 'myapp.'.freeze
69
+ OpenTelemetry::Processor::Baggage::BaggageSpanProcessor.new(
70
+ # a constant here improves performance
71
+ ->(baggage_key) { baggage_key.start_with?(OUR_BAGGAGE_KEY_PREFIX) }
72
+ )
73
+ ```
74
+
75
+ For example, to only copy baggage entries that match `myapp.`, `myapp1.` and `myapp42.`:
76
+
77
+ ```ruby
78
+ OUR_BAGGAGE_KEY_MATCHER = /\Amyapp\d*\./
79
+ OpenTelemetry::Processor::Baggage::BaggageSpanProcessor.new(
80
+ ->(baggage_key) { OUR_BAGGAGE_KEY_MATCHER.match?(baggage_key) }
81
+ )
82
+ ```
83
+
60
84
  ## How can I get involved?
61
85
 
62
86
  The `opentelemetry-processor-baggage` gem source is [on github][repo-github], along with related gems including `opentelemetry-api` and `opentelemetry-sdk`.
@@ -10,13 +10,19 @@ require 'opentelemetry-sdk'
10
10
  module OpenTelemetry
11
11
  module Processor
12
12
  module Baggage
13
+ # A baggage key predicate that allows all keys to be added to the span as attributes.
14
+ ALLOW_ALL_BAGGAGE_KEYS = ->(_) { true }
15
+
13
16
  # The BaggageSpanProcessor reads key/values stored in Baggage in the
14
- # starting span's parent context and adds them as attributes to the span.
17
+ # starting span's parent context and adds them as attributes to the span,
18
+ # if a key matches a provided predicate lambda.
19
+ #
20
+ # Keys and values added to Baggage will appear on all subsequent child spans,
21
+ # not the current active span, for a trace within this service *and* will be
22
+ # propagated to external services via propagation headers.
15
23
  #
16
- # Keys and values added to Baggage will appear on all subsequent child spans
17
- # for a trace within this service *and* will be propagated to external services
18
- # via propagation headers. If the external services also have a Baggage span
19
- # processor, the keys and values will appear in those child spans as well.
24
+ # If the external services also have a Baggage span processor, the keys and
25
+ # values will appear in those child spans as well.
20
26
  #
21
27
  # ⚠️
22
28
  # To repeat: a consequence of adding data to Baggage is that the keys and
@@ -24,10 +30,16 @@ module OpenTelemetry
24
30
  # Do not put sensitive information in Baggage.
25
31
  # ⚠️
26
32
  #
27
- # @example
33
+ # @example Adding the BaggageSpanProcessor to the SDK, only add attributes for keys that start with 'myapp.'
34
+ # OUR_BAGGAGE_KEY_PREFIX = 'myapp.'.freeze
35
+ #
28
36
  # OpenTelemetry::SDK.configure do |c|
29
37
  # # Add the BaggageSpanProcessor to the collection of span processors
30
- # c.add_span_processor(OpenTelemetry::Processor::Baggage::BaggageSpanProcessor.new)
38
+ # c.add_span_processor(
39
+ # OpenTelemetry::Processor::Baggage::BaggageSpanProcessor.new(
40
+ # ->(key) { key.start_with?(OUR_BAGGAGE_KEY_PREFIX) } # a constant here improves performance
41
+ # )
42
+ # )
31
43
  #
32
44
  # # Because the span processor list is no longer empty, the SDK will not use the
33
45
  # # values in OTEL_TRACES_EXPORTER to instantiate exporters.
@@ -41,7 +53,31 @@ module OpenTelemetry
41
53
  # )
42
54
  # )
43
55
  # end
56
+ #
57
+ # @example Allow all Baggage keys to be added to the span as attributes
58
+ # OpenTelemetry::Processor::Baggage::BaggageSpanProcessor.new(
59
+ # # This processor provides a convenience predicate that allows all keys to be added as attributes.
60
+ # OpenTelemetry::Processor::Baggage::ALLOW_ALL_BAGGAGE_KEYS
61
+ # )
44
62
  class BaggageSpanProcessor < OpenTelemetry::SDK::Trace::SpanProcessor
63
+ # Create a new BaggageSpanProcessor that reads Baggage keys and values from the parent context
64
+ # and adds them as attributes to the span.
65
+ #
66
+ # @param [lambda] baggage_key_predicate A lambda that takes a baggage key [String] and returns true if
67
+ # the key should be added to the span as an attribute, false otherwise.
68
+ #
69
+ # @example Only add attributes for keys that start with a specific prefix
70
+ # OUR_BAGGAGE_KEY_PREFIX = 'myapp.'.freeze
71
+ # OpenTelemetry::Processor::Baggage::BaggageSpanProcessor.new(
72
+ # ->(key) { key.start_with?(OUR_BAGGAGE_KEY_PREFIX) } # a constant here improves performance
73
+ # )
74
+ def initialize(baggage_key_predicate)
75
+ raise ArgumentError, 'baggage_key_predicate must respond to :call (lambda/Proc)' unless baggage_key_predicate.respond_to?(:call)
76
+
77
+ @baggage_key_predicate = baggage_key_predicate
78
+ super()
79
+ end
80
+
45
81
  # Called when a `Span` is started, adds Baggage keys/values to the span as attributes.
46
82
  #
47
83
  # @param [Span] span the `Span` that just started, expected to conform
@@ -51,7 +87,11 @@ module OpenTelemetry
51
87
  def on_start(span, parent_context)
52
88
  return unless span.respond_to?(:add_attributes) && parent_context.is_a?(::OpenTelemetry::Context)
53
89
 
54
- span.add_attributes(::OpenTelemetry::Baggage.values(context: parent_context))
90
+ span.add_attributes(
91
+ ::OpenTelemetry::Baggage
92
+ .values(context: parent_context)
93
+ .select { |k, _v| @baggage_key_predicate.call(k) }
94
+ )
55
95
  end
56
96
 
57
97
  # Called when a Span is ended, does nothing.
@@ -15,7 +15,7 @@ module OpenTelemetry
15
15
  module Processor
16
16
  # Namespace for OpenTelemetry Baggage processor
17
17
  module Baggage
18
- VERSION = '0.1.0'
18
+ VERSION = '0.2.0'
19
19
  end
20
20
  end
21
21
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: opentelemetry-processor-baggage
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.2.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: 2024-04-30 00:00:00.000000000 Z
11
+ date: 2024-06-18 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: opentelemetry-api
@@ -100,14 +100,14 @@ dependencies:
100
100
  requirements:
101
101
  - - "~>"
102
102
  - !ruby/object:Gem::Version
103
- version: '1.62'
103
+ version: 1.64.0
104
104
  type: :development
105
105
  prerelease: false
106
106
  version_requirements: !ruby/object:Gem::Requirement
107
107
  requirements:
108
108
  - - "~>"
109
109
  - !ruby/object:Gem::Version
110
- version: '1.62'
110
+ version: 1.64.0
111
111
  - !ruby/object:Gem::Dependency
112
112
  name: rubocop-performance
113
113
  requirement: !ruby/object:Gem::Requirement
@@ -168,10 +168,10 @@ homepage: https://github.com/open-telemetry/opentelemetry-ruby-contrib
168
168
  licenses:
169
169
  - Apache-2.0
170
170
  metadata:
171
- changelog_uri: https://rubydoc.info/gems/opentelemetry-processor-baggage/0.1.0/file/CHANGELOG.md
171
+ changelog_uri: https://rubydoc.info/gems/opentelemetry-processor-baggage/0.2.0/file/CHANGELOG.md
172
172
  source_code_uri: https://github.com/open-telemetry/opentelemetry-ruby-contrib/tree/main/processor/baggage
173
173
  bug_tracker_uri: https://github.com/open-telemetry/opentelemetry-ruby-contrib/issues
174
- documentation_uri: https://rubydoc.info/gems/opentelemetry-processor-baggage/0.1.0
174
+ documentation_uri: https://rubydoc.info/gems/opentelemetry-processor-baggage/0.2.0
175
175
  post_install_message:
176
176
  rdoc_options: []
177
177
  require_paths: