Skip to content

Commit 92a1327

Browse files
committed
Remove hub from Transaction#initialize
1 parent 7de03b3 commit 92a1327

19 files changed

+182
-208
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
- Remove deprecated attribute `Sentry::Transaction.configuration`
1616
- Remove deprecated attribute `Sentry::Transaction.hub`
1717
- Remove deprecated argument `hub` to `Sentry::Transaction.finish`
18+
- Remove deprecated argument `hub` to `Sentry::Transaction#initialize` ([#2739](https://github.com/getsentry/sentry-ruby/pull/2739))
1819
- Remove `:monotonic_active_support_logger` from `config.breadcrumbs_logger` ([#2717](https://github.com/getsentry/sentry-ruby/pull/2717))
1920
- Migrate from to_hash to to_h ([#2351](https://github.com/getsentry/sentry-ruby/pull/2351))
2021
- Add `before_send_check_in` for applying to `CheckInEvent` ([#2703](https://github.com/getsentry/sentry-ruby/pull/2703))

sentry-rails/spec/sentry/rails/tracing/action_view_subscriber_spec.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@
3939
end
4040

4141
it "doesn't record spans" do
42-
transaction = Sentry::Transaction.new(sampled: false, hub: Sentry.get_current_hub)
42+
transaction = Sentry::Transaction.new(sampled: false)
4343
Sentry.get_current_scope.set_span(transaction)
4444

4545
get "/view"

sentry-rails/spec/sentry/rails/tracing/active_record_subscriber_spec.rb

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
end
2222

2323
it "records database query events" do
24-
transaction = Sentry::Transaction.new(sampled: true, hub: Sentry.get_current_hub)
24+
transaction = Sentry.start_transaction(sampled: true)
2525
Sentry.get_current_scope.set_span(transaction)
2626

2727
Post.all.to_a
@@ -54,7 +54,7 @@ def foo
5454
rspec_class = self.name # RSpec::ExampleGroups::[....]
5555

5656
before do
57-
transaction = Sentry::Transaction.new(sampled: true, hub: Sentry.get_current_hub)
57+
transaction = Sentry.start_transaction(sampled: true)
5858
Sentry.get_current_scope.set_span(transaction)
5959

6060
foo
@@ -121,7 +121,7 @@ def foo
121121
end
122122

123123
it "records database cached query events", skip: Rails.version.to_f < 5.1 do
124-
transaction = Sentry::Transaction.new(sampled: true, hub: Sentry.get_current_hub)
124+
transaction = Sentry.start_transaction(sampled: true)
125125
Sentry.get_current_scope.set_span(transaction)
126126

127127
ActiveRecord::Base.connection.cache do
@@ -155,7 +155,7 @@ def foo
155155
end
156156

157157
it "doesn't record spans" do
158-
transaction = Sentry::Transaction.new(sampled: false, hub: Sentry.get_current_hub)
158+
transaction = Sentry::Transaction.new(sampled: false)
159159
Sentry.get_current_scope.set_span(transaction)
160160

161161
Post.all.to_a

sentry-rails/spec/sentry/rails/tracing/active_support_subscriber_spec.rb

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
end
1717

1818
it "tracks cache write" do
19-
transaction = Sentry::Transaction.new(sampled: true, hub: Sentry.get_current_hub)
19+
transaction = Sentry.start_transaction(sampled: true)
2020
Sentry.get_current_scope.set_span(transaction)
2121

2222
Rails.cache.write("my_cache_key", "my_cache_value")
@@ -37,7 +37,7 @@
3737

3838
Rails.cache.write("my_cache_key", 0)
3939

40-
transaction = Sentry::Transaction.new(sampled: true, hub: Sentry.get_current_hub)
40+
transaction = Sentry.start_transaction(sampled: true)
4141
Sentry.get_current_scope.set_span(transaction)
4242
Rails.cache.increment("my_cache_key")
4343

@@ -57,7 +57,7 @@
5757

5858
Rails.cache.write("my_cache_key", 0)
5959

60-
transaction = Sentry::Transaction.new(sampled: true, hub: Sentry.get_current_hub)
60+
transaction = Sentry.start_transaction(sampled: true)
6161
Sentry.get_current_scope.set_span(transaction)
6262
Rails.cache.decrement("my_cache_key")
6363

@@ -72,7 +72,7 @@
7272
end
7373

7474
it "tracks cache read" do
75-
transaction = Sentry::Transaction.new(sampled: true, hub: Sentry.get_current_hub)
75+
transaction = Sentry.start_transaction(sampled: true)
7676
Sentry.get_current_scope.set_span(transaction)
7777
Rails.cache.read("my_cache_key")
7878

@@ -87,7 +87,7 @@
8787
end
8888

8989
it "tracks cache delete" do
90-
transaction = Sentry::Transaction.new(sampled: true, hub: Sentry.get_current_hub)
90+
transaction = Sentry.start_transaction(sampled: true)
9191
Sentry.get_current_scope.set_span(transaction)
9292

9393
Rails.cache.read("my_cache_key")
@@ -102,7 +102,7 @@
102102
expect(cache_transaction[:spans][0][:origin]).to eq("auto.cache.rails")
103103
end
104104
it "tracks cache prune" do
105-
transaction = Sentry::Transaction.new(sampled: true, hub: Sentry.get_current_hub)
105+
transaction = Sentry.start_transaction(sampled: true)
106106
Sentry.get_current_scope.set_span(transaction)
107107

108108
Rails.cache.write("my_cache_key", 123, expires_in: 0.seconds)
@@ -123,7 +123,7 @@
123123
skip("cache.hit is unset on Rails 6.0.x.") if Rails.version.to_i == 6
124124

125125
Rails.cache.write("my_cache_key", "my_cache_value")
126-
transaction = Sentry::Transaction.new(sampled: true, hub: Sentry.get_current_hub)
126+
transaction = Sentry.start_transaction(sampled: true)
127127
Sentry.get_current_scope.set_span(transaction)
128128
Rails.cache.read("my_cache_key")
129129
Rails.cache.read("my_cache_key_non_existing")
@@ -146,7 +146,7 @@
146146

147147
it "tracks cache delete" do
148148
Rails.cache.write("my_cache_key", "my_cache_value")
149-
transaction = Sentry::Transaction.new(sampled: true, hub: Sentry.get_current_hub)
149+
transaction = Sentry.start_transaction(sampled: true)
150150
Sentry.get_current_scope.set_span(transaction)
151151
Rails.cache.delete("my_cache_key")
152152

sentry-rails/spec/sentry/rails/tracing_spec.rb

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -275,7 +275,6 @@
275275
status: "ok",
276276
sampled: true,
277277
name: "a/path",
278-
hub: Sentry.get_current_hub
279278
)
280279
end
281280

sentry-ruby/lib/sentry/hub.rb

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,7 @@ def start_transaction(transaction: nil, custom_sampling_context: {}, instrumente
116116
return unless configuration.tracing_enabled?
117117
return unless instrumenter == configuration.instrumenter
118118

119-
transaction ||= Transaction.new(**options.merge(hub: self))
119+
transaction ||= Transaction.new(**options)
120120

121121
sampling_context = {
122122
transaction_context: transaction.to_h,
@@ -353,7 +353,6 @@ def continue_trace(env, **options)
353353
return nil unless propagation_context.incoming_trace
354354

355355
Transaction.new(
356-
hub: self,
357356
trace_id: propagation_context.trace_id,
358357
parent_span_id: propagation_context.parent_span_id,
359358
parent_sampled: propagation_context.parent_sampled,

sentry-ruby/lib/sentry/transaction.rb

Lines changed: 34 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,6 @@ class Transaction < Span
5454
attr_reader :sample_rand
5555

5656
def initialize(
57-
hub:,
5857
name: nil,
5958
source: :custom,
6059
parent_sampled: nil,
@@ -66,26 +65,14 @@ def initialize(
6665

6766
set_name(name, source: source)
6867
@parent_sampled = parent_sampled
69-
@hub = hub
7068
@baggage = baggage
71-
@tracing_enabled = hub.configuration.tracing_enabled?
72-
@traces_sampler = hub.configuration.traces_sampler
73-
@traces_sample_rate = hub.configuration.traces_sample_rate
74-
@trace_ignore_status_codes = hub.configuration.trace_ignore_status_codes
75-
@sdk_logger = hub.configuration.sdk_logger
76-
@release = hub.configuration.release
77-
@environment = hub.configuration.environment
78-
@dsn = hub.configuration.dsn
7969
@effective_sample_rate = nil
8070
@contexts = {}
8171
@measurements = {}
8272
@sample_rand = sample_rand
8373

84-
unless hub.profiler_running?
85-
@profiler = hub.configuration.profiler_class.new(hub.configuration)
86-
end
87-
8874
init_span_recorder
75+
init_profiler
8976

9077
unless @sample_rand
9178
generator = Utils::SampleRand.new(trace_id: @trace_id)
@@ -141,7 +128,9 @@ def set_measurement(name, value, unit = "")
141128
# @param sampling_context [Hash] a context Hash that'll be passed to `traces_sampler` (if provided).
142129
# @return [void]
143130
def set_initial_sample_decision(sampling_context:)
144-
unless @tracing_enabled
131+
configuration = Sentry.configuration
132+
133+
unless configuration && configuration.tracing_enabled?
145134
@sampled = false
146135
return
147136
end
@@ -152,12 +141,12 @@ def set_initial_sample_decision(sampling_context:)
152141
end
153142

154143
sample_rate =
155-
if @traces_sampler.is_a?(Proc)
156-
@traces_sampler.call(sampling_context)
144+
if configuration.traces_sampler.is_a?(Proc)
145+
configuration.traces_sampler.call(sampling_context)
157146
elsif !sampling_context[:parent_sampled].nil?
158147
sampling_context[:parent_sampled]
159148
else
160-
@traces_sample_rate
149+
configuration.traces_sample_rate
161150
end
162151

163152
transaction_description = generate_transaction_description
@@ -207,24 +196,27 @@ def finish(end_timestamp: nil)
207196
@name = UNLABELD_NAME
208197
end
209198

210-
@hub.stop_profiler!(self)
199+
hub = Sentry.get_current_hub
200+
return unless hub
201+
202+
hub.stop_profiler!(self)
211203

212204
if @sampled && ignore_status_code?
213205
@sampled = false
214206

215207
status_code = get_http_status_code
216208
log_debug("#{MESSAGE_PREFIX} Discarding #{generate_transaction_description} due to ignored HTTP status code: #{status_code}")
217209

218-
@hub.current_client.transport.record_lost_event(:event_processor, "transaction")
219-
@hub.current_client.transport.record_lost_event(:event_processor, "span")
210+
hub.current_client.transport.record_lost_event(:event_processor, "transaction")
211+
hub.current_client.transport.record_lost_event(:event_processor, "span")
220212
elsif @sampled
221-
event = @hub.current_client.event_from_transaction(self)
222-
@hub.capture_event(event)
213+
event = hub.current_client.event_from_transaction(self)
214+
hub.capture_event(event)
223215
else
224216
is_backpressure = Sentry.backpressure_monitor&.downsample_factor&.positive?
225217
reason = is_backpressure ? :backpressure : :sample_rate
226-
@hub.current_client.transport.record_lost_event(reason, "transaction")
227-
@hub.current_client.transport.record_lost_event(reason, "span")
218+
hub.current_client.transport.record_lost_event(reason, "transaction")
219+
hub.current_client.transport.record_lost_event(reason, "span")
228220
end
229221
end
230222

@@ -275,6 +267,15 @@ def init_span_recorder(limit = 1000)
275267
@span_recorder.add(self)
276268
end
277269

270+
def init_profiler
271+
hub = Sentry.get_current_hub
272+
return unless hub
273+
274+
unless hub.profiler_running?
275+
@profiler = hub.configuration.profiler_class.new(hub.configuration)
276+
end
277+
end
278+
278279
private
279280

280281
def generate_transaction_description
@@ -285,14 +286,16 @@ def generate_transaction_description
285286
end
286287

287288
def populate_head_baggage
289+
configuration = Sentry.configuration
290+
288291
items = {
289292
"trace_id" => trace_id,
290293
"sample_rate" => effective_sample_rate&.to_s,
291294
"sample_rand" => Utils::SampleRand.format(@sample_rand),
292295
"sampled" => sampled&.to_s,
293-
"environment" => @environment,
294-
"release" => @release,
295-
"public_key" => @dsn&.public_key
296+
"environment" => configuration&.environment,
297+
"release" => configuration&.release,
298+
"public_key" => configuration&.dsn&.public_key
296299
}
297300

298301
items["transaction"] = name unless source_low_quality?
@@ -302,12 +305,13 @@ def populate_head_baggage
302305
end
303306

304307
def ignore_status_code?
305-
return false unless @trace_ignore_status_codes
308+
trace_ignore_status_codes = Sentry.configuration&.trace_ignore_status_codes
309+
return false unless trace_ignore_status_codes
306310

307311
status_code = get_http_status_code
308312
return false unless status_code
309313

310-
@trace_ignore_status_codes.any? do |ignored|
314+
trace_ignore_status_codes.any? do |ignored|
311315
ignored.is_a?(Range) ? ignored.include?(status_code) : status_code == ignored
312316
end
313317
end

sentry-ruby/lib/sentry/utils/logging_helper.rb

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,27 +3,29 @@
33
module Sentry
44
# @private
55
module LoggingHelper
6-
# @!visibility private
7-
attr_reader :sdk_logger
8-
96
# @!visibility private
107
def log_error(message, exception, debug: false)
118
message = "#{message}: #{exception.message}"
129
message += "\n#{exception.backtrace.join("\n")}" if debug
1310

14-
sdk_logger.error(LOGGER_PROGNAME) do
11+
sdk_logger&.error(LOGGER_PROGNAME) do
1512
message
1613
end
1714
end
1815

1916
# @!visibility private
2017
def log_debug(message)
21-
sdk_logger.debug(LOGGER_PROGNAME) { message }
18+
sdk_logger&.debug(LOGGER_PROGNAME) { message }
2219
end
2320

2421
# @!visibility private
2522
def log_warn(message)
26-
sdk_logger.warn(LOGGER_PROGNAME) { message }
23+
sdk_logger&.warn(LOGGER_PROGNAME) { message }
24+
end
25+
26+
# @!visibility private
27+
def sdk_logger
28+
@sdk_logger ||= Sentry.sdk_logger
2729
end
2830
end
2931
end

sentry-ruby/spec/sentry/client/event_sending_spec.rb

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111

1212
before do
1313
stub_request(:post, Sentry::TestHelper::DUMMY_DSN)
14+
allow(Sentry).to receive(:configuration).and_return configuration
1415
end
1516

1617
subject(:client) { Sentry::Client.new(configuration) }
@@ -20,7 +21,9 @@
2021
end
2122

2223
let(:transaction) do
23-
transaction = Sentry::Transaction.new(name: "test transaction", op: "rack.request", hub: hub)
24+
configuration.traces_sample_rate = 1.0
25+
26+
transaction = hub.start_transaction(name: "test transaction", op: "rack.request")
2427
5.times { |i| transaction.with_child_span(description: "span_#{i}") { } }
2528
transaction
2629
end

0 commit comments

Comments
 (0)