Skip to content

Commit db5ea0b

Browse files
committed
initial test for AutoTrace
1 parent ef0d8e3 commit db5ea0b

File tree

4 files changed

+95
-0
lines changed

4 files changed

+95
-0
lines changed
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
function yf = best_fit_line(x, y)
2+
% example code for testing auto instrumentation
3+
4+
% Copyright 2024 The MathWorks, Inc.
5+
6+
coefs = polyfit(x, y, 1);
7+
yf = polyval(coefs , x);
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
function yf = example1(n)
2+
% example code for testing auto instrumentation. Input n is the number of
3+
% data points.
4+
5+
% Copyright 2024 The MathWorks, Inc.
6+
7+
[x, y] = generate_data(n);
8+
yf = best_fit_line(x,y);
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
function [x, y] = generate_data(n)
2+
% example code for testing auto instrumentation
3+
4+
% Copyright 2024 The MathWorks, Inc.
5+
6+
% generate some random data
7+
a = 1.5;
8+
b = 0.8;
9+
sigma = 5;
10+
x = 1:n;
11+
y = a * x + b + sigma * randn(1, n);

test/tautotrace.m

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
classdef tautotrace < matlab.unittest.TestCase
2+
% tests for AutoTrace
3+
4+
% Copyright 2024 The MathWorks, Inc.
5+
6+
properties
7+
OtelConfigFile
8+
JsonFile
9+
PidFile
10+
OtelcolName
11+
Otelcol
12+
ListPid
13+
ReadPidList
14+
ExtractPid
15+
Sigint
16+
Sigterm
17+
end
18+
19+
methods (TestClassSetup)
20+
function setupOnce(testCase)
21+
% add the utils folder to the path
22+
utilsfolder = fullfile(fileparts(mfilename('fullpath')), "utils");
23+
testCase.applyFixture(matlab.unittest.fixtures.PathFixture(utilsfolder));
24+
% add the example folder to the path
25+
example1folder = fullfile(fileparts(mfilename('fullpath')), "autotrace_examples", "example1");
26+
testCase.applyFixture(matlab.unittest.fixtures.PathFixture(example1folder));
27+
commonSetupOnce(testCase);
28+
end
29+
end
30+
31+
methods (TestMethodSetup)
32+
function setup(testCase)
33+
commonSetup(testCase);
34+
end
35+
end
36+
37+
methods (TestMethodTeardown)
38+
function teardown(testCase)
39+
commonTeardown(testCase);
40+
end
41+
end
42+
43+
methods (Test)
44+
function testBasic(testCase)
45+
% testBasic: instrument a simple example
46+
47+
% configure the global tracer provider
48+
tp = opentelemetry.sdk.trace.TracerProvider();
49+
setTracerProvider(tp);
50+
clear("tp");
51+
52+
% set up AutoTrace
53+
at = opentelemetry.autoinstrument.AutoTrace(@example1);
54+
55+
% run the example
56+
[~] = beginTrace(at, 100);
57+
58+
% perform test comparisons
59+
results = readJsonResults(testCase);
60+
verifyNumElements(testCase, results, 3);
61+
62+
% check logger name, log body and severity, trace and span IDs
63+
verifyEqual(testCase, string(results{1}.resourceSpans.scopeSpans.scope.name), "AutoTrace");
64+
verifyEqual(testCase, string(results{1}.resourceSpans.scopeSpans.spans.name), "generate_data");
65+
verifyEqual(testCase, string(results{2}.resourceSpans.scopeSpans.spans.name), "best_fit_line");
66+
verifyEqual(testCase, string(results{3}.resourceSpans.scopeSpans.spans.name), "example1");
67+
end
68+
end
69+
end

0 commit comments

Comments
 (0)