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
17 changes: 14 additions & 3 deletions examples/metrics/README.md
Original file line number Diff line number Diff line change
@@ -1,10 +1,21 @@
# Metrics Example
This example shows how to emit OpenTelemetry metrics from MATLAB. It uses all 3 synchronous instruments counter, updowncounter, and histogram.
There are two examples in this directory, metrics\_example and async\_metrics\_example.

## metrics\_example
This example shows how to emit OpenTelemetry synchronous metrics from MATLAB. It uses all 3 synchronous instruments counter, updowncounter, and histogram.
* At the beginning of the first run, initialization is necessary to create and store a global meter provider.
* 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.

## Running the Example
## async\_metrics\_example
This example shows how to emit OpenTelemetry asynchronous metrics from MATLAB. Is uses all 3 asynchronous instruments observable counter, observable
updowncounter, and obervable gauge.
* Initialization is first done by creating and storing a global meter provider, and specifying a data export interval and timeout.
* The asynchronous instruments are then created, passing in their respective callback functions.
* The example then pauses for 100 seconds, allowing the asynchronous instruments to periodically fetch and export their measurements.
* Finally, the global meter provider is shut down to stop any further data exports.

## Running the Examples
1. Start an instance of [OpenTelemetry Collector](https://github.com/open-telemetry/opentelemetry-collector).
2. Start MATLAB.
3. Ensure the installation directory of OpenTelemetry-matlab is on the MATLAB path.
4. Run metrics_example.
4. Run metrics\_example or async\_metrics\_example.
63 changes: 63 additions & 0 deletions examples/metrics/async_metrics_example.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
function async_metrics_example
% This example creates 3 asynchronous metric instruments including an
% observable counter, an observable updowncounter, and an observable gauge.

% Copyright 2024 The MathWorks, Inc.

% initialize meter provider
initMetrics;

% create meter and instruments
m = opentelemetry.metrics.getMeter("async_metrics_example");
c = createObservableCounter(m, @counter_callback, "observable_counter"); %#ok<*NASGU>
u = createObservableUpDownCounter(m, @updowncounter_callback, "observable_updowncounter");
g = createObservableGauge(m, @gauge_callback, "observable_gauge");
iterations = 20; % number of iterations
pause(iterations*5);

% clean up
cleanupMetrics;
end


function initMetrics
% set up global MeterProvider
exp = opentelemetry.exporters.otlp.defaultMetricExporter();
reader = opentelemetry.sdk.metrics.PeriodicExportingMetricReader(exp, ...
"Interval", seconds(5), "Timeout", seconds(2.5)); % exports every 5 seconds
mp = opentelemetry.sdk.metrics.MeterProvider(reader);
setMeterProvider(mp);
end

function cleanupMetrics
mp = opentelemetry.metrics.Provider.getMeterProvider();
opentelemetry.sdk.common.Cleanup.shutdown(mp); % shutdown
end

function result = counter_callback()
persistent value
if isempty(value)
value = 0;
else
value = value + randi(10); % increment between 1 to 10
end
result = opentelemetry.metrics.ObservableResult;
result = result.observe(value);
end

function result = updowncounter_callback()
persistent value
if isempty(value)
value = 0;
else
value = value + randi([-5 5]); % increment between -5 to 5
end
result = opentelemetry.metrics.ObservableResult;
result = result.observe(value);
end

function result = gauge_callback()
s = second(datetime("now")); % get the current second of the minute
result = opentelemetry.metrics.ObservableResult;
result = result.observe(s);
end
8 changes: 0 additions & 8 deletions test/tmetrics.m
Original file line number Diff line number Diff line change
Expand Up @@ -612,10 +612,6 @@ function testAsynchronousInstrumentAnonymousCallback(testCase, create_async, dat

function testAsynchronousInstrumentMultipleCallbacks(testCase, create_async, datapoint_name)
% Observable counter with more than one callbacks

testCase.assumeTrue(false, ...
"Disabled due to sporadic failures.");

countername = "bar";

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

function testAsynchronousInstrumentRemoveCallback(testCase, create_async)
% removeCallback method

testCase.assumeTrue(false, ...
"Disabled due to sporadic failures.");

callback = @callbackNoAttributes;

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