Skip to content

Commit

Permalink
Merge branch 'metrics' into changes_after_v_1_3_0
Browse files Browse the repository at this point in the history
  • Loading branch information
duncanpo authored Oct 3, 2023
2 parents 9989441 + f59aa11 commit 083ee56
Show file tree
Hide file tree
Showing 18 changed files with 1,156 additions and 1 deletion.
18 changes: 17 additions & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -188,21 +188,25 @@ if(WITH_OTLP_GRPC)
endif()

set(TRACE_API_INCLUDE_DIR ${CMAKE_CURRENT_SOURCE_DIR}/api/trace/include)
set(METRICS_API_INCLUDE_DIR ${CMAKE_CURRENT_SOURCE_DIR}/api/metrics/include)
set(CONTEXT_API_INCLUDE_DIR ${CMAKE_CURRENT_SOURCE_DIR}/api/context/include)
set(BAGGAGE_API_INCLUDE_DIR ${CMAKE_CURRENT_SOURCE_DIR}/api/baggage/include)
set(COMMON_API_INCLUDE_DIR ${CMAKE_CURRENT_SOURCE_DIR}/api/common/include)
set(TRACE_SDK_INCLUDE_DIR ${CMAKE_CURRENT_SOURCE_DIR}/sdk/trace/include)
set(METRICS_SDK_INCLUDE_DIR ${CMAKE_CURRENT_SOURCE_DIR}/sdk/metrics/include)
set(COMMON_SDK_INCLUDE_DIR ${CMAKE_CURRENT_SOURCE_DIR}/sdk/common/include)
set(OTLP_EXPORTER_INCLUDE_DIR ${CMAKE_CURRENT_SOURCE_DIR}/exporters/otlp/include)
set(OPENTELEMETRY_PROXY_INCLUDE_DIRS ${CMAKE_CURRENT_SOURCE_DIR} ${TRACE_API_INCLUDE_DIR} ${CONTEXT_API_INCLUDE_DIR} ${BAGGAGE_API_INCLUDE_DIR} ${COMMON_API_INCLUDE_DIR} ${TRACE_SDK_INCLUDE_DIR} ${COMMON_SDK_INCLUDE_DIR} ${OTLP_EXPORTER_INCLUDE_DIR} ${OTEL_CPP_PREFIX}/include)
set(OPENTELEMETRY_PROXY_INCLUDE_DIRS ${CMAKE_CURRENT_SOURCE_DIR} ${TRACE_API_INCLUDE_DIR} ${METRICS_API_INCLUDE_DIR} ${CONTEXT_API_INCLUDE_DIR} ${BAGGAGE_API_INCLUDE_DIR} ${COMMON_API_INCLUDE_DIR} ${TRACE_SDK_INCLUDE_DIR} ${METRICS_SDK_INCLUDE_DIR} ${COMMON_SDK_INCLUDE_DIR} ${OTLP_EXPORTER_INCLUDE_DIR} ${OTEL_CPP_PREFIX}/include)

set(OPENTELEMETRY_PROXY_FACTORY_CLASS_NAME OtelMatlabProxyFactory)
set(OPENTELEMETRY_PROXY_FACTORY_SOURCES_DIR ${CMAKE_CURRENT_SOURCE_DIR})
set(TRACE_API_SOURCE_DIR ${CMAKE_CURRENT_SOURCE_DIR}/api/trace/src)
set(METRICS_API_SOURCE_DIR ${CMAKE_CURRENT_SOURCE_DIR}/api/metrics/src)
set(CONTEXT_API_SOURCE_DIR ${CMAKE_CURRENT_SOURCE_DIR}/api/context/src)
set(BAGGAGE_API_SOURCE_DIR ${CMAKE_CURRENT_SOURCE_DIR}/api/baggage/src)
set(COMMON_API_SOURCE_DIR ${CMAKE_CURRENT_SOURCE_DIR}/api/common/src)
set(TRACE_SDK_SOURCE_DIR ${CMAKE_CURRENT_SOURCE_DIR}/sdk/trace/src)
set(METRICS_SDK_SOURCE_DIR ${CMAKE_CURRENT_SOURCE_DIR}/sdk/metrics/src)
set(COMMON_SDK_SOURCE_DIR ${CMAKE_CURRENT_SOURCE_DIR}/sdk/common/src)
set(OTLP_EXPORTER_SOURCE_DIR ${CMAKE_CURRENT_SOURCE_DIR}/exporters/otlp/src)
set(OPENTELEMETRY_PROXY_SOURCES
Expand All @@ -212,6 +216,10 @@ 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}/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,6 +229,7 @@ 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
${COMMON_SDK_SOURCE_DIR}/resource.cpp)
if(WITH_OTLP_HTTP)
set(OPENTELEMETRY_PROXY_SOURCES ${OPENTELEMETRY_PROXY_SOURCES}
Expand Down Expand Up @@ -277,16 +286,19 @@ set(OTEL_CPP_LINK_LIBRARIES ${OTEL_CPP_PREFIX}/lib/${CMAKE_STATIC_LIBRARY_PREFIX
${OTEL_CPP_PREFIX}/lib/${CMAKE_STATIC_LIBRARY_PREFIX}opentelemetry_trace${CMAKE_STATIC_LIBRARY_SUFFIX}
${OTEL_CPP_PREFIX}/lib/${CMAKE_STATIC_LIBRARY_PREFIX}opentelemetry_version${CMAKE_STATIC_LIBRARY_SUFFIX}
${OTEL_CPP_PREFIX}/lib/${CMAKE_STATIC_LIBRARY_PREFIX}opentelemetry_logs${CMAKE_STATIC_LIBRARY_SUFFIX}
${OTEL_CPP_PREFIX}/lib/${CMAKE_STATIC_LIBRARY_PREFIX}opentelemetry_metrics${CMAKE_STATIC_LIBRARY_SUFFIX}
${Protobuf_LIBRARIES})
if(WITH_OTLP_HTTP)
set(OTEL_CPP_LINK_LIBRARIES ${OTEL_CPP_LINK_LIBRARIES} ${OTEL_CPP_PREFIX}/lib/${CMAKE_STATIC_LIBRARY_PREFIX}opentelemetry_exporter_otlp_http${CMAKE_STATIC_LIBRARY_SUFFIX}
${OTEL_CPP_PREFIX}/lib/${CMAKE_STATIC_LIBRARY_PREFIX}opentelemetry_exporter_otlp_http_client${CMAKE_STATIC_LIBRARY_SUFFIX}
${OTEL_CPP_PREFIX}/lib/${CMAKE_STATIC_LIBRARY_PREFIX}opentelemetry_exporter_otlp_http_metric${CMAKE_STATIC_LIBRARY_SUFFIX}
${OTEL_CPP_PREFIX}/lib/${CMAKE_STATIC_LIBRARY_PREFIX}opentelemetry_http_client_curl${CMAKE_STATIC_LIBRARY_SUFFIX}
${CURL_LIBRARIES})
endif()
if(WITH_OTLP_GRPC)
set(OTEL_CPP_LINK_LIBRARIES ${OTEL_CPP_LINK_LIBRARIES} ${OTEL_CPP_PREFIX}/lib/${CMAKE_STATIC_LIBRARY_PREFIX}opentelemetry_exporter_otlp_grpc${CMAKE_STATIC_LIBRARY_SUFFIX}
${OTEL_CPP_PREFIX}/lib/${CMAKE_STATIC_LIBRARY_PREFIX}opentelemetry_exporter_otlp_grpc_client${CMAKE_STATIC_LIBRARY_SUFFIX}
${OTEL_CPP_PREFIX}/lib/${CMAKE_STATIC_LIBRARY_PREFIX}opentelemetry_exporter_otlp_grpc_metrics${CMAKE_STATIC_LIBRARY_SUFFIX}
${OTEL_CPP_PREFIX}/lib/${CMAKE_SHARED_LIBRARY_PREFIX}opentelemetry_proto_grpc${OTEL_PROTO_LIBRARY_SUFFIX}
gRPC::grpc++
absl::synchronization)
Expand Down Expand Up @@ -373,21 +385,25 @@ libmexclass_client_install(

# Install M files
set(TRACE_API_MATLAB_SOURCES ${CMAKE_CURRENT_SOURCE_DIR}/api/trace/+opentelemetry)
set(METRICS_API_MATLAB_SOURCES ${CMAKE_CURRENT_SOURCE_DIR}/api/metrics/+opentelemetry)
set(CONTEXT_API_MATLAB_SOURCES ${CMAKE_CURRENT_SOURCE_DIR}/api/context/+opentelemetry)
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(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(OTLP_EXPORTERS_DIR +opentelemetry/+exporters/+otlp)

install(DIRECTORY ${TRACE_API_MATLAB_SOURCES} DESTINATION .)
install(DIRECTORY ${METRICS_API_MATLAB_SOURCES} DESTINATION .)
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(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
4 changes: 4 additions & 0 deletions OtelMatlabProxyFactory.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
#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"
#ifdef WITH_OTLP_HTTP
#include "opentelemetry-matlab/exporters/otlp/OtlpHttpSpanExporterProxy.h"
#endif
Expand Down Expand Up @@ -54,6 +55,9 @@ 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);

#ifdef WITH_OTLP_HTTP
REGISTER_PROXY(libmexclass.opentelemetry.exporters.OtlpHttpSpanExporterProxy, libmexclass::opentelemetry::exporters::OtlpHttpSpanExporterProxy);
#endif
Expand Down
59 changes: 59 additions & 0 deletions api/metrics/+opentelemetry/+metrics/Counter.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
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.

% 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)
% Private constructor. Use createCounter method of Meter
% to create Counters.
obj.Proxy = proxy;
obj.Name = ctname;
obj.Description = ctdescription;
obj.Unit = ctunit;
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

end

end


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

% 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)
% Private constructor. Use createHistogram method of Meter
% to create Histograms.
obj.Proxy = proxy;
obj.Name = hiname;
obj.Description = hidescription;
obj.Unit = hiunit;
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

end

end


end
89 changes: 89 additions & 0 deletions api/metrics/+opentelemetry/+metrics/Meter.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
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})

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);
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);
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);
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
58 changes: 58 additions & 0 deletions api/metrics/+opentelemetry/+metrics/UpDownCounter.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
classdef UpDownCounter < handle
% UpDownCounter is an instrument that adds or reduce values.

% 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 = UpDownCounter(proxy, ctname, ctdescription, ctunit)
% Private constructor. Use createUpDownCounter method of Meter
% to create UpDownCounters.
obj.Proxy = proxy;
obj.Name = ctname;
obj.Description = ctdescription;
obj.Unit = ctunit;
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

end

end


end
Loading

0 comments on commit 083ee56

Please sign in to comment.