Skip to content

Commit

Permalink
prepare for merging metrics branch into main
Browse files Browse the repository at this point in the history
  • Loading branch information
duncanpo committed Nov 13, 2023
2 parents bb15a99 + f3e7a37 commit 1e33556
Show file tree
Hide file tree
Showing 43 changed files with 2,773 additions and 73 deletions.
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
36 changes: 29 additions & 7 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -212,6 +212,11 @@ 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
${METRICS_API_SOURCE_DIR}/HistogramProxy.cpp
${CONTEXT_API_SOURCE_DIR}/TextMapPropagatorProxy.cpp
${CONTEXT_API_SOURCE_DIR}/CompositePropagatorProxy.cpp
${CONTEXT_API_SOURCE_DIR}/TextMapCarrierProxy.cpp
Expand All @@ -221,14 +226,21 @@ set(OPENTELEMETRY_PROXY_SOURCES
${TRACE_SDK_SOURCE_DIR}/SimpleSpanProcessorProxy.cpp
${TRACE_SDK_SOURCE_DIR}/BatchSpanProcessorProxy.cpp
${TRACE_SDK_SOURCE_DIR}/ParentBasedSamplerProxy.cpp
${METRICS_SDK_SOURCE_DIR}/MeterProviderProxy.cpp
${METRICS_SDK_SOURCE_DIR}/ViewProxy.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 @@ -377,9 +389,17 @@ set(CONTEXT_API_MATLAB_SOURCES ${CMAKE_CURRENT_SOURCE_DIR}/api/context/+opentele
set(BAGGAGE_API_MATLAB_SOURCES ${CMAKE_CURRENT_SOURCE_DIR}/api/baggage/+opentelemetry)
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(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(METRICS_SDK_MATLAB_SOURCES ${CMAKE_CURRENT_SOURCE_DIR}/sdk/metrics/+opentelemetry)
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 @@ -388,6 +408,8 @@ install(DIRECTORY ${CONTEXT_API_MATLAB_SOURCES} DESTINATION .)
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
13 changes: 13 additions & 0 deletions OtelMatlabProxyFactory.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,17 +22,23 @@
#include "opentelemetry-matlab/sdk/trace/AlwaysOffSamplerProxy.h"
#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/ViewProxy.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 @@ -54,11 +60,18 @@ OtelMatlabProxyFactory::make_proxy(const libmexclass::proxy::ClassName& class_na
REGISTER_PROXY(libmexclass.opentelemetry.sdk.AlwaysOffSamplerProxy, libmexclass::opentelemetry::sdk::AlwaysOffSamplerProxy);
REGISTER_PROXY(libmexclass.opentelemetry.sdk.TraceIdRatioBasedSamplerProxy, libmexclass::opentelemetry::sdk::TraceIdRatioBasedSamplerProxy);
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.ViewProxy, libmexclass::opentelemetry::sdk::ViewProxy);
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;
}
20 changes: 20 additions & 0 deletions api/metrics/+opentelemetry/+metrics/Counter.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
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.

methods (Access={?opentelemetry.metrics.Meter})
function obj = Counter(proxy, name, description, unit)
% Private constructor. Use createCounter method of Meter
% to create Counters.
[email protected](proxy, name, description, unit);
end
end

methods
function add(obj, value, varargin)
obj.processValue(value, varargin{:});
end
end
end
19 changes: 19 additions & 0 deletions api/metrics/+opentelemetry/+metrics/Histogram.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
classdef Histogram < opentelemetry.metrics.SynchronousInstrument
% Histogram is an instrument that aggregates values into bins

% Copyright 2023 The MathWorks, Inc.

methods (Access={?opentelemetry.metrics.Meter})
function obj = Histogram(proxy, name, description, unit)
% Private constructor. Use createHistogram method of Meter
% to create Histograms.
[email protected](proxy, name, description, unit);
end
end

methods
function record(obj, value, varargin)
obj.processValue(value, varargin{:});
end
end
end
95 changes: 95 additions & 0 deletions api/metrics/+opentelemetry/+metrics/Meter.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
classdef Meter < handle
% A Meter creates metric instruments, capturing measurements about a service at runtime.
% Meters are created from Meter Providers.

% Copyright 2023 The MathWorks, Inc.

properties (SetAccess=immutable)
Name (1,1) string % Meter name
Version (1,1) string % Meter version
Schema (1,1) string % URL that documents the schema of the generated spans
end

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

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

function obj = Meter(proxy, mtname, mtversion, mtschema)
% Private constructor. Use getMeter method of MeterProvider
% to create Meters.
obj.Proxy = proxy;
obj.Name = mtname;
obj.Version = mtversion;
obj.Schema = mtschema;
end

end

methods

function counter = createCounter(obj, ctname, ctdescription, ctunit)
arguments
obj
ctname
ctdescription = ""
ctunit = ""
end
import opentelemetry.common.mustBeScalarString
ctname = mustBeScalarString(ctname);
% cpp-opentelemetry end does not allow string input with spaces,
% replace any spaces with underscores as a temporary fix
ctdescription = mustBeScalarString(ctdescription);
ctunit = mustBeScalarString(ctunit);
id = obj.Proxy.createCounter(ctname, ctdescription, ctunit);
CounterProxy = libmexclass.proxy.Proxy("Name", ...
"libmexclass.opentelemetry.CounterProxy", "ID", id);
counter = opentelemetry.metrics.Counter(CounterProxy, ctname, ctdescription, ctunit);
end


function updowncounter = createUpDownCounter(obj, ctname, ctdescription, ctunit)
arguments
obj
ctname
ctdescription = ""
ctunit = ""
end

import opentelemetry.common.mustBeScalarString
ctname = mustBeScalarString(ctname);
% cpp-opentelemetry end does not allow string input with spaces,
% replace any spaces with underscores as a temporary fix
ctdescription = mustBeScalarString(ctdescription);
ctunit = mustBeScalarString(ctunit);
id = obj.Proxy.createUpDownCounter(ctname, ctdescription, ctunit);
UpDownCounterProxy = libmexclass.proxy.Proxy("Name", ...
"libmexclass.opentelemetry.UpDownCounterProxy", "ID", id);
updowncounter = opentelemetry.metrics.UpDownCounter(UpDownCounterProxy, ctname, ctdescription, ctunit);
end


function histogram = createHistogram(obj, hiname, hidescription, hiunit)
arguments
obj
hiname
hidescription = ""
hiunit = ""
end

import opentelemetry.common.mustBeScalarString
hiname = mustBeScalarString(hiname);
% cpp-opentelemetry end does not allow string input with spaces,
% replace any spaces with underscores as a temporary fix
hidescription = mustBeScalarString(hidescription);
hiunit = mustBeScalarString(hiunit);
id = obj.Proxy.createHistogram(hiname, hidescription, hiunit);
HistogramProxy = libmexclass.proxy.Proxy("Name", ...
"libmexclass.opentelemetry.HistogramProxy", "ID", id);
histogram = opentelemetry.metrics.Histogram(HistogramProxy, hiname, hidescription, hiunit);
end

end

end
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
44 changes: 44 additions & 0 deletions api/metrics/+opentelemetry/+metrics/SynchronousInstrument.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
classdef SynchronousInstrument < handle
% Base class inherited by all synchronous instruments

% 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=protected)
function obj = SynchronousInstrument(proxy, name, description, unit)
obj.Proxy = proxy;
obj.Name = name;
obj.Description = description;
obj.Unit = unit;
end

function processValue(obj, value, varargin)
% input value must be a numerical real scalar
if isnumeric(value) && isscalar(value) && isreal(value)
if nargin == 2
obj.Proxy.processValue(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.processValue(value, attrkeys, attrvals);
else
attrkeys = [varargin{1:2:length(varargin)}]';
attrvals = [varargin(2:2:length(varargin))]';
obj.Proxy.processValue(value, attrkeys, attrvals);
end
end
end
end
end
Loading

0 comments on commit 1e33556

Please sign in to comment.