From 4a12e67f4f39f84945adba85d8d6e6a0a8bd250e Mon Sep 17 00:00:00 2001 From: duncanpo Date: Fri, 3 Nov 2023 10:22:17 -0400 Subject: [PATCH] add a SynchronousInstrument base class --- api/metrics/+opentelemetry/+metrics/Counter.m | 41 ++++--------------- .../+opentelemetry/+metrics/Histogram.m | 36 ++++------------ .../+metrics/SynchronousInstrument.m | 24 +++++++++++ .../+opentelemetry/+metrics/UpDownCounter.m | 37 ++++------------- 4 files changed, 48 insertions(+), 90 deletions(-) create mode 100644 api/metrics/+opentelemetry/+metrics/SynchronousInstrument.m diff --git a/api/metrics/+opentelemetry/+metrics/Counter.m b/api/metrics/+opentelemetry/+metrics/Counter.m index f416f41..85a3448 100644 --- a/api/metrics/+opentelemetry/+metrics/Counter.m +++ b/api/metrics/+opentelemetry/+metrics/Counter.m @@ -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; + obj@opentelemetry.metrics.SynchronousInstrument(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 \ No newline at end of file diff --git a/api/metrics/+opentelemetry/+metrics/Histogram.m b/api/metrics/+opentelemetry/+metrics/Histogram.m index 8abc9ee..6cf0658 100644 --- a/api/metrics/+opentelemetry/+metrics/Histogram.m +++ b/api/metrics/+opentelemetry/+metrics/Histogram.m @@ -1,37 +1,21 @@ -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; + obj@opentelemetry.metrics.SynchronousInstrument(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}); @@ -39,17 +23,13 @@ function record(obj, value, varargin) 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 diff --git a/api/metrics/+opentelemetry/+metrics/SynchronousInstrument.m b/api/metrics/+opentelemetry/+metrics/SynchronousInstrument.m new file mode 100644 index 0000000..8dbe33c --- /dev/null +++ b/api/metrics/+opentelemetry/+metrics/SynchronousInstrument.m @@ -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 \ No newline at end of file diff --git a/api/metrics/+opentelemetry/+metrics/UpDownCounter.m b/api/metrics/+opentelemetry/+metrics/UpDownCounter.m index b008dc5..cb04451 100644 --- a/api/metrics/+opentelemetry/+metrics/UpDownCounter.m +++ b/api/metrics/+opentelemetry/+metrics/UpDownCounter.m @@ -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; + obj@opentelemetry.metrics.SynchronousInstrument(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 \ No newline at end of file