Skip to content

Commit 26a4993

Browse files
authored
Merge pull request #135 from mathworks/exampletests
Adding tests for examples
2 parents 1327409 + 511a048 commit 26a4993

11 files changed

+532
-79
lines changed
Lines changed: 10 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,22 @@
1-
function async_metrics_example
1+
function async_metrics_example(iterations)
22
% This example creates 3 asynchronous metric instruments including an
33
% observable counter, an observable updowncounter, and an observable gauge.
44

55
% Copyright 2024 The MathWorks, Inc.
66

7+
if nargin < 1
8+
iterations = 20; % default to 20 iterations
9+
end
10+
711
% initialize meter provider
812
initMetrics;
913

1014
% create meter and instruments
1115
m = opentelemetry.metrics.getMeter("async_metrics_example");
12-
c = createObservableCounter(m, @counter_callback, "observable_counter"); %#ok<*NASGU>
13-
u = createObservableUpDownCounter(m, @updowncounter_callback, "observable_updowncounter");
14-
g = createObservableGauge(m, @gauge_callback, "observable_gauge");
15-
iterations = 20; % number of iterations
16+
callbacks = async_metrics_example_callbacks;
17+
c = createObservableCounter(m, @()counterCallback(callbacks), "observable_counter"); %#ok<*NASGU>
18+
u = createObservableUpDownCounter(m, @()updowncounterCallback(callbacks), "observable_updowncounter");
19+
g = createObservableGauge(m, @()gaugeCallback(callbacks), "observable_gauge");
1620
pause(iterations*5);
1721

1822
% clean up
@@ -31,34 +35,7 @@
3135
end
3236

3337
function cleanupMetrics
38+
% clean up meter provider
3439
mp = opentelemetry.metrics.Provider.getMeterProvider();
3540
opentelemetry.sdk.common.Cleanup.shutdown(mp); % shutdown
36-
end
37-
38-
function result = counter_callback()
39-
persistent value
40-
if isempty(value)
41-
value = 0;
42-
else
43-
value = value + randi(10); % increment between 1 to 10
44-
end
45-
result = opentelemetry.metrics.ObservableResult;
46-
result = result.observe(value);
47-
end
48-
49-
function result = updowncounter_callback()
50-
persistent value
51-
if isempty(value)
52-
value = 0;
53-
else
54-
value = value + randi([-5 5]); % increment between -5 to 5
55-
end
56-
result = opentelemetry.metrics.ObservableResult;
57-
result = result.observe(value);
58-
end
59-
60-
function result = gauge_callback()
61-
s = second(datetime("now")); % get the current second of the minute
62-
result = opentelemetry.metrics.ObservableResult;
63-
result = result.observe(s);
6441
end
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
classdef async_metrics_example_callbacks < handle
2+
% Callback functions for asynchronous metrics example
3+
4+
% Copyright 2024 The MathWorks, Inc.
5+
6+
properties
7+
Count
8+
UpDownCount
9+
end
10+
11+
methods
12+
function obj = async_metrics_example_callbacks()
13+
obj.Count = 0;
14+
obj.UpDownCount = 0;
15+
end
16+
17+
function result = counterCallback(obj)
18+
% Callback function for Counter
19+
obj.Count = obj.Count + randi(10); % increment between 0 and 10
20+
result = opentelemetry.metrics.ObservableResult;
21+
result = result.observe(obj.Count);
22+
end
23+
24+
function result = updowncounterCallback(obj)
25+
% Callback function for UpdownCounter
26+
obj.UpDownCount = obj.UpDownCount + randi([-5 5]); % increment between -5 to 5
27+
result = opentelemetry.metrics.ObservableResult;
28+
result = result.observe(obj.UpDownCount);
29+
end
30+
31+
function result = gaugeCallback(~)
32+
% Callback function for Gauge
33+
s = second(datetime("now")); % get the current second of the minute
34+
result = opentelemetry.metrics.ObservableResult;
35+
result = result.observe(s);
36+
end
37+
end
38+
end

examples/metrics/metrics_example.m

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,14 @@
1-
function metrics_example
1+
function metrics_example(iterations)
22
% This example creates 3 metric instruments including a counter, an
33
% updowncounter, and a histogram. It then enters a loop and updates the
44
% value of the instruments at each iteration.
55

66
% Copyright 2023-2024 The MathWorks, Inc.
77

8+
if nargin < 1
9+
iterations = 20; % default to 20 iterations
10+
end
11+
812
% initialize meter provider
913
initMetrics;
1014

@@ -13,8 +17,9 @@
1317
c = createCounter(m, "counter");
1418
u = createUpDownCounter(m, "updowncounter");
1519
h = createHistogram(m, "histogram");
16-
iterations = 20; % number of iterations
1720

21+
% wait a little before starting
22+
pause(2);
1823
for i = 1:iterations
1924
c.add(randi(10));
2025
u.add(randi([-10 10]));

examples/parallel/parfor_example.m

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1-
function a = parfor_example
1+
function a = parfor_example(niter, nworkers)
22
% This example creates a trace through a parfor loop, by creating a span in
33
% each iteration and propagating context.
4-
%
4+
55
% Copyright 2024 The MathWorks, Inc.
66

77
% initialize tracing
@@ -12,16 +12,20 @@
1212
sp = startSpan(tr, "main function");
1313
scope = makeCurrent(sp); %#ok<*NASGU>
1414

15-
n = 80;
15+
if nargin < 2
16+
nworkers = 4; % maximum number of workers
17+
if nargin < 1
18+
niter = 80; % iterations of work
19+
end
20+
end
1621
A = 500;
17-
a = zeros(1,n); % initialize the output
18-
nworkers = 4; % maximum number of workers
22+
a = zeros(1,niter); % initialize the output
1923

2024
% propagate the current context by extracting and passing it in headers
2125
carrier = opentelemetry.context.propagation.injectContext();
2226
headers = carrier.Headers;
2327

24-
parfor (i = 1:n, nworkers)
28+
parfor (i = 1:niter, nworkers)
2529
% parfor block needs its own initialization
2630
runOnce(@initTracer);
2731

0 commit comments

Comments
 (0)