Skip to content

Commit 1e33556

Browse files
committed
prepare for merging metrics branch into main
2 parents bb15a99 + f3e7a37 commit 1e33556

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

43 files changed

+2773
-73
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,3 +4,4 @@ build
44
# Autosave files
55
*.asv
66
*.swp
7+
.vscode/settings.json

CMakeLists.txt

Lines changed: 29 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -212,6 +212,11 @@ set(OPENTELEMETRY_PROXY_SOURCES
212212
${TRACE_API_SOURCE_DIR}/SpanProxy.cpp
213213
${TRACE_API_SOURCE_DIR}/SpanContextProxy.cpp
214214
${COMMON_API_SOURCE_DIR}/attribute.cpp
215+
${METRICS_API_SOURCE_DIR}/MeterProviderProxy.cpp
216+
${METRICS_API_SOURCE_DIR}/MeterProxy.cpp
217+
${METRICS_API_SOURCE_DIR}/CounterProxy.cpp
218+
${METRICS_API_SOURCE_DIR}/UpDownCounterProxy.cpp
219+
${METRICS_API_SOURCE_DIR}/HistogramProxy.cpp
215220
${CONTEXT_API_SOURCE_DIR}/TextMapPropagatorProxy.cpp
216221
${CONTEXT_API_SOURCE_DIR}/CompositePropagatorProxy.cpp
217222
${CONTEXT_API_SOURCE_DIR}/TextMapCarrierProxy.cpp
@@ -221,14 +226,21 @@ set(OPENTELEMETRY_PROXY_SOURCES
221226
${TRACE_SDK_SOURCE_DIR}/SimpleSpanProcessorProxy.cpp
222227
${TRACE_SDK_SOURCE_DIR}/BatchSpanProcessorProxy.cpp
223228
${TRACE_SDK_SOURCE_DIR}/ParentBasedSamplerProxy.cpp
229+
${METRICS_SDK_SOURCE_DIR}/MeterProviderProxy.cpp
230+
${METRICS_SDK_SOURCE_DIR}/ViewProxy.cpp
231+
${METRICS_SDK_SOURCE_DIR}/PeriodicExportingMetricReaderProxy.cpp
224232
${COMMON_SDK_SOURCE_DIR}/resource.cpp)
225233
if(WITH_OTLP_HTTP)
226-
set(OPENTELEMETRY_PROXY_SOURCES ${OPENTELEMETRY_PROXY_SOURCES}
227-
${OTLP_EXPORTER_SOURCE_DIR}/OtlpHttpSpanExporterProxy.cpp)
234+
set(OPENTELEMETRY_PROXY_SOURCES
235+
${OPENTELEMETRY_PROXY_SOURCES}
236+
${OTLP_EXPORTER_SOURCE_DIR}/OtlpHttpSpanExporterProxy.cpp
237+
${OTLP_EXPORTER_SOURCE_DIR}/OtlpHttpMetricExporterProxy.cpp)
228238
endif()
229239
if(WITH_OTLP_GRPC)
230-
set(OPENTELEMETRY_PROXY_SOURCES ${OPENTELEMETRY_PROXY_SOURCES}
231-
${OTLP_EXPORTER_SOURCE_DIR}/OtlpGrpcSpanExporterProxy.cpp)
240+
set(OPENTELEMETRY_PROXY_SOURCES
241+
${OPENTELEMETRY_PROXY_SOURCES}
242+
${OTLP_EXPORTER_SOURCE_DIR}/OtlpGrpcSpanExporterProxy.cpp
243+
${OTLP_EXPORTER_SOURCE_DIR}/OtlpGrpcMetricExporterProxy.cpp)
232244
endif()
233245

234246
libmexclass_client_add_proxy_library(
@@ -377,9 +389,17 @@ set(CONTEXT_API_MATLAB_SOURCES ${CMAKE_CURRENT_SOURCE_DIR}/api/context/+opentele
377389
set(BAGGAGE_API_MATLAB_SOURCES ${CMAKE_CURRENT_SOURCE_DIR}/api/baggage/+opentelemetry)
378390
set(COMMON_API_MATLAB_SOURCES ${CMAKE_CURRENT_SOURCE_DIR}/api/common/+opentelemetry)
379391
set(TRACE_SDK_MATLAB_SOURCES ${CMAKE_CURRENT_SOURCE_DIR}/sdk/trace/+opentelemetry)
380-
set(DEFAULT_EXPORTER_MATLAB_SOURCES ${CMAKE_CURRENT_SOURCE_DIR}/exporters/otlp/+opentelemetry/+exporters/+otlp/defaultSpanExporter.m)
381-
set(OTLP_HTTP_EXPORTER_MATLAB_SOURCES ${CMAKE_CURRENT_SOURCE_DIR}/exporters/otlp/+opentelemetry/+exporters/+otlp/OtlpHttpSpanExporter.m)
382-
set(OTLP_GRPC_EXPORTER_MATLAB_SOURCES ${CMAKE_CURRENT_SOURCE_DIR}/exporters/otlp/+opentelemetry/+exporters/+otlp/OtlpGrpcSpanExporter.m)
392+
set(METRICS_SDK_MATLAB_SOURCES ${CMAKE_CURRENT_SOURCE_DIR}/sdk/metrics/+opentelemetry)
393+
set(COMMON_SDK_MATLAB_SOURCES ${CMAKE_CURRENT_SOURCE_DIR}/sdk/common/+opentelemetry)
394+
set(DEFAULT_EXPORTER_MATLAB_SOURCES
395+
${CMAKE_CURRENT_SOURCE_DIR}/exporters/otlp/+opentelemetry/+exporters/+otlp/defaultSpanExporter.m
396+
${CMAKE_CURRENT_SOURCE_DIR}/exporters/otlp/+opentelemetry/+exporters/+otlp/defaultMetricExporter.m)
397+
set(OTLP_HTTP_EXPORTER_MATLAB_SOURCES
398+
${CMAKE_CURRENT_SOURCE_DIR}/exporters/otlp/+opentelemetry/+exporters/+otlp/OtlpHttpSpanExporter.m
399+
${CMAKE_CURRENT_SOURCE_DIR}/exporters/otlp/+opentelemetry/+exporters/+otlp/OtlpHttpMetricExporter.m)
400+
set(OTLP_GRPC_EXPORTER_MATLAB_SOURCES
401+
${CMAKE_CURRENT_SOURCE_DIR}/exporters/otlp/+opentelemetry/+exporters/+otlp/OtlpGrpcSpanExporter.m
402+
${CMAKE_CURRENT_SOURCE_DIR}/exporters/otlp/+opentelemetry/+exporters/+otlp/OtlpGrpcMetricExporter.m)
383403

384404
set(OTLP_EXPORTERS_DIR +opentelemetry/+exporters/+otlp)
385405

@@ -388,6 +408,8 @@ install(DIRECTORY ${CONTEXT_API_MATLAB_SOURCES} DESTINATION .)
388408
install(DIRECTORY ${BAGGAGE_API_MATLAB_SOURCES} DESTINATION .)
389409
install(DIRECTORY ${COMMON_API_MATLAB_SOURCES} DESTINATION .)
390410
install(DIRECTORY ${TRACE_SDK_MATLAB_SOURCES} DESTINATION .)
411+
install(DIRECTORY ${METRICS_SDK_MATLAB_SOURCES} DESTINATION .)
412+
install(DIRECTORY ${COMMON_SDK_MATLAB_SOURCES} DESTINATION .)
391413
install(FILES ${DEFAULT_EXPORTER_MATLAB_SOURCES} DESTINATION ${OTLP_EXPORTERS_DIR})
392414
if(WITH_OTLP_HTTP)
393415
install(FILES ${OTLP_HTTP_EXPORTER_MATLAB_SOURCES} DESTINATION ${OTLP_EXPORTERS_DIR})

OtelMatlabProxyFactory.cpp

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,17 +22,23 @@
2222
#include "opentelemetry-matlab/sdk/trace/AlwaysOffSamplerProxy.h"
2323
#include "opentelemetry-matlab/sdk/trace/TraceIdRatioBasedSamplerProxy.h"
2424
#include "opentelemetry-matlab/sdk/trace/ParentBasedSamplerProxy.h"
25+
#include "opentelemetry-matlab/sdk/metrics/MeterProviderProxy.h"
26+
#include "opentelemetry-matlab/sdk/metrics/ViewProxy.h"
27+
#include "opentelemetry-matlab/sdk/metrics/PeriodicExportingMetricReaderProxy.h"
2528
#ifdef WITH_OTLP_HTTP
2629
#include "opentelemetry-matlab/exporters/otlp/OtlpHttpSpanExporterProxy.h"
30+
#include "opentelemetry-matlab/exporters/otlp/OtlpHttpMetricExporterProxy.h"
2731
#endif
2832
#ifdef WITH_OTLP_GRPC
2933
#include "opentelemetry-matlab/exporters/otlp/OtlpGrpcSpanExporterProxy.h"
34+
#include "opentelemetry-matlab/exporters/otlp/OtlpGrpcMetricExporterProxy.h"
3035
#endif
3136

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

41+
REGISTER_PROXY(libmexclass.opentelemetry.MeterProviderProxy, libmexclass::opentelemetry::MeterProviderProxy);
3642
REGISTER_PROXY(libmexclass.opentelemetry.TracerProviderProxy, libmexclass::opentelemetry::TracerProviderProxy);
3743
//REGISTER_PROXY(libmexclass.opentelemetry.TracerProxy, libmexclass::opentelemetry::TracerProxy);
3844
REGISTER_PROXY(libmexclass.opentelemetry.SpanProxy, libmexclass::opentelemetry::SpanProxy);
@@ -54,11 +60,18 @@ OtelMatlabProxyFactory::make_proxy(const libmexclass::proxy::ClassName& class_na
5460
REGISTER_PROXY(libmexclass.opentelemetry.sdk.AlwaysOffSamplerProxy, libmexclass::opentelemetry::sdk::AlwaysOffSamplerProxy);
5561
REGISTER_PROXY(libmexclass.opentelemetry.sdk.TraceIdRatioBasedSamplerProxy, libmexclass::opentelemetry::sdk::TraceIdRatioBasedSamplerProxy);
5662
REGISTER_PROXY(libmexclass.opentelemetry.sdk.ParentBasedSamplerProxy, libmexclass::opentelemetry::sdk::ParentBasedSamplerProxy);
63+
64+
REGISTER_PROXY(libmexclass.opentelemetry.sdk.MeterProviderProxy, libmexclass::opentelemetry::sdk::MeterProviderProxy);
65+
REGISTER_PROXY(libmexclass.opentelemetry.sdk.ViewProxy, libmexclass::opentelemetry::sdk::ViewProxy);
66+
REGISTER_PROXY(libmexclass.opentelemetry.sdk.PeriodicExportingMetricReaderProxy, libmexclass::opentelemetry::sdk::PeriodicExportingMetricReaderProxy);
67+
5768
#ifdef WITH_OTLP_HTTP
5869
REGISTER_PROXY(libmexclass.opentelemetry.exporters.OtlpHttpSpanExporterProxy, libmexclass::opentelemetry::exporters::OtlpHttpSpanExporterProxy);
70+
REGISTER_PROXY(libmexclass.opentelemetry.exporters.OtlpHttpMetricExporterProxy, libmexclass::opentelemetry::exporters::OtlpHttpMetricExporterProxy);
5971
#endif
6072
#ifdef WITH_OTLP_GRPC
6173
REGISTER_PROXY(libmexclass.opentelemetry.exporters.OtlpGrpcSpanExporterProxy, libmexclass::opentelemetry::exporters::OtlpGrpcSpanExporterProxy);
74+
REGISTER_PROXY(libmexclass.opentelemetry.exporters.OtlpGrpcMetricExporterProxy, libmexclass::opentelemetry::exporters::OtlpGrpcMetricExporterProxy);
6275
#endif
6376
return nullptr;
6477
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
classdef Counter < opentelemetry.metrics.SynchronousInstrument
2+
% Counter is a value that accumulates over time and can only increase
3+
% but not decrease.
4+
5+
% Copyright 2023 The MathWorks, Inc.
6+
7+
methods (Access={?opentelemetry.metrics.Meter})
8+
function obj = Counter(proxy, name, description, unit)
9+
% Private constructor. Use createCounter method of Meter
10+
% to create Counters.
11+
[email protected](proxy, name, description, unit);
12+
end
13+
end
14+
15+
methods
16+
function add(obj, value, varargin)
17+
obj.processValue(value, varargin{:});
18+
end
19+
end
20+
end
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
classdef Histogram < opentelemetry.metrics.SynchronousInstrument
2+
% Histogram is an instrument that aggregates values into bins
3+
4+
% Copyright 2023 The MathWorks, Inc.
5+
6+
methods (Access={?opentelemetry.metrics.Meter})
7+
function obj = Histogram(proxy, name, description, unit)
8+
% Private constructor. Use createHistogram method of Meter
9+
% to create Histograms.
10+
[email protected](proxy, name, description, unit);
11+
end
12+
end
13+
14+
methods
15+
function record(obj, value, varargin)
16+
obj.processValue(value, varargin{:});
17+
end
18+
end
19+
end
Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
classdef Meter < handle
2+
% A Meter creates metric instruments, capturing measurements about a service at runtime.
3+
% Meters are created from Meter Providers.
4+
5+
% Copyright 2023 The MathWorks, Inc.
6+
7+
properties (SetAccess=immutable)
8+
Name (1,1) string % Meter name
9+
Version (1,1) string % Meter version
10+
Schema (1,1) string % URL that documents the schema of the generated spans
11+
end
12+
13+
properties (Access=private)
14+
Proxy % Proxy object to interface C++ code
15+
end
16+
17+
methods (Access={?opentelemetry.sdk.metrics.MeterProvider, ?opentelemetry.metrics.MeterProvider})
18+
19+
function obj = Meter(proxy, mtname, mtversion, mtschema)
20+
% Private constructor. Use getMeter method of MeterProvider
21+
% to create Meters.
22+
obj.Proxy = proxy;
23+
obj.Name = mtname;
24+
obj.Version = mtversion;
25+
obj.Schema = mtschema;
26+
end
27+
28+
end
29+
30+
methods
31+
32+
function counter = createCounter(obj, ctname, ctdescription, ctunit)
33+
arguments
34+
obj
35+
ctname
36+
ctdescription = ""
37+
ctunit = ""
38+
end
39+
import opentelemetry.common.mustBeScalarString
40+
ctname = mustBeScalarString(ctname);
41+
% cpp-opentelemetry end does not allow string input with spaces,
42+
% replace any spaces with underscores as a temporary fix
43+
ctdescription = mustBeScalarString(ctdescription);
44+
ctunit = mustBeScalarString(ctunit);
45+
id = obj.Proxy.createCounter(ctname, ctdescription, ctunit);
46+
CounterProxy = libmexclass.proxy.Proxy("Name", ...
47+
"libmexclass.opentelemetry.CounterProxy", "ID", id);
48+
counter = opentelemetry.metrics.Counter(CounterProxy, ctname, ctdescription, ctunit);
49+
end
50+
51+
52+
function updowncounter = createUpDownCounter(obj, ctname, ctdescription, ctunit)
53+
arguments
54+
obj
55+
ctname
56+
ctdescription = ""
57+
ctunit = ""
58+
end
59+
60+
import opentelemetry.common.mustBeScalarString
61+
ctname = mustBeScalarString(ctname);
62+
% cpp-opentelemetry end does not allow string input with spaces,
63+
% replace any spaces with underscores as a temporary fix
64+
ctdescription = mustBeScalarString(ctdescription);
65+
ctunit = mustBeScalarString(ctunit);
66+
id = obj.Proxy.createUpDownCounter(ctname, ctdescription, ctunit);
67+
UpDownCounterProxy = libmexclass.proxy.Proxy("Name", ...
68+
"libmexclass.opentelemetry.UpDownCounterProxy", "ID", id);
69+
updowncounter = opentelemetry.metrics.UpDownCounter(UpDownCounterProxy, ctname, ctdescription, ctunit);
70+
end
71+
72+
73+
function histogram = createHistogram(obj, hiname, hidescription, hiunit)
74+
arguments
75+
obj
76+
hiname
77+
hidescription = ""
78+
hiunit = ""
79+
end
80+
81+
import opentelemetry.common.mustBeScalarString
82+
hiname = mustBeScalarString(hiname);
83+
% cpp-opentelemetry end does not allow string input with spaces,
84+
% replace any spaces with underscores as a temporary fix
85+
hidescription = mustBeScalarString(hidescription);
86+
hiunit = mustBeScalarString(hiunit);
87+
id = obj.Proxy.createHistogram(hiname, hidescription, hiunit);
88+
HistogramProxy = libmexclass.proxy.Proxy("Name", ...
89+
"libmexclass.opentelemetry.HistogramProxy", "ID", id);
90+
histogram = opentelemetry.metrics.Histogram(HistogramProxy, hiname, hidescription, hiunit);
91+
end
92+
93+
end
94+
95+
end
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
classdef MeterProvider < handle
2+
% A meter provider stores a set of configurations used in a distributed
3+
% metrics system.
4+
5+
% Copyright 2023 The MathWorks, Inc.
6+
7+
properties (Access={?opentelemetry.sdk.metrics.MeterProvider, ?opentelemetry.sdk.common.Cleanup})
8+
Proxy % Proxy object to interface C++ code
9+
end
10+
11+
methods (Access={?opentelemetry.metrics.Provider, ?opentelemetry.sdk.metrics.MeterProvider})
12+
function obj = MeterProvider(skip)
13+
% constructor
14+
% "skip" input signals skipping construction
15+
if nargin < 1 || skip ~= "skip"
16+
obj.Proxy = libmexclass.proxy.Proxy("Name", ...
17+
"libmexclass.opentelemetry.MeterProviderProxy", ...
18+
"ConstructorArguments", {});
19+
end
20+
end
21+
end
22+
23+
methods
24+
function meter = getMeter(obj, mname, mversion, mschema)
25+
% GETMETER Create a meter object used to generate metrics.
26+
% M = GETMETER(MP, NAME) returns a meter with the name
27+
% NAME that uses all the configurations specified in meter
28+
% provider MP.
29+
%
30+
% M = GETMETER(MP, NAME, VERSION, SCHEMA) also specifies
31+
% the meter version and the URL that documents the schema
32+
% of the generated meters.
33+
%
34+
% See also OPENTELEMETRY.METRICS.METER
35+
arguments
36+
obj
37+
mname
38+
mversion = ""
39+
mschema = ""
40+
end
41+
% name, version, schema accepts any types that can convert to a
42+
% string
43+
import opentelemetry.common.mustBeScalarString
44+
mname = mustBeScalarString(mname);
45+
mversion = mustBeScalarString(mversion);
46+
mschema = mustBeScalarString(mschema);
47+
id = obj.Proxy.getMeter(mname, mversion, mschema);
48+
meterproxy = libmexclass.proxy.Proxy("Name", ...
49+
"libmexclass.opentelemetry.MeterProxy", "ID", id);
50+
meter = opentelemetry.metrics.Meter(meterproxy, mname, mversion, mschema);
51+
end
52+
53+
function setMeterProvider(obj)
54+
% SETMETERPROVIDER Set global instance of meter provider
55+
% SETMETERPROVIDER(MP) sets the meter provider MP as
56+
% the global instance.
57+
%
58+
% See also OPENTELEMETRY.METRICS.PROVIDER.GETMETERPROVIDER
59+
obj.Proxy.setMeterProvider();
60+
end
61+
end
62+
63+
methods(Access=?opentelemetry.sdk.common.Cleanup)
64+
function postShutdown(obj)
65+
% POSTSHUTDOWN Handle post-shutdown tasks
66+
obj.Proxy.postShutdown();
67+
end
68+
end
69+
end
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
classdef Provider
2+
% Get and set the global instance of meter provider
3+
4+
% Copyright 2023 The MathWorks, Inc.
5+
6+
methods (Static)
7+
function p = getMeterProvider()
8+
% Get the global instance of meter provider
9+
% MP = OPENTELEMETRY.METRICS.PROVIDER.GETMETERPROVIDER gets
10+
% the global instance of meter provider.
11+
%
12+
% See also OPENTELEMETRY.METRICS.PROVIDER.SETMETERPROVIDER
13+
14+
p = opentelemetry.metrics.MeterProvider();
15+
end
16+
17+
function setMeterProvider(p)
18+
% Set the global instance of meter provider
19+
% OPENTELEMETRY.METRICS.PROVIDER.GETMETERPROVIDER(MP) sets
20+
% MP as the global instance of meter provider.
21+
%
22+
% See also OPENTELEMETRY.METRICS.PROVIDER.GETMETERPROVIDER
23+
p.setMeterProvider();
24+
end
25+
end
26+
27+
end
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
classdef SynchronousInstrument < handle
2+
% Base class inherited by all synchronous instruments
3+
4+
% Copyright 2023 The MathWorks, Inc.
5+
6+
properties (SetAccess=immutable)
7+
Name (1,1) string
8+
Description (1,1) string
9+
Unit (1,1) string
10+
end
11+
12+
properties (Access=private)
13+
Proxy % Proxy object to interface C++ code
14+
end
15+
16+
methods (Access=protected)
17+
function obj = SynchronousInstrument(proxy, name, description, unit)
18+
obj.Proxy = proxy;
19+
obj.Name = name;
20+
obj.Description = description;
21+
obj.Unit = unit;
22+
end
23+
24+
function processValue(obj, value, varargin)
25+
% input value must be a numerical real scalar
26+
if isnumeric(value) && isscalar(value) && isreal(value)
27+
if nargin == 2
28+
obj.Proxy.processValue(value);
29+
elseif isa(varargin{1}, "dictionary")
30+
attrkeys = keys(varargin{1});
31+
attrvals = values(varargin{1},"cell");
32+
if all(cellfun(@iscell, attrvals))
33+
attrvals = [attrvals{:}];
34+
end
35+
obj.Proxy.processValue(value, attrkeys, attrvals);
36+
else
37+
attrkeys = [varargin{1:2:length(varargin)}]';
38+
attrvals = [varargin(2:2:length(varargin))]';
39+
obj.Proxy.processValue(value, attrkeys, attrvals);
40+
end
41+
end
42+
end
43+
end
44+
end

0 commit comments

Comments
 (0)