Skip to content

Commit

Permalink
Merge pull request #49 from mathworks/sync_instrument
Browse files Browse the repository at this point in the history
add a SynchronousInstrument base class
  • Loading branch information
duncanpo authored Nov 3, 2023
2 parents b6e18f9 + 4a12e67 commit af1a71c
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 90 deletions.
41 changes: 9 additions & 32 deletions api/metrics/+opentelemetry/+metrics/Counter.m
Original file line number Diff line number Diff line change
@@ -1,59 +1,36 @@
classdef Counter < handle
% Counter is a value that accumulates over time,
% you can think of this like an odometer on a car; it only ever goes up.
classdef Counter < opentelemetry.metrics.SynchronousInstrument
% Counter is a value that accumulates over time and can only increase
% but not decrease.

% Copyright 2023 The MathWorks, Inc.

properties (SetAccess=immutable)
Name (1,1) string
Description (1,1) string
Unit (1,1) string
end

properties (Access=private)
Proxy % Proxy object to interface C++ code
end

methods (Access={?opentelemetry.metrics.Meter})

function obj = Counter(proxy, ctname, ctdescription, ctunit)
function obj = Counter(proxy, name, description, unit)
% Private constructor. Use createCounter method of Meter
% to create Counters.
obj.Proxy = proxy;
obj.Name = ctname;
obj.Description = ctdescription;
obj.Unit = ctunit;
[email protected](proxy, name, description, unit);
end

end

methods

function add(obj, value, varargin)
% input value must be a numerical scalar
if isnumeric(value) && isscalar(value)

if nargin == 2
obj.Proxy.add(value);

elseif isa(varargin{1}, "dictionary")
attrkeys = keys(varargin{1});
attrvals = values(varargin{1},"cell");
if all(cellfun(@iscell, attrvals))
attrvals = [attrvals{:}];
end
obj.Proxy.add(value,attrkeys,attrvals);

obj.Proxy.add(value, attrkeys, attrvals);
else
attrkeys = [varargin{1:2:length(varargin)}]';
attrvals = [varargin(2:2:length(varargin))]';
obj.Proxy.add(value,attrkeys,attrvals);
obj.Proxy.add(value, attrkeys, attrvals);
end
end

end

end


end
end
36 changes: 8 additions & 28 deletions api/metrics/+opentelemetry/+metrics/Histogram.m
Original file line number Diff line number Diff line change
@@ -1,55 +1,35 @@
classdef Histogram < handle
% Histogram is an instrument that adds or reduce values.
classdef Histogram < opentelemetry.metrics.SynchronousInstrument
% Histogram is an instrument that aggregates values into bins

% Copyright 2023 The MathWorks, Inc.

properties (SetAccess=immutable)
Name (1,1) string
Description (1,1) string
Unit (1,1) string
end

properties (Access=public)
Proxy % Proxy object to interface C++ code
end

methods (Access={?opentelemetry.metrics.Meter})

function obj = Histogram(proxy, hiname, hidescription, hiunit)
function obj = Histogram(proxy, name, description, unit)
% Private constructor. Use createHistogram method of Meter
% to create Histograms.
obj.Proxy = proxy;
obj.Name = hiname;
obj.Description = hidescription;
obj.Unit = hiunit;
[email protected](proxy, name, description, unit);
end

end

methods

function record(obj, value, varargin)
% input value must be a numerical scalar
if isnumeric(value) && isscalar(value)
if nargin == 2
if nargin == 2
obj.Proxy.record(value);
elseif isa(varargin{1}, "dictionary")
attrkeys = keys(varargin{1});
attrvals = values(varargin{1},"cell");
if all(cellfun(@iscell, attrvals))
attrvals = [attrvals{:}];
end
obj.Proxy.record(value,attrkeys,attrvals);
obj.Proxy.record(value, attrkeys, attrvals);
else
attrkeys = [varargin{1:2:length(varargin)}]';
attrvals = [varargin(2:2:length(varargin))]';
obj.Proxy.record(value,attrkeys,attrvals);
obj.Proxy.record(value, attrkeys, attrvals);
end
end

end

end


end
24 changes: 24 additions & 0 deletions api/metrics/+opentelemetry/+metrics/SynchronousInstrument.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
classdef SynchronousInstrument < handle
% Base class inherited by all synchronous instruments

% Copyright 2023 The MathWorks, Inc.

properties (SetAccess=immutable)
Name (1,1) string
Description (1,1) string
Unit (1,1) string
end

properties (Access=protected)
Proxy % Proxy object to interface C++ code
end

methods (Access=protected)
function obj = SynchronousInstrument(proxy, name, description, unit)
obj.Proxy = proxy;
obj.Name = name;
obj.Description = description;
obj.Unit = unit;
end
end
end
37 changes: 7 additions & 30 deletions api/metrics/+opentelemetry/+metrics/UpDownCounter.m
Original file line number Diff line number Diff line change
@@ -1,58 +1,35 @@
classdef UpDownCounter < handle
classdef UpDownCounter < opentelemetry.metrics.SynchronousInstrument
% UpDownCounter is an instrument that adds or reduce values.

% Copyright 2023 The MathWorks, Inc.

properties (SetAccess=immutable)
Name (1,1) string
Description (1,1) string
Unit (1,1) string
end

properties (Access=public)
Proxy % Proxy object to interface C++ code
end

methods (Access={?opentelemetry.metrics.Meter})

function obj = UpDownCounter(proxy, ctname, ctdescription, ctunit)
function obj = UpDownCounter(proxy, name, description, unit)
% Private constructor. Use createUpDownCounter method of Meter
% to create UpDownCounters.
obj.Proxy = proxy;
obj.Name = ctname;
obj.Description = ctdescription;
obj.Unit = ctunit;
[email protected](proxy, name, description, unit);
end

end

methods

function add(obj, value, varargin)
% input value must be a numerical scalar
if isnumeric(value) && isscalar(value)

if nargin == 2
obj.Proxy.add(value);

elseif isa(varargin{1}, "dictionary")
attrkeys = keys(varargin{1});
attrvals = values(varargin{1},"cell");
if all(cellfun(@iscell, attrvals))
attrvals = [attrvals{:}];
end
obj.Proxy.add(value,attrkeys,attrvals);

obj.Proxy.add(value, attrkeys, attrvals);
else
attrkeys = [varargin{1:2:length(varargin)}]';
attrvals = [varargin(2:2:length(varargin))]';
obj.Proxy.add(value,attrkeys,attrvals);
obj.Proxy.add(value, attrkeys, attrvals);
end
end

end

end


end
end

0 comments on commit af1a71c

Please sign in to comment.