Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: faraday add support for internal spans #873

Merged
merged 4 commits into from
Feb 20, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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