-
Notifications
You must be signed in to change notification settings - Fork 401
Expand file tree
/
Copy pathmetrics_spec.rb
More file actions
403 lines (319 loc) · 13.9 KB
/
metrics_spec.rb
File metadata and controls
403 lines (319 loc) · 13.9 KB
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
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
require 'spec_helper'
require 'datadog'
require 'datadog/core/metrics/client'
require 'datadog/core/runtime/metrics'
RSpec.describe Datadog::Core::Runtime::Metrics do
let(:logger) { logger_allowing_debug }
let(:telemetry) { double(Datadog::Core::Telemetry::Component) }
let(:options) { {experimental_propagate_process_tags_enabled: true} }
subject(:runtime_metrics) { described_class.new(logger: logger, telemetry: telemetry, **options) }
describe '::new' do
context 'given :services' do
let(:options) { super().merge(services: services) }
let(:services) { %w[service-a service-b] }
it do
expect(runtime_metrics.send(:service_tags)).to include(
"#{Datadog::Core::Runtime::Ext::Metrics::TAG_SERVICE}:service-a",
"#{Datadog::Core::Runtime::Ext::Metrics::TAG_SERVICE}:service-b"
)
end
end
end
describe '#register_service' do
subject(:register_service) { runtime_metrics.register_service(service) }
context 'when enabled' do
before do
runtime_metrics.enabled = true
register_service
end
context 'and service is a string' do
let(:service) { 'parser' }
it 'registers the span\'s service' do
expect(runtime_metrics.default_metric_options[:tags]).to include("service:#{service}")
end
end
context 'and service is nil' do
let(:service) { nil }
it 'registers the span\'s service' do
expect(runtime_metrics.default_metric_options[:tags]).to_not include('service:')
end
end
end
context 'when disabled' do
let(:service) { 'parser' }
before do
runtime_metrics.enabled = false
register_service
end
it 'registers the span\'s service' do
expect(runtime_metrics.default_metric_options[:tags]).to_not include("service:#{service}")
end
end
end
describe '#flush' do
subject(:flush) { runtime_metrics.flush }
shared_examples_for 'runtime metric flush' do |metric, metric_name|
let(:metric_value) { rand }
context 'when available' do
before { allow(runtime_metrics).to receive(:gauge) }
it do
allow(metric).to receive(:available?)
.and_return(true)
allow(metric).to receive(:value)
.and_return(metric_value)
flush
expect(runtime_metrics).to have_received(:gauge)
.with(metric_name, metric_value)
.once
end
end
context 'when unavailable' do
it do
allow(metric).to receive(:available?)
.and_return(false)
expect(metric).to_not receive(:value)
expect(runtime_metrics).to_not receive(:gauge)
.with(metric_name, anything)
flush
end
end
context 'when an error is thrown' do
before { allow(Datadog.logger).to receive(:warn) }
it do
allow(metric).to receive(:available?)
.and_raise(RuntimeError)
flush
expect(Datadog.logger).to have_received(:warn)
.with(/Error while sending runtime metric./)
.at_least(:once)
end
end
end
shared_examples_for 'a flush of all runtime metrics' do
context 'including ClassCount' do
it_behaves_like 'runtime metric flush',
Datadog::Core::Environment::ClassCount,
Datadog::Core::Runtime::Ext::Metrics::METRIC_CLASS_COUNT
end
context 'including ThreadCount' do
it_behaves_like 'runtime metric flush',
Datadog::Core::Environment::ThreadCount,
Datadog::Core::Runtime::Ext::Metrics::METRIC_THREAD_COUNT
end
context 'including GC stats' do
before { allow(runtime_metrics).to receive(:gauge) }
it do
flush
runtime_metrics.gc_metrics.each_key do |metric_name|
expect(runtime_metrics).to have_received(:gauge)
.with(metric_name, kind_of(Numeric))
.once
end
end
end
context 'including VMCache stats' do
before do
skip('This feature is only supported in CRuby') unless PlatformHelpers.mri?
allow(runtime_metrics).to receive(:gauge)
end
context 'with Ruby 2.x' do
before { skip('Test only runs on Ruby 2.x') unless RUBY_VERSION.start_with?('2.') }
it 'records the global_constant_state and global_method_state metrics' do
flush
expect(runtime_metrics).to have_received(:gauge)
.with(Datadog::Core::Runtime::Ext::Metrics::METRIC_GLOBAL_CONSTANT_STATE, kind_of(Numeric))
.once
expect(runtime_metrics).to have_received(:gauge)
.with(Datadog::Core::Runtime::Ext::Metrics::METRIC_GLOBAL_METHOD_STATE, kind_of(Numeric))
.once
end
end
context 'with Ruby 3.0 and 3.1' do
before { skip('Test only runs on Ruby 3.0 and 3.1') unless RUBY_VERSION.start_with?('3.0.', '3.1.') }
it 'records only the constant_global_state metric' do
flush
expect(runtime_metrics).to have_received(:gauge)
.with(Datadog::Core::Runtime::Ext::Metrics::METRIC_GLOBAL_CONSTANT_STATE, kind_of(Numeric))
.once
end
end
context 'with Ruby >= 3.2' do
before { skip('Test only runs on Ruby >= 3.2') if RUBY_VERSION < '3.2.' }
it 'records the constant_cache_invalidations and constant_cache_misses metrics' do
flush
expect(runtime_metrics).to have_received(:gauge)
.with(Datadog::Core::Runtime::Ext::Metrics::METRIC_CONSTANT_CACHE_INVALIDATIONS, kind_of(Numeric))
.once
expect(runtime_metrics).to have_received(:gauge)
.with(Datadog::Core::Runtime::Ext::Metrics::METRIC_CONSTANT_CACHE_MISSES, kind_of(Numeric))
.once
end
end
end
context 'including YJIT stats' do
before do
skip('This feature is only supported in CRuby') unless PlatformHelpers.mri?
skip('Test only runs on Ruby >= 3.2') if RUBY_VERSION < '3.2.'
end
context 'with YJIT enabled' do
before do
skip('Test only runs with YJIT enabled') unless Datadog::Core::Environment::YJIT.available?
allow(runtime_metrics).to receive(:gauge)
end
it do
flush
expect(runtime_metrics).to have_received(:gauge)
.with(Datadog::Core::Runtime::Ext::Metrics::METRIC_YJIT_CODE_GC_COUNT, kind_of(Numeric))
.once
expect(runtime_metrics).to have_received(:gauge)
.with(Datadog::Core::Runtime::Ext::Metrics::METRIC_YJIT_CODE_REGION_SIZE, kind_of(Numeric))
.once
expect(runtime_metrics).to have_received(:gauge)
.with(Datadog::Core::Runtime::Ext::Metrics::METRIC_YJIT_FREED_CODE_SIZE, kind_of(Numeric))
.once
expect(runtime_metrics).to have_received(:gauge)
.with(Datadog::Core::Runtime::Ext::Metrics::METRIC_YJIT_FREED_PAGE_COUNT, kind_of(Numeric))
.once
expect(runtime_metrics).to have_received(:gauge)
.with(Datadog::Core::Runtime::Ext::Metrics::METRIC_YJIT_INLINE_CODE_SIZE, kind_of(Numeric))
.once
expect(runtime_metrics).to have_received(:gauge)
.with(Datadog::Core::Runtime::Ext::Metrics::METRIC_YJIT_LIVE_PAGE_COUNT, kind_of(Numeric))
.once
expect(runtime_metrics).to have_received(:gauge)
.with(Datadog::Core::Runtime::Ext::Metrics::METRIC_YJIT_OBJECT_SHAPE_COUNT, kind_of(Numeric))
.once
expect(runtime_metrics).to have_received(:gauge)
.with(Datadog::Core::Runtime::Ext::Metrics::METRIC_YJIT_OUTLINED_CODE_SIZE, kind_of(Numeric))
.once
if RUBY_VERSION >= '3.3.0'
expect(runtime_metrics).to have_received(:gauge)
.with(Datadog::Core::Runtime::Ext::Metrics::METRIC_YJIT_YJIT_ALLOC_SIZE, kind_of(Numeric))
.once
end
end
end
context 'with YJIT enabled and RubyVM::YJIT.stats_enabled? true' do
before do
skip('Test only runs on Ruby 3.3 and 3.4') if RUBY_VERSION < '3.3.' || RUBY_VERSION >= '4.0.'
unless Datadog::Core::Environment::YJIT.available? && ::RubyVM::YJIT.stats_enabled?
skip('Test only runs with YJIT enabled and RubyVM::YJIT.stats_enabled? true')
end
allow(runtime_metrics).to receive(:gauge)
end
it do
flush
expect(runtime_metrics).to have_received(:gauge)
.with(Datadog::Core::Runtime::Ext::Metrics::METRIC_YJIT_RATIO_IN_YJIT, kind_of(Numeric))
.once
end
end
end
end
it_behaves_like 'a flush of all runtime metrics'
context 'with process tags enabled' do
let(:options) { super().merge(experimental_propagate_process_tags_enabled: true) }
let(:statsd) { spy('statsd') }
before do
allow(runtime_metrics).to receive(:statsd).and_return(statsd)
allow(statsd).to receive(:gauge)
allow(Datadog::Core::Environment::Process).to receive(:tags)
.and_return(['entrypoint.workdir:test', 'rails.application:test_app'])
runtime_metrics.enabled = true
end
it 'sends metrics with the process tags' do
flush
expect(statsd).to have_received(:gauge).with(anything, anything, hash_including(tags: array_including('entrypoint.workdir:test'))).at_least(:once)
expect(statsd).to have_received(:gauge).with(anything, anything, hash_including(tags: array_including('rails.application:test_app'))).at_least(:once)
end
end
end
describe '#gc_metrics' do
subject(:gc_metrics) { runtime_metrics.gc_metrics }
context 'on MRI' do
before { skip unless PlatformHelpers.mri? }
it 'has a metric for each value in GC.stat' do
is_expected.to have(GC.stat.keys.size).items
gc_metrics.each do |metric, value|
expect(metric).to start_with(Datadog::Core::Runtime::Ext::Metrics::METRIC_GC_PREFIX)
expect(value).to be_a_kind_of(Numeric)
end
end
end
context 'on JRuby' do
before { skip unless PlatformHelpers.jruby? }
it 'has a metric for each value in GC.stat' do
is_expected.to have_at_least(GC.stat.keys.count).items
gc_metrics.each do |metric, value|
expect(metric).to start_with(Datadog::Core::Runtime::Ext::Metrics::METRIC_GC_PREFIX)
expect(value).to be_a_kind_of(Numeric)
end
end
end
end
describe '#default_metric_options' do
subject(:default_metric_options) { runtime_metrics.default_metric_options }
describe ':tags' do
subject(:default_tags) { default_metric_options[:tags] }
context 'when :experimental_propagate_process_tags_enabled is true' do
before do
allow(Datadog::Core::Environment::Process).to receive(:tags)
.and_return(['entrypoint.workdir:test', 'entrypoint.name:test_script', 'rails.application:test_app'])
end
it 'includes process tags by default' do
is_expected.to include('entrypoint.workdir:test')
is_expected.to include('entrypoint.name:test_script')
is_expected.to include('rails.application:test_app')
end
end
context 'given :experimental_runtime_id_enabled' do
let(:options) { super().merge(experimental_runtime_id_enabled: runtime_id_enabled) }
let(:runtime_id_enabled) { true }
it do
is_expected.to include(*Datadog::Core::Metrics::Client.default_metric_options[:tags])
is_expected.to include('language:ruby')
is_expected.to include(/\Aruntime-id:/o)
end
end
context 'when no services have been registered' do
it do
is_expected.to include(*Datadog::Core::Metrics::Client.default_metric_options[:tags])
is_expected.to include('language:ruby')
is_expected.to_not include(/\Aruntime-id:/o)
end
end
context 'when services have been registered' do
let(:services) { %w[parser serializer] }
before { services.each { |service| runtime_metrics.register_service(service) } }
it do
is_expected.to include(*Datadog::Core::Metrics::Client.default_metric_options[:tags])
is_expected.to include('language:ruby')
is_expected.to include(*services.collect { |service| "service:#{service}" })
is_expected.to_not include(/\Aruntime-id:/o)
end
end
context 'when :experimental_propagate_process_tags_enabled is true' do
let(:options) { super().merge(experimental_propagate_process_tags_enabled: true) }
before do
expect(Datadog::Core::Environment::Process).to receive(:tags).and_return(['entrypoint.workdir:test', 'entrypoint.name:test_script', 'entrypoint.basedir:test', 'entrypoint.type:script', 'rails.application:test_app'])
end
it 'includes process tags when enabled' do
is_expected.to include('entrypoint.workdir:test')
is_expected.to include('entrypoint.name:test_script')
is_expected.to include('entrypoint.basedir:test')
is_expected.to include('entrypoint.type:script')
is_expected.to include('rails.application:test_app')
end
end
context 'when :experimental_propagate_process_tags_enabled is false' do
let(:options) { super().merge(experimental_propagate_process_tags_enabled: false) }
it 'does not include process tags when disabled' do
is_expected.to_not include('entrypoint.workdir:test')
is_expected.to_not include('entrypoint.name:test_script')
is_expected.to_not include('entrypoint.basedir:test')
is_expected.to_not include('entrypoint.type:script')
end
end
end
end
end