-
-
Notifications
You must be signed in to change notification settings - Fork 512
/
Copy pathmetrics_spec.rb
139 lines (116 loc) · 3.63 KB
/
metrics_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
# frozen_string_literal: true
require 'spec_helper'
RSpec.describe Sentry::Metrics do
before do
perform_basic_setup do |config|
config.metrics.enabled = true
end
end
let(:aggregator) { Sentry.metrics_aggregator }
let(:fake_time) { Time.new(2024, 1, 1, 1, 1, 3) }
describe '.increment' do
it 'passes default value of 1.0 with only key' do
expect(aggregator).to receive(:add).with(
:c,
'foo',
1.0,
unit: 'none',
tags: {},
timestamp: nil
)
described_class.increment('foo')
end
it 'passes through args to aggregator' do
expect(aggregator).to receive(:add).with(
:c,
'foo',
5.0,
unit: 'second',
tags: { fortytwo: 42 },
timestamp: fake_time
)
described_class.increment('foo', 5.0, unit: 'second', tags: { fortytwo: 42 }, timestamp: fake_time)
end
end
describe '.distribution' do
it 'passes through args to aggregator' do
expect(aggregator).to receive(:add).with(
:d,
'foo',
5.0,
unit: 'second',
tags: { fortytwo: 42 },
timestamp: fake_time
)
described_class.distribution('foo', 5.0, unit: 'second', tags: { fortytwo: 42 }, timestamp: fake_time)
end
end
describe '.set' do
it 'passes through args to aggregator' do
expect(aggregator).to receive(:add).with(
:s,
'foo',
'jane',
unit: 'none',
tags: { fortytwo: 42 },
timestamp: fake_time
)
described_class.set('foo', 'jane', tags: { fortytwo: 42 }, timestamp: fake_time)
end
end
describe '.gauge' do
it 'passes through args to aggregator' do
expect(aggregator).to receive(:add).with(
:g,
'foo',
5.0,
unit: 'second',
tags: { fortytwo: 42 },
timestamp: fake_time
)
described_class.gauge('foo', 5.0, unit: 'second', tags: { fortytwo: 42 }, timestamp: fake_time)
end
end
describe '.timing' do
it 'does nothing without a block' do
expect(aggregator).not_to receive(:add)
described_class.timing('foo')
end
it 'does nothing with a non-duration unit' do
expect(aggregator).not_to receive(:add)
result = described_class.timing('foo', unit: 'ratio') { 42 }
expect(result).to eq(42)
end
it 'measures time taken as distribution and passes through args to aggregator' do
expect(aggregator).to receive(:add).with(
:d,
'foo',
an_instance_of(Integer),
unit: 'millisecond',
tags: { fortytwo: 42 },
timestamp: fake_time
)
result = described_class.timing('foo', unit: 'millisecond', tags: { fortytwo: 42 }, timestamp: fake_time) { sleep(0.1); 42 }
expect(result).to eq(42)
end
context 'with running transaction' do
let(:transaction) { transaction = Sentry.start_transaction(name: 'metrics') }
before do
perform_basic_setup do |config|
config.traces_sample_rate = 1.0
config.metrics.enabled = true
end
Sentry.get_current_scope.set_span(transaction)
end
it 'starts a span' do
expect(Sentry).to receive(:with_child_span).with(op: Sentry::Metrics::OP_NAME, description: 'foo', origin: Sentry::Metrics::SPAN_ORIGIN).and_call_original
described_class.timing('foo') { sleep(0.1) }
end
it 'has the correct tags on the new span' do
described_class.timing('foo', tags: { a: 42, b: [1, 2] }) { sleep(0.1) }
span = transaction.span_recorder.spans.last
expect(span.tags).to eq(a: '42', b: '1, 2')
end
end
end
end