From 4ab41d2c505a8be569d736a22c88eed4ccb5a054 Mon Sep 17 00:00:00 2001 From: Pedro Lambert Date: Tue, 17 Oct 2017 15:10:35 -0400 Subject: [PATCH] Migrate `sinatra` to new configuration API --- lib/ddtrace/contrib/active_record/patcher.rb | 2 +- lib/ddtrace/contrib/sinatra/tracer.rb | 85 +++++-------------- .../sinatra/tracer_activerecord_test.rb | 4 +- test/contrib/sinatra/tracer_disabled_test.rb | 2 +- test/contrib/sinatra/tracer_test.rb | 2 +- 5 files changed, 27 insertions(+), 68 deletions(-) diff --git a/lib/ddtrace/contrib/active_record/patcher.rb b/lib/ddtrace/contrib/active_record/patcher.rb index efad7816de0..602b56583d1 100644 --- a/lib/ddtrace/contrib/active_record/patcher.rb +++ b/lib/ddtrace/contrib/active_record/patcher.rb @@ -44,7 +44,7 @@ def patch_active_record def self.datadog_trace # TODO: Consider using patcher for Rails as well. # @tracer ||= defined?(::Rails) && ::Rails.configuration.datadog_trace - @datadog_trace ||= defined?(::Sinatra) && ::Sinatra::Application.settings.datadog_tracer.cfg + @datadog_trace ||= defined?(::Sinatra) && Datadog.configuration[:sinatra].to_h end def self.adapter_name diff --git a/lib/ddtrace/contrib/sinatra/tracer.rb b/lib/ddtrace/contrib/sinatra/tracer.rb index 4f17a7e2133..75073664a6c 100644 --- a/lib/ddtrace/contrib/sinatra/tracer.rb +++ b/lib/ddtrace/contrib/sinatra/tracer.rb @@ -17,63 +17,32 @@ module Datadog module Contrib module Sinatra - # TracerCfg is used to manipulate the configuration of the Sinatra - # tracing extension. - class TracerCfg - DEFAULT_CFG = { - enabled: true, - default_service: 'sinatra', - tracer: Datadog.tracer, - debug: false, - trace_agent_hostname: Datadog::Writer::HOSTNAME, - trace_agent_port: Datadog::Writer::PORT - }.freeze() - - attr_accessor :cfg - - def initialize - @cfg = DEFAULT_CFG.dup() - end - - def configure(args = {}) - args.each do |name, value| - self[name] = value - end + # Datadog::Contrib::Sinatra::Tracer is a Sinatra extension which traces + # requests. + module Tracer + include Base + register_as :sinatra - apply() + option :enabled, default: true, depends_on: [:tracer] do |value| + get_option(:tracer).enabled = value end - def apply - Datadog::Tracer.debug_logging = @cfg[:debug] - - tracer = @cfg[:tracer] - - tracer.enabled = @cfg[:enabled] - tracer.configure(hostname: @cfg[:trace_agent_hostname], - port: @cfg[:trace_agent_port]) - - tracer.set_service_info(@cfg[:default_service], 'sinatra', - Datadog::Ext::AppTypes::WEB) + option :default_service, default: 'sinatra', depends_on: [:tracer] do |value| + get_option(:tracer).set_service_info(value, 'sinatra', Ext::AppTypes::WEB) end - def [](key) - raise ArgumentError, "unknown setting '#{key}'" unless @cfg.key? key - @cfg[key] - end + option :tracer, default: Datadog.tracer - def []=(key, value) - raise ArgumentError, "unknown setting '#{key}'" unless @cfg.key? key - @cfg[key] = value + option(:debug, default: false) { |value| Tracer.debug_logging = value } + + option :trace_agent_hostname, default: Writer::HOSTNAME, depends_on: [:tracer] do |value| + get_option(:tracer).configure(hostname: value) end - def enabled? - @cfg[:enabled] && !@cfg[:tracer].nil? + option :trace_agent_port, default: Writer::PORT, depends_on: [:tracer] do |value| + get_option(:tracer).configure(port: value) end - end - # Datadog::Contrib::Sinatra::Tracer is a Sinatra extension which traces - # requests. - module Tracer def route(verb, action, *) # Keep track of the route name when the app is instantiated for an # incoming request. @@ -89,11 +58,9 @@ def route(verb, action, *) def self.registered(app) ::Sinatra::Base.module_eval do def render(engine, data, *) - cfg = settings.datadog_tracer - output = '' - if cfg.enabled? - tracer = cfg[:tracer] + tracer = Datadog.configuration[:sinatra][:tracer] + if tracer.enabled tracer.trace('sinatra.render_template') do |span| # If data is a string, it is a literal template and we don't # want to record it. @@ -108,15 +75,8 @@ def render(engine, data, *) end end - app.set :datadog_tracer, TracerCfg.new() - - app.configure do - app.settings.datadog_tracer.apply() - end - app.before do - cfg = settings.datadog_tracer - return unless cfg.enabled? + return unless Datadog.configuration[:sinatra][:tracer].enabled if instance_variable_defined? :@datadog_request_span if @datadog_request_span @@ -126,10 +86,10 @@ def render(engine, data, *) end end - tracer = cfg[:tracer] + tracer = Datadog.configuration[:sinatra][:tracer] span = tracer.trace('sinatra.request', - service: cfg.cfg[:default_service], + service: Datadog.configuration[:sinatra][:default_service], span_type: Datadog::Ext::HTTP::TYPE) span.set_tag(Datadog::Ext::HTTP::URL, request.path) span.set_tag(Datadog::Ext::HTTP::METHOD, request.request_method) @@ -138,8 +98,7 @@ def render(engine, data, *) end app.after do - cfg = settings.datadog_tracer - return unless cfg.enabled? + return unless Datadog.configuration[:sinatra][:tracer].enabled span = @datadog_request_span begin diff --git a/test/contrib/sinatra/tracer_activerecord_test.rb b/test/contrib/sinatra/tracer_activerecord_test.rb index dae9c350f00..4914ef02d24 100644 --- a/test/contrib/sinatra/tracer_activerecord_test.rb +++ b/test/contrib/sinatra/tracer_activerecord_test.rb @@ -21,13 +21,13 @@ def setup app().set :datadog_test_writer, @writer tracer = Datadog::Tracer.new(writer: @writer) - app().settings.datadog_tracer.configure(tracer: tracer, enabled: true) + Datadog.configuration.use(:sinatra, tracer: tracer, enabled: true) conn = ActiveRecord::Base.establish_connection(adapter: 'sqlite3', database: ':memory:') app().set :datadog_test_conn, conn - Datadog::Monkey.patch_module(:active_record) + Datadog.configuration.use(:active_record) super end diff --git a/test/contrib/sinatra/tracer_disabled_test.rb b/test/contrib/sinatra/tracer_disabled_test.rb index 24ba0cce847..bca73411876 100644 --- a/test/contrib/sinatra/tracer_disabled_test.rb +++ b/test/contrib/sinatra/tracer_disabled_test.rb @@ -17,7 +17,7 @@ def setup app().set :datadog_test_writer, @writer tracer = Datadog::Tracer.new(writer: @writer) - app().settings.datadog_tracer.configure(tracer: tracer, enabled: false) + Datadog.configuration.use(:sinatra, tracer: tracer, enabled: false) super end diff --git a/test/contrib/sinatra/tracer_test.rb b/test/contrib/sinatra/tracer_test.rb index f523d962d50..4d81b665500 100644 --- a/test/contrib/sinatra/tracer_test.rb +++ b/test/contrib/sinatra/tracer_test.rb @@ -40,7 +40,7 @@ def setup app().set :datadog_test_writer, @writer tracer = Datadog::Tracer.new(writer: @writer) - app().settings.datadog_tracer.configure(tracer: tracer, enabled: true) + Datadog.configuration.use(:sinatra, tracer: tracer, enabled: true) super end