diff --git a/instrumentation/faraday/Appraisals b/instrumentation/faraday/Appraisals index 6d5e0f26e..0c19136a4 100644 --- a/instrumentation/faraday/Appraisals +++ b/instrumentation/faraday/Appraisals @@ -4,14 +4,12 @@ # # SPDX-License-Identifier: Apache-2.0 -appraise 'faraday-0.17' do - gem 'faraday', '~> 0.17.6' +%w[0.17.6 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 diff --git a/instrumentation/faraday/lib/opentelemetry/instrumentation/faraday/middlewares/tracer_middleware.rb b/instrumentation/faraday/lib/opentelemetry/instrumentation/faraday/middlewares/tracer_middleware.rb index 981e44e26..26904ee8d 100644 --- a/instrumentation/faraday/lib/opentelemetry/instrumentation/faraday/middlewares/tracer_middleware.rb +++ b/instrumentation/faraday/lib/opentelemetry/instrumentation/faraday/middlewares/tracer_middleware.rb @@ -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 diff --git a/instrumentation/faraday/test/opentelemetry/instrumentation/faraday/middlewares/tracer_middleware_test.rb b/instrumentation/faraday/test/opentelemetry/instrumentation/faraday/middlewares/tracer_middleware_test.rb index 8a0def73c..67aab661f 100644 --- a/instrumentation/faraday/test/opentelemetry/instrumentation/faraday/middlewares/tracer_middleware_test.rb +++ b/instrumentation/faraday/test/opentelemetry/instrumentation/faraday/middlewares/tracer_middleware_test.rb @@ -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 @@ -31,6 +32,8 @@ @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 @@ -38,10 +41,6 @@ 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') @@ -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')