Skip to content

Commit

Permalink
feat: faraday add support for internal spans (#873)
Browse files Browse the repository at this point in the history
  • Loading branch information
arielvalentin authored Feb 20, 2024
1 parent a2669ad commit 39f9a80
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ class Instrumentation < OpenTelemetry::Instrumentation::Base
defined?(::Faraday)
end

option :span_kind, default: :client, validate: %i[client internal]
option :peer_service, default: nil, validate: :string

private
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,13 @@ class TracerMiddleware < ::Faraday::Middleware

def call(env)
http_method = HTTP_METHODS_SYMBOL_TO_STRING[env.method]
config = Faraday::Instrumentation.instance.config

attributes = span_creation_attributes(
http_method: http_method, url: env.url
http_method: http_method, url: env.url, config: config
)
tracer.in_span(
"HTTP #{http_method}", attributes: attributes, kind: :client
"HTTP #{http_method}", attributes: attributes, kind: config.fetch(:span_kind)
) do |span|
OpenTelemetry.propagation.inject(env.request_headers)

Expand All @@ -39,21 +41,23 @@ def call(env)

private

attr_reader :app

def span_creation_attributes(http_method:, url:)
def span_creation_attributes(http_method:, url:, config:)
instrumentation_attrs = {
'http.method' => http_method,
'http.url' => OpenTelemetry::Common::Utilities.cleanse_url(url.to_s)
'http.url' => OpenTelemetry::Common::Utilities.cleanse_url(url.to_s),
'faraday.adapter.name' => app.class.name
}
instrumentation_attrs['net.peer.name'] = url.host if url.host
config = Faraday::Instrumentation.instance.config
instrumentation_attrs['peer.service'] = config[:peer_service] if config[:peer_service]

instrumentation_attrs.merge!(
OpenTelemetry::Common::HTTP::ClientContext.attributes
)
end

# Versions prior to 1.0 do not define an accessor for app
attr_reader :app if Gem::Version.new(Faraday::VERSION) < Gem::Version.new('1.0.0')

def tracer
Faraday::Instrumentation.instance.tracer
end
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,33 @@
_(span.attributes['peer.service']).must_equal 'example:faraday'
end

it 'defaults to span kind client' do
instrumentation.instance_variable_set(:@installed, false)
instrumentation.install

client.get('/success')

_(span.kind).must_equal :client
end

it 'allows overriding the span kind to internal' do
instrumentation.instance_variable_set(:@installed, false)
instrumentation.install(span_kind: :internal)

client.get('/success')

_(span.kind).must_equal :internal
end

it 'reports the name of the configured adapter' do
instrumentation.instance_variable_set(:@installed, false)
instrumentation.install

client.get('/success')

_(span.attributes.fetch('faraday.adapter.name')).must_equal Faraday::Adapter::Test.name
end

it 'prioritizes context attributes over config for peer service name' do
instrumentation.instance_variable_set(:@installed, false)
instrumentation.install(peer_service: 'example:faraday')
Expand Down

0 comments on commit 39f9a80

Please sign in to comment.