-
Notifications
You must be signed in to change notification settings - Fork 277
Add faraday instrumentation adapter #67 #148
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
Merged
mwear
merged 35 commits into
open-telemetry:master
from
mutations:faraday-instrumentation--67
Jan 9, 2020
Merged
Changes from 12 commits
Commits
Show all changes
35 commits
Select commit
Hold shift + click to select a range
079d98d
Add API OpenTelemetry::Adapter
5ac7d4e
Add faraday instrumentation adapter Gemfile/gemspec
duonoid 3f7f6b0
Add faraday instrumentation adapter
duonoid ed31c85
Add faraday example with docker-compose service
duonoid 22ccf4f
Avoid hard-dependency on faraday
duonoid 7562579
Fix undefined method `version' for nil:NilClass
duonoid 6987351
Doc: reminder to handle common use case
duonoid 92ba1bc
Add explicit 'require' statements in faraday/adapter
duonoid 60d41d6
Use in_span(attributes:) instead of set_attribute
duonoid 33f28c0
Change SDK::Trace::TracerFactory to subclass API's TracerFactory
duonoid 937cf6c
Add context propagation
duonoid d42721b
Fix occasional test failure in sdk batch_span_processor_test
duonoid 0e0d69f
Merge remote-tracking branch 'upstream/master' into dd/wip--faraday-i…
duonoid 814e330
Don't extract context, just inject
duonoid 4ee20b1
Load tracer_version lazily
duonoid 915b052
Allow auto-installer to inject a tracer
duonoid 89e6453
Merge remote-tracking branch 'upstream/master' into faraday-instrumen…
duonoid 89ba2cb
Fix circular loading issue
duonoid a45dac2
Avoid requiring changes to API (OpenTelemetry::Adapter)
duonoid a558f71
sig-feedback: remove name and version from config
00ad878
sig-feedback: Use the http text format for propagation
1658d2e
Tests: Change Gemfile, gemspec, add Rakefile
duonoid 21c8ada
Add test_helper and first test
duonoid eb243a9
Add appraisals for running tests against multiple versions
duonoid cfcdf5f
Add Circle CI config
duonoid dd680bb
Add TracerMiddleware test
duonoid 5808423
Add Adapter test
duonoid 29346a0
Add/refine tests
duonoid bc9e879
Allow for easier overriding of TracerMiddleware for per-connection rules
duonoid 73f6086
Fix failing circleci builds
duonoid fd66e7d
Fix failing circleci - jruby
duonoid 4098dd4
Appraisal: add faraday-1.0.x
duonoid 537b4c1
Merge branch 'master' into faraday-instrumentation--67
mwear 36cfd8a
circleci: Remove appraisal support, use latest bundler
duonoid dc05f82
circleci: Workaround failing appraisal+bundler(2.1.x)+ruby-2.7 issue
duonoid File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,11 @@ | ||
| # frozen_string_literal: true | ||
|
|
||
| # Copyright 2019 OpenTelemetry Authors | ||
| # | ||
| # SPDX-License-Identifier: Apache-2.0 | ||
|
|
||
| source 'https://rubygems.org' | ||
|
|
||
| gemspec | ||
|
|
||
| gem 'opentelemetry-api', path: '../../api' |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,7 @@ | ||
| # frozen_string_literal: true | ||
|
|
||
| source 'https://rubygems.org' | ||
|
|
||
| gem 'faraday' | ||
| gem 'opentelemetry-api', path: '../../../api' | ||
| gem 'opentelemetry-sdk', path: '../../../sdk' |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,21 @@ | ||
| require 'rubygems' | ||
| require 'bundler/setup' | ||
|
|
||
| require 'faraday' | ||
| require 'opentelemetry/sdk' | ||
| require_relative '../lib/opentelemetry/adapters/faraday' | ||
|
|
||
| # Set preferred tracer implementation: | ||
| SDK = OpenTelemetry::SDK | ||
|
|
||
| factory = OpenTelemetry.tracer_factory = SDK::Trace::TracerFactory.new | ||
| factory.add_span_processor( | ||
| SDK::Trace::Export::SimpleSpanProcessor.new( | ||
| SDK::Trace::Export::ConsoleSpanExporter.new | ||
| ) | ||
| ) | ||
|
|
||
| OpenTelemetry::Adapters::Faraday.install(name: 'faraday-example', version: '1.0') | ||
|
|
||
| conn = Faraday.new('http://example.com') | ||
| conn.get '/' |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,14 @@ | ||
| # frozen_string_literal: true | ||
|
|
||
| # Copyright 2019 OpenTelemetry Authors | ||
| # | ||
| # SPDX-License-Identifier: Apache-2.0 | ||
|
|
||
| module OpenTelemetry | ||
| # "Instrumentation adapters" are specified by | ||
| # https://github.com/open-telemetry/opentelemetry-specification/blob/57714f7547fe4dcb342ad0ad10a80d86118431c7/specification/overview.md#instrumentation-adapters | ||
| # | ||
| # Adapters should be able to handle the case when the library is not installed on a user's system. | ||
| module Adapters | ||
| end | ||
| end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,21 @@ | ||
| # frozen_string_literal: true | ||
|
|
||
| # Copyright 2019 OpenTelemetry Authors | ||
| # | ||
| # SPDX-License-Identifier: Apache-2.0 | ||
|
|
||
| module OpenTelemetry | ||
| module Adapters | ||
| module Faraday | ||
| module_function | ||
|
|
||
| TRACER_NAME = 'faraday' | ||
| TRACER_VERSION = Gem.loaded_specs[TRACER_NAME]&.version.to_s | ||
|
|
||
| def install(config = {name: TRACER_NAME, version: TRACER_VERSION}) | ||
| require_relative 'faraday/adapter' | ||
| Faraday::Adapter.install(config) | ||
| end | ||
| end | ||
| end | ||
| end | ||
38 changes: 38 additions & 0 deletions
38
adapters/faraday/lib/opentelemetry/adapters/faraday/adapter.rb
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,38 @@ | ||
| # frozen_string_literal: true | ||
|
|
||
| # Copyright 2019 OpenTelemetry Authors | ||
| # | ||
| # SPDX-License-Identifier: Apache-2.0 | ||
|
|
||
| # TODO: General Question: should adapters explicitly load the libraries they depend on? | ||
| # Or, should adapters assume that the libraries are already loaded by the caller/user? | ||
| require 'faraday' | ||
| require 'opentelemetry/adapter' | ||
|
|
||
| require_relative 'middlewares/tracer_middleware' | ||
mwear marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| require_relative 'patches/rack_builder' | ||
|
|
||
| module OpenTelemetry | ||
| module Adapters | ||
| module Faraday | ||
| class Adapter < OpenTelemetry::Adapter | ||
| def install | ||
| register_tracer_middleware | ||
| use_middleware_by_default | ||
| end | ||
|
|
||
| private | ||
|
|
||
| def register_tracer_middleware | ||
| ::Faraday::Middleware.register_middleware( | ||
| open_telemetry: Middlewares::TracerMiddleware | ||
| ) | ||
| end | ||
|
|
||
| def use_middleware_by_default | ||
| ::Faraday::RackBuilder.prepend(Patches::RackBuilder) | ||
| end | ||
| end | ||
| end | ||
| end | ||
| end | ||
49 changes: 49 additions & 0 deletions
49
adapters/faraday/lib/opentelemetry/adapters/faraday/middlewares/tracer_middleware.rb
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,49 @@ | ||
| # frozen_string_literal: true | ||
|
|
||
| # Copyright 2019 OpenTelemetry Authors | ||
| # | ||
| # SPDX-License-Identifier: Apache-2.0 | ||
|
|
||
| module OpenTelemetry | ||
| module Adapters | ||
| module Faraday | ||
| module Middlewares | ||
| class TracerMiddleware < ::Faraday::Middleware | ||
| def call(env) | ||
| tracer.in_span(env.url.to_s, | ||
fbogsany marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| attributes: { 'component' => 'http', | ||
duonoid marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| 'http.method' => env.method, | ||
duonoid marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| 'http.url' => env.url.to_s }, | ||
| kind: :client) do |span| | ||
| propagate_context(span, env) | ||
|
|
||
duonoid marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| app.call(env).on_complete { |resp| trace_response(span, resp) } | ||
| end | ||
| end | ||
|
|
||
| private | ||
|
|
||
| attr_reader :app | ||
|
|
||
| def propagate_context(span, env) | ||
| formatter.extract(env) | ||
| formatter.inject(span.context, env.request_headers) | ||
duonoid marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| end | ||
|
|
||
| def formatter | ||
| Faraday::Adapter.http_formatter | ||
| end | ||
|
|
||
| def tracer | ||
| Faraday::Adapter.tracer | ||
| end | ||
|
|
||
| def trace_response(span, response) | ||
| span.set_attribute('http.status_code', response.status) | ||
| span.set_attribute('http.status_text', response.reason_phrase) | ||
| end | ||
| end | ||
| end | ||
| end | ||
| end | ||
| end | ||
26 changes: 26 additions & 0 deletions
26
adapters/faraday/lib/opentelemetry/adapters/faraday/patches/rack_builder.rb
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,26 @@ | ||
| # frozen_string_literal: true | ||
|
|
||
| # Copyright 2019 OpenTelemetry Authors | ||
| # | ||
| # SPDX-License-Identifier: Apache-2.0 | ||
| require_relative '../middlewares/tracer_middleware' | ||
|
|
||
| module OpenTelemetry | ||
| module Adapters | ||
| module Faraday | ||
| module Patches | ||
| # Module to be prepended to force Faraday to use the middleware by | ||
| # default so the user doesn't have to call `use` for every connection. | ||
| module RackBuilder | ||
| def adapter(*args) | ||
| use(:open_telemetry) unless @handlers.any? do |handler| | ||
| handler.klass == Middlewares::TracerMiddleware | ||
| end | ||
|
|
||
| super | ||
| end | ||
| end | ||
| end | ||
| end | ||
| end | ||
| end |
13 changes: 13 additions & 0 deletions
13
adapters/faraday/lib/opentelemetry/adapters/faraday/version.rb
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,13 @@ | ||
| # frozen_string_literal: true | ||
|
|
||
| # Copyright 2019 OpenTelemetry Authors | ||
| # | ||
| # SPDX-License-Identifier: Apache-2.0 | ||
|
|
||
| module OpenTelemetry | ||
| module Adapters | ||
| module Faraday | ||
| VERSION = '0.0.0' | ||
| end | ||
| end | ||
| end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,32 @@ | ||
| # frozen_string_literal: true | ||
|
|
||
| # Copyright 2019 OpenTelemetry Authors | ||
| # | ||
| # SPDX-License-Identifier: Apache-2.0 | ||
|
|
||
| lib = File.expand_path('lib', __dir__) | ||
| $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib) | ||
| require 'opentelemetry/adapters/faraday/version' | ||
|
|
||
| Gem::Specification.new do |spec| | ||
| spec.name = 'opentelemetry-adapters-faraday' | ||
| spec.version = OpenTelemetry::Adapters::Faraday::VERSION | ||
| spec.authors = ['OpenTelemetry Authors'] | ||
| spec.email = ['[email protected]'] | ||
|
|
||
| spec.summary = 'Faraday instrumentation adapter for the OpenTelemetry framework' | ||
| spec.description = 'Faraday instrumentation adapter for the OpenTelemetry framework' | ||
| spec.homepage = 'https://github.com/open-telemetry/opentelemetry-ruby' | ||
| spec.license = 'Apache-2.0' | ||
|
|
||
| spec.files = ::Dir.glob('lib/**/*.rb') + | ||
| ::Dir.glob('*.md') + | ||
| ['LICENSE'] | ||
| spec.require_paths = ['lib'] | ||
| spec.required_ruby_version = '>= 2.4.0' | ||
|
|
||
| spec.add_dependency 'opentelemetry-api', '~> 0.0' | ||
|
|
||
| spec.add_development_dependency 'bundler', '>= 1.17' | ||
| spec.add_development_dependency 'faraday', '~> 0.17.0' | ||
| end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,27 @@ | ||
| # frozen_string_literal: true | ||
|
|
||
| # Copyright 2019 OpenTelemetry Authors | ||
| # | ||
| # SPDX-License-Identifier: Apache-2.0 | ||
|
|
||
| module OpenTelemetry | ||
| # The basic interface for Adapter objects | ||
| class Adapter | ||
| class << self | ||
| attr_reader :config | ||
|
|
||
| def install(config = {}) | ||
| @config = config | ||
| new.install | ||
| end | ||
|
|
||
| def http_formatter | ||
| OpenTelemetry.tracer_factory.http_text_format | ||
| end | ||
|
|
||
| def tracer | ||
| OpenTelemetry.tracer_factory.tracer(config[:name], config[:version]) | ||
| end | ||
| end | ||
| end | ||
| end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.