Skip to content

Commit 2852b29

Browse files
committed
Add an AutoTrace example
1 parent ec2be1b commit 2852b29

File tree

10 files changed

+114
-12
lines changed

10 files changed

+114
-12
lines changed

examples/autotrace/README.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
# AutoTrace Example
2+
This example shows how to use AutoTrace to automatically instrument MATLAB code. The instrumented code in *autotrace_example.m* fits a line through a cluster of data points. Notice that it does not include any instrumentation code.
3+
The function *run_example.m* first configures a tracer provider. This step only needs to be done once in a MATLAB session. Then it creates an AutoTrace object and runs it. The AutoTrace object gets cleaned up at the end.
4+
5+
## Running the Example
6+
1. Start an instance of [OpenTelemetry Collector](https://github.com/open-telemetry/opentelemetry-collector).
7+
2. Start MATLAB.
8+
3. Ensure the installation directory of OpenTelemetry-matlab is on the MATLAB path.
9+
4. Run `run_example`.
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
function yf = autotrace_example
2+
% This example shows some simple MATLAB code that fits a line through a
3+
% cluster of data points. It does not include any instrumentation code.
4+
5+
% Copyright 2024 The MathWorks, Inc.
6+
7+
[x, y] = generate_data();
8+
yf = best_fit_line(x,y);
9+
end
10+
11+
function [x, y] = generate_data
12+
% generate some random data
13+
a = 1.5;
14+
b = 0.8;
15+
sigma = 5;
16+
x = 1:100;
17+
y = a * x + b + sigma * randn(1, 100);
18+
end
19+
20+
function yf = best_fit_line(x, y)
21+
% fit a line through points defined by inputs x and y
22+
coefs = polyfit(x, y, 1);
23+
yf = polyval(coefs , x);
24+
end
25+

examples/autotrace/run_example.m

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
function yf = run_example
2+
% Use AutoTrace to automatically instrument an example to produce a trace.
3+
4+
% Copyright 2024 The MathWorks, Inc.
5+
6+
% configure tracer provider
7+
resource = dictionary("service.name", "OpenTelemetry-Matlab_examples");
8+
tp = opentelemetry.sdk.trace.TracerProvider(Resource=resource);
9+
setTracerProvider(tp);
10+
11+
% create AutoTrace object
12+
at = opentelemetry.autoinstrument.AutoTrace(@autotrace_example, ...
13+
TracerName="autotrace_example");
14+
15+
% run the example
16+
yf = beginTrace(at);

examples/context_propagation/matlab/mymagic.m

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,8 +41,7 @@
4141
function initTracer
4242
% set up global TracerProvider
4343
resource = dictionary("service.name", "OpenTelemetry-Matlab_examples");
44-
tp = opentelemetry.sdk.trace.TracerProvider(...
45-
opentelemetry.sdk.trace.SimpleSpanProcessor, Resource=resource);
44+
tp = opentelemetry.sdk.trace.TracerProvider(Resource=resource);
4645
setTracerProvider(tp);
4746

4847
% set up global propagator

examples/logs/logs_example.m

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,8 +44,7 @@
4444
function initLogger
4545
% set up global LoggerProvider
4646
resource = dictionary("service.name", "OpenTelemetry-Matlab_examples");
47-
lp = opentelemetry.sdk.logs.LoggerProvider(...
48-
opentelemetry.sdk.logs.SimpleLogRecordProcessor, Resource=resource);
47+
lp = opentelemetry.sdk.logs.LoggerProvider(Resource=resource);
4948
setLoggerProvider(lp);
5049
end
5150

examples/metrics/metrics_example.m

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,8 +34,7 @@ function metrics_example(iterations)
3434

3535
function initMetrics
3636
% set up global MeterProvider
37-
exp = opentelemetry.exporters.otlp.defaultMetricExporter();
38-
reader = opentelemetry.sdk.metrics.PeriodicExportingMetricReader(exp, ...
37+
reader = opentelemetry.sdk.metrics.PeriodicExportingMetricReader(...
3938
"Interval", seconds(5), "Timeout", seconds(2.5)); % exports every 5 seconds
4039
% Use custom histogram bins
4140
v = opentelemetry.sdk.metrics.View(InstrumentType="histogram", HistogramBinEdges=0:10:100);

examples/parallel/parfor_example.m

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,8 +50,7 @@
5050
function initTracer
5151
% set up global TracerProvider
5252
resource = dictionary("service.name", "OpenTelemetry-Matlab_examples");
53-
tp = opentelemetry.sdk.trace.TracerProvider(...
54-
opentelemetry.sdk.trace.SimpleSpanProcessor, Resource=resource);
53+
tp = opentelemetry.sdk.trace.TracerProvider(Resource=resource);
5554
setTracerProvider(tp);
5655

5756
% set up global propagator

examples/trace/trace_example.m

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,8 +42,7 @@
4242
function initTracer
4343
% set up global TracerProvider
4444
resource = dictionary("service.name", "OpenTelemetry-Matlab_examples");
45-
tp = opentelemetry.sdk.trace.TracerProvider(...
46-
opentelemetry.sdk.trace.SimpleSpanProcessor, Resource=resource);
45+
tp = opentelemetry.sdk.trace.TracerProvider(Resource=resource);
4746
setTracerProvider(tp);
4847
end
4948

examples/webread/matlab/webread_example.m

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,7 @@
2727
function initTracer
2828
% set up global TracerProvider
2929
resource = dictionary("service.name", "OpenTelemetry-Matlab_examples");
30-
tp = opentelemetry.sdk.trace.TracerProvider(...
31-
opentelemetry.sdk.trace.SimpleSpanProcessor, Resource=resource);
30+
tp = opentelemetry.sdk.trace.TracerProvider(Resource=resource);
3231
setTracerProvider(tp);
3332

3433
% set up global propagator

test/texamples.m

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -370,5 +370,63 @@ function testParallel(testCase)
370370
verifyLessThanOrEqual(testCase, str2double(worker2.resourceSpans.scopeSpans.spans.endTimeUnixNano), ...
371371
str2double(toplevel.resourceSpans.scopeSpans.spans.endTimeUnixNano));
372372
end
373+
374+
function testAutoTrace(testCase)
375+
% testAutoTrace: AutoTrace example in examples/autotrace folder
376+
377+
% add the example folder to the path
378+
examplefolder = fullfile(fileparts(mfilename('fullpath')), "..", "examples", "autotrace");
379+
testCase.applyFixture(matlab.unittest.fixtures.PathFixture(examplefolder));
380+
381+
% run the example
382+
run_example;
383+
384+
% perform test comparisons
385+
results = readJsonResults(testCase);
386+
verifyNumElements(testCase, results, 3);
387+
388+
% check generate_data span
389+
gendata = results{1};
390+
verifyEqual(testCase, gendata.resourceSpans.scopeSpans.scope.name, 'autotrace_example');
391+
verifyEqual(testCase, gendata.resourceSpans.scopeSpans.spans.name, 'generate_data');
392+
verifyEqual(testCase, gendata.resourceSpans.scopeSpans.spans.kind, 1);
393+
service_name_idx = find(string({gendata.resourceSpans.resource.attributes.key}) == "service.name");
394+
verifyNotEmpty(testCase, service_name_idx);
395+
verifyEqual(testCase, gendata.resourceSpans.resource.attributes(service_name_idx).value.stringValue, ...
396+
'OpenTelemetry-Matlab_examples');
397+
398+
% check best_fit_line span
399+
bestfitline = results{2};
400+
verifyEqual(testCase, bestfitline.resourceSpans.scopeSpans.scope.name, 'autotrace_example');
401+
verifyEqual(testCase, bestfitline.resourceSpans.scopeSpans.spans.name, 'best_fit_line');
402+
verifyEqual(testCase, bestfitline.resourceSpans.scopeSpans.spans.kind, 1);
403+
404+
% check top level function span
405+
toplevel = results{3};
406+
verifyEqual(testCase, toplevel.resourceSpans.scopeSpans.scope.name, 'autotrace_example');
407+
verifyEqual(testCase, toplevel.resourceSpans.scopeSpans.spans.name, 'autotrace_example');
408+
verifyEqual(testCase, toplevel.resourceSpans.scopeSpans.spans.kind, 1);
409+
410+
% check parent child relationships
411+
verifyEqual(testCase, gendata.resourceSpans.scopeSpans.spans.parentSpanId, ...
412+
toplevel.resourceSpans.scopeSpans.spans.spanId);
413+
verifyEqual(testCase, bestfitline.resourceSpans.scopeSpans.spans.parentSpanId, ...
414+
toplevel.resourceSpans.scopeSpans.spans.spanId);
415+
verifyEmpty(testCase, toplevel.resourceSpans.scopeSpans.spans.parentSpanId);
416+
417+
% check all spans belong to the same trace
418+
verifyEqual(testCase, gendata.resourceSpans.scopeSpans.spans.traceId, ...
419+
toplevel.resourceSpans.scopeSpans.spans.traceId);
420+
verifyEqual(testCase, bestfitline.resourceSpans.scopeSpans.spans.traceId, ...
421+
toplevel.resourceSpans.scopeSpans.spans.traceId);
422+
423+
% check for expected timing
424+
verifyLessThanOrEqual(testCase, str2double(toplevel.resourceSpans.scopeSpans.spans.startTimeUnixNano), ...
425+
str2double(gendata.resourceSpans.scopeSpans.spans.startTimeUnixNano));
426+
verifyLessThanOrEqual(testCase, str2double(gendata.resourceSpans.scopeSpans.spans.endTimeUnixNano), ...
427+
str2double(bestfitline.resourceSpans.scopeSpans.spans.startTimeUnixNano));
428+
verifyLessThanOrEqual(testCase, str2double(bestfitline.resourceSpans.scopeSpans.spans.endTimeUnixNano), ...
429+
str2double(toplevel.resourceSpans.scopeSpans.spans.endTimeUnixNano));
430+
end
373431
end
374432
end

0 commit comments

Comments
 (0)