-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathtmetrics_sdk.m
544 lines (443 loc) · 21.3 KB
/
tmetrics_sdk.m
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
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
classdef tmetrics_sdk < matlab.unittest.TestCase
% tests for metrics SDK
% Copyright 2023-2024 The MathWorks, Inc.
properties
OtelConfigFile
JsonFile
PidFile
OtelcolName
Otelcol
ListPid
ReadPidList
ExtractPid
Sigint
Sigterm
ShortIntervalReader
WaitTime
end
methods (TestClassSetup)
function setupOnce(testCase)
% add the utils folder to the path
utilsfolder = fullfile(fileparts(mfilename('fullpath')), "utils");
testCase.applyFixture(matlab.unittest.fixtures.PathFixture(utilsfolder));
commonSetupOnce(testCase);
interval = seconds(2);
timeout = seconds(1);
testCase.ShortIntervalReader = opentelemetry.sdk.metrics.PeriodicExportingMetricReader(...
"Interval", interval, "Timeout", timeout);
testCase.WaitTime = seconds(interval * 1.25);
end
end
methods (TestMethodSetup)
function setup(testCase)
commonSetup(testCase);
end
end
methods (TestMethodTeardown)
function teardown(testCase)
commonTeardown(testCase);
end
end
methods (Test)
function testDefaultExporter(testCase)
exporter = opentelemetry.exporters.otlp.defaultMetricExporter;
% do not test the exporter class or the endpoint, as they
% depend on which exporters are installed
verifyEqual(testCase, exporter.Timeout, seconds(10));
verifyEqual(testCase, string(exporter.PreferredAggregationTemporality), "cumulative");
end
function testExporterBasic(testCase)
timeout = seconds(5);
temporality = "delta";
exporter = opentelemetry.exporters.otlp.defaultMetricExporter("Timeout", timeout, ...
"PreferredAggregationTemporality", temporality);
verifyEqual(testCase, exporter.Timeout, timeout);
verifyEqual(testCase, string(exporter.PreferredAggregationTemporality), temporality);
end
function testOtlpFileExporter(testCase)
% testOtlpFileExporter: use a file exporter to write to files
testCase.assumeTrue(logical(exist("opentelemetry.exporters.otlp.OtlpFileMetricExporter", "class")), ...
"Otlp file exporter must be installed.");
% create temporary folder to write the output files
folderfixture = testCase.applyFixture(...
matlab.unittest.fixtures.TemporaryFolderFixture);
% create file exporter
output = fullfile(folderfixture.Folder,"output%n.json");
alias = fullfile(folderfixture.Folder,"output_latest.json");
exp = opentelemetry.exporters.otlp.OtlpFileMetricExporter(...
FileName=output, AliasName=alias);
reader = opentelemetry.sdk.metrics.PeriodicExportingMetricReader(exp, ...
Interval=testCase.ShortIntervalReader.Interval, ...
Timeout=testCase.ShortIntervalReader.Timeout);
p = opentelemetry.sdk.metrics.MeterProvider(reader);
metername = "foo";
countername = "bar";
mt = p.getMeter(metername);
ct = mt.createCounter(countername);
% create testing value
val = 10;
% add value
ct.add(val);
% fetch result
forceFlush(p);
clear("ct", "mt", "p");
resultstxt = readlines(alias);
results = jsondecode(resultstxt(1));
% verify meter and counter names
verifyEqual(testCase, string(results.resourceMetrics.scopeMetrics.metrics.name), countername);
verifyEqual(testCase, string(results.resourceMetrics.scopeMetrics.scope.name), metername);
% verify counter value
verifyEqual(testCase, results.resourceMetrics.scopeMetrics.metrics.sum.dataPoints.asDouble, val);
end
function testDefaultReader(testCase)
reader = opentelemetry.sdk.metrics.PeriodicExportingMetricReader();
verifyEqual(testCase, class(reader.MetricExporter), ...
class(opentelemetry.exporters.otlp.defaultMetricExporter));
verifyEqual(testCase, reader.Interval, minutes(1));
verifyEqual(testCase, reader.Interval.Format, 'm');
verifyEqual(testCase, reader.Timeout, seconds(30));
verifyEqual(testCase, reader.Timeout.Format, 's');
end
function testReaderBasic(testCase)
interval = hours(1);
timeout = minutes(30);
reader = opentelemetry.sdk.metrics.PeriodicExportingMetricReader( ...
"Interval", interval, ...
"Timeout", timeout);
verifyEqual(testCase, reader.Interval, interval);
verifyEqual(testCase, reader.Interval.Format, 'h'); % should not be converted to other units
verifyEqual(testCase, reader.Timeout, timeout);
verifyEqual(testCase, reader.Timeout.Format, 'm');
end
function testAddMetricReader(testCase)
metername = "foo";
countername = "bar";
exporter1 = opentelemetry.exporters.otlp.defaultMetricExporter(...
"PreferredAggregationTemporality", "delta");
exporter2 = opentelemetry.exporters.otlp.defaultMetricExporter(...
"PreferredAggregationTemporality", "delta");
reader1 = opentelemetry.sdk.metrics.PeriodicExportingMetricReader(exporter1, ...,
"Interval", seconds(2), "Timeout", seconds(1));
reader2 = opentelemetry.sdk.metrics.PeriodicExportingMetricReader(exporter2, ...,
"Interval", seconds(2), "Timeout", seconds(1));
p = opentelemetry.sdk.metrics.MeterProvider(reader1);
p.addMetricReader(reader2);
mt = p.getMeter(metername);
ct = mt.createCounter(countername);
% verify if the provider has two metric readers attached
reader_count = numel(p.MetricReader);
verifyEqual(testCase,reader_count, 2);
% verify if the json results has two exported instances after
% adding a single value
ct.add(1);
pause(testCase.WaitTime);
clear p;
results = readJsonResults(testCase);
result_count = numel(results);
verifyEqual(testCase,result_count, 2);
end
function testCustomResource(testCase)
% testCustomResource: check custom resources are included in
% emitted metrics
customkeys = ["foo" "bar"];
customvalues = [1 5];
mp = opentelemetry.sdk.metrics.MeterProvider(testCase.ShortIntervalReader, ...
"Resource", dictionary(customkeys, customvalues));
m = getMeter(mp, "mymeter");
c = createCounter(m, "mycounter");
% create testing value
val = 10;
% add value and attributes
c.add(val);
pause(testCase.WaitTime);
clear mp;
% perform test comparisons
results = readJsonResults(testCase);
results = results{1};
resourcekeys = string({results.resourceMetrics.resource.attributes.key});
for i = length(customkeys)
idx = find(resourcekeys == customkeys(i));
verifyNotEmpty(testCase, idx);
verifyEqual(testCase, results.resourceMetrics.resource.attributes(idx).value.doubleValue, customvalues(i));
end
end
function testViewBasic(testCase)
% testViewBasic: check view object changes the name and
% description of output metrics
view_name = "counter_view";
view_description = "view_description";
view = opentelemetry.sdk.metrics.View(Name=view_name, ....
Description=view_description, InstrumentType="Counter");
mp = opentelemetry.sdk.metrics.MeterProvider(...
testCase.ShortIntervalReader, View=view);
m = getMeter(mp, "mymeter", "1.0.0", "http://schema.org");
c = createCounter(m, "mycounter");
% add value and attributes
val = 10;
c.add(val);
pause(testCase.WaitTime);
clear mp;
results = readJsonResults(testCase);
results = results{end};
% verify view name and description
verifyEqual(testCase, string(results.resourceMetrics.scopeMetrics.metrics.name), view_name);
verifyEqual(testCase, string(results.resourceMetrics.scopeMetrics.metrics.description), view_description);
% fetch datapoint
dp = results.resourceMetrics.scopeMetrics.metrics.sum.dataPoints;
% verify counter value
verifyEqual(testCase, dp.asDouble, val);
end
function testViewHistogram(testCase)
% testViewHistogram: Change histogram bins
mp = opentelemetry.sdk.metrics.MeterProvider(testCase.ShortIntervalReader);
view_name = "histogram_view";
view_description = "view_description";
meter_name = "mymeter";
histogram_name = "myhistogram";
bin_edges = [0; 100; 200; 300; 400; 500];
view = opentelemetry.sdk.metrics.View(Name=view_name, ...
Description=view_description, InstrumentName=histogram_name, ...
InstrumentType="Histogram", MeterName=meter_name, ...
HistogramBinEdges=bin_edges);
addView(mp, view);
m = mp.getMeter(meter_name);
hist = m.createHistogram(histogram_name);
% record values
hist.record(0);
hist.record(200);
hist.record(201);
hist.record(401);
hist.record(402);
% wait for collector response
pause(testCase.WaitTime);
clear mp;
results = readJsonResults(testCase);
results = results{end};
% verify view name and description
verifyEqual(testCase, string(results.resourceMetrics.scopeMetrics.metrics.name), view_name);
verifyEqual(testCase, string(results.resourceMetrics.scopeMetrics.metrics.description), view_description);
% fetch datapoint
dp = results.resourceMetrics.scopeMetrics.metrics.histogram.dataPoints;
% verify histogram sum
expected_sum = 1204;
verifyEqual(testCase, dp.sum, expected_sum);
% verify histogram bounds
verifyEqual(testCase, dp.explicitBounds, bin_edges);
% verify histogram buckets
expected_buckets = {'1'; '0'; '1'; '1'; '0'; '2'; '0'};
verifyEqual(testCase, dp.bucketCounts, expected_buckets);
end
function testViewAggregation(testCase)
% testViewAggregation: change aggregation of metric instruments
metername = "foo";
countername = "bar";
view = opentelemetry.sdk.metrics.View(InstrumentType="Counter", ...
InstrumentName=countername, Aggregation="LastValue");
mp = opentelemetry.sdk.metrics.MeterProvider(...
testCase.ShortIntervalReader, View=view);
m = getMeter(mp, metername);
c = createCounter(m, countername);
% add values
maxi = 5;
for i = 1:maxi
c.add(i);
end
pause(testCase.WaitTime);
clear mp;
results = readJsonResults(testCase);
results = results{end};
% verify counter name
verifyEqual(testCase, string(results.resourceMetrics.scopeMetrics.metrics.name), countername);
% verify counter value is the last value rather than the sum
dp = results.resourceMetrics.scopeMetrics.metrics.gauge.dataPoints;
verifyEqual(testCase, dp.asDouble, maxi);
end
function testViewAttributes(testCase)
% testViewAttributes: filter out attributes
metername = "foo";
countername = "bar";
view = opentelemetry.sdk.metrics.View;
view.InstrumentType = "Counter";
view.InstrumentName = countername;
view.AllowedAttributes= "Building";
mp = opentelemetry.sdk.metrics.MeterProvider(...
testCase.ShortIntervalReader, View=view);
m = getMeter(mp, metername);
c = createCounter(m, countername);
% add values
values = 10:10:40;
add(c, values(1), "Building", 1, "Room", 1);
add(c, values(2), "Building", 1, "Room", 2);
add(c, values(3), "Building", 1, "Room", 1);
add(c, values(4), "Building", 1, "Room", 2);
pause(testCase.WaitTime);
clear mp;
results = readJsonResults(testCase);
results = results{end};
% verify counter name
verifyEqual(testCase, string(results.resourceMetrics.scopeMetrics.metrics.name), countername);
% verify "Room" attribute has been filtered out
dp = results.resourceMetrics.scopeMetrics.metrics.sum.dataPoints;
verifyEqual(testCase, dp.asDouble, sum(values));
verifyLength(testCase, dp.attributes, 1);
verifyEqual(testCase, string(dp.attributes(1).key), "Building");
verifyEqual(testCase, dp.attributes(1).value.doubleValue, 1);
end
function testViewAllowAllAttributes(testCase)
% testViewAllowAllAttributes: Specify AllowedAttributes to
% allow all
metername = "foo";
countername = "bar";
buildingattr = "Building";
roomattr = "Room";
view = opentelemetry.sdk.metrics.View;
view.InstrumentType = "Counter";
view.InstrumentName = countername;
view.AllowedAttributes= "*"; % allow all attributes
mp = opentelemetry.sdk.metrics.MeterProvider(...
testCase.ShortIntervalReader, View=view);
m = getMeter(mp, metername);
c = createCounter(m, countername);
% add values
values1 = [10 50];
values2 = [20 60];
add(c, values1(1), buildingattr, 1, roomattr, 1);
add(c, values2(1), buildingattr, 1, roomattr, 2);
add(c, values1(2), buildingattr, 1, roomattr, 1);
add(c, values2(2), buildingattr, 1, roomattr, 2);
pause(testCase.WaitTime);
clear mp;
results = readJsonResults(testCase);
results = results{end};
% verify counter name
verifyEqual(testCase, string(results.resourceMetrics.scopeMetrics.metrics.name), countername);
% verify all attributes are included
dp = results.resourceMetrics.scopeMetrics.metrics.sum.dataPoints;
verifyLength(testCase, dp, 2);
verifyLength(testCase, dp(1).attributes, 2);
attrkeys = string({dp(1).attributes.key});
buildingidx = find(attrkeys == buildingattr);
roomidx = find(attrkeys == roomattr);
verifyNotEmpty(testCase, buildingidx);
verifyNotEmpty(testCase, roomidx);
% verify counts are correct
roomidx = [dp(1).attributes(roomidx).value.doubleValue ...
dp(2).attributes(roomidx).value.doubleValue];
verifyEqual(testCase, dp(roomidx==1).asDouble, sum(values1));
verifyEqual(testCase, dp(roomidx==2).asDouble, sum(values2));
end
function testMultipleViews(testCase)
% testMultipleView: Applying multiple views to a meter provider
% match instrument name
instmatch_name = "match_instrument_name";
instmatch = opentelemetry.sdk.metrics.View(Name=instmatch_name, ....
InstrumentType="Counter", Instrumentname="foo(.*)");
% match meter name
metermatch_name = "match_meter_name";
metermatch = opentelemetry.sdk.metrics.View;
metermatch.Name = metermatch_name;
metermatch.InstrumentType = "Counter";
metermatch.MeterName = "abc";
mp = opentelemetry.sdk.metrics.MeterProvider(...
testCase.ShortIntervalReader, View=instmatch);
addView(mp, metermatch);
mxyz = getMeter(mp, "xyz");
foo_name = "foo1";
bar_name = "bar1";
cfoo = createCounter(mxyz, foo_name);
cbar = createCounter(mxyz, bar_name);
mabc = getMeter(mp, "abc");
quux_name = "quux1";
cquux = createCounter(mabc, quux_name);
valfoo = 10;
valbar = 25;
valquux = 40;
cfoo.add(valfoo);
cbar.add(valbar);
cquux.add(valquux);
pause(testCase.WaitTime);
clear mp;
results = readJsonResults(testCase);
results = vertcat(results{end}.resourceMetrics.scopeMetrics.metrics);
% verify view name only applied to matched metric
metricnames = {results.name};
baridx = find(strcmp(metricnames, bar_name));
fooidx = find(strcmp(metricnames, foo_name));
quuxidx = find(strcmp(metricnames, quux_name));
instmatchidx = find(strcmp(metricnames, instmatch_name));
metermatchidx = find(strcmp(metricnames, metermatch_name));
verifyNotEmpty(testCase, baridx);
verifyEmpty(testCase, fooidx);
verifyEmpty(testCase, quuxidx);
verifyNotEmpty(testCase, instmatchidx);
verifyNotEmpty(testCase, metermatchidx);
% verify count value
barcount = results(baridx).sum.dataPoints;
verifyEqual(testCase, barcount.asDouble, valbar);
instmatchcount = results(instmatchidx).sum.dataPoints;
verifyEqual(testCase, instmatchcount.asDouble, valfoo);
metermatchcount = results(metermatchidx).sum.dataPoints;
verifyEqual(testCase, metermatchcount.asDouble, valquux);
end
function testShutdown(testCase)
% testShutdown: shutdown method should stop exporting
% of metrics
mp = opentelemetry.sdk.metrics.MeterProvider(testCase.ShortIntervalReader);
% suppress internal warning logs about repeated shutdown
nologs = SuppressInternalLogs; %#ok<NASGU>
% shutdown the meter provider
verifyTrue(testCase, shutdown(mp));
% create an instrument and add some values
m = getMeter(mp, "foo");
c = createCounter(m, "bar");
c.add(5);
% wait a little and then gather results, verify no metrics are
% generated
pause(testCase.WaitTime);
clear mp;
results = readJsonResults(testCase);
verifyEmpty(testCase, results);
end
function testCleanupSdk(testCase)
% testCleanupSdk: shutdown an SDK meter provider through the Cleanup class
% Shut down an SDK meter provider instance
mp = opentelemetry.sdk.metrics.MeterProvider(testCase.ShortIntervalReader);
% suppress internal warning logs about repeated shutdown
nologs = SuppressInternalLogs; %#ok<NASGU>
% shutdown the meter provider through the Cleanup class
verifyTrue(testCase, opentelemetry.sdk.common.Cleanup.shutdown(mp));
% create an instrument and add some values
m = getMeter(mp, "foo");
c = createCounter(m, "bar");
c.add(5);
% wait a little and then gather results, verify no metrics are
% generated
pause(testCase.WaitTime);
clear mp;
results = readJsonResults(testCase);
verifyEmpty(testCase, results);
end
function testCleanupApi(testCase)
% testCleanupApi: shutdown an API meter provider through the Cleanup class
% suppress internal warning logs about repeated shutdown
nologs = SuppressInternalLogs; %#ok<NASGU>
% Shut down an API meter provider instance
mp = opentelemetry.sdk.metrics.MeterProvider(testCase.ShortIntervalReader);
setMeterProvider(mp);
clear("mp");
mp_api = opentelemetry.metrics.Provider.getMeterProvider();
% shutdown the API meter provider through the Cleanup class
verifyTrue(testCase, opentelemetry.sdk.common.Cleanup.shutdown(mp_api));
% create an instrument and add some values
m = getMeter(mp_api, "foo");
c = createCounter(m, "bar");
c.add(5);
% wait a little and then gather results, verify no metrics are
% generated
pause(testCase.WaitTime);
clear("mp_api");
results = readJsonResults(testCase);
verifyEmpty(testCase, results);
end
end
end