Skip to content

Commit 7d5f2fe

Browse files
authored
Merge pull request #82 from mathworks/metrics
Asynchronous metrics example
2 parents 4a483da + 4ead1bb commit 7d5f2fe

File tree

3 files changed

+77
-11
lines changed

3 files changed

+77
-11
lines changed

examples/metrics/README.md

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,21 @@
11
# Metrics Example
2-
This example shows how to emit OpenTelemetry metrics from MATLAB. It uses all 3 synchronous instruments counter, updowncounter, and histogram.
2+
There are two examples in this directory, metrics\_example and async\_metrics\_example.
3+
4+
## metrics\_example
5+
This example shows how to emit OpenTelemetry synchronous metrics from MATLAB. It uses all 3 synchronous instruments counter, updowncounter, and histogram.
36
* At the beginning of the first run, initialization is necessary to create and store a global meter provider.
47
* The example then enters a loop and at each iteration updates all 3 instruments. The metrics will then be exported periodically at a fixed time interval.
58

6-
## Running the Example
9+
## async\_metrics\_example
10+
This example shows how to emit OpenTelemetry asynchronous metrics from MATLAB. Is uses all 3 asynchronous instruments observable counter, observable
11+
updowncounter, and obervable gauge.
12+
* Initialization is first done by creating and storing a global meter provider, and specifying a data export interval and timeout.
13+
* The asynchronous instruments are then created, passing in their respective callback functions.
14+
* The example then pauses for 100 seconds, allowing the asynchronous instruments to periodically fetch and export their measurements.
15+
* Finally, the global meter provider is shut down to stop any further data exports.
16+
17+
## Running the Examples
718
1. Start an instance of [OpenTelemetry Collector](https://github.com/open-telemetry/opentelemetry-collector).
819
2. Start MATLAB.
920
3. Ensure the installation directory of OpenTelemetry-matlab is on the MATLAB path.
10-
4. Run metrics_example.
21+
4. Run metrics\_example or async\_metrics\_example.
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
function async_metrics_example
2+
% This example creates 3 asynchronous metric instruments including an
3+
% observable counter, an observable updowncounter, and an observable gauge.
4+
5+
% Copyright 2024 The MathWorks, Inc.
6+
7+
% initialize meter provider
8+
initMetrics;
9+
10+
% create meter and instruments
11+
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+
pause(iterations*5);
17+
18+
% clean up
19+
cleanupMetrics;
20+
end
21+
22+
23+
function initMetrics
24+
% set up global MeterProvider
25+
exp = opentelemetry.exporters.otlp.defaultMetricExporter();
26+
reader = opentelemetry.sdk.metrics.PeriodicExportingMetricReader(exp, ...
27+
"Interval", seconds(5), "Timeout", seconds(2.5)); % exports every 5 seconds
28+
mp = opentelemetry.sdk.metrics.MeterProvider(reader);
29+
setMeterProvider(mp);
30+
end
31+
32+
function cleanupMetrics
33+
mp = opentelemetry.metrics.Provider.getMeterProvider();
34+
opentelemetry.sdk.common.Cleanup.shutdown(mp); % shutdown
35+
end
36+
37+
function result = counter_callback()
38+
persistent value
39+
if isempty(value)
40+
value = 0;
41+
else
42+
value = value + randi(10); % increment between 1 to 10
43+
end
44+
result = opentelemetry.metrics.ObservableResult;
45+
result = result.observe(value);
46+
end
47+
48+
function result = updowncounter_callback()
49+
persistent value
50+
if isempty(value)
51+
value = 0;
52+
else
53+
value = value + randi([-5 5]); % increment between -5 to 5
54+
end
55+
result = opentelemetry.metrics.ObservableResult;
56+
result = result.observe(value);
57+
end
58+
59+
function result = gauge_callback()
60+
s = second(datetime("now")); % get the current second of the minute
61+
result = opentelemetry.metrics.ObservableResult;
62+
result = result.observe(s);
63+
end

test/tmetrics.m

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -612,10 +612,6 @@ function testAsynchronousInstrumentAnonymousCallback(testCase, create_async, dat
612612

613613
function testAsynchronousInstrumentMultipleCallbacks(testCase, create_async, datapoint_name)
614614
% Observable counter with more than one callbacks
615-
616-
testCase.assumeTrue(false, ...
617-
"Disabled due to sporadic failures.");
618-
619615
countername = "bar";
620616

621617
p = opentelemetry.sdk.metrics.MeterProvider(testCase.ShortIntervalReader);
@@ -653,10 +649,6 @@ function testAsynchronousInstrumentMultipleCallbacks(testCase, create_async, dat
653649

654650
function testAsynchronousInstrumentRemoveCallback(testCase, create_async)
655651
% removeCallback method
656-
657-
testCase.assumeTrue(false, ...
658-
"Disabled due to sporadic failures.");
659-
660652
callback = @callbackNoAttributes;
661653

662654
p = opentelemetry.sdk.metrics.MeterProvider(testCase.ShortIntervalReader);

0 commit comments

Comments
 (0)