From 9f752e58143dee9627215c8139b8d88ee674a636 Mon Sep 17 00:00:00 2001 From: duncanpo Date: Tue, 30 Jan 2024 10:55:23 -0500 Subject: [PATCH] Asynchronous metrics example #78, #79 --- examples/metrics/README.md | 17 +++++-- examples/metrics/async_metrics_example.m | 63 ++++++++++++++++++++++++ test/tmetrics.m | 8 --- 3 files changed, 77 insertions(+), 11 deletions(-) create mode 100644 examples/metrics/async_metrics_example.m diff --git a/examples/metrics/README.md b/examples/metrics/README.md index 3b65069..d604cec 100644 --- a/examples/metrics/README.md +++ b/examples/metrics/README.md @@ -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. diff --git a/examples/metrics/async_metrics_example.m b/examples/metrics/async_metrics_example.m new file mode 100644 index 0000000..e4fa3b5 --- /dev/null +++ b/examples/metrics/async_metrics_example.m @@ -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 \ No newline at end of file diff --git a/test/tmetrics.m b/test/tmetrics.m index a9342ca..a103218 100644 --- a/test/tmetrics.m +++ b/test/tmetrics.m @@ -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); @@ -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);