Skip to content

Commit f3884c3

Browse files
committed
Make metric reader and exporter properties editable
1 parent 60acf06 commit f3884c3

File tree

11 files changed

+323
-404
lines changed

11 files changed

+323
-404
lines changed

exporters/otlp/+opentelemetry/+exporters/+otlp/OtlpGrpcMetricExporter.m

Lines changed: 69 additions & 90 deletions
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,14 @@
55

66
% Copyright 2023 The MathWorks, Inc.
77

8-
properties (SetAccess=immutable)
9-
Endpoint (1,1) string % Export destination
10-
UseCredentials (1,1) logical % Whether to use SSL credentials
11-
CertificatePath (1,1) string % Path to .pem file for SSL encryption
12-
CertificateString (1,1) string % In-memory string representation of .pem file for SSL encryption
13-
Timeout (1,1) duration % Maximum time above which exports will abort
14-
HttpHeaders (1,1) dictionary % Additional HTTP headers
15-
PreferredAggregationTemporality (1,1) string % Preferred Aggregation Temporality
8+
properties
9+
Endpoint (1,1) string = "http://localhost:4317" % Export destination
10+
UseCredentials (1,1) logical = false % Whether to use SSL credentials
11+
CertificatePath (1,1) string = "" % Path to .pem file for SSL encryption
12+
CertificateString (1,1) string = "" % In-memory string representation of .pem file for SSL encryption
13+
Timeout (1,1) duration = seconds(10) % Maximum time above which exports will abort
14+
HttpHeaders (1,1) dictionary = dictionary(string.empty, string.empty) % Additional HTTP headers
15+
PreferredAggregationTemporality (1,1) string = "cumulative" % Preferred Aggregation Temporality
1616
end
1717

1818
methods
@@ -47,100 +47,79 @@
4747
optionvalues
4848
end
4949

50+
51+
"libmexclass.opentelemetry.exporters.OtlpGrpcMetricExporterProxy");
52+
5053
validnames = ["Endpoint", "UseCredentials ", "CertificatePath", ...
5154
"CertificateString", "Timeout", "HttpHeaders", "PreferredAggregationTemporality"];
52-
% set default values to empty or negative
53-
endpoint = "";
54-
usessl = false;
55-
certificatepath = "";
56-
certificatestring = "";
57-
timeout_millis = -1;
58-
headerkeys = string.empty();
59-
headervalues = string.empty();
60-
temporality = "";
6155
for i = 1:length(optionnames)
6256
namei = validatestring(optionnames{i}, validnames);
6357
valuei = optionvalues{i};
64-
if strcmp(namei, "Endpoint")
65-
if ~(isStringScalar(valuei) || (ischar(valuei) && isrow(valuei)))
66-
error("opentelemetry:exporters:otlp:OtlpGrpcMetricExporter:EndpointNotScalarText", "Endpoint must be a scalar string.");
67-
end
68-
endpoint = string(valuei);
69-
elseif strcmp(namei, "UseCredentials ")
70-
if ~((islogical(valuei) || isnumeric(valuei)) && isscalar(valuei))
71-
error("opentelemetry:exporters:otlp:OtlpGrpcMetricExporter:UseCredentialsNotScalarLogical", "UseCredentials must be a scalar logical.")
72-
end
73-
usessl = logical(valuei);
74-
elseif strcmp(namei, "CertificatePath")
75-
if ~(isStringScalar(valuei) || (ischar(valuei) && isrow(valuei)))
76-
error("opentelemetry:exporters:otlp:OtlpGrpcMetricExporter:CertificatePathNotScalarText", "CertificatePath must be a scalar string.");
77-
end
78-
certificatepath = string(valuei);
79-
elseif strcmp(namei, "CertificateString")
80-
if ~(isStringScalar(valuei) || (ischar(valuei) && isrow(valuei)))
81-
error("opentelemetry:exporters:otlp:OtlpGrpcMetricExporter:CertificateStringNotScalarText", "CertificateString must be a scalar string.");
82-
end
83-
certificatestring = string(valuei);
84-
elseif strcmp(namei, "Timeout")
85-
if ~(isduration(valuei) && isscalar(valuei))
86-
error("opentelemetry:exporters:otlp:OtlpGrpcMetricExporter:TimeoutNotScalarDuration", "Timeout must be a scalar duration.");
87-
end
88-
timeout = valuei;
89-
timeout_millis = milliseconds(timeout);
90-
elseif strcmp(namei, "HttpHeaders") % HttpHeaders
91-
if ~isa(valuei, "dictionary")
92-
error("opentelemetry:exporters:otlp:OtlpGrpcMetricExporter:HttpHeadersNotDictionary", "HttpHeaders input must be a dictionary.");
93-
end
94-
httpheaders = valuei;
95-
headerkeys = keys(valuei);
96-
headervalues = values(valuei);
97-
if ~isstring(headervalues)
98-
error("opentelemetry:exporters:otlp:OtlpGrpcMetricExporter:HttpHeadersNonStringValues", "HttpHeaders dictionary values must be strings.")
99-
end
100-
elseif strcmp(namei, "PreferredAggregationTemporality")
101-
temporality = validatestring(valuei, ["Cumulative", "Delta"]);
102-
end
58+
obj.(namei) = valuei;
10359
end
104-
105-
106-
"libmexclass.opentelemetry.exporters.OtlpGrpcMetricExporterProxy", ...
107-
endpoint, usessl, certificatepath, certificatestring, ...
108-
timeout_millis, headerkeys, headervalues, temporality);
60+
end
61+
62+
function obj = set.Endpoint(obj, ep)
63+
if ~(isStringScalar(ep) || (ischar(ep) && isrow(ep)))
64+
error("opentelemetry:exporters:otlp:OtlpGrpcMetricExporter:EndpointNotScalarText", "Endpoint must be a scalar string.");
65+
end
66+
ep = string(ep);
67+
obj.Proxy.setEndpoint(ep);
68+
obj.Endpoint = ep;
69+
end
70+
71+
function obj = set.UseCredentials(obj, uc)
72+
if ~((islogical(uc) || isnumeric(uc)) && isscalar(uc))
73+
error("opentelemetry:exporters:otlp:OtlpGrpcMetricExporter:UseCredentialsNotScalarLogical", "UseCredentials must be a scalar logical.")
74+
end
75+
uc = logical(uc);
76+
obj.Proxy.setUseCredentials(uc);
77+
obj.UseCredentials = uc;
78+
end
10979

110-
% populate immutable properties
111-
[defaultendpoint, defaultcertpath, defaultcertstring, defaultmillis, defaulttemporality] = ...
112-
getDefaultOptionValues(obj);
113-
if endpoint == "" % not specified, use default value
114-
obj.Endpoint = defaultendpoint;
115-
else
116-
obj.Endpoint = endpoint;
117-
end
118-
obj.UseCredentials = usessl;
119-
if certificatepath == "" % not specified, use default value
120-
obj.CertificatePath = defaultcertpath;
121-
else
122-
obj.CertificatePath = certificatepath;
80+
function obj = set.CertificatePath(obj, certpath)
81+
if ~(isStringScalar(certpath) || (ischar(certpath) && isrow(certpath)))
82+
error("opentelemetry:exporters:otlp:OtlpGrpcMetricExporter:CertificatePathNotScalarText", "CertificatePath must be a scalar string.");
12383
end
124-
if certificatestring == "" % not specified, use default value
125-
obj.CertificateString = defaultcertstring;
126-
else
127-
obj.CertificateString = certificatestring;
84+
certpath = string(certpath);
85+
obj.Proxy.setCertificatePath(certpath);
86+
obj.CertificatePath = certpath;
87+
end
88+
89+
function obj = set.CertificateString(obj, certstr)
90+
if ~(isStringScalar(certstr) || (ischar(certstr) && isrow(certstr)))
91+
error("opentelemetry:exporters:otlp:OtlpGrpcMetricExporter:CertificateStringNotScalarText", "CertificateString must be a scalar string.");
12892
end
129-
if timeout_millis < 0 % not specified, use default value
130-
obj.Timeout = milliseconds(defaultmillis);
131-
else
132-
obj.Timeout = timeout;
93+
certstr = string(certstr);
94+
obj.Proxy.setCertificateString(certstr);
95+
obj.CertificateString = certstr;
96+
end
97+
98+
function obj = set.Timeout(obj, timeout)
99+
if ~(isduration(timeout) && isscalar(timeout))
100+
error("opentelemetry:exporters:otlp:OtlpGrpcMetricExporter:TimeoutNotScalarDuration", "Timeout must be a scalar duration.");
133101
end
134-
if isempty(headerkeys) % not specified, return empty dictionary
135-
obj.HttpHeaders = dictionary(headerkeys, headervalues);
136-
else
137-
obj.HttpHeaders = httpheaders;
102+
obj.Proxy.setTimeout(milliseconds(timeout));
103+
obj.Timeout = timeout;
104+
end
105+
106+
function obj = set.HttpHeaders(obj, httpheaders)
107+
if ~isa(httpheaders, "dictionary")
108+
error("opentelemetry:exporters:otlp:OtlpGrpcMetricExporter:HttpHeadersNotDictionary", "HttpHeaders input must be a dictionary.");
138109
end
139-
if temporality == ""
140-
obj.PreferredAggregationTemporality = defaulttemporality;
141-
else
142-
obj.PreferredAggregationTemporality = temporality;
110+
headerkeys = keys(httpheaders);
111+
headervalues = values(httpheaders);
112+
if ~isstring(headervalues)
113+
error("opentelemetry:exporters:otlp:OtlpGrpcMetricExporter:HttpHeadersNonStringValues", "HttpHeaders dictionary values must be strings.")
143114
end
115+
obj.Proxy.setHttpHeaders(headerkeys, headervalues);
116+
obj.HttpHeaders = httpheaders;
117+
end
118+
119+
function obj = set.PreferredAggregationTemporality(obj, temporality)
120+
temporality = validatestring(temporality, ["cumulative", "delta"]);
121+
obj.Proxy.setTemporality(temporality);
122+
obj.PreferredAggregationTemporality = temporality;
144123
end
145124
end
146125
end

exporters/otlp/+opentelemetry/+exporters/+otlp/OtlpHttpMetricExporter.m

Lines changed: 63 additions & 88 deletions
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,14 @@
55

66
% Copyright 2023 The MathWorks, Inc.
77

8-
properties (SetAccess=immutable)
9-
Endpoint (1,1) string % Export destination
10-
Format (1,1) string % Data format, JSON or binary
11-
JsonBytesMapping (1,1) string % What to convert JSON bytes to
12-
UseJsonName (1,1) logical % Whether to use JSON name of protobuf field to set the key of JSON
13-
Timeout (1,1) duration % Maximum time above which exports will abort
14-
HttpHeaders (1,1) dictionary % Additional HTTP headers
15-
PreferredAggregationTemporality (1,1) string % Preferred Aggregation Temporality
8+
properties
9+
Endpoint (1,1) string = "http://localhost:4318/v1/metrics" % Export destination
10+
Format (1,1) string = "JSON" % Data format, JSON or binary
11+
JsonBytesMapping (1,1) string = "hexId" % What to convert JSON bytes to
12+
UseJsonName (1,1) logical = false % Whether to use JSON name of protobuf field to set the key of JSON
13+
Timeout (1,1) duration = seconds(10) % Maximum time above which exports will abort
14+
HttpHeaders (1,1) dictionary = dictionary(string.empty, string.empty) % Additional HTTP headers
15+
PreferredAggregationTemporality (1,1) string = "cumulative" % Preferred Aggregation Temporality
1616
end
1717

1818
methods
@@ -47,98 +47,73 @@
4747
optionvalues
4848
end
4949

50+
51+
"libmexclass.opentelemetry.exporters.OtlpHttpMetricExporterProxy");
52+
5053
validnames = ["Endpoint", "Format", "JsonBytesMapping", ...
5154
"UseJsonName", "Timeout", "HttpHeaders", "PreferredAggregationTemporality"];
52-
% set default values to empty or negative
53-
endpoint = "";
54-
dataformat = "";
55-
jsonbytesmapping = "";
56-
usejsonname = false;
57-
timeout_millis = -1;
58-
headerkeys = string.empty();
59-
headervalues = string.empty();
60-
temporality = "";
6155
for i = 1:length(optionnames)
6256
namei = validatestring(optionnames{i}, validnames);
6357
valuei = optionvalues{i};
64-
if strcmp(namei, "Endpoint")
65-
if ~(isStringScalar(valuei) || (ischar(valuei) && isrow(valuei)))
66-
error("opentelemetry:exporters:otlp:OtlpHttpMetricExporter:EndpointNotScalarText", "Endpoint must be a scalar string.");
67-
end
68-
endpoint = string(valuei);
69-
elseif strcmp(namei, "Format")
70-
dataformat = validatestring(valuei, ["JSON", "binary"]);
71-
elseif strcmp(namei, "JsonBytesMapping")
72-
jsonbytesmapping = validatestring(valuei, ["hex", "hexId", "base64"]);
73-
elseif strcmp(namei, "UseJsonName")
74-
if ~((islogical(valuei) || isnumeric(valuei)) && isscalar(valuei))
75-
error("opentelemetry:exporters:otlp:OtlpHttpMetricExporter:UseJsonNameNotScalarLogical", "UseJsonName must be a scalar logical.")
76-
end
77-
usejsonname = logical(valuei);
78-
elseif strcmp(namei, "Timeout")
79-
if ~(isduration(valuei) && isscalar(valuei))
80-
error("opentelemetry:exporters:otlp:OtlpHttpMetricExporter:TimeoutNotScalarDuration", "Timeout must be a scalar duration.");
81-
end
82-
timeout = valuei;
83-
timeout_millis = milliseconds(timeout);
84-
elseif strcmp(namei, "HttpHeaders")
85-
if ~isa(valuei, "dictionary")
86-
error("opentelemetry:exporters:otlp:OtlpHttpMetricExporter:HttpHeadersNotDictionary", "HttpHeaders input must be a dictionary.");
87-
end
88-
httpheaders = valuei;
89-
headerkeys = keys(valuei);
90-
headervalues = values(valuei);
91-
if ~isstring(headervalues)
92-
error("opentelemetry:exporters:otlp:OtlpHttpMetricExporter:HttpHeadersNonStringValues", "HttpHeaders dictionary values must be strings.")
93-
end
94-
elseif strcmp(namei, "PreferredAggregationTemporality")
95-
temporality = validatestring(valuei, ["Cumulative", "Delta"]);
96-
end
97-
58+
obj.(namei) = valuei;
9859
end
99-
100-
101-
"libmexclass.opentelemetry.exporters.OtlpHttpMetricExporterProxy", ...
102-
endpoint, dataformat, jsonbytesmapping, usejsonname, ...
103-
timeout_millis, headerkeys, headervalues, temporality);
60+
end
10461

105-
% populate immutable properties
106-
if endpoint == "" || dataformat == "" || jsonbytesmapping == "" || ...
107-
timeout_millis < 0
108-
[defaultendpoint, defaultformat, defaultmapping, defaultmillis, defaulttemporality] = ...
109-
getDefaultOptionValues(obj);
110-
end
111-
if endpoint == "" % not specified, use default value
112-
obj.Endpoint = defaultendpoint;
113-
else
114-
obj.Endpoint = endpoint;
62+
function obj = set.Endpoint(obj, ep)
63+
if ~(isStringScalar(ep) || (ischar(ep) && isrow(ep)))
64+
error("opentelemetry:exporters:otlp:OtlpHttpMetricExporter:EndpointNotScalarText", "Endpoint must be a scalar string.");
11565
end
116-
if dataformat == "" % not specified, use default value
117-
obj.Format = defaultformat;
118-
else
119-
obj.Format = dataformat;
120-
end
121-
if jsonbytesmapping == "" % not specified, use default value
122-
obj.JsonBytesMapping = defaultmapping;
123-
else
124-
obj.JsonBytesMapping = jsonbytesmapping;
66+
ep = string(ep);
67+
obj.Proxy.setEndpoint(ep);
68+
obj.Endpoint = ep;
69+
end
70+
71+
function obj = set.Format(obj, newformat)
72+
newformat = validatestring(newformat, ["JSON", "binary"]);
73+
obj.Proxy.setFormat(newformat);
74+
obj.Format = newformat;
75+
end
76+
77+
function obj = set.JsonBytesMapping(obj, jbm)
78+
jbm = validatestring(jbm, ["hex", "hexId", "base64"]);
79+
obj.Proxy.setJsonBytesMapping(jbm);
80+
obj.JsonBytesMapping = jbm;
81+
end
82+
83+
function obj = set.UseJsonName(obj, ujn)
84+
if ~((islogical(ujn) || isnumeric(ujn)) && isscalar(ujn))
85+
error("opentelemetry:exporters:otlp:OtlpHttpMetricExporter:UseJsonNameNotScalarLogical", "UseJsonName must be a scalar logical.")
12586
end
126-
obj.UseJsonName = usejsonname;
127-
if timeout_millis < 0 % not specified, use default value
128-
obj.Timeout = milliseconds(defaultmillis);
129-
else
130-
obj.Timeout = timeout;
87+
ujn = logical(ujn);
88+
obj.Proxy.setUseJsonName(ujn);
89+
obj.UseJsonName = ujn;
90+
end
91+
92+
function obj = set.Timeout(obj, timeout)
93+
if ~(isduration(timeout) && isscalar(timeout))
94+
error("opentelemetry:exporters:otlp:OtlpHttpMetricExporter:TimeoutNotScalarDuration", "Timeout must be a scalar duration.");
13195
end
132-
if isempty(headerkeys) % not specified, return empty dictionary
133-
obj.HttpHeaders = dictionary(headerkeys, headervalues);
134-
else
135-
obj.HttpHeaders = httpheaders;
96+
obj.Proxy.setTimeout(milliseconds(timeout));
97+
obj.Timeout = timeout;
98+
end
99+
100+
function obj = set.HttpHeaders(obj, httpheaders)
101+
if ~isa(httpheaders, "dictionary")
102+
error("opentelemetry:exporters:otlp:OtlpHttpMetricExporter:HttpHeadersNotDictionary", "HttpHeaders input must be a dictionary.");
136103
end
137-
if temporality == ""
138-
obj.PreferredAggregationTemporality = defaulttemporality;
139-
else
140-
obj.PreferredAggregationTemporality = temporality;
104+
headerkeys = keys(httpheaders);
105+
headervalues = values(httpheaders);
106+
if ~isstring(headervalues)
107+
error("opentelemetry:exporters:otlp:OtlpHttpMetricExporter:HttpHeadersNonStringValues", "HttpHeaders dictionary values must be strings.")
141108
end
109+
obj.Proxy.setHttpHeaders(headerkeys, headervalues);
110+
obj.HttpHeaders = httpheaders;
111+
end
112+
113+
function obj = set.PreferredAggregationTemporality(obj, temporality)
114+
temporality = validatestring(temporality, ["cumulative", "delta"]);
115+
obj.Proxy.setTemporality(temporality);
116+
obj.PreferredAggregationTemporality = temporality;
142117
end
143118
end
144119
end

0 commit comments

Comments
 (0)