Skip to content

Commit 6af9e20

Browse files
authored
Merge pull request #154 from mathworks/metric_reader_properties
Change sdk class constructors to accept NV pairs without first argument
2 parents a12ad18 + e1b8944 commit 6af9e20

File tree

9 files changed

+315
-220
lines changed

9 files changed

+315
-220
lines changed

sdk/logs/+opentelemetry/+sdk/+logs/BatchLogRecordProcessor.m

Lines changed: 27 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
end
1111

1212
methods
13-
function obj = BatchLogRecordProcessor(exporter, optionnames, optionvalues)
13+
function obj = BatchLogRecordProcessor(varargin)
1414
% Batch log record processor creates batches of log records and passes them to an exporter.
1515
% BLP = OPENTELEMETRY.SDK.LOGS.BATCHLOGRECORDPROCESSOR creates a
1616
% batch log record processor that uses an OTLP HTTP exporter, which
@@ -20,7 +20,7 @@
2020
% the log record exporter. Supported log record exporters are OTLP HTTP
2121
% exporter and OTLP gRPC exporter.
2222
%
23-
% BLP = OPENTELEMETRY.SDK.LOGS.BATCHLOGRECORDPROCESSOR(EXP, PARAM1,
23+
% BLP = OPENTELEMETRY.SDK.LOGS.BATCHLOGRECORDPROCESSOR(..., PARAM1,
2424
% VALUE1, PARAM2, VALUE2, ...) specifies optional parameter
2525
% name/value pairs. Parameters are:
2626
% "MaximumQueueSize" - Maximum queue size. After queue
@@ -35,24 +35,18 @@
3535
% OPENTELEMETRY.EXPORTERS.OTLP.OTLPHTTPLOGRECORDEXPORTER,
3636
% OPENTELEMETRY.EXPORTERS.OTLP.OTLPGRPCLOGRECORDEXPORTER,
3737
% OPENTELEMETRY.SDK.LOGS.LOGGERPROVIDER
38-
arguments
39-
exporter {mustBeA(exporter, "opentelemetry.sdk.logs.LogRecordExporter")} = ...
40-
opentelemetry.exporters.otlp.defaultLogRecordExporter()
41-
end
42-
arguments (Repeating)
43-
optionnames (1,:) {mustBeTextScalar}
44-
optionvalues
38+
39+
if nargin == 0 || ~isa(varargin{1}, "opentelemetry.sdk.logs.LogRecordExporter")
40+
exporter = opentelemetry.exporters.otlp.defaultLogRecordExporter;
41+
else % isa(varargin{1}, "opentelemetry.sdk.logs.LogRecordExporter")
42+
exporter = varargin{1};
43+
varargin(1) = [];
4544
end
4645

4746
obj = [email protected](exporter, ...
4847
"libmexclass.opentelemetry.sdk.BatchLogRecordProcessorProxy");
4948

50-
validnames = ["MaximumQueueSize", "ScheduledDelay", "MaximumExportBatchSize"];
51-
for i = 1:length(optionnames)
52-
namei = validatestring(optionnames{i}, validnames);
53-
valuei = optionvalues{i};
54-
obj.(namei) = valuei;
55-
end
49+
obj = obj.processOptions(varargin{:});
5650
end
5751

5852
function obj = set.MaximumQueueSize(obj, maxqsz)
@@ -86,4 +80,22 @@
8680
obj.MaximumExportBatchSize = maxbatch;
8781
end
8882
end
83+
84+
methods(Access=private)
85+
function obj = processOptions(obj, optionnames, optionvalues)
86+
arguments
87+
obj
88+
end
89+
arguments (Repeating)
90+
optionnames (1,:) {mustBeTextScalar}
91+
optionvalues
92+
end
93+
validnames = ["MaximumQueueSize", "ScheduledDelay", "MaximumExportBatchSize"];
94+
for i = 1:length(optionnames)
95+
namei = validatestring(optionnames{i}, validnames);
96+
valuei = optionvalues{i};
97+
obj.(namei) = valuei;
98+
end
99+
end
100+
end
89101
end

sdk/logs/+opentelemetry/+sdk/+logs/LoggerProvider.m

Lines changed: 55 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -14,15 +14,15 @@
1414
end
1515

1616
methods
17-
function obj = LoggerProvider(processor, optionnames, optionvalues)
17+
function obj = LoggerProvider(varargin)
1818
% SDK implementation of logger provider
1919
% LP = OPENTELEMETRY.SDK.LOGS.LOGGERPROVIDER creates a logger
2020
% provider that uses a simple log record processor and default configurations.
2121
%
2222
% LP = OPENTELEMETRY.SDK.LOGS.LOGGERPROVIDER(P) uses log record
2323
% processor P. P can be a simple or batched log record processor.
2424
%
25-
% LP = OPENTELEMETRY.SDK.LOGS.LOGGERPROVIDER(R, PARAM1, VALUE1,
25+
% LP = OPENTELEMETRY.SDK.LOGS.LOGGERPROVIDER(..., PARAM1, VALUE1,
2626
% PARAM2, VALUE2, ...) specifies optional parameter name/value pairs.
2727
% Parameters are:
2828
% "Resource" - Additional resource attributes.
@@ -31,60 +31,28 @@
3131
% See also OPENTELEMETRY.SDK.LOGS.SIMPLELOGRECORDPROCESSOR,
3232
% OPENTELEMETRY.SDK.LOGS.BATCHLOGRECORDPROCESSOR
3333

34-
arguments
35-
processor {mustBeA(processor, ["opentelemetry.sdk.logs.LogRecordProcessor", ...
36-
"libmexclass.proxy.Proxy"])} = ...
37-
opentelemetry.sdk.logs.SimpleLogRecordProcessor()
38-
end
39-
arguments (Repeating)
40-
optionnames (1,:) {mustBeTextScalar}
41-
optionvalues
42-
end
43-
4434
% explicit call to superclass constructor to make it a no-op
4535
4636

47-
if isa(processor, "libmexclass.proxy.Proxy")
37+
if nargin == 1 && isa(varargin{1}, "libmexclass.proxy.Proxy")
4838
% This code branch is used to support conversion from API
4939
% LoggerProvider to SDK equivalent, needed internally by
5040
% opentelemetry.sdk.logs.Cleanup
51-
lpproxy = processor; % rename the variable
41+
lpproxy = varargin{1};
5242
assert(lpproxy.Name == "libmexclass.opentelemetry.LoggerProviderProxy");
5343
obj.Proxy = libmexclass.proxy.Proxy("Name", ...
5444
"libmexclass.opentelemetry.sdk.LoggerProviderProxy", ...
5545
"ConstructorArguments", {lpproxy.ID});
5646
% leave other properties unassigned, they won't be used
5747
else
58-
validnames = "Resource";
59-
resourcekeys = string.empty();
60-
resourcevalues = {};
61-
62-
resource = dictionary(resourcekeys, resourcevalues);
63-
for i = 1:length(optionnames)
64-
namei = validatestring(optionnames{i}, validnames);
65-
valuei = optionvalues{i};
66-
if strcmp(namei, "Resource")
67-
if ~isa(valuei, "dictionary")
68-
error("opentelemetry:sdk:logs:LoggerProvider:InvalidResourceType", ...
69-
"Resource input must be a dictionary.");
70-
end
71-
resource = valuei;
72-
resourcekeys = keys(valuei);
73-
resourcevalues = values(valuei,"cell");
74-
% collapse one level of cells, as this may be due to
75-
% a behavior of dictionary.values
76-
if all(cellfun(@iscell, resourcevalues))
77-
resourcevalues = [resourcevalues{:}];
78-
end
79-
end
48+
if nargin == 0 || ~isa(varargin{1}, "opentelemetry.sdk.logs.LogRecordProcessor")
49+
processor = opentelemetry.sdk.logs.SimpleLogRecordProcessor(); % default processor
50+
else
51+
processor = varargin{1};
52+
varargin(1) = [];
8053
end
81-
82-
obj.Proxy = libmexclass.proxy.Proxy("Name", ...
83-
"libmexclass.opentelemetry.sdk.LoggerProviderProxy", ...
84-
"ConstructorArguments", {processor.Proxy.ID, resourcekeys, ...
85-
resourcevalues});
86-
obj.LogRecordProcessor = processor;
87-
obj.Resource = resource;
54+
obj.processOptions(processor, varargin{:});
55+
8856
end
8957
end
9058

@@ -139,4 +107,48 @@ function addLogRecordProcessor(obj, processor)
139107
end
140108
end
141109
end
110+
111+
methods(Access=private)
112+
function processOptions(obj, processor, optionnames, optionvalues)
113+
arguments
114+
obj
115+
processor
116+
end
117+
arguments (Repeating)
118+
optionnames (1,:) {mustBeTextScalar}
119+
optionvalues
120+
end
121+
122+
validnames = "Resource";
123+
resourcekeys = string.empty();
124+
resourcevalues = {};
125+
126+
resource = dictionary(resourcekeys, resourcevalues);
127+
for i = 1:length(optionnames)
128+
namei = validatestring(optionnames{i}, validnames);
129+
valuei = optionvalues{i};
130+
if strcmp(namei, "Resource")
131+
if ~isa(valuei, "dictionary")
132+
error("opentelemetry:sdk:logs:LoggerProvider:InvalidResourceType", ...
133+
"Resource input must be a dictionary.");
134+
end
135+
resource = valuei;
136+
resourcekeys = keys(valuei);
137+
resourcevalues = values(valuei,"cell");
138+
% collapse one level of cells, as this may be due to
139+
% a behavior of dictionary.values
140+
if all(cellfun(@iscell, resourcevalues))
141+
resourcevalues = [resourcevalues{:}];
142+
end
143+
end
144+
end
145+
146+
obj.Proxy = libmexclass.proxy.Proxy("Name", ...
147+
"libmexclass.opentelemetry.sdk.LoggerProviderProxy", ...
148+
"ConstructorArguments", {processor.Proxy.ID, resourcekeys, ...
149+
resourcevalues});
150+
obj.LogRecordProcessor = processor;
151+
obj.Resource = resource;
152+
end
153+
end
142154
end

sdk/metrics/+opentelemetry/+sdk/+metrics/MeterProvider.m

Lines changed: 67 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
% An SDK implementation of meter provider, which stores a set of configurations used
33
% in a metrics system.
44

5-
% Copyright 2023 The MathWorks, Inc.
5+
% Copyright 2023-2024 The MathWorks, Inc.
66

77
properties(Access=private)
88
isShutdown (1,1) logical = false
@@ -15,7 +15,7 @@
1515
end
1616

1717
methods
18-
function obj = MeterProvider(reader, optionnames, optionvalues)
18+
function obj = MeterProvider(varargin)
1919
% SDK implementation of meter provider
2020
% MP = OPENTELEMETRY.SDK.METRICS.METERPROVIDER creates a meter
2121
% provider that uses a periodic exporting metric reader and default configurations.
@@ -24,7 +24,7 @@
2424
% reader R. Currently, the only supported metric reader is the periodic
2525
% exporting metric reader.
2626
%
27-
% TP = OPENTELEMETRY.SDK.METRICS.METERPROVIDER(R, PARAM1, VALUE1,
27+
% TP = OPENTELEMETRY.SDK.METRICS.METERPROVIDER(..., PARAM1, VALUE1,
2828
% PARAM2, VALUE2, ...) specifies optional parameter name/value pairs.
2929
% Parameters are:
3030
% "View" - View object used to customize collected metrics.
@@ -34,73 +34,27 @@
3434
% See also OPENTELEMETRY.SDK.METRICS.PERIODICEXPORTINGMETRICREADER
3535
% OPENTELEMETRY.SDK.METRICS.VIEW
3636

37-
arguments
38-
reader {mustBeA(reader, ["opentelemetry.sdk.metrics.PeriodicExportingMetricReader", ...
39-
"libmexclass.proxy.Proxy"])} = ...
40-
opentelemetry.sdk.metrics.PeriodicExportingMetricReader()
41-
end
42-
43-
arguments (Repeating)
44-
optionnames (1,:) {mustBeTextScalar}
45-
optionvalues
46-
end
47-
4837
% explicit call to superclass constructor to make it a no-op
4938
5039

51-
if isa(reader, "libmexclass.proxy.Proxy")
40+
if nargin == 1 && isa(varargin{1}, "libmexclass.proxy.Proxy")
5241
% This code branch is used to support conversion from API
5342
% MeterProvider to SDK equivalent, needed internally by
5443
% opentelemetry.sdk.metrics.Cleanup
55-
mpproxy = reader; % rename the variable
44+
mpproxy = varargin{1};
5645
assert(mpproxy.Name == "libmexclass.opentelemetry.MeterProviderProxy");
5746
obj.Proxy = libmexclass.proxy.Proxy("Name", ...
5847
"libmexclass.opentelemetry.sdk.MeterProviderProxy", ...
5948
"ConstructorArguments", {mpproxy.ID});
6049
% leave other properties unassigned, they won't be used
6150
else
62-
validnames = ["Resource", "View"];
63-
resourcekeys = string.empty();
64-
resourcevalues = {};
65-
resource = dictionary(resourcekeys, resourcevalues);
66-
suppliedview = false;
67-
viewid = 0;
68-
for i = 1:length(optionnames)
69-
namei = validatestring(optionnames{i}, validnames);
70-
valuei = optionvalues{i};
71-
if strcmp(namei, "Resource")
72-
if ~isa(valuei, "dictionary")
73-
error("opentelemetry:sdk:metrics:MeterProvider:InvalidResourceType", ...
74-
"Resource input must be a dictionary.");
75-
end
76-
resource = valuei;
77-
resourcekeys = keys(valuei);
78-
resourcevalues = values(valuei,"cell");
79-
% collapse one level of cells, as this may be due to
80-
% a behavior of dictionary.values
81-
if all(cellfun(@iscell, resourcevalues))
82-
resourcevalues = [resourcevalues{:}];
83-
end
84-
elseif strcmp(namei, "View")
85-
suppliedview = true;
86-
view = valuei;
87-
if ~isa(view, "opentelemetry.sdk.metrics.View")
88-
error("opentelemetry:sdk:metrics:MeterProvider:InvalidViewType", ...
89-
"View input must be a opentelemetry.sdk.metrics.View object.");
90-
end
91-
viewid = view.Proxy.ID;
92-
end
93-
end
94-
95-
obj.Proxy = libmexclass.proxy.Proxy("Name", ...
96-
"libmexclass.opentelemetry.sdk.MeterProviderProxy", ...
97-
"ConstructorArguments", {reader.Proxy.ID, resourcekeys, ...
98-
resourcevalues, suppliedview, viewid});
99-
obj.MetricReader = reader;
100-
obj.Resource = resource;
101-
if suppliedview
102-
obj.View = view;
51+
if nargin == 0 || ~isa(varargin{1}, "opentelemetry.sdk.metrics.PeriodicExportingMetricReader")
52+
reader = opentelemetry.sdk.metrics.PeriodicExportingMetricReader(); % default metric reader
53+
else
54+
reader = varargin{1};
55+
varargin(1) = [];
10356
end
57+
obj.processOptions(reader, varargin{:});
10458
end
10559
end
10660

@@ -167,4 +121,60 @@ function addView(obj, view)
167121
end
168122

169123
end
124+
125+
methods(Access=private)
126+
function processOptions(obj, reader, optionnames, optionvalues)
127+
arguments
128+
obj
129+
reader
130+
end
131+
arguments (Repeating)
132+
optionnames (1,:) {mustBeTextScalar}
133+
optionvalues
134+
end
135+
136+
validnames = ["Resource", "View"];
137+
resourcekeys = string.empty();
138+
resourcevalues = {};
139+
resource = dictionary(resourcekeys, resourcevalues);
140+
suppliedview = false;
141+
viewid = 0;
142+
for i = 1:length(optionnames)
143+
namei = validatestring(optionnames{i}, validnames);
144+
valuei = optionvalues{i};
145+
if strcmp(namei, "Resource")
146+
if ~isa(valuei, "dictionary")
147+
error("opentelemetry:sdk:metrics:MeterProvider:InvalidResourceType", ...
148+
"Resource input must be a dictionary.");
149+
end
150+
resource = valuei;
151+
resourcekeys = keys(valuei);
152+
resourcevalues = values(valuei,"cell");
153+
% collapse one level of cells, as this may be due to
154+
% a behavior of dictionary.values
155+
if all(cellfun(@iscell, resourcevalues))
156+
resourcevalues = [resourcevalues{:}];
157+
end
158+
elseif strcmp(namei, "View")
159+
suppliedview = true;
160+
view = valuei;
161+
if ~isa(view, "opentelemetry.sdk.metrics.View")
162+
error("opentelemetry:sdk:metrics:MeterProvider:InvalidViewType", ...
163+
"View input must be a opentelemetry.sdk.metrics.View object.");
164+
end
165+
viewid = view.Proxy.ID;
166+
end
167+
end
168+
169+
obj.Proxy = libmexclass.proxy.Proxy("Name", ...
170+
"libmexclass.opentelemetry.sdk.MeterProviderProxy", ...
171+
"ConstructorArguments", {reader.Proxy.ID, resourcekeys, ...
172+
resourcevalues, suppliedview, viewid});
173+
obj.MetricReader = reader;
174+
obj.Resource = resource;
175+
if suppliedview
176+
obj.View = view;
177+
end
178+
end
179+
end
170180
end

0 commit comments

Comments
 (0)