Skip to content

Commit 65a51e0

Browse files
authored
Avoid injecting tracing timestamp to all ActiveSupport instrument events (#1576)
* Only inject tracing timestamp to the events SDK subscribes to * Re-structure breadcrumb specs * Make sure subscribed tracing events are cleared after each tests * Update changelog
1 parent a6ce224 commit 65a51e0

File tree

8 files changed

+229
-147
lines changed

8 files changed

+229
-147
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
## Unreleased
22

33
- Avoid leaking tracing timestamp to breadcrumbs [#1575](https://github.com/getsentry/sentry-ruby/pull/1575)
4+
- Avoid injecting tracing timestamp to all ActiveSupport instrument events [#1576](https://github.com/getsentry/sentry-ruby/pull/1576)
5+
- Fixes [#1574](https://github.com/getsentry/sentry-ruby/issues/1574)
46

57
## 4.7.2
68

sentry-rails/lib/sentry/rails/tracing.rb

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,18 @@ def self.subscribers
1111
@subscribers
1212
end
1313

14+
def self.subscribed_tracing_events
15+
@subscribed_tracing_events ||= []
16+
end
17+
1418
def self.subscribe_tracing_events
1519
# need to avoid duplicated subscription
1620
return if @subscribed
1721

18-
subscribers.each(&:subscribe!)
22+
subscribers.each do |subscriber|
23+
subscriber.subscribe!
24+
subscribed_tracing_events << subscriber::EVENT_NAME
25+
end
1926

2027
@subscribed = true
2128
end
@@ -24,6 +31,7 @@ def self.unsubscribe_tracing_events
2431
return unless @subscribed
2532

2633
subscribers.each(&:unsubscribe!)
34+
subscribed_tracing_events.clear
2735

2836
@subscribed = false
2937
end
@@ -37,9 +45,10 @@ def self.patch_active_support_notifications
3745

3846
SentryNotificationExtension.module_eval do
3947
def instrument(name, payload = {}, &block)
40-
is_public_event = name[0] != "!"
41-
42-
payload[START_TIMESTAMP_NAME] = Time.now.utc.to_f if is_public_event
48+
# only inject timestamp to the events the SDK subscribes to
49+
if Tracing.subscribed_tracing_events.include?(name)
50+
payload[START_TIMESTAMP_NAME] = Time.now.utc.to_f if name[0] != "!" && payload.is_a?(Hash)
51+
end
4352

4453
super(name, payload, &block)
4554
end

sentry-rails/spec/sentry/rails/breadcrumbs/active_support_breadcrumbs_spec.rb

Lines changed: 0 additions & 109 deletions
This file was deleted.
Lines changed: 48 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,15 @@
11
require "spec_helper"
22

3-
4-
RSpec.describe "Sentry::Breadcrumbs::MonotonicActiveSupportLogger", type: :request do
5-
before do
6-
make_basic_app do |sentry_config|
7-
sentry_config.breadcrumbs_logger = [:monotonic_active_support_logger]
8-
sentry_config.traces_sample_rate = 1.0
9-
end
10-
end
11-
3+
RSpec.describe "Sentry::Breadcrumbs::ActiveSupportLogger", type: :request do
124
after do
13-
require 'sentry/rails/breadcrumb/monotonic_active_support_logger'
14-
Sentry::Rails::Breadcrumb::MonotonicActiveSupportLogger.detach
5+
require 'sentry/rails/breadcrumb/active_support_logger'
6+
Sentry::Rails::Breadcrumb::ActiveSupportLogger.detach
157
# even though we cleanup breadcrumbs in the rack middleware
16-
# Breadcrumbs::MonotonicActiveSupportLogger subscribes to "every" instrumentation
8+
# Breadcrumbs::ActiveSupportLogger subscribes to "every" instrumentation
179
# so it'll create other instrumentations "after" the request is finished
1810
# and we should clear those as well
1911
Sentry.get_current_scope.clear_breadcrumbs
12+
transport.events = []
2013
end
2114

2215
let(:transport) do
@@ -31,20 +24,13 @@
3124
transport.events.first.to_json_compatible
3225
end
3326

34-
after do
35-
transport.events = []
36-
end
37-
38-
context "given a Rails version < 6.1", skip: Rails.version.to_f >= 6.1 do
39-
it "does not run instrumentation" do
40-
get "/exception"
41-
42-
breadcrumbs = event.dig("breadcrumbs", "values")
43-
expect(breadcrumbs.count).to be_zero
27+
context "without tracing" do
28+
before do
29+
make_basic_app do |sentry_config|
30+
sentry_config.breadcrumbs_logger = [:active_support_logger]
31+
end
4432
end
45-
end
4633

47-
context "given a Rails version >= 6.1", skip: Rails.version.to_f <= 6.1 do
4834
it "captures correct data of exception requests" do
4935
get "/exception"
5036

@@ -67,6 +53,35 @@
6753
expect(breadcrumb["data"].keys).not_to include("response")
6854
end
6955

56+
it "ignores exception data" do
57+
get "/view_exception"
58+
59+
expect(event.dig("breadcrumbs", "values", -1, "data").keys).not_to include("exception")
60+
expect(event.dig("breadcrumbs", "values", -1, "data").keys).not_to include("exception_object")
61+
end
62+
63+
it "ignores events that doesn't have a started timestamp" do
64+
expect do
65+
ActiveSupport::Notifications.publish "foo", Object.new
66+
end.not_to raise_error
67+
68+
expect(breadcrumb_buffer.count).to be_zero
69+
end
70+
end
71+
72+
context "with tracing" do
73+
before do
74+
make_basic_app do |sentry_config|
75+
sentry_config.breadcrumbs_logger = [:active_support_logger]
76+
sentry_config.traces_sample_rate = 1.0
77+
end
78+
end
79+
80+
after do
81+
Sentry::Rails::Tracing.unsubscribe_tracing_events
82+
Sentry::Rails::Tracing.remove_active_support_notifications_patch
83+
end
84+
7085
it "captures correct request data of normal requests" do
7186
p = Post.create!
7287

@@ -89,19 +104,18 @@
89104
expect(breadcrumb["data"].keys).not_to include("response")
90105
end
91106

92-
it "ignores exception data" do
93-
get "/view_exception"
107+
it "doesn't add internal start timestamp payload to breadcrumbs data" do
108+
p = Post.create!
94109

95-
expect(event.dig("breadcrumbs", "values", -1, "data").keys).not_to include("exception")
96-
expect(event.dig("breadcrumbs", "values", -1, "data").keys).not_to include("exception_object")
97-
end
110+
get "/posts/#{p.id}"
98111

99-
it "ignores events that doesn't have a float as started attributes" do
100-
expect do
101-
ActiveSupport::Notifications.publish "foo", Time.now
102-
end.not_to raise_error
112+
expect(transport.events.count).to eq(1)
103113

104-
expect(breadcrumb_buffer.count).to be_zero
114+
transaction = transport.events.last.to_hash
115+
breadcrumbs = transaction[:breadcrumbs][:values]
116+
process_action_crumb = breadcrumbs.last
117+
expect(process_action_crumb[:category]).to eq("process_action.action_controller")
118+
expect(process_action_crumb[:data].has_key?(Sentry::Rails::Tracing::START_TIMESTAMP_NAME)).to eq(false)
105119
end
106120
end
107121
end

0 commit comments

Comments
 (0)