-
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.
Merge pull request #1237 from sco11morgan/instrument-qless
- Loading branch information
Showing
14 changed files
with
564 additions
and
0 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
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,35 @@ | ||
require 'ddtrace/contrib/configuration/settings' | ||
require 'ddtrace/contrib/qless/ext' | ||
|
||
module Datadog | ||
module Contrib | ||
module Qless | ||
module Configuration | ||
# Custom settings for the Qless integration | ||
class Settings < Contrib::Configuration::Settings | ||
option :analytics_enabled do |o| | ||
o.default { env_to_bool(Ext::ENV_ANALYTICS_ENABLED, false) } | ||
o.lazy | ||
end | ||
|
||
option :analytics_sample_rate do |o| | ||
o.default { env_to_float(Ext::ENV_ANALYTICS_SAMPLE_RATE, 1.0) } | ||
o.lazy | ||
end | ||
|
||
option :tag_job_data do |o| | ||
o.default { env_to_bool(Ext::ENV_TAG_JOB_DATA, false) } | ||
o.lazy | ||
end | ||
|
||
option :tag_job_tags do |o| | ||
o.default { env_to_bool(Ext::ENV_TAG_JOB_TAGS, false) } | ||
o.lazy | ||
end | ||
|
||
option :service_name, default: Ext::SERVICE_NAME | ||
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,20 @@ | ||
module Datadog | ||
module Contrib | ||
module Qless | ||
# Qless integration constants | ||
module Ext | ||
APP = 'qless'.freeze | ||
ENV_ANALYTICS_ENABLED = 'DD_QLESS_ANALYTICS_ENABLED'.freeze | ||
ENV_ANALYTICS_SAMPLE_RATE = 'DD_QLESS_ANALYTICS_SAMPLE_RATE'.freeze | ||
ENV_TAG_JOB_DATA = 'DD_QLESS_TAG_JOB_DATA'.freeze | ||
ENV_TAG_JOB_TAGS = 'DD_QLESS_TAG_JOB_TAGS'.freeze | ||
SERVICE_NAME = 'qless'.freeze | ||
SPAN_JOB = 'qless.job'.freeze | ||
TAG_JOB_ID = 'qless.job.id'.freeze | ||
TAG_JOB_DATA = 'qless.job.data'.freeze | ||
TAG_JOB_QUEUE = 'qless.job.queue'.freeze | ||
TAG_JOB_TAGS = 'qless.job.tags'.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,38 @@ | ||
require 'ddtrace/contrib/integration' | ||
require 'ddtrace/contrib/qless/configuration/settings' | ||
require 'ddtrace/contrib/qless/patcher' | ||
|
||
module Datadog | ||
module Contrib | ||
module Qless | ||
# Description of Qless integration | ||
class Integration | ||
include Contrib::Integration | ||
|
||
MINIMUM_VERSION = Gem::Version.new('0.10.0') | ||
|
||
register_as :qless, auto_patch: true | ||
|
||
def self.version | ||
Gem.loaded_specs['qless'] && Gem.loaded_specs['qless'].version | ||
end | ||
|
||
def self.loaded? | ||
!defined?(::Qless).nil? | ||
end | ||
|
||
def self.compatible? | ||
super && version >= MINIMUM_VERSION | ||
end | ||
|
||
def default_configuration | ||
Configuration::Settings.new | ||
end | ||
|
||
def patcher | ||
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,35 @@ | ||
require 'ddtrace/contrib/patcher' | ||
require 'ddtrace/ext/app_types' | ||
|
||
module Datadog | ||
module Contrib | ||
module Qless | ||
# Patcher enables patching of 'qless' module. | ||
module Patcher | ||
include Contrib::Patcher | ||
|
||
module_function | ||
|
||
def target_version | ||
Integration.version | ||
end | ||
|
||
def patch | ||
require_relative 'qless_job' | ||
require_relative 'tracer_cleaner' | ||
|
||
# Instrument all Qless Workers | ||
::Qless::Workers::BaseWorker.class_eval do | ||
# These are executed in inverse order of listing here | ||
include QlessJob | ||
include TracerCleaner | ||
end | ||
end | ||
|
||
def get_option(option) | ||
Datadog.configuration[:qless].get_option(option) | ||
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,72 @@ | ||
require 'ddtrace/ext/app_types' | ||
require 'ddtrace/contrib/analytics' | ||
require 'qless' | ||
|
||
module Datadog | ||
module Contrib | ||
module Qless | ||
# Uses Qless job hooks to create traces | ||
module QlessJob | ||
def around_perform(job) | ||
return super unless datadog_configuration && tracer | ||
tracer.trace(Ext::SPAN_JOB, span_options) do |span| | ||
span.resource = job.klass_name | ||
span.span_type = Datadog::Ext::AppTypes::WORKER | ||
span.set_tag(Ext::TAG_JOB_ID, job.jid) | ||
span.set_tag(Ext::TAG_JOB_QUEUE, job.queue_name) | ||
|
||
tag_job_tags = datadog_configuration[:tag_job_tags] | ||
span.set_tag(Ext::TAG_JOB_TAGS, job.tags) if tag_job_tags | ||
|
||
tag_job_data = datadog_configuration[:tag_job_data] | ||
if tag_job_data && !job.data.empty? | ||
job_data = job.data.with_indifferent_access | ||
formatted_data = job_data.except(:tags).map do |key, value| | ||
"#{key}:#{value}".underscore | ||
end | ||
|
||
span.set_tag(Ext::TAG_JOB_DATA, formatted_data) | ||
end | ||
|
||
# Set analytics sample rate | ||
if Contrib::Analytics.enabled?(datadog_configuration[:analytics_enabled]) | ||
Contrib::Analytics.set_sample_rate(span, datadog_configuration[:analytics_sample_rate]) | ||
end | ||
|
||
# Measure service stats | ||
Contrib::Analytics.set_measured(span) | ||
|
||
super | ||
end | ||
end | ||
|
||
def after_fork | ||
configuration = Datadog.configuration[:qless] | ||
return if configuration.nil? | ||
|
||
# Add a pin, marking the job as forked. | ||
# Used to trigger shutdown in forks for performance reasons. | ||
# Cleanup happens in the TracerCleaner class | ||
Datadog::Pin.new( | ||
configuration[:service_name], | ||
config: { forked: true } | ||
).onto(::Qless) | ||
end | ||
|
||
private | ||
|
||
def span_options | ||
{ service: datadog_configuration[:service_name] } | ||
end | ||
|
||
def tracer | ||
datadog_configuration.tracer | ||
end | ||
|
||
def datadog_configuration | ||
Datadog.configuration[:qless] | ||
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,32 @@ | ||
module Datadog | ||
module Contrib | ||
module Qless | ||
# Shutdown Tracer in forks for performance reasons | ||
module TracerCleaner | ||
def around_perform(job) | ||
return super unless datadog_configuration && tracer | ||
|
||
super.tap do | ||
tracer.shutdown! if forked? | ||
end | ||
end | ||
|
||
private | ||
|
||
def forked? | ||
pin = Datadog::Pin.get_from(::Qless) | ||
return false unless pin | ||
pin.config[:forked] == true | ||
end | ||
|
||
def tracer | ||
datadog_configuration.tracer | ||
end | ||
|
||
def datadog_configuration | ||
Datadog.configuration[:qless] | ||
end | ||
end | ||
end | ||
end | ||
end |
Oops, something went wrong.