Skip to content

Commit

Permalink
initial test for AutoTrace
Browse files Browse the repository at this point in the history
  • Loading branch information
duncanpo committed Oct 21, 2024
1 parent ef0d8e3 commit db5ea0b
Show file tree
Hide file tree
Showing 4 changed files with 95 additions and 0 deletions.
7 changes: 7 additions & 0 deletions test/autotrace_examples/example1/best_fit_line.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
function yf = best_fit_line(x, y)
% example code for testing auto instrumentation

% Copyright 2024 The MathWorks, Inc.

coefs = polyfit(x, y, 1);
yf = polyval(coefs , x);
8 changes: 8 additions & 0 deletions test/autotrace_examples/example1/example1.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
function yf = example1(n)
% example code for testing auto instrumentation. Input n is the number of
% data points.

% Copyright 2024 The MathWorks, Inc.

[x, y] = generate_data(n);
yf = best_fit_line(x,y);
11 changes: 11 additions & 0 deletions test/autotrace_examples/example1/generate_data.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
function [x, y] = generate_data(n)
% example code for testing auto instrumentation

% Copyright 2024 The MathWorks, Inc.

% generate some random data
a = 1.5;
b = 0.8;
sigma = 5;
x = 1:n;
y = a * x + b + sigma * randn(1, n);
69 changes: 69 additions & 0 deletions test/tautotrace.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
classdef tautotrace < matlab.unittest.TestCase
% tests for AutoTrace

% Copyright 2024 The MathWorks, Inc.

properties
OtelConfigFile
JsonFile
PidFile
OtelcolName
Otelcol
ListPid
ReadPidList
ExtractPid
Sigint
Sigterm
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));
% add the example folder to the path
example1folder = fullfile(fileparts(mfilename('fullpath')), "autotrace_examples", "example1");
testCase.applyFixture(matlab.unittest.fixtures.PathFixture(example1folder));
commonSetupOnce(testCase);
end
end

methods (TestMethodSetup)
function setup(testCase)
commonSetup(testCase);
end
end

methods (TestMethodTeardown)
function teardown(testCase)
commonTeardown(testCase);
end
end

methods (Test)
function testBasic(testCase)
% testBasic: instrument a simple example

% configure the global tracer provider
tp = opentelemetry.sdk.trace.TracerProvider();
setTracerProvider(tp);
clear("tp");

% set up AutoTrace
at = opentelemetry.autoinstrument.AutoTrace(@example1);

% run the example
[~] = beginTrace(at, 100);

% perform test comparisons
results = readJsonResults(testCase);
verifyNumElements(testCase, results, 3);

% check logger name, log body and severity, trace and span IDs
verifyEqual(testCase, string(results{1}.resourceSpans.scopeSpans.scope.name), "AutoTrace");
verifyEqual(testCase, string(results{1}.resourceSpans.scopeSpans.spans.name), "generate_data");
verifyEqual(testCase, string(results{2}.resourceSpans.scopeSpans.spans.name), "best_fit_line");
verifyEqual(testCase, string(results{3}.resourceSpans.scopeSpans.spans.name), "example1");
end
end
end

0 comments on commit db5ea0b

Please sign in to comment.