Skip to content

Commit 4ed584d

Browse files
authored
Merge pull request #147 from mathworks/editable_view
Make view properties editable, fixes #68
2 parents 24452df + b554d89 commit 4ed584d

File tree

4 files changed

+328
-136
lines changed

4 files changed

+328
-136
lines changed

sdk/metrics/+opentelemetry/+sdk/+metrics/View.m

Lines changed: 160 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -13,18 +13,18 @@
1313
Proxy % Proxy object to interface C++ code
1414
end
1515

16-
properties (SetAccess=immutable)
17-
Name (1,1) string % View name
18-
Description (1,1) string % Description of view
19-
InstrumentName (1,1) string % Name of the instrument this view applies to
20-
InstrumentType (1,1) string % Type of instrument this view applies to
21-
InstrumentUnit (1,1) string % Unit of instrument this view applies to
22-
MeterName (1,1) string % Name of the meter this view applies to
23-
MeterVersion (1,1) string % Version of the meter this view applies to
24-
MeterSchema (1,1) string % Schema URL of the meter this view applies to
25-
AllowedAttributes (1,:) string % List of attribute keys that are kept. All other attributes are ignored.
26-
Aggregation (1,1) string % Customized aggregation type
27-
HistogramBinEdges (1,:) double % Vector of customized bin edges for histogram
16+
properties
17+
Name (1,1) string = "" % View name
18+
Description (1,1) string = "" % Description of view
19+
InstrumentName (1,1) string % Name of the instrument this view applies to
20+
InstrumentType (1,1) string % Type of instrument this view applies to
21+
InstrumentUnit (1,1) string = "" % Unit of instrument this view applies to
22+
MeterName (1,1) string = "" % Name of the meter this view applies to
23+
MeterVersion (1,1) string = "" % Version of the meter this view applies to
24+
MeterSchema (1,1) string = "" % Schema URL of the meter this view applies to
25+
AllowedAttributes (1,:) string % List of attribute keys that are kept. All other attributes are ignored.
26+
Aggregation (1,1) string % Customized aggregation type
27+
HistogramBinEdges (1,:) double = zeros(1,0) % Vector of customized bin edges for histogram
2828
end
2929

3030
methods
@@ -33,7 +33,7 @@
3333
% V = OPENTELEMETRY.SDK.METRICS.VIEW(PARAM1, VALUE1, PARAM2,
3434
% VALUE2, ...) creates a view object and specifies its
3535
% behavior using parameter name/value pairs. Parameters are:
36-
% "Name" - Name of view. Any metric this view
36+
% "Name" - Name of view. Any metric this view
3737
% applies to will be renamed to this name.
3838
% "Description" - Description of view.
3939
% "InstrumentName" - Specifies an instrument name. This
@@ -42,7 +42,7 @@
4242
% this name.
4343
% "InstrumentType" - Specifies an instrument type. This
4444
% view will be applied to all metrics
45-
% generated from all instruments of
45+
% generated from all instruments of
4646
% this type.
4747
% "InstrumentUnit" - Specifies an instrument unit. This
4848
% view will be applied to all metrics
@@ -61,10 +61,10 @@
6161
% generated from all instruments created
6262
% by meters with this schema URL.
6363
% "AllowedAttributes" - Specifies a list of attributes
64-
% that will be kept. All other
64+
% that will be kept. All other
6565
% attributes will be dropped.
6666
% "Aggregation" - Change instruments to use a
67-
% different aggregation beahvior.
67+
% different aggregation beahvior.
6868
% "HistogramBinEdges" - Use a different set of bins
6969
% in all histograms this view
7070
% applies to
@@ -83,50 +83,160 @@
8383
%
8484
% See also OPENTELEMETRY.SDK.METRICS.METERPROVIDER
8585
arguments
86-
options.Name {mustBeTextScalar} = ""
87-
options.Description {mustBeTextScalar} = ""
86+
options.Name {mustBeTextScalar}
87+
options.Description {mustBeTextScalar}
8888
options.InstrumentName {mustBeTextScalar} = "*"
8989
options.InstrumentType {mustBeTextScalar} = "counter"
90-
options.InstrumentUnit {mustBeTextScalar} = ""
91-
options.MeterName {mustBeTextScalar} = ""
92-
options.MeterVersion {mustBeTextScalar} = ""
93-
options.MeterSchema {mustBeTextScalar} = ""
94-
options.AllowedAttributes {mustBeText, mustBeVector} % no default here
90+
options.InstrumentUnit {mustBeTextScalar}
91+
options.MeterName {mustBeTextScalar}
92+
options.MeterVersion {mustBeTextScalar}
93+
options.MeterSchema {mustBeTextScalar}
94+
options.AllowedAttributes {mustBeText, mustBeVector} = "*"
9595
options.Aggregation {mustBeTextScalar} = "default"
96-
options.HistogramBinEdges {mustBeNumeric, mustBeVector} = zeros(1,0)
96+
options.HistogramBinEdges {mustBeNumeric, mustBeVector}
9797
end
9898

99+
obj.Proxy = libmexclass.proxy.Proxy("Name", "libmexclass.opentelemetry.sdk.ViewProxy", ...
100+
"ConstructorArguments", {});
101+
102+
if isfield(options, "Name")
103+
obj.Name = options.Name;
104+
end
105+
if isfield(options, "Description")
106+
obj.Description = options.Description;
107+
end
108+
obj.InstrumentName = options.InstrumentName;
109+
obj.InstrumentType = options.InstrumentType;
110+
if isfield(options, "InstrumentUnit")
111+
obj.InstrumentUnit = options.InstrumentUnit;
112+
end
113+
if isfield(options, "MeterName")
114+
obj.MeterName = options.MeterName;
115+
end
116+
if isfield(options, "MeterVersion")
117+
obj.MeterVersion = options.MeterVersion;
118+
end
119+
if isfield(options, "MeterSchema")
120+
obj.MeterSchema = options.MeterSchema;
121+
end
122+
obj.AllowedAttributes = options.AllowedAttributes;
123+
obj.Aggregation = options.Aggregation;
124+
if isfield(options, "HistogramBinEdges")
125+
obj.HistogramBinEdges = options.HistogramBinEdges;
126+
end
127+
end
128+
129+
function obj = set.Name(obj, name)
130+
arguments
131+
obj
132+
name {mustBeTextScalar}
133+
end
134+
name = string(name);
135+
obj.Proxy.setName(name); %#ok<*MCSUP>
136+
obj.Name = name;
137+
end
138+
139+
function obj = set.Description(obj, desc)
140+
arguments
141+
obj
142+
desc {mustBeTextScalar}
143+
end
144+
desc = string(desc);
145+
obj.Proxy.setDescription(desc);
146+
obj.Description = desc;
147+
end
148+
149+
function obj = set.InstrumentName(obj, instname)
150+
arguments
151+
obj
152+
instname {mustBeTextScalar}
153+
end
154+
instname = string(instname);
155+
obj.Proxy.setInstrumentName(instname);
156+
obj.InstrumentName = instname;
157+
end
158+
159+
function obj = set.InstrumentType(obj, insttype)
160+
arguments
161+
obj
162+
insttype {mustBeTextScalar}
163+
end
99164
instrument_types = ["counter", "histogram", "updowncounter", ...
100165
"observablecounter", "observableupdowncounter", "observablegauge"];
101-
instrument_type = validatestring(options.InstrumentType, instrument_types);
166+
insttype = validatestring(insttype, instrument_types);
167+
obj.Proxy.setInstrumentType(insttype);
168+
obj.InstrumentType = insttype;
169+
end
102170

103-
aggregation_types = ["drop", "histogram", "lastvalue", "sum", "default"];
104-
aggregation_type = validatestring(options.Aggregation, aggregation_types);
105-
106-
% check whether AllowedAttributes is defined
107-
filter_attributes = isfield(options, "AllowedAttributes");
108-
if ~filter_attributes
109-
% put some defaults here, which will be ignored since filter_attributes is false
110-
options.AllowedAttributes = strings(1,0);
171+
function obj = set.InstrumentUnit(obj, instunit)
172+
arguments
173+
obj
174+
instunit {mustBeTextScalar}
111175
end
176+
instunit = string(instunit);
177+
obj.Proxy.setInstrumentUnit(instunit);
178+
obj.InstrumentUnit = instunit;
179+
end
112180

113-
obj.Proxy = libmexclass.proxy.Proxy("Name", "libmexclass.opentelemetry.sdk.ViewProxy", ...
114-
"ConstructorArguments", {options.Name, options.Description, options.InstrumentName, ...
115-
instrument_type, options.InstrumentUnit, options.MeterName, ...
116-
options.MeterVersion, options.MeterSchema, filter_attributes,...
117-
options.AllowedAttributes, aggregation_type, options.HistogramBinEdges});
118-
119-
obj.Name = string(options.Name);
120-
obj.Description = string(options.Description);
121-
obj.InstrumentName = string(options.InstrumentName);
122-
obj.InstrumentType = instrument_type;
123-
obj.InstrumentUnit = string(options.InstrumentUnit);
124-
obj.MeterName = string(options.MeterName);
125-
obj.MeterVersion = string(options.MeterVersion);
126-
obj.MeterSchema = string(options.MeterSchema);
127-
obj.AllowedAttributes = reshape(string(options.AllowedAttributes),1,[]);
128-
obj.Aggregation = aggregation_type;
129-
obj.HistogramBinEdges = reshape(double(options.HistogramBinEdges),1,[]);
181+
function obj = set.MeterName(obj, metername)
182+
arguments
183+
obj
184+
metername {mustBeTextScalar}
185+
end
186+
metername = string(metername);
187+
obj.Proxy.setMeterName(metername)
188+
obj.MeterName = metername;
189+
end
190+
191+
function obj = set.MeterVersion(obj, meterversion)
192+
arguments
193+
obj
194+
meterversion {mustBeTextScalar}
195+
end
196+
meterversion = string(meterversion);
197+
obj.Proxy.setMeterVersion(meterversion);
198+
obj.MeterVersion = meterversion;
199+
end
200+
201+
function obj = set.MeterSchema(obj, meterschema)
202+
arguments
203+
obj
204+
meterschema {mustBeTextScalar}
205+
end
206+
meterschema = string(meterschema);
207+
obj.Proxy.setMeterSchema(meterschema);
208+
obj.MeterSchema = meterschema;
209+
end
210+
211+
function obj = set.AllowedAttributes(obj, attrs)
212+
arguments
213+
obj
214+
attrs {mustBeText, mustBeVector}
215+
end
216+
attrs = reshape(string(attrs),1,[]);
217+
obj.Proxy.setAllowedAttributes(attrs);
218+
obj.AllowedAttributes = attrs;
219+
end
220+
221+
function obj = set.Aggregation(obj, agg)
222+
arguments
223+
obj
224+
agg {mustBeTextScalar}
225+
end
226+
aggregation_types = ["drop", "histogram", "lastvalue", "sum", "default"];
227+
agg = validatestring(agg, aggregation_types);
228+
obj.Proxy.setAggregation(agg);
229+
obj.Aggregation = agg;
230+
end
231+
232+
function obj = set.HistogramBinEdges(obj, binedges)
233+
arguments
234+
obj
235+
binedges {mustBeNumeric, mustBeVector}
236+
end
237+
binedges = reshape(double(binedges),1,[]);
238+
obj.Proxy.setHistogramBinEdges(binedges);
239+
obj.HistogramBinEdges = binedges;
130240
end
131241
end
132242
end

sdk/metrics/include/opentelemetry-matlab/sdk/metrics/ViewProxy.h

Lines changed: 38 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Copyright 2023 The MathWorks, Inc.
1+
// Copyright 2023-2024 The MathWorks, Inc.
22

33
#pragma once
44

@@ -28,27 +28,53 @@ namespace nostd = opentelemetry::nostd;
2828
namespace libmexclass::opentelemetry::sdk {
2929
class ViewProxy : public libmexclass::proxy::Proxy {
3030
public:
31-
ViewProxy(std::string name, std::string description, std::string instrumentName,
32-
metrics_sdk::InstrumentType instrumentType, std::string instrumentUnit, std::string meterName,
33-
std::string meterVersion, std::string meterSchema, std::unordered_map<std::string, bool> allowedAttributes,
34-
bool filterAttributes, metrics_sdk::AggregationType aggregationType, std::vector<double> histogramBinEdges)
35-
: Name(std::move(name)), Description(std::move(description)), InstrumentName(std::move(instrumentName)), InstrumentType(instrumentType),
36-
InstrumentUnit(std::move(instrumentUnit)), MeterName(std::move(meterName)), MeterVersion(std::move(meterVersion)), MeterSchema(std::move(meterSchema)),
37-
AllowedAttributes(std::move(allowedAttributes)), FilterAttributes(filterAttributes), Aggregation(aggregationType), HistogramBinEdges(std::move(histogramBinEdges)) {}
3831

32+
ViewProxy()
33+
: FilterAttributes(false) {
34+
REGISTER_METHOD(ViewProxy, setName);
35+
REGISTER_METHOD(ViewProxy, setDescription);
36+
REGISTER_METHOD(ViewProxy, setInstrumentName);
37+
REGISTER_METHOD(ViewProxy, setInstrumentType);
38+
REGISTER_METHOD(ViewProxy, setInstrumentUnit);
39+
REGISTER_METHOD(ViewProxy, setMeterName);
40+
REGISTER_METHOD(ViewProxy, setMeterVersion);
41+
REGISTER_METHOD(ViewProxy, setMeterSchema);
42+
REGISTER_METHOD(ViewProxy, setAllowedAttributes);
43+
REGISTER_METHOD(ViewProxy, setAggregation);
44+
REGISTER_METHOD(ViewProxy, setHistogramBinEdges);
45+
}
46+
3947
static libmexclass::proxy::MakeResult make(const libmexclass::proxy::FunctionArguments& constructor_arguments);
4048

49+
void setName(libmexclass::proxy::method::Context& context);
50+
51+
void setDescription(libmexclass::proxy::method::Context& context);
52+
53+
void setInstrumentName(libmexclass::proxy::method::Context& context);
54+
55+
void setInstrumentType(libmexclass::proxy::method::Context& context);
56+
57+
void setInstrumentUnit(libmexclass::proxy::method::Context& context);
58+
59+
void setMeterName(libmexclass::proxy::method::Context& context);
60+
61+
void setMeterVersion(libmexclass::proxy::method::Context& context);
62+
63+
void setMeterSchema(libmexclass::proxy::method::Context& context);
64+
65+
void setAllowedAttributes(libmexclass::proxy::method::Context& context);
66+
67+
void setAggregation(libmexclass::proxy::method::Context& context);
68+
69+
void setHistogramBinEdges(libmexclass::proxy::method::Context& context);
70+
4171
std::unique_ptr<metrics_sdk::View> getView();
4272

4373
std::unique_ptr<metrics_sdk::InstrumentSelector> getInstrumentSelector();
4474

4575
std::unique_ptr<metrics_sdk::MeterSelector> getMeterSelector();
4676

4777
private:
48-
std::unique_ptr<metrics_sdk::View> View;
49-
50-
std::unique_ptr<metrics_sdk::InstrumentSelector> InstrumentSelector;
51-
5278
std::string InstrumentName;
5379
metrics_sdk::InstrumentType InstrumentType;
5480
std::string InstrumentUnit;

0 commit comments

Comments
 (0)