|
2 | 2 | % An SDK implementation of meter provider, which stores a set of configurations used |
3 | 3 | % in a metrics system. |
4 | 4 |
|
5 | | - % Copyright 2023 The MathWorks, Inc. |
| 5 | + % Copyright 2023-2024 The MathWorks, Inc. |
6 | 6 |
|
7 | 7 | properties(Access=private) |
8 | 8 | isShutdown (1,1) logical = false |
|
15 | 15 | end |
16 | 16 |
|
17 | 17 | methods |
18 | | - function obj = MeterProvider(reader, optionnames, optionvalues) |
| 18 | + function obj = MeterProvider(varargin) |
19 | 19 | % SDK implementation of meter provider |
20 | 20 | % MP = OPENTELEMETRY.SDK.METRICS.METERPROVIDER creates a meter |
21 | 21 | % provider that uses a periodic exporting metric reader and default configurations. |
|
24 | 24 | % reader R. Currently, the only supported metric reader is the periodic |
25 | 25 | % exporting metric reader. |
26 | 26 | % |
27 | | - % TP = OPENTELEMETRY.SDK.METRICS.METERPROVIDER(R, PARAM1, VALUE1, |
| 27 | + % TP = OPENTELEMETRY.SDK.METRICS.METERPROVIDER(..., PARAM1, VALUE1, |
28 | 28 | % PARAM2, VALUE2, ...) specifies optional parameter name/value pairs. |
29 | 29 | % Parameters are: |
30 | 30 | % "View" - View object used to customize collected metrics. |
|
34 | 34 | % See also OPENTELEMETRY.SDK.METRICS.PERIODICEXPORTINGMETRICREADER |
35 | 35 | % OPENTELEMETRY.SDK.METRICS.VIEW |
36 | 36 |
|
37 | | - arguments |
38 | | - reader {mustBeA(reader, ["opentelemetry.sdk.metrics.PeriodicExportingMetricReader", ... |
39 | | - "libmexclass.proxy.Proxy"])} = ... |
40 | | - opentelemetry.sdk.metrics.PeriodicExportingMetricReader() |
41 | | - end |
42 | | - |
43 | | - arguments (Repeating) |
44 | | - optionnames (1,:) {mustBeTextScalar} |
45 | | - optionvalues |
46 | | - end |
47 | | - |
48 | 37 | % explicit call to superclass constructor to make it a no-op |
49 | 38 | |
50 | 39 |
|
51 | | - if isa(reader, "libmexclass.proxy.Proxy") |
| 40 | + if nargin == 1 && isa(varargin{1}, "libmexclass.proxy.Proxy") |
52 | 41 | % This code branch is used to support conversion from API |
53 | 42 | % MeterProvider to SDK equivalent, needed internally by |
54 | 43 | % opentelemetry.sdk.metrics.Cleanup |
55 | | - mpproxy = reader; % rename the variable |
| 44 | + mpproxy = varargin{1}; |
56 | 45 | assert(mpproxy.Name == "libmexclass.opentelemetry.MeterProviderProxy"); |
57 | 46 | obj.Proxy = libmexclass.proxy.Proxy("Name", ... |
58 | 47 | "libmexclass.opentelemetry.sdk.MeterProviderProxy", ... |
59 | 48 | "ConstructorArguments", {mpproxy.ID}); |
60 | 49 | % leave other properties unassigned, they won't be used |
61 | 50 | else |
62 | | - validnames = ["Resource", "View"]; |
63 | | - resourcekeys = string.empty(); |
64 | | - resourcevalues = {}; |
65 | | - resource = dictionary(resourcekeys, resourcevalues); |
66 | | - suppliedview = false; |
67 | | - viewid = 0; |
68 | | - for i = 1:length(optionnames) |
69 | | - namei = validatestring(optionnames{i}, validnames); |
70 | | - valuei = optionvalues{i}; |
71 | | - if strcmp(namei, "Resource") |
72 | | - if ~isa(valuei, "dictionary") |
73 | | - error("opentelemetry:sdk:metrics:MeterProvider:InvalidResourceType", ... |
74 | | - "Resource input must be a dictionary."); |
75 | | - end |
76 | | - resource = valuei; |
77 | | - resourcekeys = keys(valuei); |
78 | | - resourcevalues = values(valuei,"cell"); |
79 | | - % collapse one level of cells, as this may be due to |
80 | | - % a behavior of dictionary.values |
81 | | - if all(cellfun(@iscell, resourcevalues)) |
82 | | - resourcevalues = [resourcevalues{:}]; |
83 | | - end |
84 | | - elseif strcmp(namei, "View") |
85 | | - suppliedview = true; |
86 | | - view = valuei; |
87 | | - if ~isa(view, "opentelemetry.sdk.metrics.View") |
88 | | - error("opentelemetry:sdk:metrics:MeterProvider:InvalidViewType", ... |
89 | | - "View input must be a opentelemetry.sdk.metrics.View object."); |
90 | | - end |
91 | | - viewid = view.Proxy.ID; |
92 | | - end |
93 | | - end |
94 | | - |
95 | | - obj.Proxy = libmexclass.proxy.Proxy("Name", ... |
96 | | - "libmexclass.opentelemetry.sdk.MeterProviderProxy", ... |
97 | | - "ConstructorArguments", {reader.Proxy.ID, resourcekeys, ... |
98 | | - resourcevalues, suppliedview, viewid}); |
99 | | - obj.MetricReader = reader; |
100 | | - obj.Resource = resource; |
101 | | - if suppliedview |
102 | | - obj.View = view; |
| 51 | + if nargin == 0 || ~isa(varargin{1}, "opentelemetry.sdk.metrics.PeriodicExportingMetricReader") |
| 52 | + reader = opentelemetry.sdk.metrics.PeriodicExportingMetricReader(); % default metric reader |
| 53 | + else |
| 54 | + reader = varargin{1}; |
| 55 | + varargin(1) = []; |
103 | 56 | end |
| 57 | + obj.processOptions(reader, varargin{:}); |
104 | 58 | end |
105 | 59 | end |
106 | 60 |
|
@@ -167,4 +121,60 @@ function addView(obj, view) |
167 | 121 | end |
168 | 122 |
|
169 | 123 | end |
| 124 | + |
| 125 | + methods(Access=private) |
| 126 | + function processOptions(obj, reader, optionnames, optionvalues) |
| 127 | + arguments |
| 128 | + obj |
| 129 | + reader |
| 130 | + end |
| 131 | + arguments (Repeating) |
| 132 | + optionnames (1,:) {mustBeTextScalar} |
| 133 | + optionvalues |
| 134 | + end |
| 135 | + |
| 136 | + validnames = ["Resource", "View"]; |
| 137 | + resourcekeys = string.empty(); |
| 138 | + resourcevalues = {}; |
| 139 | + resource = dictionary(resourcekeys, resourcevalues); |
| 140 | + suppliedview = false; |
| 141 | + viewid = 0; |
| 142 | + for i = 1:length(optionnames) |
| 143 | + namei = validatestring(optionnames{i}, validnames); |
| 144 | + valuei = optionvalues{i}; |
| 145 | + if strcmp(namei, "Resource") |
| 146 | + if ~isa(valuei, "dictionary") |
| 147 | + error("opentelemetry:sdk:metrics:MeterProvider:InvalidResourceType", ... |
| 148 | + "Resource input must be a dictionary."); |
| 149 | + end |
| 150 | + resource = valuei; |
| 151 | + resourcekeys = keys(valuei); |
| 152 | + resourcevalues = values(valuei,"cell"); |
| 153 | + % collapse one level of cells, as this may be due to |
| 154 | + % a behavior of dictionary.values |
| 155 | + if all(cellfun(@iscell, resourcevalues)) |
| 156 | + resourcevalues = [resourcevalues{:}]; |
| 157 | + end |
| 158 | + elseif strcmp(namei, "View") |
| 159 | + suppliedview = true; |
| 160 | + view = valuei; |
| 161 | + if ~isa(view, "opentelemetry.sdk.metrics.View") |
| 162 | + error("opentelemetry:sdk:metrics:MeterProvider:InvalidViewType", ... |
| 163 | + "View input must be a opentelemetry.sdk.metrics.View object."); |
| 164 | + end |
| 165 | + viewid = view.Proxy.ID; |
| 166 | + end |
| 167 | + end |
| 168 | + |
| 169 | + obj.Proxy = libmexclass.proxy.Proxy("Name", ... |
| 170 | + "libmexclass.opentelemetry.sdk.MeterProviderProxy", ... |
| 171 | + "ConstructorArguments", {reader.Proxy.ID, resourcekeys, ... |
| 172 | + resourcevalues, suppliedview, viewid}); |
| 173 | + obj.MetricReader = reader; |
| 174 | + obj.Resource = resource; |
| 175 | + if suppliedview |
| 176 | + obj.View = view; |
| 177 | + end |
| 178 | + end |
| 179 | + end |
170 | 180 | end |
0 commit comments