Skip to content

Commit 80f1909

Browse files
wperronfbogsany
andauthored
feat: add ResponseTextMapPropagator
* feat: add ResponseTextMapPropagator Adds a TextMapPropagator in the experimental SDK to inject `traceresponse` headers as defined in the [Trace Context][1] draft specification. [1]: https://w3c.github.io/trace-context/#traceresponse-header * more PR comments * Update sdk_experimental/lib/opentelemetry/sdk/trace/propagation/trace_context/response_text_map_propagator.rb Co-authored-by: Francis Bogsanyi <[email protected]> * conditionally define class unless already defined in stable Co-authored-by: Francis Bogsanyi <[email protected]>
1 parent 32f4a7a commit 80f1909

File tree

3 files changed

+103
-0
lines changed

3 files changed

+103
-0
lines changed

sdk_experimental/lib/opentelemetry/sdk/experimental.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,3 +14,4 @@ module Experimental
1414
end
1515

1616
require 'opentelemetry/sdk/experimental/samplers_patch'
17+
require 'opentelemetry/sdk/trace/propagation/trace_context/response_text_map_propagator'
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
# frozen_string_literal: true
2+
3+
# Copyright The OpenTelemetry Authors
4+
#
5+
# SPDX-License-Identifier: Apache-2.0
6+
7+
unless OpenTelemetry::Trace::Propagation::TraceContext.const_defined?(:ResponseTextMapPropagator)
8+
module OpenTelemetry
9+
module Trace
10+
module Propagation
11+
module TraceContext
12+
# Propagates trace response using the W3C Trace Context format
13+
# https://w3c.github.io/trace-context/#traceresponse-header
14+
class ResponseTextMapPropagator
15+
TRACERESPONSE_KEY = 'traceresponse'
16+
FIELDS = [TRACERESPONSE_KEY].freeze
17+
18+
private_constant :TRACERESPONSE_KEY, :FIELDS
19+
20+
# Inject trace context into the supplied carrier.
21+
#
22+
# @param [Carrier] carrier The mutable carrier to inject trace context into
23+
# @param [Context] context The context to read trace context from
24+
# @param [optional Setter] setter If the optional setter is provided, it
25+
# will be used to write context into the carrier, otherwise the default
26+
# text map setter will be used.
27+
def inject(carrier, context: Context.current, setter: Context::Propagation.text_map_setter)
28+
span_context = Trace.current_span(context).context
29+
return unless span_context.valid?
30+
31+
setter.set(carrier, TRACERESPONSE_KEY, TraceParent.from_span_context(span_context).to_s)
32+
nil
33+
end
34+
35+
# Extract trace context from the supplied carrier. This is a no-op for
36+
# this propagator, and will return the provided context.
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] the original context.
46+
def extract(carrier, context: Context.current, getter: Context::Propagation.text_map_getter)
47+
context
48+
end
49+
50+
# Returns the predefined propagation fields. If your carrier is reused, you
51+
# should delete the fields returned by this method before calling +inject+.
52+
#
53+
# @return [Array<String>] a list of fields that will be used by this propagator.
54+
def fields
55+
FIELDS
56+
end
57+
end
58+
end
59+
end
60+
end
61+
end
62+
end
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
# frozen_string_literal: true
2+
3+
# Copyright The OpenTelemetry Authors
4+
#
5+
# SPDX-License-Identifier: Apache-2.0
6+
7+
require 'test_helper'
8+
9+
describe OpenTelemetry::Trace::Propagation::TraceContext::ResponseTextMapPropagator do
10+
let(:traceresponse_key) { 'traceresponse' }
11+
let(:propagator) do
12+
OpenTelemetry::Trace::Propagation::TraceContext::ResponseTextMapPropagator.new
13+
end
14+
15+
let(:carrier) do
16+
{
17+
traceresponse_key => valid_traceresponse_header
18+
}
19+
end
20+
let(:context) { OpenTelemetry::Context.empty }
21+
let(:context_valid) do
22+
span_context = OpenTelemetry::Trace::SpanContext.new(trace_id: ("\xff" * 16).b, span_id: ("\x11" * 8).b)
23+
span = OpenTelemetry::Trace.non_recording_span(span_context)
24+
OpenTelemetry::Trace.context_with_span(span)
25+
end
26+
27+
describe '#inject' do
28+
it 'writes traceresponse into the carrier' do
29+
carrier = {}
30+
propagator.inject(carrier, context: context_valid)
31+
_(carrier[traceresponse_key]).must_equal('00-ffffffffffffffffffffffffffffffff-1111111111111111-00')
32+
end
33+
34+
it "doesn't write if the context is not valid" do
35+
carrier = {}
36+
propagator.inject(carrier, context: context)
37+
_(carrier).wont_include(traceresponse_key)
38+
end
39+
end
40+
end

0 commit comments

Comments
 (0)