Skip to content

Commit 3d41622

Browse files
committed
feat: Support including host name in faraday span names
1 parent 4db8fc9 commit 3d41622

File tree

3 files changed

+27
-3
lines changed

3 files changed

+27
-3
lines changed

instrumentation/faraday/lib/opentelemetry/instrumentation/faraday/instrumentation.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ class Instrumentation < OpenTelemetry::Instrumentation::Base
2828

2929
option :span_kind, default: :client, validate: %i[client internal]
3030
option :peer_service, default: nil, validate: :string
31+
option :span_naming, default: :http_method, validate: %i[http_method host]
3132

3233
private
3334

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

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -35,9 +35,14 @@ def call(env)
3535
)
3636

3737
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|
38+
span_name = case config.fetch(:span_naming)
39+
when :host
40+
['HTTP', http_method, env.url.host].compact.join(' ')
41+
else
42+
"HTTP #{http_method}"
43+
end
44+
45+
tracer.in_span(span_name, attributes: attrs, kind: config.fetch(:span_kind)) do |span|
4146
OpenTelemetry.propagation.inject(env.request_headers)
4247

4348
app.call(env).on_complete { |resp| trace_response(span, resp.status) }

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

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,24 @@
121121
_(span.attributes['peer.service']).must_equal 'example:faraday'
122122
end
123123

124+
it 'defaults to span naming by http method' do
125+
instrumentation.instance_variable_set(:@installed, false)
126+
instrumentation.install
127+
128+
client.get('/success')
129+
130+
_(span.name).must_equal 'HTTP GET'
131+
end
132+
133+
it 'allows including the host in the span name' do
134+
instrumentation.instance_variable_set(:@installed, false)
135+
instrumentation.install(span_naming: :host)
136+
137+
client.get('/success')
138+
139+
_(span.name).must_equal 'HTTP GET example.com'
140+
end
141+
124142
it 'defaults to span kind client' do
125143
instrumentation.instance_variable_set(:@installed, false)
126144
instrumentation.install

0 commit comments

Comments
 (0)