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 cleanup #53

Closed
wants to merge 52 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
52 commits
Select commit Hold shift + click to select a range
f6a1c55
feat: metric_exporter, metric_reader
ricktu-mw Sep 25, 2023
0984062
update 1
ricktu-mw Sep 27, 2023
98bd3a6
v2
ricktu-mw Sep 28, 2023
4d8b990
v2
ricktu-mw Sep 28, 2023
081f16e
v2
ricktu-mw Sep 28, 2023
dd8e0e9
v3
ricktu-mw Oct 2, 2023
cc9cf8f
v4
ricktu-mw Oct 2, 2023
883f261
v5
ricktu-mw Oct 2, 2023
137fed8
v6
ricktu-mw Oct 3, 2023
ba013e1
Merge pull request #25 from mathworks/changes_after_v_1_3_0
duncanpo Oct 3, 2023
1b833de
v7
ricktu-mw Oct 3, 2023
6380ae1
v8: addMetricReader
ricktu-mw Oct 4, 2023
300445c
v9: update comments
ricktu-mw Oct 4, 2023
2636072
Merge pull request #26 from mathworks/metrics_reader
duncanpo Oct 5, 2023
40ecc12
Creating MeterProvider API
dnarula-mw Oct 5, 2023
68a0985
fix: invalid instrument names with spaces
ricktu-mw Oct 6, 2023
1773af8
Finishing and cleaning up MeterProvider api
dnarula-mw Oct 6, 2023
e995d79
fix: comments
ricktu-mw Oct 6, 2023
9af0a3d
Merge pull request #29 from mathworks/metrics_reader
duncanpo Oct 6, 2023
6e11722
Merge pull request #38 from mathworks/changes_after_v_1_3_0
duncanpo Oct 10, 2023
ac793c5
Adding test for get and set meter
dnarula-mw Oct 11, 2023
a0777dd
Resolving merge conflicts
dnarula-mw Oct 11, 2023
ec00310
Fixing error from merge
dnarula-mw Oct 11, 2023
3af790a
Removing .vs folder
dnarula-mw Oct 11, 2023
8c3a10c
Add resource property
dnarula-mw Oct 12, 2023
8268a15
Lowering reader interval for test
dnarula-mw Oct 12, 2023
f9b7c45
clearning meter provider in test
dnarula-mw Oct 12, 2023
39e01fa
fixing typo
dnarula-mw Oct 12, 2023
a630aa7
Addressing PR comments
dnarula-mw Oct 12, 2023
fd75583
Fixing crash and comment out test
dnarula-mw Oct 12, 2023
cf7f8ec
Merge pull request #39 from mathworks/meter_provider_api
duncanpo Oct 12, 2023
ff030ae
Adding shutdown and forceflush to fix getsetmeterprovider test
dnarula-mw Oct 26, 2023
e1c58c4
Clean up code and finish shutdown
dnarula-mw Oct 30, 2023
e245e2e
Removing unnecessary change in tests
dnarula-mw Oct 30, 2023
caab36b
Addressing pull request comments
dnarula-mw Oct 31, 2023
60acf06
Merge pull request #42 from mathworks/meter_provider_api
dnarula-mw Oct 31, 2023
f21e7c6
Fixing merge conflicts
dnarula-mw Oct 31, 2023
f3884c3
Make metric reader and exporter properties editable
duncanpo Nov 2, 2023
66b73f2
Merge pull request #46 from mathworks/metrics_readonly_props
duncanpo Nov 2, 2023
58392ca
Finishing add resource property to metrics
dnarula-mw Nov 2, 2023
3e9a03f
Merge branch 'metrics' into meter_provider_resource
dnarula-mw Nov 2, 2023
b90544a
add a test about issue #34
duncanpo Nov 2, 2023
ee27f2b
Cleaning up code
dnarula-mw Nov 2, 2023
2628bd7
Merge pull request #47 from mathworks/metrics_readonly_props
duncanpo Nov 2, 2023
ac97746
Change error message
dnarula-mw Nov 2, 2023
b7bb9cf
Merge pull request #48 from mathworks/meter_provider_resource
dnarula-mw Nov 2, 2023
2bff102
Correctly clearing meter providers
dnarula-mw Nov 3, 2023
4a12e67
add a SynchronousInstrument base class
duncanpo Nov 3, 2023
b6e18f9
Merge pull request #50 from mathworks/fixing_tests
duncanpo Nov 3, 2023
af1a71c
Merge pull request #49 from mathworks/sync_instrument
duncanpo Nov 3, 2023
adb6cd3
enable Cleanup class to work for SDK objects
duncanpo Nov 8, 2023
9f9fbc7
Complex number handling and more code sharing between metric instruments
duncanpo Nov 9, 2023
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
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,4 @@ build
# Autosave files
*.asv
*.swp
.vscode/settings.json
29 changes: 22 additions & 7 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -216,6 +216,7 @@ set(OPENTELEMETRY_PROXY_SOURCES
${TRACE_API_SOURCE_DIR}/SpanProxy.cpp
${TRACE_API_SOURCE_DIR}/SpanContextProxy.cpp
${COMMON_API_SOURCE_DIR}/attribute.cpp
${METRICS_API_SOURCE_DIR}/MeterProviderProxy.cpp
${METRICS_API_SOURCE_DIR}/MeterProxy.cpp
${METRICS_API_SOURCE_DIR}/CounterProxy.cpp
${METRICS_API_SOURCE_DIR}/UpDownCounterProxy.cpp
Expand All @@ -230,14 +231,19 @@ set(OPENTELEMETRY_PROXY_SOURCES
${TRACE_SDK_SOURCE_DIR}/BatchSpanProcessorProxy.cpp
${TRACE_SDK_SOURCE_DIR}/ParentBasedSamplerProxy.cpp
${METRICS_SDK_SOURCE_DIR}/MeterProviderProxy.cpp
${METRICS_SDK_SOURCE_DIR}/PeriodicExportingMetricReaderProxy.cpp
${COMMON_SDK_SOURCE_DIR}/resource.cpp)
if(WITH_OTLP_HTTP)
set(OPENTELEMETRY_PROXY_SOURCES ${OPENTELEMETRY_PROXY_SOURCES}
${OTLP_EXPORTER_SOURCE_DIR}/OtlpHttpSpanExporterProxy.cpp)
set(OPENTELEMETRY_PROXY_SOURCES
${OPENTELEMETRY_PROXY_SOURCES}
${OTLP_EXPORTER_SOURCE_DIR}/OtlpHttpSpanExporterProxy.cpp
${OTLP_EXPORTER_SOURCE_DIR}/OtlpHttpMetricExporterProxy.cpp)
endif()
if(WITH_OTLP_GRPC)
set(OPENTELEMETRY_PROXY_SOURCES ${OPENTELEMETRY_PROXY_SOURCES}
${OTLP_EXPORTER_SOURCE_DIR}/OtlpGrpcSpanExporterProxy.cpp)
set(OPENTELEMETRY_PROXY_SOURCES
${OPENTELEMETRY_PROXY_SOURCES}
${OTLP_EXPORTER_SOURCE_DIR}/OtlpGrpcSpanExporterProxy.cpp
${OTLP_EXPORTER_SOURCE_DIR}/OtlpGrpcMetricExporterProxy.cpp)
endif()

libmexclass_client_add_proxy_library(
Expand Down Expand Up @@ -391,9 +397,17 @@ set(BAGGAGE_API_MATLAB_SOURCES ${CMAKE_CURRENT_SOURCE_DIR}/api/baggage/+opentele
set(COMMON_API_MATLAB_SOURCES ${CMAKE_CURRENT_SOURCE_DIR}/api/common/+opentelemetry)
set(TRACE_SDK_MATLAB_SOURCES ${CMAKE_CURRENT_SOURCE_DIR}/sdk/trace/+opentelemetry)
set(METRICS_SDK_MATLAB_SOURCES ${CMAKE_CURRENT_SOURCE_DIR}/sdk/metrics/+opentelemetry)
set(DEFAULT_EXPORTER_MATLAB_SOURCES ${CMAKE_CURRENT_SOURCE_DIR}/exporters/otlp/+opentelemetry/+exporters/+otlp/defaultSpanExporter.m)
set(OTLP_HTTP_EXPORTER_MATLAB_SOURCES ${CMAKE_CURRENT_SOURCE_DIR}/exporters/otlp/+opentelemetry/+exporters/+otlp/OtlpHttpSpanExporter.m)
set(OTLP_GRPC_EXPORTER_MATLAB_SOURCES ${CMAKE_CURRENT_SOURCE_DIR}/exporters/otlp/+opentelemetry/+exporters/+otlp/OtlpGrpcSpanExporter.m)
set(COMMON_SDK_MATLAB_SOURCES ${CMAKE_CURRENT_SOURCE_DIR}/sdk/common/+opentelemetry)
set(DEFAULT_EXPORTER_MATLAB_SOURCES
${CMAKE_CURRENT_SOURCE_DIR}/exporters/otlp/+opentelemetry/+exporters/+otlp/defaultSpanExporter.m
${CMAKE_CURRENT_SOURCE_DIR}/exporters/otlp/+opentelemetry/+exporters/+otlp/defaultMetricExporter.m)
set(OTLP_HTTP_EXPORTER_MATLAB_SOURCES
${CMAKE_CURRENT_SOURCE_DIR}/exporters/otlp/+opentelemetry/+exporters/+otlp/OtlpHttpSpanExporter.m
${CMAKE_CURRENT_SOURCE_DIR}/exporters/otlp/+opentelemetry/+exporters/+otlp/OtlpHttpMetricExporter.m)
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)

Expand All @@ -404,6 +418,7 @@ install(DIRECTORY ${BAGGAGE_API_MATLAB_SOURCES} DESTINATION .)
install(DIRECTORY ${COMMON_API_MATLAB_SOURCES} DESTINATION .)
install(DIRECTORY ${TRACE_SDK_MATLAB_SOURCES} DESTINATION .)
install(DIRECTORY ${METRICS_SDK_MATLAB_SOURCES} DESTINATION .)
install(DIRECTORY ${COMMON_SDK_MATLAB_SOURCES} DESTINATION .)
install(FILES ${DEFAULT_EXPORTER_MATLAB_SOURCES} DESTINATION ${OTLP_EXPORTERS_DIR})
if(WITH_OTLP_HTTP)
install(FILES ${OTLP_HTTP_EXPORTER_MATLAB_SOURCES} DESTINATION ${OTLP_EXPORTERS_DIR})
Expand Down
7 changes: 7 additions & 0 deletions OtelMatlabProxyFactory.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,17 +23,21 @@
#include "opentelemetry-matlab/sdk/trace/TraceIdRatioBasedSamplerProxy.h"
#include "opentelemetry-matlab/sdk/trace/ParentBasedSamplerProxy.h"
#include "opentelemetry-matlab/sdk/metrics/MeterProviderProxy.h"
#include "opentelemetry-matlab/sdk/metrics/PeriodicExportingMetricReaderProxy.h"
#ifdef WITH_OTLP_HTTP
#include "opentelemetry-matlab/exporters/otlp/OtlpHttpSpanExporterProxy.h"
#include "opentelemetry-matlab/exporters/otlp/OtlpHttpMetricExporterProxy.h"
#endif
#ifdef WITH_OTLP_GRPC
#include "opentelemetry-matlab/exporters/otlp/OtlpGrpcSpanExporterProxy.h"
#include "opentelemetry-matlab/exporters/otlp/OtlpGrpcMetricExporterProxy.h"
#endif

libmexclass::proxy::MakeResult
OtelMatlabProxyFactory::make_proxy(const libmexclass::proxy::ClassName& class_name,
const libmexclass::proxy::FunctionArguments& constructor_arguments) {

REGISTER_PROXY(libmexclass.opentelemetry.MeterProviderProxy, libmexclass::opentelemetry::MeterProviderProxy);
REGISTER_PROXY(libmexclass.opentelemetry.TracerProviderProxy, libmexclass::opentelemetry::TracerProviderProxy);
//REGISTER_PROXY(libmexclass.opentelemetry.TracerProxy, libmexclass::opentelemetry::TracerProxy);
REGISTER_PROXY(libmexclass.opentelemetry.SpanProxy, libmexclass::opentelemetry::SpanProxy);
Expand All @@ -57,12 +61,15 @@ OtelMatlabProxyFactory::make_proxy(const libmexclass::proxy::ClassName& class_na
REGISTER_PROXY(libmexclass.opentelemetry.sdk.ParentBasedSamplerProxy, libmexclass::opentelemetry::sdk::ParentBasedSamplerProxy);

REGISTER_PROXY(libmexclass.opentelemetry.sdk.MeterProviderProxy, libmexclass::opentelemetry::sdk::MeterProviderProxy);
REGISTER_PROXY(libmexclass.opentelemetry.sdk.PeriodicExportingMetricReaderProxy, libmexclass::opentelemetry::sdk::PeriodicExportingMetricReaderProxy);

#ifdef WITH_OTLP_HTTP
REGISTER_PROXY(libmexclass.opentelemetry.exporters.OtlpHttpSpanExporterProxy, libmexclass::opentelemetry::exporters::OtlpHttpSpanExporterProxy);
REGISTER_PROXY(libmexclass.opentelemetry.exporters.OtlpHttpMetricExporterProxy, libmexclass::opentelemetry::exporters::OtlpHttpMetricExporterProxy);
#endif
#ifdef WITH_OTLP_GRPC
REGISTER_PROXY(libmexclass.opentelemetry.exporters.OtlpGrpcSpanExporterProxy, libmexclass::opentelemetry::exporters::OtlpGrpcSpanExporterProxy);
REGISTER_PROXY(libmexclass.opentelemetry.exporters.OtlpGrpcMetricExporterProxy, libmexclass::opentelemetry::exporters::OtlpGrpcMetricExporterProxy);
#endif
return nullptr;
}
55 changes: 8 additions & 47 deletions api/metrics/+opentelemetry/+metrics/Counter.m
Original file line number Diff line number Diff line change
@@ -1,59 +1,20 @@
classdef Counter < handle
% Counter is a value that accumulates over time,
% you can think of this like an odometer on a car; it only ever goes up.
classdef Counter < opentelemetry.metrics.SynchronousInstrument
% Counter is a value that accumulates over time and can only increase
% but not decrease.

% Copyright 2023 The MathWorks, Inc.

properties (SetAccess=immutable)
Name (1,1) string
Description (1,1) string
Unit (1,1) string
end

properties (Access=private)
Proxy % Proxy object to interface C++ code
end

methods (Access={?opentelemetry.metrics.Meter})

function obj = Counter(proxy, ctname, ctdescription, ctunit)
function obj = Counter(proxy, name, description, unit)
% Private constructor. Use createCounter method of Meter
% to create Counters.
obj.Proxy = proxy;
obj.Name = ctname;
obj.Description = ctdescription;
obj.Unit = ctunit;
[email protected](proxy, name, description, unit);
end

end

methods

function add(obj, value, varargin)
% input value must be a numerical scalar
if isnumeric(value) && isscalar(value)

if nargin == 2
obj.Proxy.add(value);

elseif isa(varargin{1}, "dictionary")
attrkeys = keys(varargin{1});
attrvals = values(varargin{1},"cell");
if all(cellfun(@iscell, attrvals))
attrvals = [attrvals{:}];
end
obj.Proxy.add(value,attrkeys,attrvals);

else
attrkeys = [varargin{1:2:length(varargin)}]';
attrvals = [varargin(2:2:length(varargin))]';
obj.Proxy.add(value,attrkeys,attrvals);
end
end

obj.processValue(value, varargin{:});
end

end


end
end
48 changes: 6 additions & 42 deletions api/metrics/+opentelemetry/+metrics/Histogram.m
Original file line number Diff line number Diff line change
@@ -1,55 +1,19 @@
classdef Histogram < handle
% Histogram is an instrument that adds or reduce values.
classdef Histogram < opentelemetry.metrics.SynchronousInstrument
% Histogram is an instrument that aggregates values into bins

% Copyright 2023 The MathWorks, Inc.

properties (SetAccess=immutable)
Name (1,1) string
Description (1,1) string
Unit (1,1) string
end

properties (Access=public)
Proxy % Proxy object to interface C++ code
end

methods (Access={?opentelemetry.metrics.Meter})

function obj = Histogram(proxy, hiname, hidescription, hiunit)
function obj = Histogram(proxy, name, description, unit)
% Private constructor. Use createHistogram method of Meter
% to create Histograms.
obj.Proxy = proxy;
obj.Name = hiname;
obj.Description = hidescription;
obj.Unit = hiunit;
[email protected](proxy, name, description, unit);
end

end

methods

function record(obj, value, varargin)
% input value must be a numerical scalar
if isnumeric(value) && isscalar(value)
if nargin == 2
obj.Proxy.record(value);
elseif isa(varargin{1}, "dictionary")
attrkeys = keys(varargin{1});
attrvals = values(varargin{1},"cell");
if all(cellfun(@iscell, attrvals))
attrvals = [attrvals{:}];
end
obj.Proxy.record(value,attrkeys,attrvals);
else
attrkeys = [varargin{1:2:length(varargin)}]';
attrvals = [varargin(2:2:length(varargin))]';
obj.Proxy.record(value,attrkeys,attrvals);
end
end

obj.processValue(value, varargin{:});
end

end


end
17 changes: 13 additions & 4 deletions api/metrics/+opentelemetry/+metrics/Meter.m
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
Proxy % Proxy object to interface C++ code
end

methods (Access={?opentelemetry.sdk.metrics.MeterProvider})
methods (Access={?opentelemetry.sdk.metrics.MeterProvider, ?opentelemetry.metrics.MeterProvider})

function obj = Meter(proxy, mtname, mtversion, mtschema)
% Private constructor. Use getMeter method of MeterProvider
Expand All @@ -37,7 +37,10 @@
ctunit = ""
end
import opentelemetry.common.mustBeScalarString
ctname = mustBeScalarString(ctname);
ctname = mustBeScalarString(ctname);
% cpp-opentelemetry end does not allow string input with spaces,
% replace any spaces with underscores as a temporary fix
ctname = strrep(ctname, ' ', '_');
ctdescription = mustBeScalarString(ctdescription);
ctunit = mustBeScalarString(ctunit);
id = obj.Proxy.createCounter(ctname, ctdescription, ctunit);
Expand All @@ -56,7 +59,10 @@
end

import opentelemetry.common.mustBeScalarString
ctname = mustBeScalarString(ctname);
ctname = mustBeScalarString(ctname);
% cpp-opentelemetry end does not allow string input with spaces,
% replace any spaces with underscores as a temporary fix
ctname = strrep(ctname, ' ', '_');
ctdescription = mustBeScalarString(ctdescription);
ctunit = mustBeScalarString(ctunit);
id = obj.Proxy.createUpDownCounter(ctname, ctdescription, ctunit);
Expand All @@ -75,7 +81,10 @@
end

import opentelemetry.common.mustBeScalarString
hiname = mustBeScalarString(hiname);
hiname = mustBeScalarString(hiname);
% cpp-opentelemetry end does not allow string input with spaces,
% replace any spaces with underscores as a temporary fix
hiname = strrep(hiname, ' ', '_');
hidescription = mustBeScalarString(hidescription);
hiunit = mustBeScalarString(hiunit);
id = obj.Proxy.createHistogram(hiname, hidescription, hiunit);
Expand Down
69 changes: 69 additions & 0 deletions api/metrics/+opentelemetry/+metrics/MeterProvider.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
classdef MeterProvider < handle
% A meter provider stores a set of configurations used in a distributed
% metrics system.

% Copyright 2023 The MathWorks, Inc.

properties (Access={?opentelemetry.sdk.metrics.MeterProvider, ?opentelemetry.sdk.common.Cleanup})
Proxy % Proxy object to interface C++ code
end

methods (Access={?opentelemetry.metrics.Provider, ?opentelemetry.sdk.metrics.MeterProvider})
function obj = MeterProvider(skip)
% constructor
% "skip" input signals skipping construction
if nargin < 1 || skip ~= "skip"
obj.Proxy = libmexclass.proxy.Proxy("Name", ...
"libmexclass.opentelemetry.MeterProviderProxy", ...
"ConstructorArguments", {});
end
end
end

methods
function meter = getMeter(obj, mname, mversion, mschema)
% GETMETER Create a meter object used to generate metrics.
% M = GETMETER(MP, NAME) returns a meter with the name
% NAME that uses all the configurations specified in meter
% provider MP.
%
% M = GETMETER(MP, NAME, VERSION, SCHEMA) also specifies
% the meter version and the URL that documents the schema
% of the generated meters.
%
% See also OPENTELEMETRY.METRICS.METER
arguments
obj
mname
mversion = ""
mschema = ""
end
% name, version, schema accepts any types that can convert to a
% string
import opentelemetry.common.mustBeScalarString
mname = mustBeScalarString(mname);
mversion = mustBeScalarString(mversion);
mschema = mustBeScalarString(mschema);
id = obj.Proxy.getMeter(mname, mversion, mschema);
meterproxy = libmexclass.proxy.Proxy("Name", ...
"libmexclass.opentelemetry.MeterProxy", "ID", id);
meter = opentelemetry.metrics.Meter(meterproxy, mname, mversion, mschema);
end

function setMeterProvider(obj)
% SETMETERPROVIDER Set global instance of meter provider
% SETMETERPROVIDER(MP) sets the meter provider MP as
% the global instance.
%
% See also OPENTELEMETRY.METRICS.PROVIDER.GETMETERPROVIDER
obj.Proxy.setMeterProvider();
end
end

methods(Access=?opentelemetry.sdk.common.Cleanup)
function postShutdown(obj)
% POSTSHUTDOWN Handle post-shutdown tasks
obj.Proxy.postShutdown();
end
end
end
27 changes: 27 additions & 0 deletions api/metrics/+opentelemetry/+metrics/Provider.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
classdef Provider
% Get and set the global instance of meter provider

% Copyright 2023 The MathWorks, Inc.

methods (Static)
function p = getMeterProvider()
% Get the global instance of meter provider
% MP = OPENTELEMETRY.METRICS.PROVIDER.GETMETERPROVIDER gets
% the global instance of meter provider.
%
% See also OPENTELEMETRY.METRICS.PROVIDER.SETMETERPROVIDER

p = opentelemetry.metrics.MeterProvider();
end

function setMeterProvider(p)
% Set the global instance of meter provider
% OPENTELEMETRY.METRICS.PROVIDER.GETMETERPROVIDER(MP) sets
% MP as the global instance of meter provider.
%
% See also OPENTELEMETRY.METRICS.PROVIDER.GETMETERPROVIDER
p.setMeterProvider();
end
end

end
Loading