Skip to content

Commit 99f23be

Browse files
authored
Merge pull request #67 from mathworks/metrics_view_sdk
Metrics view sdk
2 parents 3a6b77f + c10af76 commit 99f23be

File tree

3 files changed

+132
-105
lines changed

3 files changed

+132
-105
lines changed

sdk/metrics/+opentelemetry/+sdk/+metrics/View.m

Lines changed: 37 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -21,28 +21,45 @@
2121
end
2222

2323
methods
24-
function obj = View(name, description, unit, instrumentName, instrumentType, ...
25-
meterName, meterVersion, meterSchemaURL, attributeKeys, ...
26-
aggregation, histogramBinEdges, varargin)
27-
28-
instrumentTypeCategory = int32(find(instrumentType==["kCounter", "kHistogram", "kUpDownCounter", "kObservableCounter", "kObservableGauge", "kObservableUpDownCounter"])-1);
29-
30-
aggregationCategory = int32(find(instrumentType==["kDrop", "kHistogram", "kLastValue", "kSum", "kDefault"])-1);
24+
function obj = View(options)
25+
arguments
26+
options.Name=""
27+
options.Description=""
28+
options.Unit=""
29+
options.InstrumentName=""
30+
options.InstrumentType=""
31+
options.MeterName=""
32+
options.MeterVersion=""
33+
options.MeterSchemaURL=""
34+
options.AttributeKeys=""
35+
options.Aggregation=""
36+
options.HistogramBinEdges=[]
37+
end
38+
39+
instrument_types = ["Counter", "Histogram", "UpDownCounter", "ObservableCounter", "ObservableGauge", "ObservableUpDownCounter"];
40+
instrument_type = validatestring(options.InstrumentType, instrument_types);
41+
instrumentTypeCategory = find(instrument_type==instrument_types)-1;
3142

43+
aggregation_types = ["Drop", "Histogram", "LastValue", "Sum", "Default"];
44+
aggregation_type = validatestring(options.Aggregation, aggregation_types);
45+
aggregationCategory = find(aggregation_type==aggregation_types)-1;
46+
3247
obj.Proxy = libmexclass.proxy.Proxy("Name", "libmexclass.opentelemetry.sdk.ViewProxy", ...
33-
"ConstructorArguments", {name, description, unit, instrumentName, ...
34-
instrumentTypeCategory, meterName, meterVersion, meterSchemaURL, ...
35-
attributeKeys, aggregationCategory, histogramBinEdges, varargin});
36-
obj.Description = description;
37-
obj.Unit = unit;
38-
obj.InstrumentName = instrumentName;
39-
obj.InstrumentType = instrumentType;
40-
obj.MeterName = meterName;
41-
obj.MeterVersion = meterVersion;
42-
obj.MeterSchemaURL = meterSchemaURL;
43-
obj.AttributeKeys = attributeKeys;
44-
obj.Aggregation = aggregation;
45-
obj.HistogramBinEdges = histogramBinEdges;
48+
"ConstructorArguments", {options.Name, options.Description, options.Unit, options.InstrumentName, ...
49+
instrumentTypeCategory, options.MeterName, options.MeterVersion, options.MeterSchemaURL, ...
50+
options.AttributeKeys, aggregationCategory, options.HistogramBinEdges});
51+
52+
obj.Name = options.Name;
53+
obj.Description = options.Description;
54+
obj.Unit = options.Unit;
55+
obj.InstrumentName = options.InstrumentName;
56+
obj.InstrumentType = options.InstrumentType;
57+
obj.MeterName = options.MeterName;
58+
obj.MeterVersion = options.MeterVersion;
59+
obj.MeterSchemaURL = options.MeterSchemaURL;
60+
obj.AttributeKeys = options.AttributeKeys;
61+
obj.Aggregation = options.Aggregation;
62+
obj.HistogramBinEdges = options.HistogramBinEdges;
4663
end
4764
end
4865
end

sdk/metrics/src/ViewProxy.cpp

Lines changed: 16 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,8 @@ libmexclass::proxy::MakeResult ViewProxy::make(const libmexclass::proxy::Functio
2929
matlab::data::StringArray unit_mda = constructor_arguments[2];
3030
auto unit = unit_mda[0];
3131

32-
matlab::data::TypedArray<int> aggregation_type_mda = constructor_arguments[9];
33-
auto aggregation_type = static_cast<metrics_sdk::AggregationType>(static_cast<int>(aggregation_type_mda[0]));
32+
matlab::data::TypedArray<double> aggregation_type_mda = constructor_arguments[9];
33+
metrics_sdk::AggregationType aggregation_type = static_cast<metrics_sdk::AggregationType>(static_cast<int>(aggregation_type_mda[0]));
3434

3535
std::shared_ptr<metrics_sdk::HistogramAggregationConfig> aggregation_config = std::shared_ptr<metrics_sdk::HistogramAggregationConfig>(new metrics_sdk::HistogramAggregationConfig());
3636
if(aggregation_type == metrics_sdk::AggregationType::kHistogram){
@@ -43,59 +43,49 @@ libmexclass::proxy::MakeResult ViewProxy::make(const libmexclass::proxy::Functio
4343
}
4444

4545
std::unique_ptr<metrics_sdk::AttributesProcessor> attributes_processor;
46-
matlab::data::StringArray attributes_mda = constructor_arguments[8];
47-
if(attributes_mda.getNumberOfElements()==0){
46+
matlab::data::TypedArray<matlab::data::MATLABString> attributes_mda = constructor_arguments[8];
47+
if(attributes_mda.getNumberOfElements()==1 && attributes_mda[0]==""){
4848
attributes_processor = std::unique_ptr<metrics_sdk::AttributesProcessor>(new metrics_sdk::DefaultAttributesProcessor());
4949
}else{
5050
std::unordered_map<std::string, bool> allowed_attribute_keys;
51-
for (auto a : attributes_mda) {
52-
allowed_attribute_keys[a] = true;
51+
for (size_t a=0; a<attributes_mda.getNumberOfElements(); a++) {
52+
allowed_attribute_keys[attributes_mda[a]] = true;
5353
}
5454
attributes_processor = std::unique_ptr<metrics_sdk::AttributesProcessor>(new metrics_sdk::FilteringAttributesProcessor(allowed_attribute_keys));
5555
}
5656

5757
auto view = metrics_sdk::ViewFactory::Create(name, description,
58-
unit, aggregation_type, std::move(aggregation_config), std::move(attributes_processor));
58+
unit, aggregation_type, aggregation_config, std::move(attributes_processor));
5959

60+
6061
// Create Instrument Selector
61-
matlab::data::TypedArray<int> instrument_type_mda = constructor_arguments[4];
62-
auto instrument_type = static_cast<metrics_sdk::InstrumentType>(static_cast<int>(instrument_type_mda[0]));
62+
matlab::data::TypedArray<double> instrument_type_mda = constructor_arguments[4];
63+
metrics_sdk::InstrumentType instrument_type = static_cast<metrics_sdk::InstrumentType>(static_cast<int>(instrument_type_mda[0]));
6364

6465
matlab::data::StringArray instrument_name_mda = constructor_arguments[3];
6566
auto instrument_name = static_cast<std::string>(instrument_name_mda[0]);
66-
auto instrument_name_view = nostd::string_view(instrument_name);
6767

68-
auto unit_view = nostd::string_view(static_cast<std::string>(unit));
68+
auto unit_str = static_cast<std::string>(unit);
6969

7070
auto instrumentSelector = metrics_sdk::InstrumentSelectorFactory::Create(instrument_type,
71-
instrument_name, unit_view);
71+
instrument_name, unit_str);
7272

7373

7474
// Create Meter Selector
7575
matlab::data::StringArray meter_name_mda = constructor_arguments[5];
7676
auto meter_name = static_cast<std::string>(meter_name_mda[0]);
77-
auto meter_name_view = nostd::string_view(meter_name);
7877

7978
matlab::data::StringArray meter_version_mda = constructor_arguments[6];
8079
auto meter_version = static_cast<std::string>(meter_version_mda[0]);
81-
auto meter_version_view = nostd::string_view(meter_version);
8280

8381
matlab::data::StringArray meter_schema_mda = constructor_arguments[7];
8482
auto meter_schema = static_cast<std::string>(meter_schema_mda[0]);
85-
auto meter_schema_view = nostd::string_view(meter_schema);
8683

87-
auto meterSelector = metrics_sdk::MeterSelectorFactory::Create(meter_name_view,
88-
meter_version_view, meter_schema_view);
89-
90-
91-
92-
// out = std::make_shared<ViewProxy>(nostd::shared_ptr<metrics_sdk::View>(
93-
// std::move(metrics_sdk::ViewFactory::Create(name, description,
94-
// unit, aggregation_type, std::move(aggregation_config), std::move(attributes_processor)))));
95-
96-
97-
// return out;
84+
auto meterSelector = metrics_sdk::MeterSelectorFactory::Create(meter_name,
85+
meter_version, meter_schema);
9886

87+
88+
// Call View Proxy Constructor
9989
return std::make_shared<ViewProxy>(std::move(view), std::move(instrumentSelector), std::move(meterSelector));
10090
}
10191

test/tmetrics_sdk.m

Lines changed: 79 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -146,65 +146,85 @@ function testCustomResource(testCase)
146146
end
147147
end
148148

149-
% function testViewCounter(testCase)
150-
% % testCustomResource: check custom resources are included in
151-
% % emitted metrics
152-
% commonSetup(testCase)
153-
%
154-
% exporter = opentelemetry.exporters.otlp.OtlpHttpMetricExporter();
155-
% reader = opentelemetry.sdk.metrics.PeriodicExportingMetricReader(exporter, ...
156-
% "Interval", seconds(2), "Timeout", seconds(1));
157-
% mp = opentelemetry.sdk.metrics.MeterProvider(reader);
158-
%
159-
% m = getMeter(mp, "mymeter");
160-
% c = createCounter(m, "mycounter");
161-
%
162-
% % create testing value
163-
% val = 10;
164-
%
165-
% % add value and attributes
166-
% c.add(val);
167-
%
168-
% pause(2.5);
169-
%
170-
% view = opentelemetry.sdk.metrics.View("View", "my View", "Unit", "Instrument", "kCounter", "mymeter", "", "", ["One" "Two" "Three"], "kDrop", [0 100 200 300 400 500]);
171-
%
172-
% addView(mp, view);
173-
%
174-
% clear mp;
175-
%
176-
% % % TODO: add test comparisons
177-
% end
178-
179-
% function testViewHistogram(testCase)
180-
% % testCustomResource: check custom resources are included in
181-
% % emitted metrics
182-
% commonSetup(testCase)
183-
%
184-
% exporter = opentelemetry.exporters.otlp.OtlpHttpMetricExporter();
185-
% reader = opentelemetry.sdk.metrics.PeriodicExportingMetricReader(exporter, ...
186-
% "Interval", seconds(2), "Timeout", seconds(1));
187-
% mp = opentelemetry.sdk.metrics.MeterProvider(reader);
188-
% m = mp.getMeter("mymeter");
189-
% hist = m.createHistogram("histogram");
190-
%
191-
% % create value for histogram
192-
% val = 1;
193-
%
194-
% % record value
195-
% hist.record(val);
196-
%
197-
% % wait for collector response
198-
% pause(2.5);
199-
%
200-
% view = opentelemetry.sdk.metrics.View("View", "my View", "Unit", "Instrument", "kHistogram", "mymeter", "", "", ["One" "Two" "Three"], "kHistogram", [0 100 200 300 400 500]);
201-
%
202-
% addView(mp, view);
203-
%
204-
% clear mp;
205-
%
206-
% % % TODO: add test comparisons
207-
% end
149+
function testViewBasic(testCase)
150+
mp = opentelemetry.sdk.metrics.MeterProvider(testCase.ShortIntervalReader);
151+
152+
view_name = "counter_view";
153+
view_description = "view_description";
154+
view = opentelemetry.sdk.metrics.View(Name="counter_view", Description="view_description", InstrumentName="mycounter", InstrumentType="Counter", MeterName="mymeter", MeterVersion="1.2.0", MeterSchemaURL="", Aggregation="Sum");
155+
156+
addView(mp, view);
157+
158+
m = getMeter(mp, "mymeter", "1.2.0", "");
159+
c = createCounter(m, "mycounter");
160+
161+
% add value and attributes
162+
val = 10;
163+
c.add(val);
164+
165+
pause(2.5);
166+
167+
clear m;
168+
results = readJsonResults(testCase);
169+
results = results{end};
170+
171+
% verify view name and description
172+
verifyEqual(testCase, string(results.resourceMetrics.scopeMetrics.metrics.name), view_name);
173+
verifyEqual(testCase, string(results.resourceMetrics.scopeMetrics.metrics.description), view_description);
174+
175+
% fetch datapoint
176+
dp = results.resourceMetrics.scopeMetrics.metrics.sum.dataPoints;
177+
178+
% verify counter value
179+
verifyEqual(testCase, dp.asDouble, val);
180+
end
181+
182+
183+
function testViewHistogram(testCase)
184+
mp = opentelemetry.sdk.metrics.MeterProvider(testCase.ShortIntervalReader);
185+
186+
view_name = "histogram_view";
187+
view_description = "view_description";
188+
bin_edges = [0; 100; 200; 300; 400; 500];
189+
view = opentelemetry.sdk.metrics.View(Name="histogram_view", Description="view_description", InstrumentName="myhistogram", InstrumentType="Histogram", MeterName="mymeter", Aggregation="Histogram", HistogramBinEdges=bin_edges);
190+
191+
addView(mp, view);
192+
193+
m = mp.getMeter("mymeter");
194+
hist = m.createHistogram("myhistogram");
195+
196+
% record values
197+
hist.record(0);
198+
hist.record(200);
199+
hist.record(201);
200+
hist.record(401);
201+
hist.record(402);
202+
203+
% wait for collector response
204+
pause(2.5);
205+
206+
clear m;
207+
results = readJsonResults(testCase);
208+
results = results{end};
209+
210+
% verify view name and description
211+
verifyEqual(testCase, string(results.resourceMetrics.scopeMetrics.metrics.name), view_name);
212+
verifyEqual(testCase, string(results.resourceMetrics.scopeMetrics.metrics.description), view_description);
213+
214+
% fetch datapoint
215+
dp = results.resourceMetrics.scopeMetrics.metrics.histogram.dataPoints;
216+
217+
% verify histogram sum
218+
expected_sum = 1204;
219+
verifyEqual(testCase, dp.sum, expected_sum);
220+
221+
% verify histogram bounds
222+
verifyEqual(testCase, dp.explicitBounds, bin_edges);
223+
224+
% verify histogram buckets
225+
expected_buckets = {'1'; '0'; '1'; '1'; '0'; '2'; '0'};
226+
verifyEqual(testCase, dp.bucketCounts, expected_buckets);
227+
end
208228

209229
function testShutdown(testCase)
210230
% testShutdown: shutdown method should stop exporting

0 commit comments

Comments
 (0)