-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathttrace_sdk.m
305 lines (255 loc) · 11.8 KB
/
ttrace_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
classdef ttrace_sdk < matlab.unittest.TestCase
% tests for tracing SDK (span processors, exporters, samplers, resource)
% Copyright 2023-2024 The MathWorks, Inc.
properties
OtelConfigFile
JsonFile
PidFile
OtelcolName
Otelcol
ListPid
ReadPidList
ExtractPid
Sigint
Sigterm
ForceFlushTimeout
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);
testCase.ForceFlushTimeout = seconds(2);
end
end
methods (TestMethodSetup)
function setup(testCase)
commonSetup(testCase);
end
end
methods (TestMethodTeardown)
function teardown(testCase)
commonTeardown(testCase);
end
end
methods (Test)
function testBatchSpanProcessor(testCase)
% testBatchSpanProcessor: setting properties of
% BatchSpanProcessor
tracername = "foo";
spanname = "bar";
queuesize = 500;
delay = seconds(2);
batchsize = 50;
b = opentelemetry.sdk.trace.BatchSpanProcessor(...
MaximumQueueSize=queuesize, ...
ScheduledDelay=delay, ...
MaximumExportBatchSize=batchsize);
tp = opentelemetry.sdk.trace.TracerProvider(b);
tr = getTracer(tp, tracername);
sp = startSpan(tr, spanname);
pause(1);
endSpan(sp);
% verify batch properties set correctly
verifyEqual(testCase, b.MaximumQueueSize, queuesize);
verifyEqual(testCase, b.ScheduledDelay, delay);
verifyEqual(testCase, b.MaximumExportBatchSize, batchsize)
verifyEqual(testCase, class(b.SpanExporter), ...
class(opentelemetry.exporters.otlp.defaultSpanExporter));
% perform test comparisons
forceFlush(tp, testCase.ForceFlushTimeout);
results = readJsonResults(testCase);
results = results{1};
% check span and tracer names
verifyEqual(testCase, string(results.resourceSpans.scopeSpans.spans.name), spanname);
verifyEqual(testCase, string(results.resourceSpans.scopeSpans.scope.name), tracername);
end
function testAlwaysOffSampler(testCase)
% testAlwaysOffSampler: should not produce any spans
tp = opentelemetry.sdk.trace.TracerProvider( ...
"Sampler", opentelemetry.sdk.trace.AlwaysOffSampler);
tr = getTracer(tp, "mytracer");
sp = startSpan(tr, "myspan");
pause(1);
endSpan(sp);
% verify no spans are generated
results = readJsonResults(testCase);
verifyEmpty(testCase, results);
end
function testAlwaysOnSampler(testCase)
% testAlwaysOnSampler: should produce all spans
tracername = "foo";
spanname = "bar";
tp = opentelemetry.sdk.trace.TracerProvider( ...
"Sampler", opentelemetry.sdk.trace.AlwaysOnSampler);
tr = getTracer(tp, tracername);
sp = startSpan(tr, spanname);
pause(1);
endSpan(sp);
% perform test comparisons
results = readJsonResults(testCase);
results = results{1};
% check span and tracer names
verifyEqual(testCase, string(results.resourceSpans.scopeSpans.spans.name), spanname);
verifyEqual(testCase, string(results.resourceSpans.scopeSpans.scope.name), tracername);
end
function testTraceIdRatioBasedSampler(testCase)
% testTraceIdRatioBasedSampler: filter spans based on a ratio
s = opentelemetry.sdk.trace.TraceIdRatioBasedSampler(0); % equivalent to always off
tracername = "mytracer";
offspan = "offspan";
tp = opentelemetry.sdk.trace.TracerProvider("Sampler", s);
tr = getTracer(tp, tracername);
sp = startSpan(tr, offspan);
pause(1);
endSpan(sp);
s.Ratio = 1; % equivalent to always on
onspan = "onspan";
tp = opentelemetry.sdk.trace.TracerProvider("Sampler", s);
tr = getTracer(tp, tracername);
sp = startSpan(tr, onspan);
pause(1);
endSpan(sp);
s.Ratio = 0.5; % filter half of the spans
sampledspan = "sampledspan";
numspans = 10;
tp = opentelemetry.sdk.trace.TracerProvider("Sampler", s);
tr = getTracer(tp, tracername);
for i = 1:numspans
sp = startSpan(tr, sampledspan + i);
pause(1);
endSpan(sp);
end
% perform test comparisons
results = readJsonResults(testCase);
n = length(results);
% total spans should be 1 span when ratio == 1, plus a number of
% spans between 0 and numspans when ratio == 0.5
% Verifying 1 < total_spans < numspans+1. If this fails, there
% is still a chance nothing went wrong, because number of spans
% are non-deterministic when ratio == 0.5. When ratio == 0.5,
% it is still possible to get 0 or numspans spans. But that
% probability is small, so we fail the test to flag something
% may have gone wrong.
verifyGreaterThan(testCase, n, 1);
verifyLessThan(testCase, n, 1 + numspans);
verifyEqual(testCase, string(results{1}.resourceSpans.scopeSpans.spans.name), onspan);
for i = 2:n
verifySubstring(testCase, string(results{i}.resourceSpans.scopeSpans.spans.name), ...
sampledspan);
end
end
function testOtlpFileExporter(testCase)
% testOtlpFileExporter: use a file exporter to write to files
testCase.assumeTrue(logical(exist("opentelemetry.exporters.otlp.OtlpFileSpanExporter", "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.OtlpFileSpanExporter(...
FileName=output, AliasName=alias);
tp = opentelemetry.sdk.trace.TracerProvider(...
opentelemetry.sdk.trace.SimpleSpanProcessor(exp));
tracername = "foo";
spanname = "bar";
tr = getTracer(tp, tracername);
sp = startSpan(tr, spanname); %#ok<NASGU>
pause(1);
clear("sp", "tr", "tp");
% perform test comparisons
resultstxt = readlines(alias);
results = jsondecode(resultstxt(1));
% check span and tracer names
verifyEqual(testCase, string(results.resourceSpans.scopeSpans.spans.name), spanname);
verifyEqual(testCase, string(results.resourceSpans.scopeSpans.scope.name), tracername);
end
function testCustomResource(testCase)
% testCustomResource: check custom resources are included in
% emitted spans
customkeys = ["foo" "bar"];
customvalues = [1 5];
tp = opentelemetry.sdk.trace.TracerProvider("Resource", dictionary(customkeys, customvalues));
tr = getTracer(tp, "mytracer");
sp = startSpan(tr, "myspan");
pause(1);
endSpan(sp);
% perform test comparisons
results = readJsonResults(testCase);
results = results{1};
resourcekeys = string({results.resourceSpans.resource.attributes.key});
for i = length(customkeys)
idx = find(resourcekeys == customkeys(i));
verifyNotEmpty(testCase, idx);
verifyEqual(testCase, results.resourceSpans.resource.attributes(idx).value.doubleValue, customvalues(i));
end
end
function testShutdown(testCase)
% testShutdown: shutdown method should stop exporting
% of spans
tp = opentelemetry.sdk.trace.TracerProvider();
tr = getTracer(tp, "foo");
% start and end a span
spanname = "bar";
sp = startSpan(tr, spanname);
endSpan(sp);
% shutdown the tracer provider
verifyTrue(testCase, shutdown(tp));
% suppress internal error logs about span export failure
nologs = SuppressInternalLogs; %#ok<NASGU>
% start and end another span
sp1 = startSpan(tr, "quux");
endSpan(sp1);
% verify only the first span was generated
results = readJsonResults(testCase);
verifyNumElements(testCase, results, 1);
verifyEqual(testCase, string(results{1}.resourceSpans.scopeSpans.spans.name), spanname);
end
function testCleanupSdk(testCase)
% testCleanupSdk: shutdown an SDK tracer provider through the Cleanup class
tp = opentelemetry.sdk.trace.TracerProvider();
tr = getTracer(tp, "foo");
% start and end a span
spanname = "bar";
sp = startSpan(tr, spanname);
endSpan(sp);
% shutdown the SDK tracer provider through the Cleanup class
verifyTrue(testCase, opentelemetry.sdk.common.Cleanup.shutdown(tp));
% suppress internal error logs about span export failure
nologs = SuppressInternalLogs; %#ok<NASGU>
% start and end another span
sp1 = startSpan(tr, "quux");
endSpan(sp1);
% verify only the first span was generated
results = readJsonResults(testCase);
verifyNumElements(testCase, results, 1);
verifyEqual(testCase, string(results{1}.resourceSpans.scopeSpans.spans.name), spanname);
end
function testCleanupApi(testCase)
% testCleanupApi: shutdown an API tracer provider through the Cleanup class
tp = opentelemetry.sdk.trace.TracerProvider();
setTracerProvider(tp);
clear("tp");
tp_api = opentelemetry.trace.Provider.getTracerProvider();
tr = getTracer(tp_api, "foo");
% start and end a span
spanname = "bar";
sp = startSpan(tr, spanname);
endSpan(sp);
% shutdown the API tracer provider through the Cleanup class
verifyTrue(testCase, opentelemetry.sdk.common.Cleanup.shutdown(tp_api));
% suppress internal error logs about span export failure
nologs = SuppressInternalLogs; %#ok<NASGU>
% start and end another span
sp1 = startSpan(tr, "quux");
endSpan(sp1);
% verify only the first span was generated
results = readJsonResults(testCase);
verifyNumElements(testCase, results, 1);
verifyEqual(testCase, string(results{1}.resourceSpans.scopeSpans.spans.name), spanname);
end
end
end