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 Minimum v1.0 #1267

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 2 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
14 changes: 6 additions & 8 deletions instrumentation/faraday/Appraisals
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,12 @@
#
# SPDX-License-Identifier: Apache-2.0

appraise 'faraday-0.17' do
gem 'faraday', '~> 0.17.6'
%w[1.0 2.0].each do |version|
appraise "faraday-#{version}" do
gem 'faraday', "~> #{version}"
end
end

appraise 'faraday-1.x' do
gem 'faraday', '~> 1.0'
end

appraise 'faraday-2.x' do
gem 'faraday', '~> 2.0'
appraise 'faraday-latest' do
gem 'faraday'
end
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,18 @@ module Faraday
# The Instrumentation class contains logic to detect and install the Faraday
# instrumentation
class Instrumentation < OpenTelemetry::Instrumentation::Base
MINIMUM_VERSION = Gem::Version.new('1.0')

install do |_config|
require_dependencies
register_tracer_middleware
use_middleware_by_default
end

compatible do
gem_version >= MINIMUM_VERSION
end

present do
defined?(::Faraday)
end
Expand All @@ -25,10 +31,13 @@ class Instrumentation < OpenTelemetry::Instrumentation::Base

private

def gem_version
Gem::Version.new(::Faraday::VERSION)
end

def require_dependencies
require_relative 'middlewares/tracer_middleware'
require_relative 'patches/connection'
require_relative 'patches/rack_builder'
end

def register_tracer_middleware
Expand All @@ -38,11 +47,7 @@ def register_tracer_middleware
end

def use_middleware_by_default
if Gem::Version.new(::Faraday::VERSION) >= Gem::Version.new('1')
::Faraday::Connection.prepend(Patches::Connection)
else
::Faraday::RackBuilder.prepend(Patches::RackBuilder)
end
::Faraday::Connection.prepend(Patches::Connection)
end
end
end
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,31 +33,34 @@ def call(env)
attributes = span_creation_attributes(
http_method: http_method, url: env.url, config: config
)
tracer.in_span(
"HTTP #{http_method}", attributes: attributes, kind: config.fetch(:span_kind)
) do |span|
OpenTelemetry.propagation.inject(env.request_headers)

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

raise
app.call(env).on_complete { |resp| trace_response(span, resp.status) }
rescue ::Faraday::Error => e
trace_response(span, e.response[:status]) if e.response

raise
end
end
end

private

def span_creation_attributes(http_method:, url:, config:)
instrumentation_attrs = {
attrs = {
'http.method' => http_method,
'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
instrumentation_attrs['peer.service'] = config[:peer_service] if config[:peer_service]
attrs['net.peer.name'] = url.host if url.host
attrs['peer.service'] = config[:peer_service] if config[:peer_service]

instrumentation_attrs.merge!(
attrs.merge!(
OpenTelemetry::Common::HTTP::ClientContext.attributes
)
end
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ module Patches
module Connection
# Wraps Faraday::Connection#initialize:
# https://github.com/lostisland/faraday/blob/ff9dc1d1219a1bbdba95a9a4cf5d135b97247ee2/lib/faraday/connection.rb#L62-L92
def initialize(*args)
def initialize(...)
super.tap do
use(:open_telemetry) unless builder.handlers.any? do |handler|
handler.klass == Middlewares::TracerMiddleware
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
stub.get('/success') { |_| [200, {}, 'OK'] }
stub.get('/failure') { |_| [500, {}, 'OK'] }
stub.get('/not_found') { |_| [404, {}, 'OK'] }
stub.get('/show-shared-attributes') { |_| [200, {}, OpenTelemetry::Common::HTTP::ClientContext.attributes.to_json] }
end
end
end
Expand All @@ -31,17 +32,15 @@
@orig_propagation = OpenTelemetry.propagation
propagator = OpenTelemetry::Trace::Propagation::TraceContext.text_map_propagator
OpenTelemetry.propagation = propagator
instrumentation.instance_variable_set(:@installed, false)
instrumentation.install
end

after do
OpenTelemetry.propagation = @orig_propagation
end

describe 'first span' do
before do
instrumentation.install
end

describe 'given a client with a base url' do
it 'has http 200 attributes' do
response = client.get('/success')
Expand Down Expand Up @@ -101,6 +100,18 @@
)
end

it 'ammends attributes to client context' do
response = client.get('/show-shared-attributes')
shared_attributes = JSON.parse(response.body)
expected_attributes = {
'http.method' => 'GET', 'http.url' => 'http://example.com/show-shared-attributes',
'faraday.adapter.name' => 'Faraday::Adapter::Test',
'net.peer.name' => 'example.com'
}

_(shared_attributes).must_equal expected_attributes
end

it 'accepts peer service name from config' do
instrumentation.instance_variable_set(:@installed, false)
instrumentation.install(peer_service: 'example:faraday')
Expand Down
Loading