|
270 | 270 | end
|
271 | 271 | end
|
272 | 272 | end
|
| 273 | + |
| 274 | + describe 'code location reporting' do |
| 275 | + it 'does not record location if off' do |
| 276 | + perform_basic_setup do |config| |
| 277 | + config.metrics.enabled = true |
| 278 | + config.metrics.enable_code_locations = false |
| 279 | + end |
| 280 | + |
| 281 | + subject.add(:c, 'incr', 1) |
| 282 | + expect(subject.code_locations).to eq({}) |
| 283 | + end |
| 284 | + |
| 285 | + it 'records the code location with a timestamp for the day' do |
| 286 | + subject.add(:c, 'incr', 1, unit: 'second', stacklevel: 3) |
| 287 | + |
| 288 | + timestamp = Time.now.utc |
| 289 | + start_of_day = Time.utc(timestamp.year, timestamp.month, timestamp.day).to_i |
| 290 | + expect(subject.code_locations.keys.first).to eq(start_of_day) |
| 291 | + end |
| 292 | + |
| 293 | + it 'has the code location keyed with mri (metric resource identifier) from type/key/unit' do |
| 294 | + subject.add(:c, 'incr', 1, unit: 'second', stacklevel: 3) |
| 295 | + mri = subject.code_locations.values.first.keys.first |
| 296 | + expect(mri).to eq([:c, 'incr', 'second']) |
| 297 | + end |
| 298 | + |
| 299 | + it 'has the code location information in the hash' do |
| 300 | + subject.add(:c, 'incr', 1, unit: 'second', stacklevel: 3) |
| 301 | + |
| 302 | + location = subject.code_locations.values.first.values.first |
| 303 | + expect(location).to include(:abs_path, :filename, :pre_context, :context_line, :post_context, :lineno) |
| 304 | + expect(location[:abs_path]).to match(/aggregator_spec.rb/) |
| 305 | + expect(location[:filename]).to match(/aggregator_spec.rb/) |
| 306 | + expect(location[:context_line]).to include("subject.add(:c, 'incr', 1, unit: 'second', stacklevel: 3)") |
| 307 | + end |
| 308 | + |
| 309 | + it 'does not add code location for the same mri twice' do |
| 310 | + subject.add(:c, 'incr', 1, unit: 'second', stacklevel: 3) |
| 311 | + subject.add(:c, 'incr', 1, unit: 'second', stacklevel: 3) |
| 312 | + expect(subject.code_locations.values.first.size).to eq(1) |
| 313 | + end |
| 314 | + |
| 315 | + it 'adds code location for different mris twice' do |
| 316 | + subject.add(:c, 'incr', 1, unit: 'second', stacklevel: 3) |
| 317 | + subject.add(:c, 'incr', 1, unit: 'none', stacklevel: 3) |
| 318 | + expect(subject.code_locations.values.first.size).to eq(2) |
| 319 | + end |
| 320 | + end |
273 | 321 | end
|
274 | 322 |
|
275 | 323 | describe '#flush' do
|
276 |
| - context 'with empty buckets' do |
| 324 | + context 'with empty buckets and empty locations' do |
277 | 325 | it 'returns early and does nothing' do
|
278 | 326 | expect(sentry_envelopes.count).to eq(0)
|
279 | 327 | subject.flush
|
|
289 | 337 | before do
|
290 | 338 | allow(Time).to receive(:now).and_return(fake_time)
|
291 | 339 | 10.times { subject.add(:c, 'incr', 1) }
|
292 |
| - 5.times { |i| subject.add(:d, 'dist', i, unit: 'second', tags: { "foö$-bar" => "snöwmän% 23{}" }) } |
| 340 | + 5.times { |i| subject.add(:d, 'disöt', i, unit: 'second', tags: { "foö$-bar" => "snöwmän% 23{}" }) } |
293 | 341 |
|
294 | 342 | allow(Time).to receive(:now).and_return(fake_time + 9)
|
295 | 343 | 5.times { subject.add(:c, 'incr', 1) }
|
296 |
| - 5.times { |i| subject.add(:d, 'dist', i + 5, unit: 'second', tags: { "foö$-bar" => "snöwmän% 23{}" }) } |
| 344 | + 5.times { |i| subject.add(:d, 'disöt', i + 5, unit: 'second', tags: { "foö$-bar" => "snöwmän% 23{}" }) } |
297 | 345 |
|
298 | 346 | expect(subject.buckets.keys).to eq([fake_time.to_i - 3, fake_time.to_i + 7])
|
299 | 347 | expect(subject.buckets.values[0].length).to eq(2)
|
|
311 | 359 | expect(subject.buckets.values[0].length).to eq(2)
|
312 | 360 | end
|
313 | 361 |
|
| 362 | + it 'empties the pending code locations in place' do |
| 363 | + subject.flush |
| 364 | + expect(subject.code_locations).to eq({}) |
| 365 | + end |
| 366 | + |
314 | 367 | it 'calls the background worker' do
|
315 | 368 | expect(Sentry.background_worker).to receive(:perform)
|
316 | 369 | subject.flush
|
|
327 | 380 |
|
328 | 381 | incr, dist = item.payload.split("\n")
|
329 | 382 | expect(incr).to eq("incr@none:10.0|c|#environment:test,release:test-release|T#{fake_time.to_i - 3}")
|
330 |
| - expect(dist).to eq("dist@second:0.0:1.0:2.0:3.0:4.0|d|" + |
| 383 | + expect(dist).to eq("dis_t@second:0.0:1.0:2.0:3.0:4.0|d|" + |
331 | 384 | "#environment:test,fo_-bar:snöwmän 23{},release:test-release|" +
|
332 | 385 | "T#{fake_time.to_i - 3}")
|
333 | 386 | end
|
| 387 | + |
| 388 | + it 'sends the pending code locations in metric_meta envelope item with correct payload' do |
| 389 | + subject.flush |
| 390 | + |
| 391 | + envelope = sentry_envelopes.first |
| 392 | + expect(envelope.headers).to eq({}) |
| 393 | + |
| 394 | + item = envelope.items.last |
| 395 | + expect(item.headers).to eq({ type: 'metric_meta', content_type: 'application/json' }) |
| 396 | + expect(item.payload[:timestamp]).to be_a(Integer) |
| 397 | + |
| 398 | + mapping = item.payload[:mapping] |
| 399 | + expect(mapping.keys).to eq(['c:incr@none', 'd:dis_t@second']) |
| 400 | + |
| 401 | + location_1 = mapping['c:incr@none'].first |
| 402 | + expect(location_1[:type]).to eq('location') |
| 403 | + expect(location_1).to include(:abs_path, :filename, :pre_context, :context_line, :post_context, :lineno) |
| 404 | + |
| 405 | + location_2 = mapping['d:dis_t@second'].first |
| 406 | + expect(location_2[:type]).to eq('location') |
| 407 | + expect(location_2).to include(:abs_path, :filename, :pre_context, :context_line, :post_context, :lineno) |
| 408 | + end |
334 | 409 | end
|
335 | 410 |
|
336 | 411 | context 'with force' do
|
|
355 | 430 |
|
356 | 431 | incr1, dist1, incr2, dist2 = item.payload.split("\n")
|
357 | 432 | expect(incr1).to eq("incr@none:10.0|c|#environment:test,release:test-release|T#{fake_time.to_i - 3}")
|
358 |
| - expect(dist1).to eq("dist@second:0.0:1.0:2.0:3.0:4.0|d|" + |
| 433 | + expect(dist1).to eq("dis_t@second:0.0:1.0:2.0:3.0:4.0|d|" + |
359 | 434 | "#environment:test,fo_-bar:snöwmän 23{},release:test-release|" +
|
360 | 435 | "T#{fake_time.to_i - 3}")
|
361 | 436 | expect(incr2).to eq("incr@none:5.0|c|#environment:test,release:test-release|T#{fake_time.to_i + 7}")
|
362 |
| - expect(dist2).to eq("dist@second:5.0:6.0:7.0:8.0:9.0|d|" + |
| 437 | + expect(dist2).to eq("dis_t@second:5.0:6.0:7.0:8.0:9.0|d|" + |
363 | 438 | "#environment:test,fo_-bar:snöwmän 23{},release:test-release|" +
|
364 | 439 | "T#{fake_time.to_i + 7}")
|
365 | 440 | end
|
|
0 commit comments