Skip to content

Commit ac78aac

Browse files
committed
Make properties of exporters, span processors, and samplers no longer read-only #36
1 parent b55ffdf commit ac78aac

File tree

16 files changed

+589
-372
lines changed

16 files changed

+589
-372
lines changed

exporters/otlp/+opentelemetry/+exporters/+otlp/OtlpGrpcSpanExporter.m

Lines changed: 63 additions & 83 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,13 @@
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
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
1515
end
1616

1717
methods
@@ -43,93 +43,73 @@
4343
optionvalues
4444
end
4545

46+
47+
"libmexclass.opentelemetry.exporters.OtlpGrpcSpanExporterProxy");
48+
4649
validnames = ["Endpoint", "UseCredentials ", "CertificatePath", ...
4750
"CertificateString", "Timeout", "HttpHeaders"];
48-
% set default values to empty or negative
49-
endpoint = "";
50-
usessl = false;
51-
certificatepath = "";
52-
certificatestring = "";
53-
timeout_millis = -1;
54-
headerkeys = string.empty();
55-
headervalues = string.empty();
5651
for i = 1:length(optionnames)
5752
namei = validatestring(optionnames{i}, validnames);
5853
valuei = optionvalues{i};
59-
if strcmp(namei, "Endpoint")
60-
if ~(isStringScalar(valuei) || (ischar(valuei) && isrow(valuei)))
61-
error("opentelemetry:exporters:otlp:OtlpGrpcSpanExporter:EndpointNotScalarText", "Endpoint must be a scalar string.");
62-
end
63-
endpoint = string(valuei);
64-
elseif strcmp(namei, "UseCredentials ")
65-
if ~((islogical(valuei) || isnumeric(valuei)) && isscalar(valuei))
66-
error("opentelemetry:exporters:otlp:OtlpGrpcSpanExporter:UseCredentialsNotScalarLogical", "UseCredentials must be a scalar logical.")
67-
end
68-
usessl = logical(valuei);
69-
elseif strcmp(namei, "CertificatePath")
70-
if ~(isStringScalar(valuei) || (ischar(valuei) && isrow(valuei)))
71-
error("opentelemetry:exporters:otlp:OtlpGrpcSpanExporter:CertificatePathNotScalarText", "CertificatePath must be a scalar string.");
72-
end
73-
certificatepath = string(valuei);
74-
elseif strcmp(namei, "CertificateString")
75-
if ~(isStringScalar(valuei) || (ischar(valuei) && isrow(valuei)))
76-
error("opentelemetry:exporters:otlp:OtlpGrpcSpanExporter:CertificateStringNotScalarText", "CertificateString must be a scalar string.");
77-
end
78-
certificatestring = string(valuei);
79-
elseif strcmp(namei, "Timeout")
80-
if ~(isduration(valuei) && isscalar(valuei))
81-
error("opentelemetry:exporters:otlp:OtlpGrpcSpanExporter:TimeoutNotScalarDuration", "Timeout must be a scalar duration.");
82-
end
83-
timeout = valuei;
84-
timeout_millis = milliseconds(timeout);
85-
else % HttpHeaders
86-
if ~isa(valuei, "dictionary")
87-
error("opentelemetry:exporters:otlp:OtlpGrpcSpanExporter:HttpHeadersNotDictionary", "HttpHeaders input must be a dictionary.");
88-
end
89-
httpheaders = valuei;
90-
headerkeys = keys(valuei);
91-
headervalues = values(valuei);
92-
if ~isstring(headervalues)
93-
error("opentelemetry:exporters:otlp:OtlpGrpcSpanExporter:HttpHeadersNonStringValues", "HttpHeaders dictionary values must be strings.")
94-
end
95-
end
54+
obj.(namei) = valuei;
9655
end
97-
98-
99-
"libmexclass.opentelemetry.exporters.OtlpGrpcSpanExporterProxy", ...
100-
endpoint, usessl, certificatepath, certificatestring, ...
101-
timeout_millis, headerkeys, headervalues);
56+
end
10257

103-
% populate immutable properties
104-
[defaultendpoint, defaultcertpath, defaultcertstring, defaultmillis] = ...
105-
getDefaultOptionValues(obj);
106-
if endpoint == "" % not specified, use default value
107-
obj.Endpoint = defaultendpoint;
108-
else
109-
obj.Endpoint = endpoint;
110-
end
111-
obj.UseCredentials = usessl;
112-
if certificatepath == "" % not specified, use default value
113-
obj.CertificatePath = defaultcertpath;
114-
else
115-
obj.CertificatePath = certificatepath;
58+
function obj = set.Endpoint(obj, ep)
59+
if ~(isStringScalar(ep) || (ischar(ep) && isrow(ep)))
60+
error("opentelemetry:exporters:otlp:OtlpGrpcSpanExporter:EndpointNotScalarText", "Endpoint must be a scalar string.");
11661
end
117-
if certificatestring == "" % not specified, use default value
118-
obj.CertificateString = defaultcertstring;
119-
else
120-
obj.CertificateString = certificatestring;
62+
ep = string(ep);
63+
obj.Proxy.setEndpoint(ep);
64+
obj.Endpoint = ep;
65+
end
66+
67+
function obj = set.UseCredentials(obj, uc)
68+
if ~((islogical(uc) || isnumeric(uc)) && isscalar(uc))
69+
error("opentelemetry:exporters:otlp:OtlpGrpcSpanExporter:UseCredentialsNotScalarLogical", "UseCredentials must be a scalar logical.")
70+
end
71+
uc = logical(uc);
72+
obj.Proxy.setUseCredentials(uc);
73+
obj.UseCredentials = uc;
74+
end
75+
76+
function obj = set.CertificatePath(obj, certpath)
77+
if ~(isStringScalar(certpath) || (ischar(certpath) && isrow(certpath)))
78+
error("opentelemetry:exporters:otlp:OtlpGrpcSpanExporter:CertificatePathNotScalarText", "CertificatePath must be a scalar string.");
12179
end
122-
if timeout_millis < 0 % not specified, use default value
123-
obj.Timeout = milliseconds(defaultmillis);
124-
else
125-
obj.Timeout = timeout;
80+
certpath = string(certpath);
81+
obj.Proxy.setCertificatePath(certpath);
82+
obj.CertificatePath = certpath;
83+
end
84+
85+
function obj = set.CertificateString(obj, certstr)
86+
if ~(isStringScalar(certstr) || (ischar(certstr) && isrow(certstr)))
87+
error("opentelemetry:exporters:otlp:OtlpGrpcSpanExporter:CertificateStringNotScalarText", "CertificateString must be a scalar string.");
88+
end
89+
certstr = string(certstr);
90+
obj.Proxy.setCertificateString(certstr);
91+
obj.CertificateString = certstr;
92+
end
93+
94+
function obj = set.Timeout(obj, timeout)
95+
if ~(isduration(timeout) && isscalar(timeout))
96+
error("opentelemetry:exporters:otlp:OtlpGrpcSpanExporter:TimeoutNotScalarDuration", "Timeout must be a scalar duration.");
97+
end
98+
obj.Proxy.setTimeout(milliseconds(timeout));
99+
obj.Timeout = timeout;
100+
end
101+
102+
function obj = set.HttpHeaders(obj, httpheaders)
103+
if ~isa(httpheaders, "dictionary")
104+
error("opentelemetry:exporters:otlp:OtlpGrpcSpanExporter:HttpHeadersNotDictionary", "HttpHeaders input must be a dictionary.");
126105
end
127-
if isempty(headerkeys) % not specified, return empty dictionary
128-
obj.HttpHeaders = dictionary(headerkeys, headervalues);
129-
else
130-
obj.HttpHeaders = httpheaders;
106+
headerkeys = keys(httpheaders);
107+
headervalues = values(httpheaders);
108+
if ~isstring(headervalues)
109+
error("opentelemetry:exporters:otlp:OtlpGrpcSpanExporter:HttpHeadersNonStringValues", "HttpHeaders dictionary values must be strings.")
131110
end
132-
111+
obj.Proxy.setHttpHeaders(headerkeys, headervalues);
112+
obj.HttpHeaders = httpheaders;
133113
end
134114
end
135115
end

exporters/otlp/+opentelemetry/+exporters/+otlp/OtlpHttpSpanExporter.m

Lines changed: 56 additions & 78 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,13 @@
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
8+
properties
9+
Endpoint (1,1) string = "http://localhost:4318/v1/traces" % 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
1515
end
1616

1717
methods
@@ -43,89 +43,67 @@
4343
optionvalues
4444
end
4545

46+
47+
"libmexclass.opentelemetry.exporters.OtlpHttpSpanExporterProxy");
48+
4649
validnames = ["Endpoint", "Format", "JsonBytesMapping", ...
4750
"UseJsonName", "Timeout", "HttpHeaders"];
48-
% set default values to empty or negative
49-
endpoint = "";
50-
dataformat = "";
51-
jsonbytesmapping = "";
52-
usejsonname = false;
53-
timeout_millis = -1;
54-
headerkeys = string.empty();
55-
headervalues = string.empty();
5651
for i = 1:length(optionnames)
5752
namei = validatestring(optionnames{i}, validnames);
5853
valuei = optionvalues{i};
59-
if strcmp(namei, "Endpoint")
60-
if ~(isStringScalar(valuei) || (ischar(valuei) && isrow(valuei)))
61-
error("opentelemetry:exporters:otlp:OtlpHttpSpanExporter:EndpointNotScalarText", "Endpoint must be a scalar string.");
62-
end
63-
endpoint = string(valuei);
64-
elseif strcmp(namei, "Format")
65-
dataformat = validatestring(valuei, ["JSON", "binary"]);
66-
elseif strcmp(namei, "JsonBytesMapping")
67-
jsonbytesmapping = validatestring(valuei, ["hex", "hexId", "base64"]);
68-
elseif strcmp(namei, "UseJsonName")
69-
if ~((islogical(valuei) || isnumeric(valuei)) && isscalar(valuei))
70-
error("opentelemetry:exporters:otlp:OtlpHttpSpanExporter:UseJsonNameNotScalarLogical", "UseJsonName must be a scalar logical.")
71-
end
72-
usejsonname = logical(valuei);
73-
elseif strcmp(namei, "Timeout")
74-
if ~(isduration(valuei) && isscalar(valuei))
75-
error("opentelemetry:exporters:otlp:OtlpHttpSpanExporter:TimeoutNotScalarDuration", "Timeout must be a scalar duration.");
76-
end
77-
timeout = valuei;
78-
timeout_millis = milliseconds(timeout);
79-
else % HttpHeaders
80-
if ~isa(valuei, "dictionary")
81-
error("opentelemetry:exporters:otlp:OtlpHttpSpanExporter:HttpHeadersNotDictionary", "HttpHeaders input must be a dictionary.");
82-
end
83-
httpheaders = valuei;
84-
headerkeys = keys(valuei);
85-
headervalues = values(valuei);
86-
if ~isstring(headervalues)
87-
error("opentelemetry:exporters:otlp:OtlpHttpSpanExporter:HttpHeadersNonStringValues", "HttpHeaders dictionary values must be strings.")
88-
end
89-
end
54+
obj.(namei) = valuei;
9055
end
91-
92-
93-
"libmexclass.opentelemetry.exporters.OtlpHttpSpanExporterProxy", ...
94-
endpoint, dataformat, jsonbytesmapping, usejsonname, ...
95-
timeout_millis, headerkeys, headervalues);
56+
end
9657

97-
% populate immutable properties
98-
if endpoint == "" || dataformat == "" || jsonbytesmapping == "" || ...
99-
timeout_millis < 0
100-
[defaultendpoint, defaultformat, defaultmapping, defaultmillis] = ...
101-
getDefaultOptionValues(obj);
58+
function obj = set.Endpoint(obj, ep)
59+
if ~(isStringScalar(ep) || (ischar(ep) && isrow(ep)))
60+
error("opentelemetry:exporters:otlp:OtlpHttpSpanExporter:EndpointNotScalarText", "Endpoint must be a scalar string.");
10261
end
103-
if endpoint == "" % not specified, use default value
104-
obj.Endpoint = defaultendpoint;
105-
else
106-
obj.Endpoint = endpoint;
107-
end
108-
if dataformat == "" % not specified, use default value
109-
obj.Format = defaultformat;
110-
else
111-
obj.Format = dataformat;
62+
ep = string(ep);
63+
obj.Proxy.setEndpoint(ep);
64+
obj.Endpoint = ep;
65+
end
66+
67+
function obj = set.Format(obj, newformat)
68+
newformat = validatestring(newformat, ["JSON", "binary"]);
69+
obj.Proxy.setFormat(newformat);
70+
obj.Format = newformat;
71+
end
72+
73+
function obj = set.JsonBytesMapping(obj, jbm)
74+
jbm = validatestring(jbm, ["hex", "hexId", "base64"]);
75+
obj.Proxy.setJsonBytesMapping(jbm);
76+
obj.JsonBytesMapping = jbm;
77+
end
78+
79+
function obj = set.UseJsonName(obj, ujn)
80+
if ~((islogical(ujn) || isnumeric(ujn)) && isscalar(ujn))
81+
error("opentelemetry:exporters:otlp:OtlpHttpSpanExporter:UseJsonNameNotScalarLogical", "UseJsonName must be a scalar logical.")
11282
end
113-
if jsonbytesmapping == "" % not specified, use default value
114-
obj.JsonBytesMapping = defaultmapping;
115-
else
116-
obj.JsonBytesMapping = jsonbytesmapping;
83+
ujn = logical(ujn);
84+
obj.Proxy.setUseJsonName(ujn);
85+
obj.UseJsonName = ujn;
86+
end
87+
88+
function obj = set.Timeout(obj, timeout)
89+
if ~(isduration(timeout) && isscalar(timeout))
90+
error("opentelemetry:exporters:otlp:OtlpHttpSpanExporter:TimeoutNotScalarDuration", "Timeout must be a scalar duration.");
11791
end
118-
obj.UseJsonName = usejsonname;
119-
if timeout_millis < 0 % not specified, use default value
120-
obj.Timeout = milliseconds(defaultmillis);
121-
else
122-
obj.Timeout = timeout;
92+
obj.Proxy.setTimeout(milliseconds(timeout));
93+
obj.Timeout = timeout;
94+
end
95+
96+
function obj = set.HttpHeaders(obj, httpheaders)
97+
if ~isa(httpheaders, "dictionary")
98+
error("opentelemetry:exporters:otlp:OtlpHttpSpanExporter:HttpHeadersNotDictionary", "HttpHeaders input must be a dictionary.");
12399
end
124-
if isempty(headerkeys) % not specified, return empty dictionary
125-
obj.HttpHeaders = dictionary(headerkeys, headervalues);
126-
else
127-
obj.HttpHeaders = httpheaders;
100+
headerkeys = keys(httpheaders);
101+
headervalues = values(httpheaders);
102+
if ~isstring(headervalues)
103+
error("opentelemetry:exporters:otlp:OtlpHttpSpanExporter:HttpHeadersNonStringValues", "HttpHeaders dictionary values must be strings.")
128104
end
105+
obj.Proxy.setHttpHeaders(headerkeys, headervalues);
106+
obj.HttpHeaders = httpheaders;
129107
end
130108
end
131109
end

exporters/otlp/include/opentelemetry-matlab/exporters/otlp/OtlpGrpcSpanExporterProxy.h

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,14 +17,24 @@ namespace libmexclass::opentelemetry::exporters {
1717
class OtlpGrpcSpanExporterProxy: public libmexclass::opentelemetry::sdk::SpanExporterProxy {
1818
public:
1919
OtlpGrpcSpanExporterProxy(otlp_exporter::OtlpGrpcExporterOptions options) : CppOptions(options) {
20-
REGISTER_METHOD(OtlpGrpcSpanExporterProxy, getDefaultOptionValues);
20+
REGISTER_METHOD(OtlpGrpcSpanExporterProxy, setEndpoint);
21+
REGISTER_METHOD(OtlpGrpcSpanExporterProxy, setUseCredentials);
22+
REGISTER_METHOD(OtlpGrpcSpanExporterProxy, setCertificatePath);
23+
REGISTER_METHOD(OtlpGrpcSpanExporterProxy, setCertificateString);
24+
REGISTER_METHOD(OtlpGrpcSpanExporterProxy, setTimeout);
25+
REGISTER_METHOD(OtlpGrpcSpanExporterProxy, setHttpHeaders);
2126
}
2227

2328
static libmexclass::proxy::MakeResult make(const libmexclass::proxy::FunctionArguments& constructor_arguments);
2429

2530
std::unique_ptr<trace_sdk::SpanExporter> getInstance() override;
2631

27-
void getDefaultOptionValues(libmexclass::proxy::method::Context& context);
32+
void setEndpoint(libmexclass::proxy::method::Context& context);
33+
void setUseCredentials(libmexclass::proxy::method::Context& context);
34+
void setCertificatePath(libmexclass::proxy::method::Context& context);
35+
void setCertificateString(libmexclass::proxy::method::Context& context);
36+
void setTimeout(libmexclass::proxy::method::Context& context);
37+
void setHttpHeaders(libmexclass::proxy::method::Context& context);
2838

2939
private:
3040
otlp_exporter::OtlpGrpcExporterOptions CppOptions;

0 commit comments

Comments
 (0)