-
-
Notifications
You must be signed in to change notification settings - Fork 484
/
scope_spec.rb
339 lines (277 loc) · 10.5 KB
/
scope_spec.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
require "spec_helper"
RSpec.describe Sentry::Scope do
let(:new_breadcrumb) do
new_breadcrumb = Sentry::Breadcrumb.new
new_breadcrumb.message = "foo"
new_breadcrumb
end
let(:configuration) { Sentry::Configuration.new }
let(:client) { Sentry::Client.new(configuration) }
let(:hub) do
Sentry::Hub.new(client, subject)
end
describe "#initialize" do
it "contains correct defaults" do
expect(subject.breadcrumbs).to be_a(Sentry::BreadcrumbBuffer)
expect(subject.contexts[:os].keys).to match_array([:name, :version, :build, :kernel_version, :machine])
expect(subject.contexts.dig(:runtime, :version)).to match(/ruby/)
expect(subject.extra).to eq({})
expect(subject.tags).to eq({})
expect(subject.user).to eq({})
expect(subject.fingerprint).to eq([])
expect(subject.transaction_names).to eq([])
expect(subject.transaction_sources).to eq([])
expect(subject.propagation_context).to be_a(Sentry::PropagationContext)
end
it "allows setting breadcrumb buffer's size limit" do
scope = described_class.new(max_breadcrumbs: 10)
expect(scope.breadcrumbs.buffer.count).to eq(10)
end
end
describe "#dup" do
it "copies the values instead of just references to values" do
copy = subject.dup
copy.breadcrumbs.record(new_breadcrumb)
copy.contexts.merge!(os: {})
copy.extra.merge!(foo: "bar")
copy.tags.merge!(foo: "bar")
copy.user.merge!(foo: "bar")
copy.transaction_names << "foo"
copy.transaction_sources << :url
copy.fingerprint << "bar"
expect(subject.breadcrumbs.to_hash).to eq({ values: [] })
expect(subject.contexts[:os].keys).to match_array([:name, :version, :build, :kernel_version, :machine])
expect(subject.contexts.dig(:runtime, :version)).to match(/ruby/)
expect(subject.extra).to eq({})
expect(subject.tags).to eq({})
expect(subject.user).to eq({})
expect(subject.fingerprint).to eq([])
expect(subject.transaction_names).to eq([])
expect(subject.transaction_sources).to eq([])
expect(subject.span).to eq(nil)
end
it "deep-copies span as well" do
perform_basic_setup
span = Sentry::Transaction.new(sampled: true, hub: hub)
subject.set_span(span)
copy = subject.dup
span.start_child
expect(copy.span.span_recorder.spans.count).to eq(1)
expect(subject.span.span_recorder.spans.count).to eq(2)
end
end
describe "#add_breadcrumb" do
it "adds the breadcrumb to the buffer" do
expect(subject.breadcrumbs.empty?).to eq(true)
subject.add_breadcrumb(new_breadcrumb)
expect(subject.breadcrumbs.peek).to eq(new_breadcrumb)
end
end
describe "#clear_breadcrumbs" do
subject do
described_class.new(max_breadcrumbs: 10)
end
before do
subject.add_breadcrumb(new_breadcrumb)
expect(subject.breadcrumbs.peek).to eq(new_breadcrumb)
end
it "clears all breadcrumbs by replacing the buffer object" do
subject.clear_breadcrumbs
expect(subject.breadcrumbs.empty?).to eq(true)
expect(subject.breadcrumbs.buffer.size).to eq(10)
end
end
describe "#add_event_processor" do
it "adds the processor to the scope" do
expect(subject.event_processors.count).to eq(0)
expect do
subject.add_event_processor { |e| e }
end.to change { subject.event_processors.count }.by(1)
end
end
describe ".add_global_event_processor" do
after { described_class.global_event_processors.clear }
it "adds the global processor to the scope" do
expect(described_class.global_event_processors.count).to eq(0)
expect do
described_class.add_global_event_processor { |e| e }
end.to change { described_class.global_event_processors.count }.by(1)
end
end
describe "#clear" do
it "resets the scope's data" do
subject.set_tags({foo: "bar"})
subject.set_extras({additional_info: "hello"})
subject.set_user({id: 1})
subject.set_transaction_name("WelcomeController#index")
subject.set_span(Sentry::Transaction.new(op: "foo", hub: hub))
subject.set_fingerprint(["foo"])
scope_id = subject.object_id
subject.clear
expect(subject.object_id).to eq(scope_id)
expect(subject.breadcrumbs).to be_a(Sentry::BreadcrumbBuffer)
expect(subject.contexts[:os].keys).to match_array([:name, :version, :build, :kernel_version, :machine])
expect(subject.contexts.dig(:runtime, :version)).to match(/ruby/)
expect(subject.extra).to eq({})
expect(subject.tags).to eq({})
expect(subject.user).to eq({})
expect(subject.fingerprint).to eq([])
expect(subject.transaction_names).to eq([])
expect(subject.transaction_sources).to eq([])
expect(subject.span).to eq(nil)
end
end
describe "#get_transaction & #get_span" do
before do
# because initializing transactions requires an active hub
perform_basic_setup
end
let(:transaction) do
Sentry::Transaction.new(op: "parent", hub: hub)
end
context "with span in the scope" do
let(:span) do
transaction.start_child(op: "child")
end
before do
subject.set_span(span)
end
it "gets the span from the scope" do
expect(subject.get_span).to eq(span)
end
it "gets the transaction from the span recorder" do
expect(subject.get_transaction).to eq(transaction)
end
end
context "without span in the scope" do
it "returns nil" do
expect(subject.get_transaction).to eq(nil)
end
it "returns nil" do
expect(subject.get_span).to eq(nil)
end
end
end
describe "#apply_to_event" do
before { perform_basic_setup }
let(:client) do
Sentry.get_current_client
end
subject do
scope = described_class.new
scope.set_tags({foo: "bar"})
scope.set_extras({additional_info: "hello"})
scope.set_user({id: 1})
scope.set_transaction_name("WelcomeController#index", source: :view)
scope.set_fingerprint(["foo"])
scope
end
let(:event) { client.event_from_message("test message") }
let(:check_in_event) { client.event_from_check_in("test_slug", :ok) }
it "applies the contextual data to event" do
subject.apply_to_event(event)
expect(event.tags).to eq({foo: "bar"})
expect(event.user).to eq({id: 1})
expect(event.extra).to eq({additional_info: "hello"})
expect(event.transaction).to eq("WelcomeController#index")
expect(event.transaction_info).to eq({ source: :view })
expect(event.breadcrumbs).to be_a(Sentry::BreadcrumbBuffer)
expect(event.fingerprint).to eq(["foo"])
expect(event.contexts).to include(:trace)
expect(event.contexts[:os].keys).to match_array([:name, :version, :build, :kernel_version, :machine])
expect(event.contexts.dig(:runtime, :version)).to match(/ruby/)
end
it "does not apply the contextual data to a check-in event" do
subject.apply_to_event(check_in_event)
expect(check_in_event.tags).to eq({})
expect(check_in_event.user).to eq({})
expect(check_in_event.extra).to eq({})
expect(check_in_event.transaction).to eq(nil)
expect(check_in_event.transaction_info).to eq(nil)
expect(check_in_event.breadcrumbs).to eq(nil)
expect(check_in_event.fingerprint).to eq([])
expect(check_in_event.contexts).to include(:trace)
end
it "doesn't override event's pre-existing data" do
event.tags = {foo: "baz"}
event.user = {id: 2}
event.extra = {additional_info: "nothing"}
event.contexts = {os: nil}
subject.apply_to_event(event)
expect(event.tags).to eq({foo: "baz"})
expect(event.user).to eq({id: 2})
expect(event.extra[:additional_info]).to eq("nothing")
expect(event.contexts[:os]).to eq(nil)
end
it "applies event processors to the event" do
subject.add_event_processor do |event, hint|
event.tags = { processed: true }
event.extra = hint
event
end
subject.apply_to_event(event, { foo: "bar" })
expect(event.tags).to eq({ processed: true })
expect(event.extra).to eq({ foo: "bar" })
end
context "with global event processor" do
before do
described_class.add_global_event_processor do |event, hint|
event.tags = { bar: 99 }
event.extra = hint
event
end
end
after { described_class.global_event_processors.clear }
it "applies global event processors to the event" do
subject.apply_to_event(event, { foo: 42 })
expect(event.tags).to eq({ bar: 99 })
expect(event.extra).to eq({ foo: 42 })
end
it "scope event processors take precedence over global event processors" do
subject.add_event_processor do |event, hint|
event.tags = { foo: 42 }
event
end
subject.apply_to_event(event)
expect(event.tags).to eq({ foo: 42 })
end
end
it "sets trace context from span if there's a span" do
transaction = Sentry::Transaction.new(op: "foo", hub: hub)
subject.set_span(transaction)
subject.apply_to_event(event)
expect(event.contexts[:trace]).to eq(transaction.get_trace_context)
expect(event.contexts.dig(:trace, :op)).to eq("foo")
end
it "sets trace context and dynamic_sampling_context from propagation context if there's no span" do
subject.apply_to_event(event)
expect(event.contexts[:trace]).to eq(subject.propagation_context.get_trace_context)
expect(event.dynamic_sampling_context).to eq(subject.propagation_context.get_dynamic_sampling_context)
end
context "with Rack", rack: true do
let(:env) do
Rack::MockRequest.env_for("/test", {})
end
subject do
scope = described_class.new
scope.set_rack_env(env)
scope
end
it "sets the request info the Event" do
subject.apply_to_event(event)
expect(event.to_hash.dig(:request, :url)).to eq("http:https://example.org/test")
end
end
end
describe "#generate_propagation_context" do
it "initializes new propagation context without env" do
expect(Sentry::PropagationContext).to receive(:new).with(subject, nil)
subject.generate_propagation_context
end
it "initializes new propagation context without env" do
env = { foo: 42 }
expect(Sentry::PropagationContext).to receive(:new).with(subject, env)
subject.generate_propagation_context(env)
end
end
end