Skip to content

Commit 6b133c3

Browse files
authored
refactor(ruby): drop the job extension registry
1 parent 45695de commit 6b133c3

10 files changed

Lines changed: 35 additions & 342 deletions

File tree

‎sentry-ruby/lib/sentry-ruby.rb‎

Lines changed: 0 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -66,8 +66,6 @@ module Sentry
6666
exception_locals_tp
6767
].freeze
6868

69-
JOB_EXTENSIONS = JobExtensions::Registry.new
70-
7169
class << self
7270
# @!visibility private
7371
def exception_locals_tp
@@ -153,27 +151,6 @@ def register_integration(name, version)
153151
integrations[name.to_s] = meta
154152
end
155153

156-
##### Job Extensions #####
157-
158-
# Returns the process-wide registry of background-job extensions.
159-
#
160-
# @return [JobExtensions::Registry]
161-
def job_extensions
162-
JOB_EXTENSIONS
163-
end
164-
165-
# Registers a background-job extension built by a backend gem. It can be
166-
# called at load time, before or after {Sentry.init}.
167-
#
168-
# @param extension [JobExtensions::Extension] an extension instance,
169-
# constructed with its own configuration and collaborators
170-
# @return [JobExtensions::Extension] the registered extension
171-
# @raise [ArgumentError] if the argument is not a {JobExtensions::Extension}
172-
# instance, or an extension with the same name is already registered
173-
def register_job_extension(extension)
174-
job_extensions.register(extension)
175-
end
176-
177154
##### Method Delegation #####
178155

179156
extend Forwardable
Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
# frozen_string_literal: true
22

33
require "sentry/job_extensions/extension"
4-
require "sentry/job_extensions/registry"
54
require "sentry/job_extensions/instrumentation"
65
require "sentry/job_extensions/consumer"
76
require "sentry/job_extensions/producer"

‎sentry-ruby/lib/sentry/job_extensions/consumer.rb‎

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,15 +7,14 @@ class Consumer < Instrumentation
77
OP_NAME = "queue.process"
88

99
def apply_scope_data(scope, job)
10-
data = registry.for(job).scope_data(job: job)
10+
data = extension.scope_data(job: job)
1111
scope.set_contexts(data[:contexts]) if data[:contexts]
1212
scope.set_tags(data[:tags]) if data[:tags]
1313
scope.set_extras(data[:extra]) if data[:extra]
1414
scope.set_user(data[:user]) if data[:user]
1515
end
1616

1717
def start_transaction(scope, job, trace_headers: {})
18-
extension = registry.for(job)
1918
options = {
2019
name: scope.transaction_name,
2120
source: scope.transaction_source,
@@ -31,9 +30,9 @@ def start_transaction(scope, job, trace_headers: {})
3130
return unless transaction
3231

3332
scope.set_span(transaction)
34-
safely(extension, "record the messaging data") do
33+
safely("record the messaging data") do
3534
set_messaging_data(transaction, extension.messaging_data(job: job))
36-
set_retry_count(transaction, extension, job)
35+
set_retry_count(transaction, job)
3736
end
3837

3938
transaction
@@ -47,15 +46,14 @@ def finish_transaction(transaction, status)
4746
end
4847

4948
def report_exception?(job, exception)
50-
registry.for(job).report_exception?(job: job, exception: exception)
49+
extension.report_exception?(job: job, exception: exception)
5150
end
5251

5352
def report_retried_exception?(job, exception)
54-
registry.for(job).report_retried_exception?(job: job, exception: exception)
53+
extension.report_retried_exception?(job: job, exception: exception)
5554
end
5655

5756
def capture_exception(job, exception, contexts: {}, tags: {}, extra: {})
58-
extension = registry.for(job)
5957
error_data = additive_error_data(extension.error_data(job: job, exception: exception))
6058

6159
options = {

‎sentry-ruby/lib/sentry/job_extensions/extension.rb‎

Lines changed: 7 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -5,26 +5,12 @@ module JobExtensions
55
# Base class for objects that let a background-job backend enrich Sentry's
66
# job integrations.
77
#
8-
# A backend gem subclasses this class, receives everything the extension
9-
# depends on (configuration, collaborators) through its constructor, and
10-
# registers one instance with {Sentry.register_job_extension}. Sentry treats
11-
# jobs as opaque objects: only the extension interprets the job it is given.
12-
#
13-
# @example Defining and registering an extension
14-
# class MyBackend::SentryJobExtension < Sentry::JobExtensions::Extension
15-
# def initialize(config:)
16-
# super(name: "my_backend")
17-
# @config = config
18-
# end
19-
#
20-
# def handles?(job)
21-
# job.class.queue_adapter_name.to_s == "my_backend"
22-
# end
23-
# end
24-
#
25-
# Sentry.register_job_extension(MyBackend::SentryJobExtension.new(config: MyBackend.config))
8+
# A backend gem subclasses this class and receives everything the extension
9+
# depends on (configuration, collaborators) through its constructor. Sentry
10+
# treats jobs as opaque objects: only the extension interprets the job it is
11+
# given.
2612
class Extension
27-
# The extension's unique name. No two registered extensions can share it.
13+
# The extension's name, used to identify it in SDK log messages.
2814
#
2915
# @return [String]
3016
attr_reader :name
@@ -40,22 +26,10 @@ def initialize(name:)
4026
@name = name.to_s.dup.freeze
4127
end
4228

43-
# Whether this extension handles the given job. Override it to claim
44-
# the jobs run by the extension's backend.
45-
#
46-
# If this method raises a StandardError, the failure is logged through the
47-
# SDK logger and the extension is treated as not handling the job.
48-
#
49-
# @param job [Object] the job, as passed by the job integration
50-
# @return [Boolean] false unless overridden
51-
def handles?(job)
52-
false
53-
end
54-
5529
# @!group Hooks
5630
#
57-
# Hooks are called directly by job integrations on the extension that
58-
# handles the job. Each hook maps its input to data and must not mutate
31+
# Hooks are called directly by job integrations on the extension they
32+
# are given. Each hook maps its input to data and must not mutate
5933
# the objects it is given; the job integration applies the returned
6034
# data. Override only the hooks the backend can supply.
6135

‎sentry-ruby/lib/sentry/job_extensions/instrumentation.rb‎

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,15 +13,15 @@ class Instrumentation
1313
receive_latency: Span::DataConventions::MESSAGING_MESSAGE_RECEIVE_LATENCY
1414
}.freeze
1515

16-
attr_reader :registry
16+
attr_reader :extension
1717

18-
def initialize(registry: Sentry.job_extensions)
19-
@registry = registry
18+
def initialize(extension:)
19+
@extension = extension
2020
end
2121

2222
private
2323

24-
def safely(extension, context, fallback: nil)
24+
def safely(context, fallback: nil)
2525
yield
2626
rescue StandardError => e
2727
log_error("Job extension #{extension.name.inspect} failed to #{context}", e)
@@ -34,7 +34,7 @@ def set_messaging_data(span, data)
3434
end
3535
end
3636

37-
def set_retry_count(span, extension, job)
37+
def set_retry_count(span, job)
3838
span.set_data(Span::DataConventions::MESSAGING_MESSAGE_RETRY_COUNT, extension.retry_info(job: job)[:count])
3939
end
4040

‎sentry-ruby/lib/sentry/job_extensions/producer.rb‎

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,8 @@ class Producer < Instrumentation
77
OP_NAME = "queue.publish"
88

99
def record_span(job)
10-
extension = registry.for(job)
11-
1210
Sentry.with_child_span(op: OP_NAME) do |span|
13-
safely(extension, "record the publish span") { describe_span(span, extension, job) } if span
11+
safely("record the publish span") { describe_span(span, job) } if span
1412

1513
yield
1614
end
@@ -21,16 +19,16 @@ def prepare_payload(job, payload)
2119
end
2220

2321
def inject_trace_headers(job, payload, headers)
24-
registry.for(job).inject_trace_headers(job: job, payload: payload, headers: headers)
22+
extension.inject_trace_headers(job: job, payload: payload, headers: headers)
2523
end
2624

2725
private
2826

29-
def describe_span(span, extension, job)
27+
def describe_span(span, job)
3028
span.set_description(extension.publish_description(job: job))
3129
span.set_origin(extension.span_origin(job: job))
3230
set_messaging_data(span, extension.publish_messaging_data(job: job))
33-
set_retry_count(span, extension, job)
31+
set_retry_count(span, job)
3432
end
3533

3634
class Delegated < Producer

‎sentry-ruby/lib/sentry/job_extensions/registry.rb‎

Lines changed: 0 additions & 91 deletions
This file was deleted.

‎sentry-ruby/spec/sentry/job_extensions/consumer_spec.rb‎

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -15,16 +15,12 @@ module TestJobsIntegration
1515
let(:string_io) { StringIO.new }
1616

1717
def consumer(**hooks)
18-
registry = Sentry::JobExtensions::Registry.new
19-
registry.register(
20-
Class.new(Sentry::JobExtensions::Extension) do
21-
define_method(:handles?) { |_job| true }
22-
define_method(:span_origin) { |job:| "auto.queue.test" }
23-
define_method(:integration) { |job:| Sentry::TestJobsIntegration }
24-
hooks.each { |hook, body| define_method(hook, &body) }
25-
end.new(name: "test_backend")
26-
)
27-
described_class.new(registry: registry)
18+
extension = Class.new(Sentry::JobExtensions::Extension) do
19+
define_method(:span_origin) { |job:| "auto.queue.test" }
20+
define_method(:integration) { |job:| Sentry::TestJobsIntegration }
21+
hooks.each { |hook, body| define_method(hook, &body) }
22+
end.new(name: "test_backend")
23+
described_class.new(extension: extension)
2824
end
2925

3026
def scope

‎sentry-ruby/spec/sentry/job_extensions/producer_spec.rb‎

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -5,16 +5,12 @@
55
let(:string_io) { StringIO.new }
66

77
def producer(**hooks)
8-
registry = Sentry::JobExtensions::Registry.new
9-
registry.register(
10-
Class.new(Sentry::JobExtensions::Extension) do
11-
define_method(:handles?) { |_job| true }
12-
define_method(:span_origin) { |job:| "auto.queue.test" }
13-
define_method(:publish_description) { |job:| "TestJob" }
14-
hooks.each { |hook, body| define_method(hook, &body) }
15-
end.new(name: "test_backend")
16-
)
17-
described_class.new(registry: registry)
8+
extension = Class.new(Sentry::JobExtensions::Extension) do
9+
define_method(:span_origin) { |job:| "auto.queue.test" }
10+
define_method(:publish_description) { |job:| "TestJob" }
11+
hooks.each { |hook, body| define_method(hook, &body) }
12+
end.new(name: "test_backend")
13+
described_class.new(extension: extension)
1814
end
1915

2016
def enriching_producer

0 commit comments

Comments
 (0)