Skip to content

Commit 8def74b

Browse files
fix: Share Faraday Attrs with Adapter Spans (#1266)
The Faraday gem has additional http attributes that are sometimes missing or difficult to derive in Adapter instrumentations.
1 parent 38e26a9 commit 8def74b

File tree

3 files changed

+36
-24
lines changed

3 files changed

+36
-24
lines changed

instrumentation/faraday/Appraisals

+6-8
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,12 @@
44
#
55
# SPDX-License-Identifier: Apache-2.0
66

7-
appraise 'faraday-0.17' do
8-
gem 'faraday', '~> 0.17.6'
7+
%w[0.17.6 1.0 2.0].each do |version|
8+
appraise "faraday-#{version}" do
9+
gem 'faraday', "~> #{version}"
10+
end
911
end
1012

11-
appraise 'faraday-1.x' do
12-
gem 'faraday', '~> 1.0'
13-
end
14-
15-
appraise 'faraday-2.x' do
16-
gem 'faraday', '~> 2.0'
13+
appraise 'faraday-latest' do
14+
gem 'faraday'
1715
end

instrumentation/faraday/lib/opentelemetry/instrumentation/faraday/middlewares/tracer_middleware.rb

+15-12
Original file line numberDiff line numberDiff line change
@@ -33,31 +33,34 @@ def call(env)
3333
attributes = span_creation_attributes(
3434
http_method: http_method, url: env.url, config: config
3535
)
36-
tracer.in_span(
37-
"HTTP #{http_method}", attributes: attributes, kind: config.fetch(:span_kind)
38-
) do |span|
39-
OpenTelemetry.propagation.inject(env.request_headers)
4036

41-
app.call(env).on_complete { |resp| trace_response(span, resp.status) }
42-
rescue ::Faraday::Error => e
43-
trace_response(span, e.response[:status]) if e.response
37+
OpenTelemetry::Common::HTTP::ClientContext.with_attributes(attributes) do |attrs, _|
38+
tracer.in_span(
39+
"HTTP #{http_method}", attributes: attrs, kind: config.fetch(:span_kind)
40+
) do |span|
41+
OpenTelemetry.propagation.inject(env.request_headers)
4442

45-
raise
43+
app.call(env).on_complete { |resp| trace_response(span, resp.status) }
44+
rescue ::Faraday::Error => e
45+
trace_response(span, e.response[:status]) if e.response
46+
47+
raise
48+
end
4649
end
4750
end
4851

4952
private
5053

5154
def span_creation_attributes(http_method:, url:, config:)
52-
instrumentation_attrs = {
55+
attrs = {
5356
'http.method' => http_method,
5457
'http.url' => OpenTelemetry::Common::Utilities.cleanse_url(url.to_s),
5558
'faraday.adapter.name' => app.class.name
5659
}
57-
instrumentation_attrs['net.peer.name'] = url.host if url.host
58-
instrumentation_attrs['peer.service'] = config[:peer_service] if config[:peer_service]
60+
attrs['net.peer.name'] = url.host if url.host
61+
attrs['peer.service'] = config[:peer_service] if config[:peer_service]
5962

60-
instrumentation_attrs.merge!(
63+
attrs.merge!(
6164
OpenTelemetry::Common::HTTP::ClientContext.attributes
6265
)
6366
end

instrumentation/faraday/test/opentelemetry/instrumentation/faraday/middlewares/tracer_middleware_test.rb

+15-4
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
stub.get('/success') { |_| [200, {}, 'OK'] }
2121
stub.get('/failure') { |_| [500, {}, 'OK'] }
2222
stub.get('/not_found') { |_| [404, {}, 'OK'] }
23+
stub.get('/show-shared-attributes') { |_| [200, {}, OpenTelemetry::Common::HTTP::ClientContext.attributes.to_json] }
2324
end
2425
end
2526
end
@@ -31,17 +32,15 @@
3132
@orig_propagation = OpenTelemetry.propagation
3233
propagator = OpenTelemetry::Trace::Propagation::TraceContext.text_map_propagator
3334
OpenTelemetry.propagation = propagator
35+
instrumentation.instance_variable_set(:@installed, false)
36+
instrumentation.install
3437
end
3538

3639
after do
3740
OpenTelemetry.propagation = @orig_propagation
3841
end
3942

4043
describe 'first span' do
41-
before do
42-
instrumentation.install
43-
end
44-
4544
describe 'given a client with a base url' do
4645
it 'has http 200 attributes' do
4746
response = client.get('/success')
@@ -101,6 +100,18 @@
101100
)
102101
end
103102

103+
it 'ammends attributes to client context' do
104+
response = client.get('/show-shared-attributes')
105+
shared_attributes = JSON.parse(response.body)
106+
expected_attributes = {
107+
'http.method' => 'GET', 'http.url' => 'http://example.com/show-shared-attributes',
108+
'faraday.adapter.name' => 'Faraday::Adapter::Test',
109+
'net.peer.name' => 'example.com'
110+
}
111+
112+
_(shared_attributes).must_equal expected_attributes
113+
end
114+
104115
it 'accepts peer service name from config' do
105116
instrumentation.instance_variable_set(:@installed, false)
106117
instrumentation.install(peer_service: 'example:faraday')

0 commit comments

Comments
 (0)