Skip to content

Commit

Permalink
Merge pull request #544 from DataDog/refactor/upgrade_to_integration_…
Browse files Browse the repository at this point in the history
…configuration

Upgrade to integrations to new configuration API
  • Loading branch information
delner authored Oct 2, 2018
2 parents edd3d55 + fa7327b commit 11985ec
Show file tree
Hide file tree
Showing 174 changed files with 2,711 additions and 1,274 deletions.
1 change: 1 addition & 0 deletions docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -217,6 +217,7 @@ services:
mysql:
image: mysql:5.6
environment:
- MYSQL_DATABASE=$TEST_MYSQL_DB
- MYSQL_ROOT_PASSWORD=$TEST_MYSQL_ROOT_PASSWORD
- MYSQL_PASSWORD=$TEST_MYSQL_PASSWORD
- MYSQL_USER=$TEST_MYSQL_USER
Expand Down
5 changes: 2 additions & 3 deletions docs/GettingStarted.md
Original file line number Diff line number Diff line change
Expand Up @@ -1163,13 +1163,12 @@ Where `options` is an optional `Hash` that accepts the following parameters:

The Sinatra integration traces requests and template rendering.

To start using the tracing client, make sure you import ``ddtrace`` and ``ddtrace/contrib/sinatra/tracer`` after
either ``sinatra`` or ``sinatra/base``:
To start using the tracing client, make sure you import ``ddtrace`` and ``use :sinatra`` after
either ``sinatra`` or ``sinatra/base``, and before you define your application/routes:

```ruby
require 'sinatra'
require 'ddtrace'
require 'ddtrace/contrib/sinatra/tracer'
Datadog.configure do |c|
c.use :sinatra, options
Expand Down
43 changes: 22 additions & 21 deletions lib/ddtrace.rb
Original file line number Diff line number Diff line change
Expand Up @@ -44,30 +44,31 @@ def configure(target = configuration, opts = {})
end

require 'ddtrace/contrib/base'
require 'ddtrace/contrib/active_model_serializers/patcher'
require 'ddtrace/contrib/active_model_serializers/integration'
require 'ddtrace/contrib/active_record/integration'
require 'ddtrace/contrib/aws/patcher'
require 'ddtrace/contrib/aws/integration'
require 'ddtrace/contrib/concurrent_ruby/integration'
require 'ddtrace/contrib/dalli/patcher'
require 'ddtrace/contrib/delayed_job/patcher'
require 'ddtrace/contrib/elasticsearch/patcher'
require 'ddtrace/contrib/excon/patcher'
require 'ddtrace/contrib/faraday/patcher'
require 'ddtrace/contrib/grape/patcher'
require 'ddtrace/contrib/graphql/patcher'
require 'ddtrace/contrib/grpc/patcher'
require 'ddtrace/contrib/http/patcher'
require 'ddtrace/contrib/dalli/integration'
require 'ddtrace/contrib/delayed_job/integration'
require 'ddtrace/contrib/elasticsearch/integration'
require 'ddtrace/contrib/excon/integration'
require 'ddtrace/contrib/faraday/integration'
require 'ddtrace/contrib/grape/integration'
require 'ddtrace/contrib/graphql/integration'
require 'ddtrace/contrib/grpc/integration'
require 'ddtrace/contrib/http/integration'
require 'ddtrace/contrib/integration'
require 'ddtrace/contrib/mongodb/patcher'
require 'ddtrace/contrib/mysql2/patcher'
require 'ddtrace/contrib/racecar/patcher'
require 'ddtrace/contrib/rack/patcher'
require 'ddtrace/contrib/rails/patcher'
require 'ddtrace/contrib/rake/patcher'
require 'ddtrace/contrib/redis/patcher'
require 'ddtrace/contrib/resque/patcher'
require 'ddtrace/contrib/mysql2/integration'
require 'ddtrace/contrib/mongodb/integration'
require 'ddtrace/contrib/racecar/integration'
require 'ddtrace/contrib/rack/integration'
require 'ddtrace/contrib/rails/integration'
require 'ddtrace/contrib/rake/integration'
require 'ddtrace/contrib/redis/integration'
require 'ddtrace/contrib/resque/integration'
require 'ddtrace/contrib/rest_client/integration'
require 'ddtrace/contrib/sequel/integration'
require 'ddtrace/contrib/sidekiq/patcher'
require 'ddtrace/contrib/sucker_punch/patcher'
require 'ddtrace/contrib/sidekiq/integration'
require 'ddtrace/contrib/sinatra/integration'
require 'ddtrace/contrib/sucker_punch/integration'
require 'ddtrace/monkey'
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
require 'ddtrace/contrib/configuration/settings'
require 'ddtrace/contrib/active_model_serializers/ext'

module Datadog
module Contrib
module ActiveModelSerializers
module Configuration
# Custom settings for the ActiveModelSerializers integration
class Settings < Contrib::Configuration::Settings
option :service_name, default: Ext::SERVICE_NAME
option :tracer, default: Datadog.tracer do |value|
(value || Datadog.tracer).tap do |v|
# Make sure to update tracers of all subscriptions
Events.subscriptions.each do |subscription|
subscription.tracer = v
end
end
end
end
end
end
end
end
6 changes: 4 additions & 2 deletions lib/ddtrace/contrib/active_model_serializers/event.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
require 'ddtrace/ext/http'
require 'ddtrace/contrib/active_support/notifications/event'
require 'ddtrace/contrib/active_model_serializers/ext'

module Datadog
module Contrib
Expand Down Expand Up @@ -31,12 +33,12 @@ def process(span, event, _id, payload)
# Set the resource name and serializer name
res = resource(payload[:serializer])
span.resource = res
span.set_tag('active_model_serializers.serializer', res)
span.set_tag(Ext::TAG_SERIALIZER, res)

span.span_type = Datadog::Ext::HTTP::TEMPLATE

# Will be nil in 0.9
span.set_tag('active_model_serializers.adapter', payload[:adapter].class) unless payload[:adapter].nil?
span.set_tag(Ext::TAG_ADAPTER, payload[:adapter].class) unless payload[:adapter].nil?
end

private
Expand Down
4 changes: 2 additions & 2 deletions lib/ddtrace/contrib/active_model_serializers/events/render.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
require 'ddtrace/contrib/active_model_serializers/ext'
require 'ddtrace/contrib/active_model_serializers/event'

module Datadog
Expand All @@ -9,7 +10,6 @@ module Render
include ActiveModelSerializers::Event

EVENT_NAME = 'render.active_model_serializers'.freeze
SPAN_NAME = 'active_model_serializers.render'.freeze

module_function

Expand All @@ -23,7 +23,7 @@ def event_name
end

def span_name
self::SPAN_NAME
Ext::SPAN_RENDER
end
end
end
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
require 'ddtrace/contrib/active_model_serializers/ext'
require 'ddtrace/contrib/active_model_serializers/event'

module Datadog
Expand All @@ -9,7 +10,6 @@ module Serialize
include ActiveModelSerializers::Event

EVENT_NAME = '!serialize.active_model_serializers'.freeze
SPAN_NAME = 'active_model_serializers.serialize'.freeze

module_function

Expand All @@ -26,7 +26,7 @@ def event_name
end

def span_name
self::SPAN_NAME
Ext::SPAN_SERIALIZE
end
end
end
Expand Down
17 changes: 17 additions & 0 deletions lib/ddtrace/contrib/active_model_serializers/ext.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
module Datadog
module Contrib
module ActiveModelSerializers
# ActiveModelSerializers integration constants
module Ext
APP = 'active_model_serializers'.freeze
SERVICE_NAME = 'active_model_serializers'.freeze

SPAN_RENDER = 'active_model_serializers.render'.freeze
SPAN_SERIALIZE = 'active_model_serializers.serialize'.freeze

TAG_ADAPTER = 'active_model_serializers.adapter'.freeze
TAG_SERIALIZER = 'active_model_serializers.serializer'.freeze
end
end
end
end
39 changes: 39 additions & 0 deletions lib/ddtrace/contrib/active_model_serializers/integration.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
require 'ddtrace/contrib/integration'
require 'ddtrace/contrib/active_model_serializers/configuration/settings'
require 'ddtrace/contrib/active_model_serializers/patcher'

module Datadog
module Contrib
module ActiveModelSerializers
# Description of ActiveModelSerializers integration
class Integration
include Contrib::Integration

register_as :active_model_serializers

def self.version
Gem.loaded_specs['active_model_serializers'] \
&& Gem.loaded_specs['active_model_serializers'].version
end

def self.present?
super && defined?(::ActiveModel::Serializer)
end

def self.compatible?
super \
&& defined?(::ActiveSupport::Notifications) \
&& version >= Gem::Version.new('0.9.0')
end

def default_configuration
Configuration::Settings.new
end

def patcher
Patcher
end
end
end
end
end
67 changes: 24 additions & 43 deletions lib/ddtrace/contrib/active_model_serializers/patcher.rb
Original file line number Diff line number Diff line change
@@ -1,60 +1,41 @@
require 'ddtrace/contrib/patcher'
require 'ddtrace/ext/app_types'
require 'ddtrace/ext/http'
require 'ddtrace/contrib/active_model_serializers/ext'
require 'ddtrace/contrib/active_model_serializers/events'

module Datadog
module Contrib
module ActiveModelSerializers
# Provides instrumentation for ActiveModelSerializers through ActiveSupport instrumentation signals
# Patcher enables patching of 'active_model_serializers' module.
module Patcher
include Base
include Contrib::Patcher

VERSION_REQUIRED = Gem::Version.new('0.9.0')
module_function

register_as :active_model_serializers
def patched?
done?(:active_model_serializers)
end

option :service_name, default: 'active_model_serializers'
option :tracer, default: Datadog.tracer do |value|
(value || Datadog.tracer).tap do |v|
# Make sure to update tracers of all subscriptions
Events.subscriptions.each do |subscription|
subscription.tracer = v
def patch
do_once(:active_model_serializers) do
begin
# Subscribe to ActiveModelSerializers events
Events.subscribe!

# Set service info
get_option(:tracer).set_service_info(
get_option(:service_name),
Ext::APP,
Datadog::Ext::AppTypes::WEB
)
rescue StandardError => e
Datadog::Tracer.log.error("Unable to apply ActiveModelSerializers integration: #{e}")
end
end
end

class << self
def patch
return patched? if patched? || !compatible?

# Subscribe to ActiveModelSerializers events
Events.subscribe!

# Set service info
configuration[:tracer].set_service_info(
configuration[:service_name],
'active_model_serializers',
Ext::AppTypes::WEB
)

@patched = true
end

def patched?
return @patched if defined?(@patched)
@patched = false
end

private

def configuration
Datadog.configuration[:active_model_serializers]
end

def compatible?
Gem.loaded_specs['active_model_serializers'] && Gem.loaded_specs['activesupport'] \
&& Gem.loaded_specs['active_model_serializers'].version >= VERSION_REQUIRED
end
def get_option(option)
Datadog.configuration[:active_model_serializers].get_option(option)
end
end
end
Expand Down
5 changes: 3 additions & 2 deletions lib/ddtrace/contrib/active_record/configuration/settings.rb
Original file line number Diff line number Diff line change
@@ -1,16 +1,17 @@
require 'ddtrace/contrib/configuration/settings'
require 'ddtrace/contrib/active_record/ext'
require 'ddtrace/contrib/active_record/utils'

module Datadog
module Contrib
module ActiveRecord
module Configuration
# Unique settings for ActiveRecord
# Custom settings for the ActiveRecord integration
class Settings < Contrib::Configuration::Settings
option :orm_service_name
option :service_name, depends_on: [:tracer] do |value|
(value || Utils.adapter_name).tap do |service_name|
tracer.set_service_info(service_name, 'active_record', Ext::AppTypes::DB)
tracer.set_service_info(service_name, Ext::APP, Datadog::Ext::AppTypes::DB)
end
end

Expand Down
13 changes: 6 additions & 7 deletions lib/ddtrace/contrib/active_record/events/instantiation.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
require 'ddtrace/contrib/active_record/ext'
require 'ddtrace/contrib/active_record/event'

module Datadog
Expand All @@ -9,8 +10,6 @@ module Instantiation
include ActiveRecord::Event

EVENT_NAME = 'instantiation.active_record'.freeze
SPAN_NAME = 'active_record.instantiation'.freeze
DEFAULT_SERVICE_NAME = 'active_record'.freeze

module_function

Expand All @@ -24,7 +23,7 @@ def event_name
end

def span_name
self::SPAN_NAME
Ext::SPAN_INSTANTIATION
end

def process(span, event, _id, payload)
Expand All @@ -34,13 +33,13 @@ def process(span, event, _id, payload)
elsif span.parent
span.parent.service
else
self::DEFAULT_SERVICE_NAME
Ext::SERVICE_NAME
end

span.resource = payload.fetch(:class_name)
span.span_type = 'custom'
span.set_tag('active_record.instantiation.class_name', payload.fetch(:class_name))
span.set_tag('active_record.instantiation.record_count', payload.fetch(:record_count))
span.span_type = Ext::SPAN_TYPE_INSTANTIATION
span.set_tag(Ext::TAG_INSTANTIATION_CLASS_NAME, payload.fetch(:class_name))
span.set_tag(Ext::TAG_INSTANTIATION_RECORD_COUNT, payload.fetch(:record_count))
rescue StandardError => e
Datadog::Tracer.log.debug(e.message)
end
Expand Down
Loading

0 comments on commit 11985ec

Please sign in to comment.