|
| 1 | +classdef OtlpHttpMetricExporter < opentelemetry.sdk.metrics.MetricExporter |
| 2 | +% OtlpHttpMetricExporter exports Metrics in OpenTelemetry Protocol format via |
| 3 | +% HTTP. By default, it exports to the default address of the OpenTelemetry |
| 4 | +% Collector. |
| 5 | + |
| 6 | +% Copyright 2023 The MathWorks, Inc. |
| 7 | + |
| 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 | + end |
| 16 | + |
| 17 | + methods |
| 18 | + function obj = OtlpHttpMetricExporter(optionnames, optionvalues) |
| 19 | + % OtlpHttpMetricExporter exports Metrics in OpenTelemetry Protocol format via HTTP. |
| 20 | + % EXP = OPENTELEMETRY.EXPORTERS.OTLP.OTLPHTTPMetricEXPORTER |
| 21 | + % creates an exporter that uses default configurations. |
| 22 | + % |
| 23 | + % EXP = |
| 24 | + % OPENTELEMETRY.EXPORTERS.OTLP.OTLPHTTPMetricEXPORTER(PARAM1, |
| 25 | + % VALUE1, PARAM2, VALUE2, ...) specifies optional parameter |
| 26 | + % name/value pairs. Parameters are: |
| 27 | + % "Endpoint" - Endpoint to export to |
| 28 | + % "Format" - Data format: "JSON" (default) or "binary" |
| 29 | + % "JsonBytesMapping" - What to convert JSON bytes to. Supported |
| 30 | + % values are "hex", "hexId" (default), and |
| 31 | + % "base64". Default "hexId" |
| 32 | + % converts to base 64 except for IDs |
| 33 | + % which are converted to hexadecimals. |
| 34 | + % "UseJsonName" - Whether to use JSON name of protobuf |
| 35 | + % field to set the key of JSON |
| 36 | + % "Timeout" - Maximum time above which exports |
| 37 | + % will abort |
| 38 | + % "HTTPHeaders" - Additional HTTP Headers |
| 39 | + % |
| 40 | + % See also OPENTELEMETRY.EXPORTERS.OTLP.OTLPGRPCMetricEXPORTER |
| 41 | + arguments (Repeating) |
| 42 | + optionnames (1,:) {mustBeTextScalar} |
| 43 | + optionvalues |
| 44 | + end |
| 45 | + |
| 46 | + validnames = ["Endpoint", "Format", "JsonBytesMapping", ... |
| 47 | + "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(); |
| 56 | + for i = 1:length(optionnames) |
| 57 | + namei = validatestring(optionnames{i}, validnames); |
| 58 | + valuei = optionvalues{i}; |
| 59 | + if strcmp(namei, "Endpoint") |
| 60 | + if ~(isStringScalar(valuei) || (ischar(valuei) && isrow(valuei))) |
| 61 | + error("opentelemetry:exporters:otlp:OtlpHttpMetricExporter: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:OtlpHttpMetricExporter: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:OtlpHttpMetricExporter: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:OtlpHttpMetricExporter: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:OtlpHttpMetricExporter:HttpHeadersNonStringValues", "HttpHeaders dictionary values must be strings.") |
| 88 | + end |
| 89 | + end |
| 90 | + end |
| 91 | + |
| 92 | + |
| 93 | + "libmexclass.opentelemetry.exporters.OtlpHttpMetricExporterProxy", ... |
| 94 | + endpoint, dataformat, jsonbytesmapping, usejsonname, ... |
| 95 | + timeout_millis, headerkeys, headervalues); |
| 96 | + |
| 97 | + % populate immutable properties |
| 98 | + if endpoint == "" || dataformat == "" || jsonbytesmapping == "" || ... |
| 99 | + timeout_millis < 0 |
| 100 | + [defaultendpoint, defaultformat, defaultmapping, defaultmillis] = ... |
| 101 | + getDefaultOptionValues(obj); |
| 102 | + 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; |
| 112 | + end |
| 113 | + if jsonbytesmapping == "" % not specified, use default value |
| 114 | + obj.JsonBytesMapping = defaultmapping; |
| 115 | + else |
| 116 | + obj.JsonBytesMapping = jsonbytesmapping; |
| 117 | + 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; |
| 123 | + end |
| 124 | + if isempty(headerkeys) % not specified, return empty dictionary |
| 125 | + obj.HttpHeaders = dictionary(headerkeys, headervalues); |
| 126 | + else |
| 127 | + obj.HttpHeaders = httpheaders; |
| 128 | + end |
| 129 | + end |
| 130 | + end |
| 131 | +end |
0 commit comments