Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Metrics merge #58

Merged
merged 7 commits into from
Nov 13, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 2 additions & 3 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -187,8 +187,8 @@ if(WITH_OTLP_GRPC)
endif()
endif()

set(TRACE_API_INCLUDE_DIR ${CMAKE_CURRENT_SOURCE_DIR}/api/trace/include)
set(METRICS_API_INCLUDE_DIR ${CMAKE_CURRENT_SOURCE_DIR}/api/metrics/include)
set(TRACE_API_INCLUDE_DIR ${CMAKE_CURRENT_SOURCE_DIR}/api/trace/include)
set(METRICS_API_INCLUDE_DIR ${CMAKE_CURRENT_SOURCE_DIR}/api/metrics/include)
set(CONTEXT_API_INCLUDE_DIR ${CMAKE_CURRENT_SOURCE_DIR}/api/context/include)
set(BAGGAGE_API_INCLUDE_DIR ${CMAKE_CURRENT_SOURCE_DIR}/api/baggage/include)
set(COMMON_API_INCLUDE_DIR ${CMAKE_CURRENT_SOURCE_DIR}/api/common/include)
Expand Down Expand Up @@ -409,7 +409,6 @@ set(OTLP_GRPC_EXPORTER_MATLAB_SOURCES
${CMAKE_CURRENT_SOURCE_DIR}/exporters/otlp/+opentelemetry/+exporters/+otlp/OtlpGrpcSpanExporter.m
${CMAKE_CURRENT_SOURCE_DIR}/exporters/otlp/+opentelemetry/+exporters/+otlp/OtlpGrpcMetricExporter.m)


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

install(DIRECTORY ${TRACE_API_MATLAB_SOURCES} DESTINATION .)
Expand Down
10 changes: 7 additions & 3 deletions api/trace/+opentelemetry/+trace/Span.m
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,11 @@ function setStatus(obj, status, description)
% new status is not valid, ignore
return
end
description = opentelemetry.common.mustBeScalarString(description);
if nargin < 3
description = "";
else
description = opentelemetry.common.mustBeScalarString(description);
end
obj.Proxy.setStatus(status, description);
end

Expand Down Expand Up @@ -181,8 +185,7 @@ function setStatus(obj, status, description)
function attrs = processAttributes(attrsin)
import opentelemetry.trace.Span.processAttribute

nin = length(attrsin);
if nin == 1 && isa(attrsin{1}, "dictionary")
if isscalar(attrsin) && isa(attrsin{1}, "dictionary")
% dictionary case
attrtbl = entries(attrsin{1});
nattr = height(attrtbl);
Expand All @@ -203,6 +206,7 @@ function setStatus(obj, status, description)
attrs = attrs(:);
else
% NV pairs
nin = length(attrsin);
if rem(nin,2) ~= 0
% Mismatched name-value pairs. Ignore all attributes.
attrs = cell(1,0);
Expand Down
146 changes: 63 additions & 83 deletions exporters/otlp/+opentelemetry/+exporters/+otlp/OtlpGrpcSpanExporter.m
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,13 @@

% Copyright 2023 The MathWorks, Inc.

properties (SetAccess=immutable)
Endpoint (1,1) string % Export destination
UseCredentials (1,1) logical % Whether to use SSL credentials
CertificatePath (1,1) string % Path to .pem file for SSL encryption
CertificateString (1,1) string % In-memory string representation of .pem file for SSL encryption
Timeout (1,1) duration % Maximum time above which exports will abort
HttpHeaders (1,1) dictionary % Additional HTTP headers
properties
Endpoint (1,1) string = "http://localhost:4317" % Export destination
UseCredentials (1,1) logical = false % Whether to use SSL credentials
CertificatePath (1,1) string = "" % Path to .pem file for SSL encryption
CertificateString (1,1) string = "" % In-memory string representation of .pem file for SSL encryption
Timeout (1,1) duration = seconds(10) % Maximum time above which exports will abort
HttpHeaders (1,1) dictionary = dictionary(string.empty, string.empty) % Additional HTTP headers
end

methods
Expand Down Expand Up @@ -43,93 +43,73 @@
optionvalues
end

obj = [email protected](...
"libmexclass.opentelemetry.exporters.OtlpGrpcSpanExporterProxy");

validnames = ["Endpoint", "UseCredentials ", "CertificatePath", ...
"CertificateString", "Timeout", "HttpHeaders"];
% set default values to empty or negative
endpoint = "";
usessl = false;
certificatepath = "";
certificatestring = "";
timeout_millis = -1;
headerkeys = string.empty();
headervalues = string.empty();
for i = 1:length(optionnames)
namei = validatestring(optionnames{i}, validnames);
valuei = optionvalues{i};
if strcmp(namei, "Endpoint")
if ~(isStringScalar(valuei) || (ischar(valuei) && isrow(valuei)))
error("opentelemetry:exporters:otlp:OtlpGrpcSpanExporter:EndpointNotScalarText", "Endpoint must be a scalar string.");
end
endpoint = string(valuei);
elseif strcmp(namei, "UseCredentials ")
if ~((islogical(valuei) || isnumeric(valuei)) && isscalar(valuei))
error("opentelemetry:exporters:otlp:OtlpGrpcSpanExporter:UseCredentialsNotScalarLogical", "UseCredentials must be a scalar logical.")
end
usessl = logical(valuei);
elseif strcmp(namei, "CertificatePath")
if ~(isStringScalar(valuei) || (ischar(valuei) && isrow(valuei)))
error("opentelemetry:exporters:otlp:OtlpGrpcSpanExporter:CertificatePathNotScalarText", "CertificatePath must be a scalar string.");
end
certificatepath = string(valuei);
elseif strcmp(namei, "CertificateString")
if ~(isStringScalar(valuei) || (ischar(valuei) && isrow(valuei)))
error("opentelemetry:exporters:otlp:OtlpGrpcSpanExporter:CertificateStringNotScalarText", "CertificateString must be a scalar string.");
end
certificatestring = string(valuei);
elseif strcmp(namei, "Timeout")
if ~(isduration(valuei) && isscalar(valuei))
error("opentelemetry:exporters:otlp:OtlpGrpcSpanExporter:TimeoutNotScalarDuration", "Timeout must be a scalar duration.");
end
timeout = valuei;
timeout_millis = milliseconds(timeout);
else % HttpHeaders
if ~isa(valuei, "dictionary")
error("opentelemetry:exporters:otlp:OtlpGrpcSpanExporter:HttpHeadersNotDictionary", "HttpHeaders input must be a dictionary.");
end
httpheaders = valuei;
headerkeys = keys(valuei);
headervalues = values(valuei);
if ~isstring(headervalues)
error("opentelemetry:exporters:otlp:OtlpGrpcSpanExporter:HttpHeadersNonStringValues", "HttpHeaders dictionary values must be strings.")
end
end
obj.(namei) = valuei;
end

obj = [email protected](...
"libmexclass.opentelemetry.exporters.OtlpGrpcSpanExporterProxy", ...
endpoint, usessl, certificatepath, certificatestring, ...
timeout_millis, headerkeys, headervalues);
end

% populate immutable properties
[defaultendpoint, defaultcertpath, defaultcertstring, defaultmillis] = ...
getDefaultOptionValues(obj);
if endpoint == "" % not specified, use default value
obj.Endpoint = defaultendpoint;
else
obj.Endpoint = endpoint;
end
obj.UseCredentials = usessl;
if certificatepath == "" % not specified, use default value
obj.CertificatePath = defaultcertpath;
else
obj.CertificatePath = certificatepath;
function obj = set.Endpoint(obj, ep)
if ~(isStringScalar(ep) || (ischar(ep) && isrow(ep)))
error("opentelemetry:exporters:otlp:OtlpGrpcSpanExporter:EndpointNotScalarText", "Endpoint must be a scalar string.");
end
if certificatestring == "" % not specified, use default value
obj.CertificateString = defaultcertstring;
else
obj.CertificateString = certificatestring;
ep = string(ep);
obj.Proxy.setEndpoint(ep);
obj.Endpoint = ep;
end

function obj = set.UseCredentials(obj, uc)
if ~((islogical(uc) || isnumeric(uc)) && isscalar(uc))
error("opentelemetry:exporters:otlp:OtlpGrpcSpanExporter:UseCredentialsNotScalarLogical", "UseCredentials must be a scalar logical.")
end
uc = logical(uc);
obj.Proxy.setUseCredentials(uc);
obj.UseCredentials = uc;
end

function obj = set.CertificatePath(obj, certpath)
if ~(isStringScalar(certpath) || (ischar(certpath) && isrow(certpath)))
error("opentelemetry:exporters:otlp:OtlpGrpcSpanExporter:CertificatePathNotScalarText", "CertificatePath must be a scalar string.");
end
if timeout_millis < 0 % not specified, use default value
obj.Timeout = milliseconds(defaultmillis);
else
obj.Timeout = timeout;
certpath = string(certpath);
obj.Proxy.setCertificatePath(certpath);
obj.CertificatePath = certpath;
end

function obj = set.CertificateString(obj, certstr)
if ~(isStringScalar(certstr) || (ischar(certstr) && isrow(certstr)))
error("opentelemetry:exporters:otlp:OtlpGrpcSpanExporter:CertificateStringNotScalarText", "CertificateString must be a scalar string.");
end
certstr = string(certstr);
obj.Proxy.setCertificateString(certstr);
obj.CertificateString = certstr;
end

function obj = set.Timeout(obj, timeout)
if ~(isduration(timeout) && isscalar(timeout))
error("opentelemetry:exporters:otlp:OtlpGrpcSpanExporter:TimeoutNotScalarDuration", "Timeout must be a scalar duration.");
end
obj.Proxy.setTimeout(milliseconds(timeout));
obj.Timeout = timeout;
end

function obj = set.HttpHeaders(obj, httpheaders)
if ~isa(httpheaders, "dictionary")
error("opentelemetry:exporters:otlp:OtlpGrpcSpanExporter:HttpHeadersNotDictionary", "HttpHeaders input must be a dictionary.");
end
if isempty(headerkeys) % not specified, return empty dictionary
obj.HttpHeaders = dictionary(headerkeys, headervalues);
else
obj.HttpHeaders = httpheaders;
headerkeys = keys(httpheaders);
headervalues = values(httpheaders);
if ~isstring(headervalues)
error("opentelemetry:exporters:otlp:OtlpGrpcSpanExporter:HttpHeadersNonStringValues", "HttpHeaders dictionary values must be strings.")
end

obj.Proxy.setHttpHeaders(headerkeys, headervalues);
obj.HttpHeaders = httpheaders;
end
end
end
134 changes: 56 additions & 78 deletions exporters/otlp/+opentelemetry/+exporters/+otlp/OtlpHttpSpanExporter.m
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,13 @@

% Copyright 2023 The MathWorks, Inc.

properties (SetAccess=immutable)
Endpoint (1,1) string % Export destination
Format (1,1) string % Data format, JSON or binary
JsonBytesMapping (1,1) string % What to convert JSON bytes to
UseJsonName (1,1) logical % Whether to use JSON name of protobuf field to set the key of JSON
Timeout (1,1) duration % Maximum time above which exports will abort
HttpHeaders (1,1) dictionary % Additional HTTP headers
properties
Endpoint (1,1) string = "http://localhost:4318/v1/traces" % Export destination
Format (1,1) string = "JSON" % Data format, JSON or binary
JsonBytesMapping (1,1) string = "hexId" % What to convert JSON bytes to
UseJsonName (1,1) logical = false % Whether to use JSON name of protobuf field to set the key of JSON
Timeout (1,1) duration = seconds(10) % Maximum time above which exports will abort
HttpHeaders (1,1) dictionary = dictionary(string.empty, string.empty) % Additional HTTP headers
end

methods
Expand Down Expand Up @@ -43,89 +43,67 @@
optionvalues
end

obj = [email protected](...
"libmexclass.opentelemetry.exporters.OtlpHttpSpanExporterProxy");

validnames = ["Endpoint", "Format", "JsonBytesMapping", ...
"UseJsonName", "Timeout", "HttpHeaders"];
% set default values to empty or negative
endpoint = "";
dataformat = "";
jsonbytesmapping = "";
usejsonname = false;
timeout_millis = -1;
headerkeys = string.empty();
headervalues = string.empty();
for i = 1:length(optionnames)
namei = validatestring(optionnames{i}, validnames);
valuei = optionvalues{i};
if strcmp(namei, "Endpoint")
if ~(isStringScalar(valuei) || (ischar(valuei) && isrow(valuei)))
error("opentelemetry:exporters:otlp:OtlpHttpSpanExporter:EndpointNotScalarText", "Endpoint must be a scalar string.");
end
endpoint = string(valuei);
elseif strcmp(namei, "Format")
dataformat = validatestring(valuei, ["JSON", "binary"]);
elseif strcmp(namei, "JsonBytesMapping")
jsonbytesmapping = validatestring(valuei, ["hex", "hexId", "base64"]);
elseif strcmp(namei, "UseJsonName")
if ~((islogical(valuei) || isnumeric(valuei)) && isscalar(valuei))
error("opentelemetry:exporters:otlp:OtlpHttpSpanExporter:UseJsonNameNotScalarLogical", "UseJsonName must be a scalar logical.")
end
usejsonname = logical(valuei);
elseif strcmp(namei, "Timeout")
if ~(isduration(valuei) && isscalar(valuei))
error("opentelemetry:exporters:otlp:OtlpHttpSpanExporter:TimeoutNotScalarDuration", "Timeout must be a scalar duration.");
end
timeout = valuei;
timeout_millis = milliseconds(timeout);
else % HttpHeaders
if ~isa(valuei, "dictionary")
error("opentelemetry:exporters:otlp:OtlpHttpSpanExporter:HttpHeadersNotDictionary", "HttpHeaders input must be a dictionary.");
end
httpheaders = valuei;
headerkeys = keys(valuei);
headervalues = values(valuei);
if ~isstring(headervalues)
error("opentelemetry:exporters:otlp:OtlpHttpSpanExporter:HttpHeadersNonStringValues", "HttpHeaders dictionary values must be strings.")
end
end
obj.(namei) = valuei;
end

obj = [email protected](...
"libmexclass.opentelemetry.exporters.OtlpHttpSpanExporterProxy", ...
endpoint, dataformat, jsonbytesmapping, usejsonname, ...
timeout_millis, headerkeys, headervalues);
end

% populate immutable properties
if endpoint == "" || dataformat == "" || jsonbytesmapping == "" || ...
timeout_millis < 0
[defaultendpoint, defaultformat, defaultmapping, defaultmillis] = ...
getDefaultOptionValues(obj);
function obj = set.Endpoint(obj, ep)
if ~(isStringScalar(ep) || (ischar(ep) && isrow(ep)))
error("opentelemetry:exporters:otlp:OtlpHttpSpanExporter:EndpointNotScalarText", "Endpoint must be a scalar string.");
end
if endpoint == "" % not specified, use default value
obj.Endpoint = defaultendpoint;
else
obj.Endpoint = endpoint;
end
if dataformat == "" % not specified, use default value
obj.Format = defaultformat;
else
obj.Format = dataformat;
ep = string(ep);
obj.Proxy.setEndpoint(ep);
obj.Endpoint = ep;
end

function obj = set.Format(obj, newformat)
newformat = validatestring(newformat, ["JSON", "binary"]);
obj.Proxy.setFormat(newformat);
obj.Format = newformat;
end

function obj = set.JsonBytesMapping(obj, jbm)
jbm = validatestring(jbm, ["hex", "hexId", "base64"]);
obj.Proxy.setJsonBytesMapping(jbm);
obj.JsonBytesMapping = jbm;
end

function obj = set.UseJsonName(obj, ujn)
if ~((islogical(ujn) || isnumeric(ujn)) && isscalar(ujn))
error("opentelemetry:exporters:otlp:OtlpHttpSpanExporter:UseJsonNameNotScalarLogical", "UseJsonName must be a scalar logical.")
end
if jsonbytesmapping == "" % not specified, use default value
obj.JsonBytesMapping = defaultmapping;
else
obj.JsonBytesMapping = jsonbytesmapping;
ujn = logical(ujn);
obj.Proxy.setUseJsonName(ujn);
obj.UseJsonName = ujn;
end

function obj = set.Timeout(obj, timeout)
if ~(isduration(timeout) && isscalar(timeout))
error("opentelemetry:exporters:otlp:OtlpHttpSpanExporter:TimeoutNotScalarDuration", "Timeout must be a scalar duration.");
end
obj.UseJsonName = usejsonname;
if timeout_millis < 0 % not specified, use default value
obj.Timeout = milliseconds(defaultmillis);
else
obj.Timeout = timeout;
obj.Proxy.setTimeout(milliseconds(timeout));
obj.Timeout = timeout;
end

function obj = set.HttpHeaders(obj, httpheaders)
if ~isa(httpheaders, "dictionary")
error("opentelemetry:exporters:otlp:OtlpHttpSpanExporter:HttpHeadersNotDictionary", "HttpHeaders input must be a dictionary.");
end
if isempty(headerkeys) % not specified, return empty dictionary
obj.HttpHeaders = dictionary(headerkeys, headervalues);
else
obj.HttpHeaders = httpheaders;
headerkeys = keys(httpheaders);
headervalues = values(httpheaders);
if ~isstring(headervalues)
error("opentelemetry:exporters:otlp:OtlpHttpSpanExporter:HttpHeadersNonStringValues", "HttpHeaders dictionary values must be strings.")
end
obj.Proxy.setHttpHeaders(headerkeys, headervalues);
obj.HttpHeaders = httpheaders;
end
end
end
Loading