-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathtbuildtoolplugin.m
More file actions
477 lines (359 loc) · 15.8 KB
/
tbuildtoolplugin.m
File metadata and controls
477 lines (359 loc) · 15.8 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
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
classdef tbuildtoolplugin < matlab.unittest.TestCase
properties
OtelConfigFile
JsonFile
PidFile
ListPid
ReadPidList
ExtractPid
Sigint
Sigterm
OtelcolName
Otelcol
BuildRunner
end
methods (TestClassSetup)
function setupOnce(testCase)
% add the utils, callbacks, and fixtures folders to the path
folders = fullfile(fileparts(mfilename('fullpath')), ["utils" "callbacks" "fixtures"]);
testCase.applyFixture(matlab.unittest.fixtures.PathFixture(folders));
commonSetupOnce(testCase);
end
end
methods (TestMethodSetup)
function setup(testCase)
commonSetup(testCase);
end
function onlyTestIfgRPCIsInstalled(testCase)
% Do not run if gRPC is not installed
testCase.assumeTrue(logical(exist("opentelemetry.exporters.otlp.OtlpGrpcSpanExporter", "class")), ...
"Otlp gRPC exporter must be installed.");
% Set up build runner
plugin = matlab.buildtool.plugins.OpenTelemetryPlugin();
testCase.BuildRunner = matlab.buildtool.BuildRunner.withNoPlugins();
testCase.BuildRunner.addPlugin(plugin);
end
end
methods (TestMethodTeardown)
function teardown(testCase)
commonTeardown(testCase);
end
end
methods (Test)
function pluginIsAddedAsADefaultPluginInNewReleases(testCase)
testCase.assumeFalse(isMATLABReleaseOlderThan("R2026a"));
runner = matlab.buildtool.BuildRunner.withDefaultPlugins();
tf = arrayfun(@(x)isa(x, "matlab.buildtool.plugins.OpenTelemetryPlugin"), runner.Plugins);
testCase.verifyTrue(any(tf));
end
function pluginIsNotAddedAsADefaultPluginInOlderReleases(testCase)
testCase.assumeTrue(isMATLABReleaseOlderThan("R2026a"));
runner = matlab.buildtool.BuildRunner.withDefaultPlugins();
tf = arrayfun(@(x)isa(x, "matlab.buildtool.plugins.OpenTelemetryPlugin"), runner.Plugins);
testCase.verifyFalse(any(tf));
end
function runOneTaskHasCorrectSpans(testCase)
testCase.assumeFalse(isMATLABReleaseOlderThan("R2026a"));
% Create plan with 1 successful task
plan = buildplan();
plan("task") = matlab.buildtool.Task();
% Run build
testCase.BuildRunner.run(plan, "task");
% Get results
results = readJsonResults(testCase);
spanData = results{1}.resourceSpans;
% Verify spans
spans = spanData.scopeSpans;
testCase.verifyEqual(numel(spans), 2);
% First span is overall build span
buildSpan = spanData.scopeSpans(1).spans;
testCase.verifyEqual(string(buildSpan.name), "buildtool");
testCase.verifyEqual(buildSpan.status.code, 1);
% Build span attributes
att1.key = 'buildtool.tasks';
att1.value.doubleValue = 1;
att2.key = 'buildtool.tasks.successful';
att2.value.stringValue = 'task';
att3.key = 'buildtool.tasks.failed';
att3.value.arrayValue = struct();
sizeZero.doubleValue = 0;
att4.key = 'buildtool.tasks.failed.size';
att4.value.arrayValue.values = [sizeZero; sizeZero];
att5.key = 'buildtool.tasks.skipped';
att5.value.arrayValue = struct();
att6.key = 'buildtool.tasks.skipped.size';
att6.value.arrayValue.values = [sizeZero; sizeZero];
att7.key = 'buildtool.build.successes';
att7.value.doubleValue = 1;
att8.key = 'buildtool.build.failures';
att8.value.doubleValue = 0;
att9.key = 'buildtool.build.skips';
att9.value.doubleValue = 0;
expected = [ ...
att1, ...
att2, ...
att3, ...
att4, ...
att5, ...
att6, ...
att7, ...
att8, ...
att9, ...
]';
testCase.verifyEqual(buildSpan.attributes, expected);
% Second span is "task" span
taskSpan = spanData.scopeSpans(2).spans;
testCase.verifyEqual(string(taskSpan.name), "task");
testCase.verifyEqual(taskSpan.status.code, 1);
testCase.verifyEqual(taskSpan.parentSpanId, buildSpan.spanId);
% Task span attributes
tAtt1.key = 'buildtool.task.name';
tAtt1.value.stringValue = 'task';
tAtt2.key = 'buildtool.task.description';
tAtt2.value.stringValue = '';
tAtt3.key = 'buildtool.task.successful';
tAtt3.value.boolValue = true;
tAtt4.key = 'buildtool.task.failed';
tAtt4.value.boolValue = false;
tAtt5.key = 'buildtool.task.skipped';
tAtt5.value.boolValue = false;
expected = [ ...
tAtt1, ...
tAtt2, ...
tAtt3, ...
tAtt4, ...
tAtt5, ...
]';
testCase.verifyEqual(taskSpan.attributes, expected);
end
function runOneTaskHasCorrectMetrics(testCase)
testCase.assumeFalse(isMATLABReleaseOlderThan("R2026a"));
% Create plan with 1 successful task
plan = buildplan();
plan("task") = matlab.buildtool.Task();
% Run build
testCase.BuildRunner.run(plan, "task");
% Get results
results = readJsonResults(testCase);
metricData = results{2}.resourceMetrics;
metrics = metricData.scopeMetrics.metrics;
% Order appears to be deterministic on my machine.
testCase.verifyEqual(metrics(1).name, 'buildtool.build.failures');
testCase.verifyEqual(metrics(1).sum.dataPoints.asDouble, 0);
testCase.verifyEqual(metrics(2).name, 'buildtool.build.successes');
testCase.verifyEqual(metrics(2).sum.dataPoints.asDouble, 1);
testCase.verifyEqual(metrics(3).name, 'buildtool.tasks.skipped');
testCase.verifyEqual(metrics(3).sum.dataPoints.asDouble, 0);
testCase.verifyEqual(metrics(4).name, 'buildtool.tasks.failed');
testCase.verifyEqual(metrics(4).sum.dataPoints.asDouble, 0);
testCase.verifyEqual(metrics(5).name, 'buildtool.tasks.successful');
testCase.verifyEqual(metrics(5).sum.dataPoints.asDouble, 1);
end
function runningSeveralTasksProducesCorrectSpans(testCase)
testCase.assumeFalse(isMATLABReleaseOlderThan("R2026a"));
% Create plan with 3 successful tasks and a failing task
plan = buildplan();
plan("t1") = matlab.buildtool.Task();
plan("t2") = matlab.buildtool.Task(Dependencies="t1");
plan("t3") = matlab.buildtool.Task(Dependencies="t2");
plan("error") = matlab.buildtool.Task(Actions=@(~)error("bam"), Dependencies="t3");
% Run build
testCase.BuildRunner.run(plan, "error");
% Get results
results = readJsonResults(testCase);
spanData = results{1}.resourceSpans;
% Verify spans
spans = spanData.scopeSpans;
testCase.verifyEqual(numel(spans), 5);
% First span is overall build span
buildSpan = findSpan("buildtool", spans);
testCase.verifyEqual(string(buildSpan.name), "buildtool");
testCase.verifyEqual(buildSpan.status.code, 2); % Build should fail
% Build span attributes
att1.key = 'buildtool.tasks';
att1.value.doubleValue = 4;
val1.stringValue = 't1';
val2.stringValue = 't2';
val3.stringValue = 't3';
att2.key = 'buildtool.tasks.successful';
att2.value.arrayValue.values = [val1; val2; val3];
size1.doubleValue = 1;
size3.doubleValue = 3;
att3.key = 'buildtool.tasks.successful.size';
att3.value.arrayValue.values = [size1; size3];
att4.key = 'buildtool.tasks.failed';
att4.value.stringValue = 'error';
att5.key = 'buildtool.tasks.skipped';
att5.value.arrayValue = struct();
sizeZero.doubleValue = 0;
att6.key = 'buildtool.tasks.skipped.size';
att6.value.arrayValue.values = [sizeZero; sizeZero];
att7.key = 'buildtool.build.successes';
att7.value.doubleValue = 3;
att8.key = 'buildtool.build.failures';
att8.value.doubleValue = 1;
att9.key = 'buildtool.build.skips';
att9.value.doubleValue = 0;
expected = [ ...
att1, ...
att2, ...
att3, ...
att4, ...
att5, ...
att6, ...
att7, ...
att8, ...
att9, ...
]';
testCase.verifyEqual(buildSpan.attributes, expected);
% "t1" span
taskSpan = findSpan("t1", spans);
testCase.verifyEqual(string(taskSpan.name), "t1");
testCase.verifyEqual(taskSpan.status.code, 1);
testCase.verifyEqual(taskSpan.parentSpanId, buildSpan.spanId);
% Task span attributes
tAtt1.key = 'buildtool.task.name';
tAtt1.value.stringValue = 't1';
tAtt2.key = 'buildtool.task.description';
tAtt2.value.stringValue = '';
tAtt3.key = 'buildtool.task.successful';
tAtt3.value.boolValue = true;
tAtt4.key = 'buildtool.task.failed';
tAtt4.value.boolValue = false;
tAtt5.key = 'buildtool.task.skipped';
tAtt5.value.boolValue = false;
expected = [ ...
tAtt1, ...
tAtt2, ...
tAtt3, ...
tAtt4, ...
tAtt5, ...
]';
testCase.verifyEqual(taskSpan.attributes, expected);
% "t2" span
taskSpan = findSpan("t2", spans);
testCase.verifyEqual(string(taskSpan.name), "t2");
testCase.verifyEqual(taskSpan.status.code, 1);
testCase.verifyEqual(taskSpan.parentSpanId, buildSpan.spanId);
% Task span attributes
tAtt1.key = 'buildtool.task.name';
tAtt1.value.stringValue = 't2';
tAtt2.key = 'buildtool.task.description';
tAtt2.value.stringValue = '';
tAtt3.key = 'buildtool.task.successful';
tAtt3.value.boolValue = true;
tAtt4.key = 'buildtool.task.failed';
tAtt4.value.boolValue = false;
tAtt5.key = 'buildtool.task.skipped';
tAtt5.value.boolValue = false;
expected = [ ...
tAtt1, ...
tAtt2, ...
tAtt3, ...
tAtt4, ...
tAtt5, ...
]';
testCase.verifyEqual(taskSpan.attributes, expected);
% "t3" span
taskSpan = findSpan("t3", spans);
testCase.verifyEqual(string(taskSpan.name), "t3");
testCase.verifyEqual(taskSpan.status.code, 1);
testCase.verifyEqual(taskSpan.parentSpanId, buildSpan.spanId);
% Task span attributes
tAtt1.key = 'buildtool.task.name';
tAtt1.value.stringValue = 't3';
tAtt2.key = 'buildtool.task.description';
tAtt2.value.stringValue = '';
tAtt3.key = 'buildtool.task.successful';
tAtt3.value.boolValue = true;
tAtt4.key = 'buildtool.task.failed';
tAtt4.value.boolValue = false;
tAtt5.key = 'buildtool.task.skipped';
tAtt5.value.boolValue = false;
expected = [ ...
tAtt1, ...
tAtt2, ...
tAtt3, ...
tAtt4, ...
tAtt5, ...
]';
testCase.verifyEqual(taskSpan.attributes, expected);
% "error" span
taskSpan = findSpan("error", spans);
testCase.verifyEqual(string(taskSpan.name), "error");
testCase.verifyEqual(taskSpan.status.code, 2); % Should error
testCase.verifyEqual(taskSpan.parentSpanId, buildSpan.spanId);
% Task span attributes
tAtt1.key = 'buildtool.task.name';
tAtt1.value.stringValue = 'error';
tAtt2.key = 'buildtool.task.description';
tAtt2.value.stringValue = '';
tAtt3.key = 'buildtool.task.successful';
tAtt3.value.boolValue = false;
tAtt4.key = 'buildtool.task.failed';
tAtt4.value.boolValue = true;
tAtt5.key = 'buildtool.task.skipped';
tAtt5.value.boolValue = false;
expected = [ ...
tAtt1, ...
tAtt2, ...
tAtt3, ...
tAtt4, ...
tAtt5, ...
]';
testCase.verifyEqual(taskSpan.attributes, expected);
end
function runningSeveralTasksProducesCorrectMetrics(testCase)
testCase.assumeFalse(isMATLABReleaseOlderThan("R2026a"));
% Create plan with 3 successful tasks and a failing task
plan = buildplan();
plan("t1") = matlab.buildtool.Task();
plan("t2") = matlab.buildtool.Task(Dependencies="t1");
plan("t3") = matlab.buildtool.Task(Dependencies="t2");
plan("error") = matlab.buildtool.Task(Actions=@(~)error("bam"), Dependencies="t3");
% Run build
testCase.BuildRunner.run(plan, "error");
% Get results
results = readJsonResults(testCase);
metricData = results{2}.resourceMetrics;
metrics = metricData.scopeMetrics.metrics;
% Order appears to be deterministic on my machine.
testCase.verifyEqual(metrics(1).name, 'buildtool.build.failures');
testCase.verifyEqual(metrics(1).sum.dataPoints.asDouble, 1);
testCase.verifyEqual(metrics(2).name, 'buildtool.build.successes');
testCase.verifyEqual(metrics(2).sum.dataPoints.asDouble, 0);
testCase.verifyEqual(metrics(3).name, 'buildtool.tasks.skipped');
testCase.verifyEqual(metrics(3).sum.dataPoints.asDouble, 0);
testCase.verifyEqual(metrics(4).name, 'buildtool.tasks.failed');
testCase.verifyEqual(metrics(4).sum.dataPoints.asDouble, 1);
testCase.verifyEqual(metrics(5).name, 'buildtool.tasks.successful');
testCase.verifyEqual(metrics(5).sum.dataPoints.asDouble, 3);
end
function buildToolDoesNotConfigureOTelWhenEnvVariableSet(testCase)
testCase.assumeFalse(isMATLABReleaseOlderThan("R2026a"));
% Restore environment variable
testCase.addTeardown(@()setenv("NO_MBT_OTEL_CONFIG", ""));
% Set environment variable
setenv("NO_MBT_OTEL_CONFIG", "1");
% Create plan with 1 successful task
plan = buildplan();
plan("task") = matlab.buildtool.Task();
% Run build
testCase.BuildRunner.run(plan, "task");
% Get results
results = readJsonResults(testCase);
% Verify results are empty since we never set up any providers
testCase.verifyEmpty(results);
end
end
end
function span = findSpan(name, spans)
for s = spans'
realSpan = s.spans;
if (strcmp(name, realSpan.name))
span = realSpan;
return;
end
end
error("No span found");
end