Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 8 additions & 3 deletions auto-instrumentation/+opentelemetry/+autoinstrument/AutoTrace.m
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
classdef AutoTrace < handle
% Automatic instrumentation with OpenTelemetry tracing.

% Copyright 2024 The MathWorks, Inc.
% Copyright 2024-2025 The MathWorks, Inc.

properties (SetAccess=private)
StartFunction function_handle % entry function
Expand Down Expand Up @@ -71,14 +71,19 @@
if isdeployed
% matlab.codetools.requiredFilesAndProducts is not
% deployable. Instead instrument all files under CTFROOT
fileinfo = dir(fullfile(ctfroot, "**", "*.m"));
fileinfo = [reshape(dir(fullfile(ctfroot, "**", "*.m")), [], 1); ...
reshape(dir(fullfile(ctfroot, "**", "*.mlx")), [], 1)];
files = fullfile(string({fileinfo.folder}), string({fileinfo.name}));

% filter out internal files in the toolbox directory
files = files(~startsWith(files, fullfile(ctfroot, "toolbox")));
else
%#exclude matlab.codetools.requiredFilesAndProducts
files = string(matlab.codetools.requiredFilesAndProducts(startfunname));

% keep only .m and .mlx files. Filter out everything else
[~,~,fext] = fileparts(files);
files = files(ismember(fext, [".m" ".mlx"]));
end
else
% only include the input file, not its dependencies
Expand Down Expand Up @@ -190,7 +195,7 @@ function handleError(obj, ME)
mfiles = fullfile(string({mfileinfo.folder}), string({mfileinfo.name}));
mlxfileinfo = dir(fullfile(f, "*.mlx"));
mlxfiles = fullfile(string({mlxfileinfo.folder}), string({mlxfileinfo.name}));
f = [mfiles; mlxfiles];
f = [mfiles(:); mlxfiles(:)];
else
% file
f = processFileInput(f);
Expand Down
7 changes: 7 additions & 0 deletions test/autotrace_examples/matfile_example/matfile_example.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
function y = matfile_example
% Example code for testing auto instrumentation, which loads a .mat file

% Copyright 2025 The MathWorks, Inc.

load("mymagic", "x");
y = sum(x);
Binary file added test/autotrace_examples/matfile_example/mymagic.mat
Binary file not shown.
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
function x = subfolder_helper1_1(x)
% example code for testing auto instrumentation, helper function

% Copyright 2025 The MathWorks, Inc.

x = x * 2;
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
function x = subfolder_helper1_2(x)
% example code for testing auto instrumentation, helper function

% Copyright 2025 The MathWorks, Inc.

x = x * 3;
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
function x = subfolder_helper2_1(x)
% example code for testing auto instrumentation, helper function

% Copyright 2025 The MathWorks, Inc.

x = x * 4;
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
function x = subfolder_helper2_2(x)
% example code for testing auto instrumentation, helper function

% Copyright 2025 The MathWorks, Inc.

x = x * 5;
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
function x = subfolder_helper2_3(x)
% example code for testing auto instrumentation, helper function

% Copyright 2025 The MathWorks, Inc.

x = x * 5;
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
function x = two_subfolders_example
% Example code for testing auto instrumentation, with helper functions
% in a two subfolders

% Copyright 2025 The MathWorks, Inc.

x = 10;
x = subfolder_helper1_1(x);
x = subfolder_helper1_2(x);
x = subfolder_helper2_1(x);
x = subfolder_helper2_2(x);
x = subfolder_helper2_3(x);

53 changes: 52 additions & 1 deletion test/tautotrace.m
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
classdef tautotrace < matlab.unittest.TestCase
% tests for AutoTrace

% Copyright 2024 The MathWorks, Inc.
% Copyright 2024-2025 The MathWorks, Inc.

properties
OtelConfigFile
Expand Down Expand Up @@ -155,6 +155,35 @@ function testIncludeFolder(testCase)
verifyEqual(testCase, results{2}.resourceSpans.scopeSpans.spans.parentSpanId, results{3}.resourceSpans.scopeSpans.spans.spanId);
end

function testMultipleIncludeFolders(testCase)
% testMultipleIncludeFolders: specify multiple folders in AdditionalFiles

% Add example folders to the path
examplefolder = fullfile(fileparts(mfilename('fullpath')), "autotrace_examples", "two_subfolders_example");
testCase.applyFixture(matlab.unittest.fixtures.PathFixture(examplefolder, ...
"IncludingSubfolders",true));

% set up AutoTrace, turn off automatic detection and specify
% dependencies using their folder name
at = opentelemetry.autoinstrument.AutoTrace(@two_subfolders_example, ...
"AutoDetectFiles", false, "AdditionalFiles", fullfile(examplefolder, ["helper1" "helper2"]));

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

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

% check span names
verifyEqual(testCase, string(results{1}.resourceSpans.scopeSpans.spans.name), "subfolder_helper1_1");
verifyEqual(testCase, string(results{2}.resourceSpans.scopeSpans.spans.name), "subfolder_helper1_2");
verifyEqual(testCase, string(results{3}.resourceSpans.scopeSpans.spans.name), "subfolder_helper2_1");
verifyEqual(testCase, string(results{4}.resourceSpans.scopeSpans.spans.name), "subfolder_helper2_2");
verifyEqual(testCase, string(results{5}.resourceSpans.scopeSpans.spans.name), "subfolder_helper2_3");
verifyEqual(testCase, string(results{6}.resourceSpans.scopeSpans.spans.name), "two_subfolders_example");
end

function testExcludeFolder(testCase)
% testExcludeFolder: specify a folder in ExcludeFiles

Expand Down Expand Up @@ -227,6 +256,28 @@ function testNonFileOptions(testCase)
end
end

function testIgnoreUnsupportedDependencies(testCase)
% testIgnoreUnsupportedDependencies: Check that File dependencies that are not
% .m or .mlx files are ignored. For example, .mat file.

% Add example folders to the path
examplefolder = fullfile(fileparts(mfilename('fullpath')), "autotrace_examples", "matfile_example");
testCase.applyFixture(matlab.unittest.fixtures.PathFixture(examplefolder));

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

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

% perform test comparisons
results = readJsonResults(testCase);

% should only return 1 span
verifyNumElements(testCase, results, 1);
verifyEqual(testCase, string(results{1}.resourceSpans.scopeSpans.spans.name), "matfile_example");
end

function testError(testCase)
% testError: handling error situation

Expand Down
Loading