-
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 4bae1cb
Showing
20 changed files
with
842 additions
and
46 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,48 @@ | ||
# typed: true | ||
require 'ddtrace/contrib/active_support/notifications/event' | ||
require 'ddtrace/contrib/active_job/ext' | ||
|
||
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 | ||
|
||
def set_common_tags(span, payload) | ||
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) | ||
|
||
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) | ||
span.set_tag(Ext::TAG_JOB_EXECUTIONS, payload[:job].executions) | ||
|
||
span.set_tag(Ext::TAG_JOB_SCHEDULED_AT, Time.at(payload[:job].scheduled_at)) if payload[:job].scheduled_at | ||
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,39 @@ | ||
# typed: false | ||
require 'ddtrace/contrib/active_job/events/discard' | ||
require 'ddtrace/contrib/active_job/events/enqueue' | ||
require 'ddtrace/contrib/active_job/events/enqueue_at' | ||
require 'ddtrace/contrib/active_job/events/enqueue_retry' | ||
require 'ddtrace/contrib/active_job/events/perform' | ||
require 'ddtrace/contrib/active_job/events/retry_stopped' | ||
|
||
module Datadog | ||
module Contrib | ||
module ActiveJob | ||
# Defines collection of instrumented ActiveJob events | ||
module Events | ||
ALL = [ | ||
Events::Discard, | ||
Events::Enqueue, | ||
Events::EnqueueAt, | ||
Events::EnqueueRetry, | ||
Events::Perform, | ||
Events::RetryStopped, | ||
].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,46 @@ | ||
# 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 Discard | ||
include ActiveJob::Event | ||
|
||
EVENT_NAME = 'discard.active_job'.freeze | ||
|
||
module_function | ||
|
||
def event_name | ||
self::EVENT_NAME | ||
end | ||
|
||
def span_name | ||
Ext::SPAN_DISCARD | ||
end | ||
|
||
def process(span, event, _id, payload) | ||
span.name = span_name | ||
span.service = configuration[:service_name] | ||
span.resource = payload[:job].class.name | ||
|
||
# Set analytics sample rate | ||
if Contrib::Analytics.enabled?(configuration[:analytics_enabled]) | ||
Contrib::Analytics.set_sample_rate(span, configuration[:analytics_sample_rate]) | ||
end | ||
|
||
set_common_tags(span, payload) | ||
span.set_tag(Ext::TAG_JOB_ERROR, payload[:error]) | ||
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,45 @@ | ||
# 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 | ||
|
||
# Set analytics sample rate | ||
if Contrib::Analytics.enabled?(configuration[:analytics_enabled]) | ||
Contrib::Analytics.set_sample_rate(span, configuration[:analytics_sample_rate]) | ||
end | ||
|
||
set_common_tags(span, payload) | ||
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,45 @@ | ||
# 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 EnqueueAt | ||
include ActiveJob::Event | ||
|
||
EVENT_NAME = 'enqueue_at.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 | ||
|
||
# Set analytics sample rate | ||
if Contrib::Analytics.enabled?(configuration[:analytics_enabled]) | ||
Contrib::Analytics.set_sample_rate(span, configuration[:analytics_sample_rate]) | ||
end | ||
|
||
set_common_tags(span, payload) | ||
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,47 @@ | ||
# 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 EnqueueRetry | ||
include ActiveJob::Event | ||
|
||
EVENT_NAME = 'enqueue_retry.active_job'.freeze | ||
|
||
module_function | ||
|
||
def event_name | ||
self::EVENT_NAME | ||
end | ||
|
||
def span_name | ||
Ext::SPAN_ENQUEUE_RETRY | ||
end | ||
|
||
def process(span, event, _id, payload) | ||
span.name = span_name | ||
span.service = configuration[:service_name] | ||
span.resource = payload[:job].class.name | ||
|
||
# Set analytics sample rate | ||
if Contrib::Analytics.enabled?(configuration[:analytics_enabled]) | ||
Contrib::Analytics.set_sample_rate(span, configuration[:analytics_sample_rate]) | ||
end | ||
|
||
set_common_tags(span, payload) | ||
span.set_tag(Ext::TAG_JOB_ERROR, payload[:error]) | ||
span.set_tag(Ext::TAG_JOB_RETRY_WAIT, payload[:wait]) | ||
rescue StandardError => e | ||
Datadog.logger.debug(e.message) | ||
end | ||
end | ||
end | ||
end | ||
end | ||
end |
Oops, something went wrong.