Skip to content

Commit

Permalink
Merge pull request #135 from mathworks/exampletests
Browse files Browse the repository at this point in the history
Adding tests for examples
  • Loading branch information
duncanpo authored Jul 9, 2024
2 parents 1327409 + 511a048 commit 26a4993
Show file tree
Hide file tree
Showing 11 changed files with 532 additions and 79 deletions.
43 changes: 10 additions & 33 deletions examples/metrics/async_metrics_example.m
Original file line number Diff line number Diff line change
@@ -1,18 +1,22 @@
function async_metrics_example
function async_metrics_example(iterations)
% This example creates 3 asynchronous metric instruments including an
% observable counter, an observable updowncounter, and an observable gauge.

% Copyright 2024 The MathWorks, Inc.

if nargin < 1
iterations = 20; % default to 20 iterations
end

% 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
callbacks = async_metrics_example_callbacks;
c = createObservableCounter(m, @()counterCallback(callbacks), "observable_counter"); %#ok<*NASGU>
u = createObservableUpDownCounter(m, @()updowncounterCallback(callbacks), "observable_updowncounter");
g = createObservableGauge(m, @()gaugeCallback(callbacks), "observable_gauge");
pause(iterations*5);

% clean up
Expand All @@ -31,34 +35,7 @@
end

function cleanupMetrics
% clean up meter provider
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
38 changes: 38 additions & 0 deletions examples/metrics/async_metrics_example_callbacks.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
classdef async_metrics_example_callbacks < handle
% Callback functions for asynchronous metrics example

% Copyright 2024 The MathWorks, Inc.

properties
Count
UpDownCount
end

methods
function obj = async_metrics_example_callbacks()
obj.Count = 0;
obj.UpDownCount = 0;
end

function result = counterCallback(obj)
% Callback function for Counter
obj.Count = obj.Count + randi(10); % increment between 0 and 10
result = opentelemetry.metrics.ObservableResult;
result = result.observe(obj.Count);
end

function result = updowncounterCallback(obj)
% Callback function for UpdownCounter
obj.UpDownCount = obj.UpDownCount + randi([-5 5]); % increment between -5 to 5
result = opentelemetry.metrics.ObservableResult;
result = result.observe(obj.UpDownCount);
end

function result = gaugeCallback(~)
% Callback function for Gauge
s = second(datetime("now")); % get the current second of the minute
result = opentelemetry.metrics.ObservableResult;
result = result.observe(s);
end
end
end
9 changes: 7 additions & 2 deletions examples/metrics/metrics_example.m
Original file line number Diff line number Diff line change
@@ -1,10 +1,14 @@
function metrics_example
function metrics_example(iterations)
% This example creates 3 metric instruments including a counter, an
% updowncounter, and a histogram. It then enters a loop and updates the
% value of the instruments at each iteration.

% Copyright 2023-2024 The MathWorks, Inc.

if nargin < 1
iterations = 20; % default to 20 iterations
end

% initialize meter provider
initMetrics;

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

% wait a little before starting
pause(2);
for i = 1:iterations
c.add(randi(10));
u.add(randi([-10 10]));
Expand Down
16 changes: 10 additions & 6 deletions examples/parallel/parfor_example.m
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
function a = parfor_example
function a = parfor_example(niter, nworkers)
% This example creates a trace through a parfor loop, by creating a span in
% each iteration and propagating context.
%

% Copyright 2024 The MathWorks, Inc.

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

n = 80;
if nargin < 2
nworkers = 4; % maximum number of workers
if nargin < 1
niter = 80; % iterations of work
end
end
A = 500;
a = zeros(1,n); % initialize the output
nworkers = 4; % maximum number of workers
a = zeros(1,niter); % initialize the output

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

parfor (i = 1:n, nworkers)
parfor (i = 1:niter, nworkers)
% parfor block needs its own initialization
runOnce(@initTracer);

Expand Down
Loading

0 comments on commit 26a4993

Please sign in to comment.