Skip to content

Commit 4f08222

Browse files
Add a copy of the upstream delayed_job otel instrumentation
This is commit is there just to comply with the Apache2 License, so that modifications will be later visible/traceable. Co-Authored-By: MarcWoern <[email protected]>
1 parent 06b2f48 commit 4f08222

File tree

5 files changed

+191
-0
lines changed

5 files changed

+191
-0
lines changed
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
# frozen_string_literal: true
2+
3+
# Copyright The OpenTelemetry Authors
4+
#
5+
# SPDX-License-Identifier: Apache-2.0
6+
7+
# OpenTelemetry is an open source observability framework, providing a
8+
# general-purpose API, SDK, and related tools required for the instrumentation
9+
# of cloud-native software, frameworks, and libraries.
10+
#
11+
# The OpenTelemetry module provides global accessors for telemetry objects.
12+
# See the documentation for the `opentelemetry-api` gem for details.
13+
module OpenTelemetry
14+
# "Instrumentation" are specified by
15+
# https://github.com/open-telemetry/opentelemetry-specification/blob/784635d01d8690c8f5fcd1f55bdbc8a13cf2f4f2/specification/glossary.md#instrumentation-library
16+
#
17+
# Instrumentation should be able to handle the case when the library is not installed on a user's system.
18+
module Instrumentation
19+
end
20+
end
21+
22+
require_relative 'instrumentation/delayed_job'
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
# frozen_string_literal: true
2+
3+
# Copyright The OpenTelemetry Authors
4+
#
5+
# SPDX-License-Identifier: Apache-2.0
6+
7+
require 'opentelemetry'
8+
require 'opentelemetry-instrumentation-base'
9+
10+
module OpenTelemetry
11+
module Instrumentation
12+
# Contains the OpenTelemetry instrumentation for the DelayedJob gem
13+
module DelayedJob
14+
end
15+
end
16+
end
17+
18+
require_relative 'delayed_job/instrumentation'
19+
require_relative 'delayed_job/version'
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
# frozen_string_literal: true
2+
3+
# Copyright The OpenTelemetry Authors
4+
#
5+
# SPDX-License-Identifier: Apache-2.0
6+
7+
module OpenTelemetry
8+
module Instrumentation
9+
module DelayedJob
10+
# Instrumentation class that detects and installs the DelayedJob instrumentation
11+
class Instrumentation < OpenTelemetry::Instrumentation::Base
12+
MINIMUM_VERSION = Gem::Version.new('4.1')
13+
14+
install do |_config|
15+
require_dependencies
16+
register_tracer_plugin
17+
end
18+
19+
present do
20+
!defined?(::Delayed).nil?
21+
end
22+
23+
compatible do
24+
# Version is hardcoded in the gemspec
25+
# https://github.com/collectiveidea/delayed_job/blob/master/delayed_job.gemspec#L16
26+
gem_version = Gem.loaded_specs['delayed_job']&.version
27+
gem_version && gem_version >= MINIMUM_VERSION
28+
end
29+
30+
private
31+
32+
def require_dependencies
33+
require_relative 'plugins/tracer_plugin'
34+
end
35+
36+
def register_tracer_plugin
37+
::Delayed::Worker.plugins << Plugins::TracerPlugin
38+
end
39+
end
40+
end
41+
end
42+
end
Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
# frozen_string_literal: true
2+
3+
# Copyright The OpenTelemetry Authors
4+
#
5+
# SPDX-License-Identifier: Apache-2.0
6+
7+
require 'delayed/plugin'
8+
9+
module OpenTelemetry
10+
module Instrumentation
11+
module DelayedJob
12+
module Plugins
13+
# Delayed Job plugin that instruments invoke_job and other hooks
14+
class TracerPlugin < Delayed::Plugin
15+
class << self
16+
def instrument_enqueue(job, &block)
17+
return yield(job) unless enabled?
18+
19+
attributes = build_attributes(job)
20+
attributes['messaging.operation'] = 'publish'
21+
attributes.compact!
22+
23+
tracer.in_span("#{job_queue(job)} publish", attributes: attributes, kind: :producer) do |span|
24+
yield job
25+
span.set_attribute('messaging.message_id', job.id.to_s)
26+
add_events(span, job)
27+
end
28+
end
29+
30+
def instrument_invoke(job, &block)
31+
return yield(job) unless enabled?
32+
33+
attributes = build_attributes(job)
34+
attributes['messaging.delayed_job.attempts'] = job.attempts if job.attempts
35+
attributes['messaging.delayed_job.locked_by'] = job.locked_by if job.locked_by
36+
attributes['messaging.operation'] = 'process'
37+
attributes['messaging.message_id'] = job.id.to_s
38+
attributes.compact!
39+
40+
tracer.in_span("#{job_queue(job)} process", attributes: attributes, kind: :consumer) do |span|
41+
add_events(span, job)
42+
yield job
43+
end
44+
end
45+
46+
protected
47+
48+
def build_attributes(job)
49+
{
50+
'messaging.system' => 'delayed_job',
51+
'messaging.destination' => job_queue(job),
52+
'messaging.destination_kind' => 'queue',
53+
'messaging.delayed_job.name' => job_name(job),
54+
'messaging.delayed_job.priority' => job.priority
55+
}
56+
end
57+
58+
def add_events(span, job)
59+
span.add_event('run_at', timestamp: job.run_at) if job.run_at
60+
span.add_event('locked_at', timestamp: job.locked_at) if job.locked_at
61+
end
62+
63+
def enabled?
64+
DelayedJob::Instrumentation.instance.enabled?
65+
end
66+
67+
def tracer
68+
DelayedJob::Instrumentation.instance.tracer
69+
end
70+
71+
def job_name(job)
72+
# If Delayed Job is used via ActiveJob then get the job name from the payload
73+
if job.payload_object.respond_to?(:job_data)
74+
job.payload_object.job_data['job_class']
75+
else
76+
job.name
77+
end
78+
end
79+
80+
def job_queue(job)
81+
job.queue || 'default'
82+
end
83+
end
84+
85+
callbacks do |lifecycle|
86+
# rubocop:disable Performance/MethodObjectAsBlock
87+
lifecycle.around(:enqueue, &method(:instrument_enqueue))
88+
lifecycle.around(:invoke_job, &method(:instrument_invoke))
89+
# rubocop:enable Performance/MethodObjectAsBlock
90+
end
91+
end
92+
end
93+
end
94+
end
95+
end
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
# frozen_string_literal: true
2+
3+
# Copyright The OpenTelemetry Authors
4+
#
5+
# SPDX-License-Identifier: Apache-2.0
6+
7+
module OpenTelemetry
8+
module Instrumentation
9+
module DelayedJob
10+
VERSION = '0.22.1'
11+
end
12+
end
13+
end

0 commit comments

Comments
 (0)