-
Notifications
You must be signed in to change notification settings - Fork 375
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add standalone Rails ActiveJob integration
- Loading branch information
1 parent
bad39d4
commit f16f4b9
Showing
16 changed files
with
550 additions
and
47 deletions.
There are no files selected for viewing
This file contains 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 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 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 @@ | ||
# typed: false | ||
require 'ddtrace/contrib/configuration/settings' | ||
require 'ddtrace/contrib/active_job/ext' | ||
|
||
module Datadog | ||
module Contrib | ||
module ActiveJob | ||
module Configuration | ||
# Custom settings for the DelayedJob integration | ||
class Settings < Contrib::Configuration::Settings | ||
option :enabled do |o| | ||
o.default { env_to_bool(Ext::ENV_ENABLED, true) } | ||
o.lazy | ||
end | ||
|
||
option :analytics_enabled do |o| | ||
o.default { env_to_bool([Ext::ENV_ANALYTICS_ENABLED, Ext::ENV_ANALYTICS_ENABLED_OLD], false) } | ||
o.lazy | ||
end | ||
|
||
option :analytics_sample_rate do |o| | ||
o.default { env_to_float([Ext::ENV_ANALYTICS_SAMPLE_RATE, Ext::ENV_ANALYTICS_SAMPLE_RATE_OLD], 1.0) } | ||
o.lazy | ||
end | ||
|
||
option :service_name, default: Ext::SERVICE_NAME | ||
option :error_handler, default: Datadog::Tracer::DEFAULT_ON_ERROR | ||
end | ||
end | ||
end | ||
end | ||
end |
This file contains 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,31 @@ | ||
# typed: true | ||
require 'ddtrace/contrib/active_support/notifications/event' | ||
|
||
module Datadog | ||
module Contrib | ||
module ActiveJob | ||
# Defines basic behaviors for an ActiveJob event. | ||
module Event | ||
def self.included(base) | ||
base.include(ActiveSupport::Notifications::Event) | ||
base.extend(ClassMethods) | ||
end | ||
|
||
# Class methods for ActiveJob events. | ||
module ClassMethods | ||
def span_options | ||
{ service: configuration[:service_name] } | ||
end | ||
|
||
def tracer | ||
-> { configuration[:tracer] } | ||
end | ||
|
||
def configuration | ||
Datadog.configuration[:active_job] | ||
end | ||
end | ||
end | ||
end | ||
end | ||
end |
This file contains 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,31 @@ | ||
# typed: false | ||
require 'ddtrace/contrib/active_job/events/enqueue' | ||
require 'ddtrace/contrib/active_job/events/perform' | ||
|
||
module Datadog | ||
module Contrib | ||
module ActiveJob | ||
# Defines collection of instrumented ActiveJob events | ||
module Events | ||
ALL = [ | ||
Events::Enqueue, | ||
Events::Perform, | ||
].freeze | ||
|
||
module_function | ||
|
||
def all | ||
self::ALL | ||
end | ||
|
||
def subscriptions | ||
all.collect(&:subscriptions).collect(&:to_a).flatten | ||
end | ||
|
||
def subscribe! | ||
all.each(&:subscribe!) | ||
end | ||
end | ||
end | ||
end | ||
end |
This file contains 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,55 @@ | ||
# typed: false | ||
require 'ddtrace/ext/integration' | ||
require 'ddtrace/contrib/analytics' | ||
require 'ddtrace/contrib/active_job/ext' | ||
require 'ddtrace/contrib/active_job/event' | ||
|
||
module Datadog | ||
module Contrib | ||
module ActiveJob | ||
module Events | ||
# Defines instrumentation for enqueue.active_job event | ||
module Enqueue | ||
include ActiveJob::Event | ||
|
||
EVENT_NAME = 'enqueue.active_job'.freeze | ||
|
||
module_function | ||
|
||
def event_name | ||
self::EVENT_NAME | ||
end | ||
|
||
def span_name | ||
Ext::SPAN_ENQUEUE | ||
end | ||
|
||
def process(span, event, _id, payload) | ||
span.name = span_name | ||
span.service = configuration[:service_name] | ||
span.resource = payload[:job].class.name | ||
|
||
adapter_name = if payload[:adapter].is_a?(Class) | ||
payload[:adapter].name | ||
else | ||
payload[:adapter].class.name | ||
end | ||
span.set_tag(Ext::TAG_ADAPTER, adapter_name) | ||
|
||
# Set analytics sample rate | ||
if Contrib::Analytics.enabled?(configuration[:analytics_enabled]) | ||
Contrib::Analytics.set_sample_rate(span, configuration[:analytics_sample_rate]) | ||
end | ||
|
||
span.set_tag(Ext::TAG_JOB_ID, payload[:job].job_id) | ||
span.set_tag(Ext::TAG_JOB_QUEUE, payload[:job].queue_name) | ||
|
||
span.set_tag(Ext::TAG_JOB_PRIORITY, payload[:job].priority) if payload[:job].respond_to?(:priority) | ||
rescue StandardError => e | ||
Datadog.logger.debug(e.message) | ||
end | ||
end | ||
end | ||
end | ||
end | ||
end |
This file contains 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,55 @@ | ||
# typed: false | ||
require 'ddtrace/ext/integration' | ||
require 'ddtrace/contrib/analytics' | ||
require 'ddtrace/contrib/active_job/ext' | ||
require 'ddtrace/contrib/active_job/event' | ||
|
||
module Datadog | ||
module Contrib | ||
module ActiveJob | ||
module Events | ||
# Defines instrumentation for perform.active_job event | ||
module Perform | ||
include ActiveJob::Event | ||
|
||
EVENT_NAME = 'perform.active_job'.freeze | ||
|
||
module_function | ||
|
||
def event_name | ||
self::EVENT_NAME | ||
end | ||
|
||
def span_name | ||
Ext::SPAN_PERFORM | ||
end | ||
|
||
def process(span, event, _id, payload) | ||
span.name = span_name | ||
span.service = configuration[:service_name] | ||
span.resource = payload[:job].class.name | ||
|
||
adapter_name = if payload[:adapter].is_a?(Class) | ||
payload[:adapter].name | ||
else | ||
payload[:adapter].class.name | ||
end | ||
span.set_tag(Ext::TAG_ADAPTER, adapter_name) | ||
|
||
# Set analytics sample rate | ||
if Contrib::Analytics.enabled?(configuration[:analytics_enabled]) | ||
Contrib::Analytics.set_sample_rate(span, configuration[:analytics_sample_rate]) | ||
end | ||
|
||
span.set_tag(Ext::TAG_JOB_ID, payload[:job].job_id) | ||
span.set_tag(Ext::TAG_JOB_QUEUE, payload[:job].queue_name) | ||
|
||
span.set_tag(Ext::TAG_JOB_PRIORITY, payload[:job].priority) if payload[:job].respond_to?(:priority) | ||
rescue StandardError => e | ||
Datadog.logger.debug(e.message) | ||
end | ||
end | ||
end | ||
end | ||
end | ||
end |
This file contains 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,25 @@ | ||
# typed: true | ||
module Datadog | ||
module Contrib | ||
module ActiveJob | ||
module Ext | ||
APP = 'active_job'.freeze | ||
SERVICE_NAME = 'active_job'.freeze | ||
|
||
ENV_ENABLED = 'DD_TRACE_ACTIVE_JOB_ENABLED'.freeze | ||
ENV_ANALYTICS_ENABLED = 'DD_TRACE_ACTIVE_JOB_ANALYTICS_ENABLED'.freeze | ||
ENV_ANALYTICS_ENABLED_OLD = 'DD_ACTIVE_JOB_ANALYTICS_ENABLED'.freeze | ||
ENV_ANALYTICS_SAMPLE_RATE = 'DD_TRACE_ACTIVE_JOB_ANALYTICS_SAMPLE_RATE'.freeze | ||
ENV_ANALYTICS_SAMPLE_RATE_OLD = 'DD_ACTIVE_JOB_ANALYTICS_SAMPLE_RATE'.freeze | ||
|
||
SPAN_PERFORM = 'active_job.perform'.freeze | ||
SPAN_ENQUEUE = 'active_job.enqueue'.freeze | ||
|
||
TAG_ADAPTER = 'active_job.adapter'.freeze | ||
TAG_JOB_ID = 'active_job.job.id'.freeze | ||
TAG_JOB_QUEUE = 'active_job.job.queue'.freeze | ||
TAG_JOB_PRIORITY = 'active_job.job.priority'.freeze | ||
end | ||
end | ||
end | ||
end |
This file contains 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,46 @@ | ||
# typed: false | ||
require 'ddtrace/contrib/integration' | ||
require 'ddtrace/contrib/active_job/configuration/settings' | ||
require 'ddtrace/contrib/active_job/patcher' | ||
require 'ddtrace/contrib/rails/utils' | ||
|
||
module Datadog | ||
module Contrib | ||
module ActiveJob | ||
# Describes the ActiveJob integration | ||
class Integration | ||
include Contrib::Integration | ||
|
||
MINIMUM_VERSION = Gem::Version.new('4.2') | ||
|
||
register_as :active_job, auto_patch: false | ||
|
||
def self.version | ||
Gem.loaded_specs['activejob'] && Gem.loaded_specs['activejob'].version | ||
end | ||
|
||
def self.loaded? | ||
!defined?(::ActiveJob).nil? | ||
end | ||
|
||
def self.compatible? | ||
super && version >= MINIMUM_VERSION | ||
end | ||
|
||
# enabled by rails integration so should only auto instrument | ||
# if detected that it is being used without rails | ||
def auto_instrument? | ||
!Datadog::Contrib::Rails::Utils.railtie_supported? | ||
end | ||
|
||
def default_configuration | ||
Configuration::Settings.new | ||
end | ||
|
||
def patcher | ||
ActiveJob::Patcher | ||
end | ||
end | ||
end | ||
end | ||
end |
This file contains 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,25 @@ | ||
# typed: true | ||
require 'ddtrace/contrib/patcher' | ||
require 'ddtrace/contrib/active_job/ext' | ||
require 'ddtrace/contrib/active_job/events' | ||
|
||
module Datadog | ||
module Contrib | ||
module ActiveJob | ||
# Patcher enables patching of 'active_job' module. | ||
module Patcher | ||
include Contrib::Patcher | ||
|
||
module_function | ||
|
||
def target_version | ||
Integration.version | ||
end | ||
|
||
def patch | ||
Events.subscribe! | ||
end | ||
end | ||
end | ||
end | ||
end |
This file contains 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
Oops, something went wrong.