Skip to content

Commit 1e27933

Browse files
committed
refactor exporter code to enable more code sharing and remove duplications
1 parent 9222d37 commit 1e27933

File tree

11 files changed

+149
-123
lines changed

11 files changed

+149
-123
lines changed

CMakeLists.txt

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -399,15 +399,18 @@ set(COMMON_API_MATLAB_SOURCES ${CMAKE_CURRENT_SOURCE_DIR}/api/common/+openteleme
399399
set(TRACE_SDK_MATLAB_SOURCES ${CMAKE_CURRENT_SOURCE_DIR}/sdk/trace/+opentelemetry)
400400
set(METRICS_SDK_MATLAB_SOURCES ${CMAKE_CURRENT_SOURCE_DIR}/sdk/metrics/+opentelemetry)
401401
set(COMMON_SDK_MATLAB_SOURCES ${CMAKE_CURRENT_SOURCE_DIR}/sdk/common/+opentelemetry)
402-
set(DEFAULT_EXPORTER_MATLAB_SOURCES
402+
set(EXPORTER_MATLAB_SOURCES
403403
${CMAKE_CURRENT_SOURCE_DIR}/exporters/otlp/+opentelemetry/+exporters/+otlp/defaultSpanExporter.m
404-
${CMAKE_CURRENT_SOURCE_DIR}/exporters/otlp/+opentelemetry/+exporters/+otlp/defaultMetricExporter.m)
404+
${CMAKE_CURRENT_SOURCE_DIR}/exporters/otlp/+opentelemetry/+exporters/+otlp/defaultMetricExporter.m
405+
${CMAKE_CURRENT_SOURCE_DIR}/exporters/otlp/+opentelemetry/+exporters/+otlp/OtlpValidator.m)
405406
set(OTLP_HTTP_EXPORTER_MATLAB_SOURCES
406407
${CMAKE_CURRENT_SOURCE_DIR}/exporters/otlp/+opentelemetry/+exporters/+otlp/OtlpHttpSpanExporter.m
407-
${CMAKE_CURRENT_SOURCE_DIR}/exporters/otlp/+opentelemetry/+exporters/+otlp/OtlpHttpMetricExporter.m)
408+
${CMAKE_CURRENT_SOURCE_DIR}/exporters/otlp/+opentelemetry/+exporters/+otlp/OtlpHttpMetricExporter.m
409+
${CMAKE_CURRENT_SOURCE_DIR}/exporters/otlp/+opentelemetry/+exporters/+otlp/OtlpHttpValidator.m)
408410
set(OTLP_GRPC_EXPORTER_MATLAB_SOURCES
409411
${CMAKE_CURRENT_SOURCE_DIR}/exporters/otlp/+opentelemetry/+exporters/+otlp/OtlpGrpcSpanExporter.m
410-
${CMAKE_CURRENT_SOURCE_DIR}/exporters/otlp/+opentelemetry/+exporters/+otlp/OtlpGrpcMetricExporter.m)
412+
${CMAKE_CURRENT_SOURCE_DIR}/exporters/otlp/+opentelemetry/+exporters/+otlp/OtlpGrpcMetricExporter.m
413+
${CMAKE_CURRENT_SOURCE_DIR}/exporters/otlp/+opentelemetry/+exporters/+otlp/OtlpGrpcValidator.m)
411414

412415
set(OTLP_EXPORTERS_DIR +opentelemetry/+exporters/+otlp)
413416

@@ -419,7 +422,7 @@ install(DIRECTORY ${COMMON_API_MATLAB_SOURCES} DESTINATION .)
419422
install(DIRECTORY ${TRACE_SDK_MATLAB_SOURCES} DESTINATION .)
420423
install(DIRECTORY ${METRICS_SDK_MATLAB_SOURCES} DESTINATION .)
421424
install(DIRECTORY ${COMMON_SDK_MATLAB_SOURCES} DESTINATION .)
422-
install(FILES ${DEFAULT_EXPORTER_MATLAB_SOURCES} DESTINATION ${OTLP_EXPORTERS_DIR})
425+
install(FILES ${EXPORTER_MATLAB_SOURCES} DESTINATION ${OTLP_EXPORTERS_DIR})
423426
if(WITH_OTLP_HTTP)
424427
install(FILES ${OTLP_HTTP_EXPORTER_MATLAB_SOURCES} DESTINATION ${OTLP_EXPORTERS_DIR})
425428
endif()

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

Lines changed: 10 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,10 @@
1212
CertificateString (1,1) string = "" % In-memory string representation of .pem file for SSL encryption
1313
Timeout (1,1) duration = seconds(10) % Maximum time above which exports will abort
1414
HttpHeaders (1,1) dictionary = dictionary(string.empty, string.empty) % Additional HTTP headers
15-
PreferredAggregationTemporality (1,1) string = "cumulative" % Preferred Aggregation Temporality
15+
end
16+
17+
properties (Constant)
18+
Validator = opentelemetry.exporters.otlp.OtlpGrpcValidator
1619
end
1720

1821
methods
@@ -60,66 +63,39 @@
6063
end
6164

6265
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);
66+
ep = obj.Validator.validateEndpoint(ep);
6767
obj.Proxy.setEndpoint(ep);
6868
obj.Endpoint = ep;
6969
end
7070

7171
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);
72+
uc = obj.Validator.validateUseCredentials(uc);
7673
obj.Proxy.setUseCredentials(uc);
7774
obj.UseCredentials = uc;
7875
end
7976

8077
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.");
83-
end
84-
certpath = string(certpath);
78+
certpath = obj.Validator.validateCertificatePath(certpath);
8579
obj.Proxy.setCertificatePath(certpath);
8680
obj.CertificatePath = certpath;
8781
end
8882

8983
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.");
92-
end
93-
certstr = string(certstr);
84+
certstr = obj.Validator.validateCertificateString(certstr);
9485
obj.Proxy.setCertificateString(certstr);
9586
obj.CertificateString = certstr;
9687
end
9788

9889
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.");
101-
end
90+
obj.Validator.validateTimeout(timeout);
10291
obj.Proxy.setTimeout(milliseconds(timeout));
10392
obj.Timeout = timeout;
10493
end
10594

10695
function obj = set.HttpHeaders(obj, httpheaders)
107-
if ~isa(httpheaders, "dictionary")
108-
error("opentelemetry:exporters:otlp:OtlpGrpcMetricExporter:HttpHeadersNotDictionary", "HttpHeaders input must be a dictionary.");
109-
end
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.")
114-
end
96+
[headerkeys, headervalues] = obj.Validator.validateHttpHeaders(httpheaders);
11597
obj.Proxy.setHttpHeaders(headerkeys, headervalues);
11698
obj.HttpHeaders = httpheaders;
11799
end
118-
119-
function obj = set.PreferredAggregationTemporality(obj, temporality)
120-
temporality = validatestring(temporality, ["cumulative", "delta"]);
121-
obj.Proxy.setTemporality(temporality);
122-
obj.PreferredAggregationTemporality = temporality;
123-
end
124100
end
125101
end

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

Lines changed: 10 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,10 @@
1414
HttpHeaders (1,1) dictionary = dictionary(string.empty, string.empty) % Additional HTTP headers
1515
end
1616

17+
properties (Constant)
18+
Validator = opentelemetry.exporters.otlp.OtlpGrpcValidator
19+
end
20+
1721
methods
1822
function obj = OtlpGrpcSpanExporter(optionnames, optionvalues)
1923
% OtlpGrpcSpanExporter exports spans in OpenTelemetry Protocol format via gRPC.
@@ -56,58 +60,37 @@
5660
end
5761

5862
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.");
61-
end
62-
ep = string(ep);
63+
ep = obj.Validator.validateEndpoint(ep);
6364
obj.Proxy.setEndpoint(ep);
6465
obj.Endpoint = ep;
6566
end
6667

6768
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);
69+
uc = obj.Validator.validateUseCredentials(uc);
7270
obj.Proxy.setUseCredentials(uc);
7371
obj.UseCredentials = uc;
7472
end
7573

7674
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.");
79-
end
80-
certpath = string(certpath);
75+
certpath = obj.Validator.validateCertificatePath(certpath);
8176
obj.Proxy.setCertificatePath(certpath);
8277
obj.CertificatePath = certpath;
8378
end
8479

8580
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);
81+
certstr = obj.Validator.validateCertificateString(certstr);
9082
obj.Proxy.setCertificateString(certstr);
9183
obj.CertificateString = certstr;
9284
end
9385

9486
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
87+
obj.Validator.validateTimeout(timeout);
9888
obj.Proxy.setTimeout(milliseconds(timeout));
9989
obj.Timeout = timeout;
10090
end
10191

10292
function obj = set.HttpHeaders(obj, httpheaders)
103-
if ~isa(httpheaders, "dictionary")
104-
error("opentelemetry:exporters:otlp:OtlpGrpcSpanExporter:HttpHeadersNotDictionary", "HttpHeaders input must be a dictionary.");
105-
end
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.")
110-
end
93+
[headerkeys, headervalues] = obj.Validator.validateHttpHeaders(httpheaders);
11194
obj.Proxy.setHttpHeaders(headerkeys, headervalues);
11295
obj.HttpHeaders = httpheaders;
11396
end
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
classdef OtlpGrpcValidator < opentelemetry.exporters.otlp.OtlpValidator
2+
% OtlpGrpcValidator Validate options inputs for OtlpGrpcSpanExporter and
3+
% OtlpGrpcMetricExporter
4+
5+
% Copyright 2023 The MathWorks, Inc.
6+
7+
methods (Static)
8+
function uc = validateUseCredentials(uc)
9+
if ~((islogical(uc) || isnumeric(uc)) && isscalar(uc))
10+
error("opentelemetry:exporters:otlp:OtlpGrpcValidator:UseCredentialsNotScalarLogical", ...
11+
"UseCredentials must be a scalar logical.")
12+
end
13+
uc = logical(uc);
14+
end
15+
16+
function certpath = validateCertificatePath(certpath)
17+
if ~(isStringScalar(certpath) || (ischar(certpath) && isrow(certpath)))
18+
error("opentelemetry:exporters:otlp:OtlpGrpcValidator:CertificatePathNotScalarText", ...
19+
"CertificatePath must be a scalar string.");
20+
end
21+
certpath = string(certpath);
22+
end
23+
24+
function certstr = validateCertificateString(certstr)
25+
if ~(isStringScalar(certstr) || (ischar(certstr) && isrow(certstr)))
26+
error("opentelemetry:exporters:otlp:OtlpGrpcValidator:CertificateStringNotScalarText", ...
27+
"CertificateString must be a scalar string.");
28+
end
29+
certstr = string(certstr);
30+
end
31+
end
32+
end

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

Lines changed: 10 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,10 @@
1212
UseJsonName (1,1) logical = false % Whether to use JSON name of protobuf field to set the key of JSON
1313
Timeout (1,1) duration = seconds(10) % Maximum time above which exports will abort
1414
HttpHeaders (1,1) dictionary = dictionary(string.empty, string.empty) % Additional HTTP headers
15-
PreferredAggregationTemporality (1,1) string = "cumulative" % Preferred Aggregation Temporality
15+
end
16+
17+
properties (Constant)
18+
Validator = opentelemetry.exporters.otlp.OtlpHttpValidator
1619
end
1720

1821
methods
@@ -60,60 +63,39 @@
6063
end
6164

6265
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.");
65-
end
66-
ep = string(ep);
66+
ep = obj.Validator.validateEndpoint(ep);
6767
obj.Proxy.setEndpoint(ep);
6868
obj.Endpoint = ep;
6969
end
7070

7171
function obj = set.Format(obj, newformat)
72-
newformat = validatestring(newformat, ["JSON", "binary"]);
72+
newformat = obj.Validator.validateFormat(newformat);
7373
obj.Proxy.setFormat(newformat);
7474
obj.Format = newformat;
7575
end
7676

7777
function obj = set.JsonBytesMapping(obj, jbm)
78-
jbm = validatestring(jbm, ["hex", "hexId", "base64"]);
78+
jbm = obj.Validator.validateJsonBytesMapping(jbm);
7979
obj.Proxy.setJsonBytesMapping(jbm);
8080
obj.JsonBytesMapping = jbm;
8181
end
8282

8383
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.")
86-
end
87-
ujn = logical(ujn);
84+
ujn = obj.Validator.validateUseJsonName(ujn);
8885
obj.Proxy.setUseJsonName(ujn);
8986
obj.UseJsonName = ujn;
9087
end
9188

9289
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.");
95-
end
90+
obj.Validator.validateTimeout(timeout);
9691
obj.Proxy.setTimeout(milliseconds(timeout));
9792
obj.Timeout = timeout;
9893
end
9994

10095
function obj = set.HttpHeaders(obj, httpheaders)
101-
if ~isa(httpheaders, "dictionary")
102-
error("opentelemetry:exporters:otlp:OtlpHttpMetricExporter:HttpHeadersNotDictionary", "HttpHeaders input must be a dictionary.");
103-
end
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.")
108-
end
96+
[headerkeys, headervalues] = obj.Validator.validateHttpHeaders(httpheaders);
10997
obj.Proxy.setHttpHeaders(headerkeys, headervalues);
11098
obj.HttpHeaders = httpheaders;
11199
end
112-
113-
function obj = set.PreferredAggregationTemporality(obj, temporality)
114-
temporality = validatestring(temporality, ["cumulative", "delta"]);
115-
obj.Proxy.setTemporality(temporality);
116-
obj.PreferredAggregationTemporality = temporality;
117-
end
118100
end
119101
end

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

Lines changed: 10 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,10 @@
1414
HttpHeaders (1,1) dictionary = dictionary(string.empty, string.empty) % Additional HTTP headers
1515
end
1616

17+
properties (Constant)
18+
Validator = opentelemetry.exporters.otlp.OtlpHttpValidator
19+
end
20+
1721
methods
1822
function obj = OtlpHttpSpanExporter(optionnames, optionvalues)
1923
% OtlpHttpSpanExporter exports spans in OpenTelemetry Protocol format via HTTP.
@@ -56,52 +60,37 @@
5660
end
5761

5862
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.");
61-
end
62-
ep = string(ep);
63+
ep = obj.Validator.validateEndpoint(ep);
6364
obj.Proxy.setEndpoint(ep);
6465
obj.Endpoint = ep;
6566
end
6667

6768
function obj = set.Format(obj, newformat)
68-
newformat = validatestring(newformat, ["JSON", "binary"]);
69+
newformat = obj.Validator.validateFormat(newformat);
6970
obj.Proxy.setFormat(newformat);
7071
obj.Format = newformat;
7172
end
7273

7374
function obj = set.JsonBytesMapping(obj, jbm)
74-
jbm = validatestring(jbm, ["hex", "hexId", "base64"]);
75+
jbm = obj.Validator.validateJsonBytesMapping(jbm);
7576
obj.Proxy.setJsonBytesMapping(jbm);
7677
obj.JsonBytesMapping = jbm;
7778
end
7879

7980
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.")
82-
end
83-
ujn = logical(ujn);
81+
ujn = obj.Validator.validateUseJsonName(ujn);
8482
obj.Proxy.setUseJsonName(ujn);
8583
obj.UseJsonName = ujn;
8684
end
8785

8886
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.");
91-
end
87+
obj.Validator.validateTimeout(timeout);
9288
obj.Proxy.setTimeout(milliseconds(timeout));
9389
obj.Timeout = timeout;
9490
end
9591

9692
function obj = set.HttpHeaders(obj, httpheaders)
97-
if ~isa(httpheaders, "dictionary")
98-
error("opentelemetry:exporters:otlp:OtlpHttpSpanExporter:HttpHeadersNotDictionary", "HttpHeaders input must be a dictionary.");
99-
end
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.")
104-
end
93+
[headerkeys, headervalues] = obj.Validator.validateHttpHeaders(httpheaders);
10594
obj.Proxy.setHttpHeaders(headerkeys, headervalues);
10695
obj.HttpHeaders = httpheaders;
10796
end
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
classdef OtlpHttpValidator < opentelemetry.exporters.otlp.OtlpValidator
2+
% OtlpHttpValidator Validate options inputs for OtlpHttpSpanExporter and
3+
% OtlpHttpMetricExporter
4+
5+
% Copyright 2023 The MathWorks, Inc.
6+
7+
methods (Static)
8+
function newformat = validateFormat(newformat)
9+
newformat = validatestring(newformat, ["JSON", "binary"]);
10+
end
11+
12+
function jbm = validateJsonBytesMapping(jbm)
13+
jbm = validatestring(jbm, ["hex", "hexId", "base64"]);
14+
end
15+
16+
function ujn = validateUseJsonName(ujn)
17+
if ~((islogical(ujn) || isnumeric(ujn)) && isscalar(ujn))
18+
error("opentelemetry:exporters:otlp:OtlpHttpValidator:UseJsonNameNotScalarLogical", "UseJsonName must be a scalar logical.")
19+
end
20+
ujn = logical(ujn);
21+
end
22+
end
23+
end

0 commit comments

Comments
 (0)