From 34b540d7013d9a88f3aceddbdd0976e8e0125e38 Mon Sep 17 00:00:00 2001 From: Matthew Wear Date: Sat, 25 Jan 2020 11:32:46 -0800 Subject: [PATCH 1/4] Fix Faraday example --- adapters/faraday/example/Gemfile | 1 + adapters/faraday/example/faraday.rb | 18 ++++-------------- 2 files changed, 5 insertions(+), 14 deletions(-) diff --git a/adapters/faraday/example/Gemfile b/adapters/faraday/example/Gemfile index 1c162edde..e88bd8671 100644 --- a/adapters/faraday/example/Gemfile +++ b/adapters/faraday/example/Gemfile @@ -5,3 +5,4 @@ source 'https://rubygems.org' gem 'faraday' gem 'opentelemetry-api', path: '../../../api' gem 'opentelemetry-sdk', path: '../../../sdk' +gem 'opentelemetry-adapters-faraday', path: '../../../adapters/faraday' diff --git a/adapters/faraday/example/faraday.rb b/adapters/faraday/example/faraday.rb index c278c65a5..a9da19b75 100644 --- a/adapters/faraday/example/faraday.rb +++ b/adapters/faraday/example/faraday.rb @@ -1,19 +1,11 @@ require 'rubygems' require 'bundler/setup' -require 'faraday' -require 'opentelemetry/sdk' -require_relative '../lib/opentelemetry/adapters/faraday' +Bundler.require -# 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::SDK.configure do |c| + c.use 'OpenTelemetry::Adapters::Faraday' +end # Demonstrate disabling span reporting: # @@ -25,7 +17,5 @@ # end # OpenTelemetry::Adapters::Faraday.install(tracer_middleware: NoOp) -OpenTelemetry::Adapters::Faraday.install - conn = Faraday.new('http://example.com') conn.get '/' From aa7a29558aaf9e01ec9a2ee443a14b058cb9093f Mon Sep 17 00:00:00 2001 From: Matthew Wear Date: Sat, 25 Jan 2020 11:37:28 -0800 Subject: [PATCH 2/4] Fix Sinatra example --- adapters/sinatra/example/Gemfile | 1 + adapters/sinatra/example/server.rb | 18 ++++-------------- 2 files changed, 5 insertions(+), 14 deletions(-) diff --git a/adapters/sinatra/example/Gemfile b/adapters/sinatra/example/Gemfile index 543be5a98..8d5dcc1dc 100644 --- a/adapters/sinatra/example/Gemfile +++ b/adapters/sinatra/example/Gemfile @@ -5,3 +5,4 @@ source 'https://rubygems.org' gem 'sinatra' gem 'opentelemetry-api', path: '../../../api' gem 'opentelemetry-sdk', path: '../../../sdk' +gem 'opentelemetry-adapters-sinatra', path: '../../../adapters/sinatra' diff --git a/adapters/sinatra/example/server.rb b/adapters/sinatra/example/server.rb index 11b0bcb60..cd3f95ec5 100755 --- a/adapters/sinatra/example/server.rb +++ b/adapters/sinatra/example/server.rb @@ -2,21 +2,11 @@ require 'rubygems' require 'bundler/setup' -require 'sinatra/base' -require 'opentelemetry/sdk' -require_relative '../lib/opentelemetry/adapters/sinatra' +Bundler.require -# 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::Sinatra.install +OpenTelemetry::SDK.configure do |c| + c.use 'OpenTelemetry::Adapters::Sinatra' +end class App < Sinatra::Base set :bind, '0.0.0.0' From 742db83fd6b29dc19b219d2dd9f74626ca99463e Mon Sep 17 00:00:00 2001 From: Matthew Wear Date: Sat, 25 Jan 2020 12:31:36 -0800 Subject: [PATCH 3/4] Check for constants instead of Gem.loaded_specs A gemspec can be laoded, but that does not necessarily mean the has been required. It's better to check for presence using expected constants from the target library. --- adapters/faraday/lib/opentelemetry/adapters/faraday/adapter.rb | 2 +- adapters/sinatra/lib/opentelemetry/adapters/sinatra/adapter.rb | 2 +- api/lib/opentelemetry/instrumentation/adapter.rb | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/adapters/faraday/lib/opentelemetry/adapters/faraday/adapter.rb b/adapters/faraday/lib/opentelemetry/adapters/faraday/adapter.rb index 6a7643908..e8b2726ad 100644 --- a/adapters/faraday/lib/opentelemetry/adapters/faraday/adapter.rb +++ b/adapters/faraday/lib/opentelemetry/adapters/faraday/adapter.rb @@ -16,7 +16,7 @@ class Adapter < OpenTelemetry::Instrumentation::Adapter end present do - Gem.loaded_specs.include?('faraday') + defined?(::Faraday) end private diff --git a/adapters/sinatra/lib/opentelemetry/adapters/sinatra/adapter.rb b/adapters/sinatra/lib/opentelemetry/adapters/sinatra/adapter.rb index b17cfc413..f93e05606 100644 --- a/adapters/sinatra/lib/opentelemetry/adapters/sinatra/adapter.rb +++ b/adapters/sinatra/lib/opentelemetry/adapters/sinatra/adapter.rb @@ -15,7 +15,7 @@ class Adapter < OpenTelemetry::Instrumentation::Adapter end present do - Gem.loaded_specs.include?("sinatra") + defined?(::Sinatra) end end end diff --git a/api/lib/opentelemetry/instrumentation/adapter.rb b/api/lib/opentelemetry/instrumentation/adapter.rb index 584e9897d..a422e5cd4 100644 --- a/api/lib/opentelemetry/instrumentation/adapter.rb +++ b/api/lib/opentelemetry/instrumentation/adapter.rb @@ -27,7 +27,7 @@ module Instrumentation # # # determine if the target library is present # present do - # Gem.loaded_specs.key?('sinatra') + # defined?(::Sinatra) # end # # # if the target library is present, is it compatible? From 1fc760d213a2b50d7eb318fa677b212b6e887347 Mon Sep 17 00:00:00 2001 From: Matthew Wear Date: Sat, 25 Jan 2020 12:40:49 -0800 Subject: [PATCH 4/4] Simplify SDK setup in adapter tests --- adapters/faraday/test/test_helper.rb | 12 +++++------- adapters/sinatra/test/test_helper.rb | 12 +++++------- 2 files changed, 10 insertions(+), 14 deletions(-) diff --git a/adapters/faraday/test/test_helper.rb b/adapters/faraday/test/test_helper.rb index 347cc5d90..0dadb0644 100644 --- a/adapters/faraday/test/test_helper.rb +++ b/adapters/faraday/test/test_helper.rb @@ -12,11 +12,9 @@ require 'webmock/minitest' # global opentelemetry-sdk setup: -sdk = OpenTelemetry::SDK -exporter = sdk::Trace::Export::InMemorySpanExporter.new -span_processor = sdk::Trace::Export::SimpleSpanProcessor.new(exporter) -OpenTelemetry.tracer_factory = sdk::Trace::TracerFactory.new.tap do |factory| - factory.add_span_processor(span_processor) -end +EXPORTER = OpenTelemetry::SDK::Trace::Export::InMemorySpanExporter.new +span_processor = OpenTelemetry::SDK::Trace::Export::SimpleSpanProcessor.new(EXPORTER) -EXPORTER = exporter \ No newline at end of file +OpenTelemetry::SDK.configure do |c| + c.add_span_processor span_processor +end diff --git a/adapters/sinatra/test/test_helper.rb b/adapters/sinatra/test/test_helper.rb index 80d365eb7..91208e52f 100644 --- a/adapters/sinatra/test/test_helper.rb +++ b/adapters/sinatra/test/test_helper.rb @@ -12,11 +12,9 @@ require 'rack/test' # global opentelemetry-sdk setup: -sdk = OpenTelemetry::SDK -exporter = sdk::Trace::Export::InMemorySpanExporter.new -span_processor = sdk::Trace::Export::SimpleSpanProcessor.new(exporter) -OpenTelemetry.tracer_factory = sdk::Trace::TracerFactory.new.tap do |factory| - factory.add_span_processor(span_processor) -end +EXPORTER = OpenTelemetry::SDK::Trace::Export::InMemorySpanExporter.new +span_processor = OpenTelemetry::SDK::Trace::Export::SimpleSpanProcessor.new(EXPORTER) -EXPORTER = exporter +OpenTelemetry::SDK.configure do |c| + c.add_span_processor span_processor +end