Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature / Introduce Que instrumentation #1146

Merged
merged 8 commits into from
Aug 27, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion Appraisals
Original file line number Diff line number Diff line change
Expand Up @@ -182,7 +182,7 @@ elsif Gem::Version.new('2.1.0') <= Gem::Version.new(RUBY_VERSION) \
gem 'delayed_job'
gem 'delayed_job_active_record'
gem 'elasticsearch-transport'
gem 'presto-client', '>= 0.5.14'
gem 'presto-client', '>= 0.5.14'
gem 'ethon'
gem 'excon'
gem 'hiredis'
Expand Down Expand Up @@ -369,6 +369,7 @@ elsif Gem::Version.new('2.2.0') <= Gem::Version.new(RUBY_VERSION) \
gem 'sqlite3', '~> 1.3.6'
gem 'sucker_punch'
gem 'typhoeus'
gem 'que', '>= 1.0.0.beta2'
end
end
elsif Gem::Version.new('2.3.0') <= Gem::Version.new(RUBY_VERSION) \
Expand Down Expand Up @@ -533,6 +534,7 @@ elsif Gem::Version.new('2.3.0') <= Gem::Version.new(RUBY_VERSION) \
gem 'sqlite3', '~> 1.3.6'
gem 'sucker_punch'
gem 'typhoeus'
gem 'que', '>= 1.0.0.beta2'
end

appraise 'contrib-old' do
Expand Down Expand Up @@ -616,6 +618,7 @@ elsif Gem::Version.new('2.4.0') <= Gem::Version.new(RUBY_VERSION) \
gem 'sqlite3', '~> 1.3.6'
gem 'sucker_punch'
gem 'typhoeus'
gem 'que', '>= 1.0.0.beta2'
end

appraise 'contrib-old' do
Expand Down Expand Up @@ -743,6 +746,7 @@ elsif Gem::Version.new('2.5.0') <= Gem::Version.new(RUBY_VERSION) \
gem 'sqlite3', '~> 1.4.1', platform: :ruby
gem 'sucker_punch'
gem 'typhoeus'
gem 'que', '>= 1.0.0.beta2'
end

appraise 'contrib-old' do
Expand Down Expand Up @@ -858,6 +862,7 @@ elsif Gem::Version.new('2.6.0') <= Gem::Version.new(RUBY_VERSION) \
gem 'sqlite3', '~> 1.4.1'
gem 'sucker_punch'
gem 'typhoeus'
gem 'que', '>= 1.0.0.beta2'
end

appraise 'contrib-old' do
Expand Down Expand Up @@ -976,6 +981,7 @@ elsif Gem::Version.new('2.7.0') <= Gem::Version.new(RUBY_VERSION)
gem 'sqlite3', '~> 1.4.1'
gem 'sucker_punch'
gem 'typhoeus'
gem 'que', '>= 1.0.0.beta2'
end

appraise 'contrib-old' do
Expand Down
7 changes: 7 additions & 0 deletions Rakefile
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,7 @@ namespace :spec do
:mongodb,
:mysql2,
:presto,
:que,
:racecar,
:rack,
:rake,
Expand Down Expand Up @@ -339,6 +340,7 @@ task :ci do
sh 'bundle exec appraisal contrib rake spec:mongodb'
sh 'bundle exec appraisal contrib rake spec:mysql2'
sh 'bundle exec appraisal contrib rake spec:presto'
sh 'bundle exec appraisal contrib rake spec:que'
sh 'bundle exec appraisal contrib rake spec:racecar'
sh 'bundle exec appraisal contrib rake spec:rack'
sh 'bundle exec appraisal contrib rake spec:rake'
Expand Down Expand Up @@ -413,6 +415,7 @@ task :ci do
sh 'bundle exec appraisal contrib rake spec:mongodb'
sh 'bundle exec appraisal contrib rake spec:mysql2'
sh 'bundle exec appraisal contrib rake spec:presto'
sh 'bundle exec appraisal contrib rake spec:que'
sh 'bundle exec appraisal contrib rake spec:racecar'
sh 'bundle exec appraisal contrib rake spec:rack'
sh 'bundle exec appraisal contrib rake spec:rake'
Expand Down Expand Up @@ -492,6 +495,7 @@ task :ci do
sh 'bundle exec appraisal contrib rake spec:mongodb'
sh 'bundle exec appraisal contrib rake spec:mysql2'
sh 'bundle exec appraisal contrib rake spec:presto'
sh 'bundle exec appraisal contrib rake spec:que'
sh 'bundle exec appraisal contrib rake spec:racecar'
sh 'bundle exec appraisal contrib rake spec:rack'
sh 'bundle exec appraisal contrib rake spec:rake'
Expand Down Expand Up @@ -553,6 +557,7 @@ task :ci do
sh 'bundle exec appraisal contrib rake spec:mongodb'
sh 'bundle exec appraisal contrib rake spec:mysql2' if RUBY_PLATFORM != 'java' # built-in jdbc is used instead
sh 'bundle exec appraisal contrib rake spec:presto'
sh 'bundle exec appraisal contrib rake spec:que'
sh 'bundle exec appraisal contrib rake spec:racecar'
sh 'bundle exec appraisal contrib rake spec:rack'
sh 'bundle exec appraisal contrib rake spec:rake'
Expand Down Expand Up @@ -626,6 +631,7 @@ task :ci do
sh 'bundle exec appraisal contrib rake spec:mongodb'
sh 'bundle exec appraisal contrib rake spec:mysql2'
sh 'bundle exec appraisal contrib rake spec:presto'
sh 'bundle exec appraisal contrib rake spec:que'
sh 'bundle exec appraisal contrib rake spec:racecar'
sh 'bundle exec appraisal contrib rake spec:rack'
sh 'bundle exec appraisal contrib rake spec:rake'
Expand Down Expand Up @@ -699,6 +705,7 @@ task :ci do
sh 'bundle exec appraisal contrib rake spec:mongodb'
sh 'bundle exec appraisal contrib rake spec:mysql2'
sh 'bundle exec appraisal contrib rake spec:presto'
sh 'bundle exec appraisal contrib rake spec:que'
sh 'bundle exec appraisal contrib rake spec:racecar'
sh 'bundle exec appraisal contrib rake spec:rack'
sh 'bundle exec appraisal contrib rake spec:rake'
Expand Down
26 changes: 26 additions & 0 deletions docs/GettingStarted.md
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ To contribute, check out the [contribution guidelines][contribution docs] and [d
- [MySQL2](#mysql2)
- [Net/HTTP](#net-http)
- [Presto](#presto)
- [Que](#que)
- [Racecar](#racecar)
- [Rack](#rack)
- [Rails](#rails)
Expand Down Expand Up @@ -353,6 +354,7 @@ For a list of available integrations, and their configuration options, please re
| MySQL2 | `mysql2` | `>= 0.3.21` | *gem not available* | *[Link](#mysql2)* | *[Link](https://github.com/brianmario/mysql2)* |
| Net/HTTP | `http` | *(Any supported Ruby)* | *(Any supported Ruby)* | *[Link](#nethttp)* | *[Link](https://ruby-doc.org/stdlib-2.4.0/libdoc/net/http/rdoc/Net/HTTP.html)* |
| Presto | `presto` | `>= 0.5.14` | `>= 0.5.14` | *[Link](#presto)* | *[Link](https://github.com/treasure-data/presto-client-ruby)* |
| Que | `que` | `>= 1.0.0.beta2` | `>= 1.0.0.beta2` | *[Link](#que)* | *[Link](https://github.com/que-rb/que)* |
| Racecar | `racecar` | `>= 0.3.5` | `>= 0.3.5` | *[Link](#racecar)* | *[Link](https://github.com/zendesk/racecar)* |
| Rack | `rack` | `>= 1.1` | `>= 1.1` | *[Link](#rack)* | *[Link](https://github.com/rack/rack)* |
| Rails | `rails` | `>= 3.0` | `>= 3.0` | *[Link](#rails)* | *[Link](https://github.com/rails/rails)* |
Expand Down Expand Up @@ -1111,6 +1113,30 @@ Where `options` is an optional `Hash` that accepts the following parameters:
| `analytics_enabled` | Enable analytics for spans produced by this integration. `true` for on, `nil` to defer to global setting, `false` for off. | `false` |
| `service_name` | Service name used for `presto` instrumentation | `'presto'` |

### Que

The Que integration is a middleware which will trace job executions.

You can enable it through `Datadog.configure`:

```ruby
require 'ddtrace'

Datadog.configure do |c|
c.use :que, options
end
```

Where `options` is an optional `Hash` that accepts the following parameters:

| Key | Description | Default |
| --- | ----------- | ------- |
| `analytics_enabled` | Enable analytics for spans produced by this integration. `true` for on, `nil` to defer to global setting, `false` for off. | `false` |
| `enabled` | Defines whether Que should be traced. Useful for temporarily disabling tracing. `true` or `false` | `true` |
| `service_name` | Service name used for `que` instrumentation | `'que'` |
| `tag_args` | Enable tagging of a job's args field. `true` for on, `false` for off. | `false` |
| `tag_data` | Enable tagging of a job's data field. `true` for on, `false` for off. | `false` |

### Racecar

The Racecar integration provides tracing for Racecar jobs.
Expand Down
1 change: 1 addition & 0 deletions lib/ddtrace.rb
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ module Datadog
require 'ddtrace/contrib/integration'
require 'ddtrace/contrib/kafka/integration'
require 'ddtrace/contrib/presto/integration'
require 'ddtrace/contrib/que/integration'
require 'ddtrace/contrib/mysql2/integration'
require 'ddtrace/contrib/mongodb/integration'
require 'ddtrace/contrib/racecar/integration'
Expand Down
42 changes: 42 additions & 0 deletions lib/ddtrace/contrib/que/configuration/settings.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
# frozen_string_literal: true

require 'ddtrace/contrib/configuration/settings'

module Datadog
module Contrib
module Que
module Configuration
# Default settings for the Que integration
class Settings < Datadog::Contrib::Configuration::Settings
option :service_name, default: Ext::SERVICE_NAME
option :distributed_tracing, default: true

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 :tag_args do |o|
o.default { env_to_bool(Ext::ENV_TAG_ARGS_ENABLED, false) }
o.lazy
end

option :tag_data do |o|
o.default { env_to_bool(Ext::ENV_TAG_DATA_ENABLED, false) }
o.lazy
end
end
end
end
end
end
30 changes: 30 additions & 0 deletions lib/ddtrace/contrib/que/ext.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
# frozen_string_literal: true

module Datadog
module Contrib
module Que
# Que integration constants
module Ext
APP = 'que'.freeze
ENV_ANALYTICS_ENABLED = 'DD_TRACE_QUE_ANALYTICS_ENABLED'.freeze
ENV_ANALYTICS_ENABLED_OLD = 'DD_QUE_ANALYTICS_ENABLED'.freeze
ENV_ANALYTICS_SAMPLE_RATE = 'DD_TRACE_QUE_ANALYTICS_SAMPLE_RATE'.freeze
ENV_ANALYTICS_SAMPLE_RATE_OLD = 'DD_QUE_ANALYTICS_SAMPLE_RATE'.freeze
ENV_ENABLED = 'DD_TRACE_QUE_ENABLED'.freeze
ENV_TAG_ARGS_ENABLED = 'DD_TRACE_QUE_TAG_ARGS_ENABLED'.freeze
ENV_TAG_DATA_ENABLED = 'DD_TRACE_QUE_TAG_DATA_ENABLED'.freeze
SERVICE_NAME = 'que'.freeze
SPAN_JOB = 'que.job'.freeze
TAG_JOB_ARGS = 'que.job.args'.freeze
TAG_JOB_DATA = 'que.job.data'.freeze
TAG_JOB_ERROR_COUNT = 'que.job.error_count'.freeze
TAG_JOB_EXPIRED_AT = 'que.job.expired_at'.freeze
TAG_JOB_FINISHED_AT = 'que.job.finished_at'.freeze
TAG_JOB_ID = 'que.job.id'.freeze
TAG_JOB_PRIORITY = 'que.job.priority'.freeze
TAG_JOB_QUEUE = 'que.job.queue'.freeze
TAG_JOB_RUN_AT = 'que.job.run_at'.freeze
end
end
end
end
42 changes: 42 additions & 0 deletions lib/ddtrace/contrib/que/integration.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
# frozen_string_literal: true

require 'ddtrace/contrib/integration'
require 'ddtrace/contrib/que/ext'
require 'ddtrace/contrib/que/configuration/settings'
require 'ddtrace/contrib/que/patcher'

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

MINIMUM_VERSION = Gem::Version.new('1.0.0.beta2')

register_as :que, auto_patch: true

def self.version
Gem.loaded_specs[Datadog::Contrib::Que::Ext::APP] &&
Gem.loaded_specs[Datadog::Contrib::Que::Ext::APP].version
end

def self.loaded?
!defined?(::Que).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
24 changes: 24 additions & 0 deletions lib/ddtrace/contrib/que/patcher.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
# frozen_string_literal: true

require 'ddtrace/contrib/que/tracer'

module Datadog
module Contrib
module Que
# Patcher enables patching of 'que' module.
module Patcher
include Datadog::Contrib::Patcher

module_function

def target_version
Integration.version
end

def patch
::Que.job_middleware.push(Que::Tracer.new)
end
end
end
end
end
56 changes: 56 additions & 0 deletions lib/ddtrace/contrib/que/tracer.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
# frozen_string_literal: true

require 'ddtrace/contrib/analytics'

module Datadog
module Contrib
module Que
# Tracer is a Que's server-side middleware which traces executed jobs
class Tracer
def call(job)
trace_options = {
service: configuration[:service_name],
span_type: Datadog::Ext::AppTypes::WORKER
}

tracer.trace(Ext::SPAN_JOB, trace_options) do |request_span|
request_span.resource = job.class.name.to_s
request_span.set_tag(Ext::TAG_JOB_QUEUE, job.que_attrs[:queue])
request_span.set_tag(Ext::TAG_JOB_ID, job.que_attrs[:id])
request_span.set_tag(Ext::TAG_JOB_PRIORITY, job.que_attrs[:priority])
request_span.set_tag(Ext::TAG_JOB_ERROR_COUNT, job.que_attrs[:error_count])
request_span.set_tag(Ext::TAG_JOB_RUN_AT, job.que_attrs[:run_at])
request_span.set_tag(Ext::TAG_JOB_EXPIRED_AT, job.que_attrs[:expired_at])
request_span.set_tag(Ext::TAG_JOB_FINISHED_AT, job.que_attrs[:finished_at])
request_span.set_tag(Ext::TAG_JOB_ARGS, job.que_attrs[:args]) if configuration[:tag_args]
request_span.set_tag(Ext::TAG_JOB_DATA, job.que_attrs[:data]) if configuration[:tag_data]

set_sample_rate(request_span)
Contrib::Analytics.set_measured(request_span)

yield
end
end

private

def set_sample_rate(request_span)
if Contrib::Analytics.enabled?(configuration[:analytics_enabled])
Contrib::Analytics.set_sample_rate(
request_span,
configuration[:analytics_sample_rate]
)
end
end

def tracer
configuration[:tracer]
end

def configuration
Datadog.configuration[Datadog::Contrib::Que::Ext::APP.to_sym]
end
end
end
end
end
Loading