Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit 5b6c609

Browse files
committedFeb 13, 2025··
WIP
1 parent 74684e7 commit 5b6c609

File tree

5 files changed

+40
-52
lines changed

5 files changed

+40
-52
lines changed
 

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

+1-21
Original file line numberDiff line numberDiff line change
@@ -18,26 +18,6 @@ def initialize(client, scope)
1818
first_layer = Layer.new(client, scope)
1919
@stack = [first_layer]
2020
@last_event_id = nil
21-
@current_profiler = {}
22-
end
23-
24-
def start_profiler!(transaction)
25-
MUTEX.synchronize do
26-
transaction.start_profiler!
27-
@current_profiler[transaction.__id__] = transaction.profiler
28-
end
29-
end
30-
31-
def stop_profiler!(transaction)
32-
MUTEX.synchronize do
33-
@current_profiler.delete(transaction.__id__)&.stop
34-
end
35-
end
36-
37-
def profiler_running?
38-
MUTEX.synchronize do
39-
!@current_profiler.empty?
40-
end
4121
end
4222

4323
def new_from_top
@@ -120,7 +100,7 @@ def start_transaction(transaction: nil, custom_sampling_context: {}, instrumente
120100
sampling_context.merge!(custom_sampling_context)
121101
transaction.set_initial_sample_decision(sampling_context: sampling_context)
122102

123-
start_profiler!(transaction)
103+
transaction.start_profiler!
124104

125105
transaction
126106
end

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

+2-5
Original file line numberDiff line numberDiff line change
@@ -85,10 +85,7 @@ def initialize(
8585
@effective_sample_rate = nil
8686
@contexts = {}
8787
@measurements = {}
88-
89-
unless @hub.profiler_running?
90-
@profiler = @configuration.profiler_class.new(@configuration)
91-
end
88+
@profiler = @configuration.profiler_class.new(@configuration)
9289

9390
init_span_recorder
9491
end
@@ -261,7 +258,7 @@ def finish(hub: nil, end_timestamp: nil)
261258
@name = UNLABELD_NAME
262259
end
263260

264-
@hub.stop_profiler!(self)
261+
@profiler.stop
265262

266263
if @sampled
267264
event = hub.current_client.event_from_transaction(self)

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

+31-17
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,14 @@ module Sentry
88
module Vernier
99
class Profiler
1010
EMPTY_RESULT = {}.freeze
11+
MUTEX = Mutex.new
1112

12-
attr_reader :started, :event_id, :result
13+
attr_reader :event_id, :result
1314

1415
def initialize(configuration)
1516
@event_id = SecureRandom.uuid.delete("-")
1617

17-
@started = false
18+
@vernier_collector = nil
1819
@sampled = nil
1920

2021
@profiling_enabled = defined?(Vernier) && configuration.profiling_enabled?
@@ -51,15 +52,28 @@ def set_initial_sample_decision(transaction_sampled)
5152
log("Discarding profile due to sampling decision") unless @sampled
5253
end
5354

54-
def start
55-
return unless @sampled
56-
return if @started
55+
def other_vernier_running?
56+
MUTEX.synchronize do
57+
@vernier_collector && vernier_collector_id != @vernier_collector.__id__
58+
end
59+
end
5760

58-
@started = ::Vernier.start_profile
61+
def this_vernier_running?
62+
MUTEX.synchronize do
63+
@vernier_collector && vernier_collector_id == @vernier_collector.__id__
64+
end
65+
end
5966

60-
log("Started")
67+
def vernier_collector_id
68+
Vernier.instance_variable_get(:@collector).__id__ if Vernier.instance_variable_get(:@collector)
69+
end
6170

62-
@started
71+
def start
72+
return if !@sampled || other_vernier_running?
73+
74+
@vernier_collector = ::Vernier.start_profile
75+
76+
log("Started with Vernier collector id: #{vernier_collector_id}")
6377
rescue RuntimeError => e
6478
# TODO: once Vernier raises something more dedicated, we should catch that instead
6579
if e.message.include?("Profile already started")
@@ -71,18 +85,18 @@ def start
7185

7286
def stop
7387
return unless @sampled
74-
return unless @started
7588

76-
@result = ::Vernier.stop_profile
77-
@started = false
89+
@result = ::Vernier.stop_profile if this_vernier_running?
7890

7991
log("Stopped")
80-
rescue RuntimeError => e
81-
if e.message.include?("Profile not started")
82-
log("Not stopped since not started")
83-
else
84-
log("Failed to stop Vernier: #{e.message}")
85-
end
92+
# rescue RuntimeError => e
93+
# if e.message.include?("Profile not started")
94+
# log("Not stopped since not started")
95+
# else
96+
# log("Failed to stop Vernier: #{e.message}")
97+
# end
98+
ensure
99+
@vernier_collector = nil
86100
end
87101

88102
def active_thread_id

‎sentry-ruby/spec/sentry/transactions/profiler_spec.rb

+4-7
Original file line numberDiff line numberDiff line change
@@ -15,19 +15,16 @@
1515
end
1616

1717
it 'starts profiler just once inside nested transactions' do
18-
10.times do
18+
10000.times do
1919
parent_transaction = Sentry.start_transaction(name: "parent")
2020
nested_transaction = Sentry.start_transaction(name: "nested")
2121

22-
ProfilerTest::Bar.bar
23-
24-
expect(Sentry.get_current_hub.profiler_running?).to be(true)
25-
26-
expect(parent_transaction.profiler).to_not be_nil
27-
expect(nested_transaction.profiler).to be_nil
22+
ProfilerTest::Bar::Foo.foo(10)
2823

2924
nested_transaction.finish
3025
parent_transaction.finish
26+
27+
expect(parent_transaction.profiler.to_hash).to_not be_empty
3128
end
3229
end
3330
end

‎sentry-ruby/spec/support/profiler.rb

+2-2
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@
33
module ProfilerTest
44
module Bar
55
module Foo
6-
def self.foo
7-
1e6.to_i.times { 2**2 }
6+
def self.foo(count = 1e6.to_i)
7+
count.times { 2**2 }
88
end
99
end
1010

0 commit comments

Comments
 (0)
Please sign in to comment.