-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #48 from mathworks/meter_provider_resource
Adding Resource Property to MeterProvider
- Loading branch information
Showing
4 changed files
with
120 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -10,11 +10,12 @@ | |
|
||
properties (Access=public) | ||
MetricReader | ||
Resource | ||
end | ||
|
||
methods | ||
function obj = MeterProvider(reader) | ||
% SDK implementation of tracer provider | ||
function obj = MeterProvider(reader, optionnames, optionvalues) | ||
% SDK implementation of meter provider | ||
% MP = OPENTELEMETRY.SDK.METRICS.METERPROVIDER creates a meter | ||
% provider that uses a periodic exporting metric reader and default configurations. | ||
% | ||
|
@@ -38,6 +39,11 @@ | |
opentelemetry.sdk.metrics.PeriodicExportingMetricReader() | ||
end | ||
|
||
arguments (Repeating) | ||
optionnames (1,:) {mustBeTextScalar} | ||
optionvalues | ||
end | ||
|
||
% explicit call to superclass constructor to make it a no-op | ||
[email protected]("skip"); | ||
|
||
|
@@ -49,13 +55,37 @@ | |
assert(mpproxy.Name == "libmexclass.opentelemetry.MeterProviderProxy"); | ||
obj.Proxy = libmexclass.proxy.Proxy("Name", ... | ||
"libmexclass.opentelemetry.sdk.MeterProviderProxy", ... | ||
"ConstructorArguments", {mpproxy.ID, true}); | ||
"ConstructorArguments", {mpproxy.ID}); | ||
% leave other properties unassigned, they won't be used | ||
else | ||
validnames = ["Resource"]; | ||
resourcekeys = string.empty(); | ||
resourcevalues = {}; | ||
resource = dictionary(resourcekeys, resourcevalues); | ||
for i = 1:length(optionnames) | ||
namei = validatestring(optionnames{i}, validnames); | ||
valuei = optionvalues{i}; | ||
if strcmp(namei, "Resource") | ||
if ~isa(valuei, "dictionary") | ||
error("opentelemetry:sdk:metrics:MeterProvider:InvalidResourceType", ... | ||
"Resource input must be a dictionary."); | ||
end | ||
resource = valuei; | ||
resourcekeys = keys(valuei); | ||
resourcevalues = values(valuei,"cell"); | ||
% collapse one level of cells, as this may be due to | ||
% a behavior of dictionary.values | ||
if all(cellfun(@iscell, resourcevalues)) | ||
resourcevalues = [resourcevalues{:}]; | ||
end | ||
end | ||
end | ||
|
||
obj.Proxy = libmexclass.proxy.Proxy("Name", ... | ||
"libmexclass.opentelemetry.sdk.MeterProviderProxy", ... | ||
"ConstructorArguments", {reader.Proxy.ID, false}); | ||
"ConstructorArguments", {reader.Proxy.ID, resourcekeys, resourcevalues}); | ||
obj.MetricReader = reader; | ||
obj.Resource = resource; | ||
end | ||
end | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
classdef tmetrics_sdk < matlab.unittest.TestCase | ||
% tests for metrics SDK | ||
|
||
% Copyright 2023 The MathWorks, Inc. | ||
|
||
properties | ||
OtelConfigFile | ||
JsonFile | ||
PidFile | ||
OtelcolName | ||
Otelcol | ||
ListPid | ||
ReadPidList | ||
ExtractPid | ||
Sigint | ||
Sigterm | ||
end | ||
|
||
methods (TestClassSetup) | ||
function setupOnce(testCase) | ||
commonSetupOnce(testCase); | ||
end | ||
end | ||
|
||
methods (TestMethodTeardown) | ||
function teardown(testCase) | ||
commonTeardown(testCase); | ||
end | ||
end | ||
|
||
methods (Test) | ||
function testCustomResource(testCase) | ||
% testCustomResource: check custom resources are included in | ||
% emitted metrics | ||
commonSetup(testCase) | ||
|
||
customkeys = ["foo" "bar"]; | ||
customvalues = [1 5]; | ||
exporter = opentelemetry.exporters.otlp.OtlpHttpMetricExporter(); | ||
reader = opentelemetry.sdk.metrics.PeriodicExportingMetricReader(exporter, ... | ||
"Interval", seconds(2), "Timeout", seconds(1)); | ||
mp = opentelemetry.sdk.metrics.MeterProvider(reader, ... | ||
"Resource", dictionary(customkeys, customvalues)); | ||
|
||
m = getMeter(mp, "mymeter"); | ||
c = createCounter(m, "mycounter"); | ||
|
||
% create testing value | ||
val = 10; | ||
|
||
% add value and attributes | ||
c.add(val); | ||
|
||
pause(2.5); | ||
|
||
% perform test comparisons | ||
results = readJsonResults(testCase); | ||
results = results{1}; | ||
|
||
resourcekeys = string({results.resourceMetrics.resource.attributes.key}); | ||
for i = length(customkeys) | ||
idx = find(resourcekeys == customkeys(i)); | ||
verifyNotEmpty(testCase, idx); | ||
verifyEqual(testCase, results.resourceMetrics.resource.attributes(idx).value.doubleValue, customvalues(i)); | ||
end | ||
end | ||
|
||
end | ||
end |