Skip to content

Commit 7fcd34e

Browse files
authored
Merge pull request #198 from mathworks/timestamps
Fix two AutoTrace bugs
2 parents f66398e + f9c5528 commit 7fcd34e

File tree

10 files changed

+110
-4
lines changed

10 files changed

+110
-4
lines changed

auto-instrumentation/+opentelemetry/+autoinstrument/AutoTrace.m

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
classdef AutoTrace < handle
22
% Automatic instrumentation with OpenTelemetry tracing.
33

4-
% Copyright 2024 The MathWorks, Inc.
4+
% Copyright 2024-2025 The MathWorks, Inc.
55

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

7778
% filter out internal files in the toolbox directory
7879
files = files(~startsWith(files, fullfile(ctfroot, "toolbox")));
7980
else
8081
%#exclude matlab.codetools.requiredFilesAndProducts
8182
files = string(matlab.codetools.requiredFilesAndProducts(startfunname));
83+
84+
% keep only .m and .mlx files. Filter out everything else
85+
[~,~,fext] = fileparts(files);
86+
files = files(ismember(fext, [".m" ".mlx"]));
8287
end
8388
else
8489
% only include the input file, not its dependencies
@@ -190,7 +195,7 @@ function handleError(obj, ME)
190195
mfiles = fullfile(string({mfileinfo.folder}), string({mfileinfo.name}));
191196
mlxfileinfo = dir(fullfile(f, "*.mlx"));
192197
mlxfiles = fullfile(string({mlxfileinfo.folder}), string({mlxfileinfo.name}));
193-
f = [mfiles; mlxfiles];
198+
f = [mfiles(:); mlxfiles(:)];
194199
else
195200
% file
196201
f = processFileInput(f);
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
function y = matfile_example
2+
% Example code for testing auto instrumentation, which loads a .mat file
3+
4+
% Copyright 2025 The MathWorks, Inc.
5+
6+
load("mymagic", "x");
7+
y = sum(x);
183 Bytes
Binary file not shown.
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
function x = subfolder_helper1_1(x)
2+
% example code for testing auto instrumentation, helper function
3+
4+
% Copyright 2025 The MathWorks, Inc.
5+
6+
x = x * 2;
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
function x = subfolder_helper1_2(x)
2+
% example code for testing auto instrumentation, helper function
3+
4+
% Copyright 2025 The MathWorks, Inc.
5+
6+
x = x * 3;
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
function x = subfolder_helper2_1(x)
2+
% example code for testing auto instrumentation, helper function
3+
4+
% Copyright 2025 The MathWorks, Inc.
5+
6+
x = x * 4;
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
function x = subfolder_helper2_2(x)
2+
% example code for testing auto instrumentation, helper function
3+
4+
% Copyright 2025 The MathWorks, Inc.
5+
6+
x = x * 5;
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
function x = subfolder_helper2_3(x)
2+
% example code for testing auto instrumentation, helper function
3+
4+
% Copyright 2025 The MathWorks, Inc.
5+
6+
x = x * 5;
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
function x = two_subfolders_example
2+
% Example code for testing auto instrumentation, with helper functions
3+
% in a two subfolders
4+
5+
% Copyright 2025 The MathWorks, Inc.
6+
7+
x = 10;
8+
x = subfolder_helper1_1(x);
9+
x = subfolder_helper1_2(x);
10+
x = subfolder_helper2_1(x);
11+
x = subfolder_helper2_2(x);
12+
x = subfolder_helper2_3(x);
13+

test/tautotrace.m

Lines changed: 52 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
classdef tautotrace < matlab.unittest.TestCase
22
% tests for AutoTrace
33

4-
% Copyright 2024 The MathWorks, Inc.
4+
% Copyright 2024-2025 The MathWorks, Inc.
55

66
properties
77
OtelConfigFile
@@ -155,6 +155,35 @@ function testIncludeFolder(testCase)
155155
verifyEqual(testCase, results{2}.resourceSpans.scopeSpans.spans.parentSpanId, results{3}.resourceSpans.scopeSpans.spans.spanId);
156156
end
157157

158+
function testMultipleIncludeFolders(testCase)
159+
% testMultipleIncludeFolders: specify multiple folders in AdditionalFiles
160+
161+
% Add example folders to the path
162+
examplefolder = fullfile(fileparts(mfilename('fullpath')), "autotrace_examples", "two_subfolders_example");
163+
testCase.applyFixture(matlab.unittest.fixtures.PathFixture(examplefolder, ...
164+
"IncludingSubfolders",true));
165+
166+
% set up AutoTrace, turn off automatic detection and specify
167+
% dependencies using their folder name
168+
at = opentelemetry.autoinstrument.AutoTrace(@two_subfolders_example, ...
169+
"AutoDetectFiles", false, "AdditionalFiles", fullfile(examplefolder, ["helper1" "helper2"]));
170+
171+
% run the example
172+
[~] = beginTrace(at);
173+
174+
% perform test comparisons
175+
results = readJsonResults(testCase);
176+
verifyNumElements(testCase, results, 6);
177+
178+
% check span names
179+
verifyEqual(testCase, string(results{1}.resourceSpans.scopeSpans.spans.name), "subfolder_helper1_1");
180+
verifyEqual(testCase, string(results{2}.resourceSpans.scopeSpans.spans.name), "subfolder_helper1_2");
181+
verifyEqual(testCase, string(results{3}.resourceSpans.scopeSpans.spans.name), "subfolder_helper2_1");
182+
verifyEqual(testCase, string(results{4}.resourceSpans.scopeSpans.spans.name), "subfolder_helper2_2");
183+
verifyEqual(testCase, string(results{5}.resourceSpans.scopeSpans.spans.name), "subfolder_helper2_3");
184+
verifyEqual(testCase, string(results{6}.resourceSpans.scopeSpans.spans.name), "two_subfolders_example");
185+
end
186+
158187
function testExcludeFolder(testCase)
159188
% testExcludeFolder: specify a folder in ExcludeFiles
160189

@@ -227,6 +256,28 @@ function testNonFileOptions(testCase)
227256
end
228257
end
229258

259+
function testIgnoreUnsupportedDependencies(testCase)
260+
% testIgnoreUnsupportedDependencies: Check that File dependencies that are not
261+
% .m or .mlx files are ignored. For example, .mat file.
262+
263+
% Add example folders to the path
264+
examplefolder = fullfile(fileparts(mfilename('fullpath')), "autotrace_examples", "matfile_example");
265+
testCase.applyFixture(matlab.unittest.fixtures.PathFixture(examplefolder));
266+
267+
% set up AutoTrace
268+
at = opentelemetry.autoinstrument.AutoTrace(@matfile_example);
269+
270+
% run the example
271+
[~] = beginTrace(at);
272+
273+
% perform test comparisons
274+
results = readJsonResults(testCase);
275+
276+
% should only return 1 span
277+
verifyNumElements(testCase, results, 1);
278+
verifyEqual(testCase, string(results{1}.resourceSpans.scopeSpans.spans.name), "matfile_example");
279+
end
280+
230281
function testError(testCase)
231282
% testError: handling error situation
232283

0 commit comments

Comments
 (0)