Skip to content

Commit 5cd5d03

Browse files
authored
Merge pull request #183 from mathworks/synchronous_gauge
Synchronous gauge
2 parents 91e1dd9 + 129c89f commit 5cd5d03

File tree

24 files changed

+360
-32
lines changed

24 files changed

+360
-32
lines changed

CMakeLists.txt

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -195,6 +195,9 @@ if(NOT DEFINED OTEL_CPP_INSTALLED_DIR)
195195

196196
set(OTEL_CPP_CXX_STANDARD 14)
197197

198+
# The synchronous gauge metric instrument is only defined in ABI version 2
199+
set(OTEL_CPP_ABI_VERSION OPENTELEMETRY_ABI_VERSION_NO=2)
200+
198201
set(PATCHES_DIR ${CMAKE_SOURCE_DIR}/patches)
199202
if(SKIP_OTEL_CPP_PATCH)
200203
set(patch_comand "")
@@ -209,14 +212,16 @@ if(NOT DEFINED OTEL_CPP_INSTALLED_DIR)
209212
set(patch_command "")
210213
endif()
211214

215+
# Note: examples are temporarily turned off (-DWITH_EXAMPLES=OFF) due to a build issue that is now fixed in #3284
216+
# Renable examples after pulling in the fix
212217
ExternalProject_Add(
213218
${OTEL_CPP_PROJECT_NAME}
214219
GIT_REPOSITORY ${OTEL_CPP_GIT_REPOSITORY}
215220
GIT_TAG ${OTEL_CPP_GIT_TAG}
216221
PREFIX ${OTEL_CPP_PREFIX}
217222
UPDATE_DISCONNECTED 1
218223
PATCH_COMMAND ${patch_command}
219-
CMAKE_ARGS -DCMAKE_POSITION_INDEPENDENT_CODE=ON -DCMAKE_CXX_COMPILER=${CMAKE_CXX_COMPILER} -DCMAKE_C_COMPILER=${CMAKE_C_COMPILER} -DCMAKE_BUILD_TYPE=${CMAKE_BUILD_TYPE} -DWITH_OTLP_HTTP=${WITH_OTLP_HTTP} -DWITH_OTLP_GRPC=${WITH_OTLP_GRPC} -DWITH_OTLP_FILE=${WITH_OTLP_FILE} -DBUILD_TESTING=OFF -DWITH_BENCHMARK=OFF -DOPENTELEMETRY_INSTALL=ON -DCMAKE_TOOLCHAIN_FILE=${CMAKE_TOOLCHAIN_FILE} -DCMAKE_CXX_STANDARD=${OTEL_CPP_CXX_STANDARD} -DVCPKG_INSTALLED_DIR=${VCPKG_INSTALLED_DIR} ${TRIPLET_DEFINITIONS}
224+
CMAKE_ARGS -DCMAKE_POSITION_INDEPENDENT_CODE=ON -DCMAKE_CXX_COMPILER=${CMAKE_CXX_COMPILER} -DCMAKE_C_COMPILER=${CMAKE_C_COMPILER} -DCMAKE_BUILD_TYPE=${CMAKE_BUILD_TYPE} -DWITH_ABI_VERSION_1=OFF -DWITH_ABI_VERSION_2=ON -DWITH_OTLP_HTTP=${WITH_OTLP_HTTP} -DWITH_OTLP_GRPC=${WITH_OTLP_GRPC} -DWITH_OTLP_FILE=${WITH_OTLP_FILE} -DWITH_EXAMPLES=OFF -DBUILD_TESTING=OFF -DWITH_BENCHMARK=OFF -DOPENTELEMETRY_INSTALL=ON -DCMAKE_TOOLCHAIN_FILE=${CMAKE_TOOLCHAIN_FILE} -DCMAKE_CXX_STANDARD=${OTEL_CPP_CXX_STANDARD} -DVCPKG_INSTALLED_DIR=${VCPKG_INSTALLED_DIR} ${TRIPLET_DEFINITIONS}
220225
BUILD_BYPRODUCTS ${OTEL_CPP_LIBRARIES}
221226
INSTALL_DIR ${OTEL_CPP_PREFIX}
222227
INSTALL_COMMAND ${CMAKE_COMMAND} --install . --prefix ${OTEL_CPP_PREFIX} --config $<CONFIG>
@@ -291,6 +296,7 @@ set(OPENTELEMETRY_PROXY_SOURCES
291296
${METRICS_API_SOURCE_DIR}/CounterProxy.cpp
292297
${METRICS_API_SOURCE_DIR}/UpDownCounterProxy.cpp
293298
${METRICS_API_SOURCE_DIR}/HistogramProxy.cpp
299+
${METRICS_API_SOURCE_DIR}/GaugeProxy.cpp
294300
${METRICS_API_SOURCE_DIR}/SynchronousInstrumentProxyFactory.cpp
295301
${METRICS_API_SOURCE_DIR}/MeasurementFetcher.cpp
296302
${METRICS_API_SOURCE_DIR}/AsynchronousInstrumentProxy.cpp
@@ -365,6 +371,8 @@ if(WITH_OTLP_FILE)
365371
target_compile_definitions(${OPENTELEMETRY_PROXY_LIBRARY_NAME} PRIVATE WITH_OTLP_FILE)
366372
endif()
367373

374+
target_compile_definitions(${OPENTELEMETRY_PROXY_LIBRARY_NAME} PRIVATE ${OTEL_CPP_ABI_VERSION})
375+
368376
# pass in version number
369377
target_compile_definitions(${OPENTELEMETRY_PROXY_LIBRARY_NAME} PRIVATE OTEL_MATLAB_VERSION="${OTEL_MATLAB_VERSION}")
370378
if(WIN32)
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
classdef Gauge < opentelemetry.metrics.SynchronousInstrument
2+
% Gauge is an instrument for recording non-aggregatable measurements.
3+
4+
% Copyright 2025 The MathWorks, Inc.
5+
6+
methods (Access={?opentelemetry.metrics.Meter})
7+
function obj = Gauge(proxy, name, description, unit)
8+
% Private constructor. Use createGauge method of Meter
9+
% to create gauges.
10+
[email protected](proxy, name, description, unit);
11+
end
12+
end
13+
14+
methods
15+
function record(obj, value, varargin)
16+
% RECORD Record a value
17+
% RECORD(G, VALUE) records a scalar numeric value. VALUE can be positive or negative.
18+
%
19+
% RECORD(G, VALUE, ATTRIBUTES) also specifies attributes as a
20+
% dictionary
21+
%
22+
% RECORD(G, VALUE, ATTRNAME1, ATTRVALUE1, ATTRNAME2,
23+
% ATTRVALUE2, ...) specifies attributes as trailing
24+
% name-value pairs.
25+
obj.processValue(value, varargin{:});
26+
end
27+
end
28+
end

api/metrics/+opentelemetry/+metrics/Meter.m

Lines changed: 32 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
% A Meter creates metric instruments, capturing measurements about a service at runtime.
33
% Meters are created from Meter Providers.
44

5-
% Copyright 2023-2024 The MathWorks, Inc.
5+
% Copyright 2023-2025 The MathWorks, Inc.
66

77
properties (SetAccess=immutable)
88
Name (1,1) string % Meter name
@@ -38,7 +38,7 @@
3838
% C = CREATECOUNTER(M, NAME, DESCRIPTION, UNIT) also
3939
% specifies a description and a unit.
4040
%
41-
% See also CREATEUPDOWNCOUNTER, CREATEHISTOGRAM,
41+
% See also CREATEUPDOWNCOUNTER, CREATEHISTOGRAM, CREATEGAUGE,
4242
% CREATEOBSERVABLECOUNTER
4343
arguments
4444
obj
@@ -64,7 +64,7 @@
6464
% C = CREATEUPDOWNCOUNTER(M, NAME, DESCRIPTION, UNIT) also
6565
% specifies a description and a unit.
6666
%
67-
% See also CREATECOUNTER, CREATEHISTOGRAM,
67+
% See also CREATECOUNTER, CREATEHISTOGRAM, CREATEGAUGE,
6868
% CREATEOBSERVABLEUPDOWNCOUNTER
6969
arguments
7070
obj
@@ -91,14 +91,14 @@
9191
% H = CREATEHISTOGRAM(M, NAME, DESCRIPTION, UNIT) also
9292
% specifies a description and a unit.
9393
%
94-
% See also CREATECOUNTER, CREATEUPDOWNCOUNTER,
94+
% See also CREATECOUNTER, CREATEUPDOWNCOUNTER, CREATEGAUGE,
9595
% OPENTELEMETRY.SDK.METRICS.VIEW
9696
arguments
9797
obj
9898
name
9999
description = ""
100100
unit = ""
101-
end
101+
end
102102

103103
[name, description, unit] = processSynchronousInputs(name, ...
104104
description, unit);
@@ -108,6 +108,32 @@
108108
histogram = opentelemetry.metrics.Histogram(HistogramProxy, name, description, unit);
109109
end
110110

111+
function gauge = createGauge(obj, name, description, unit)
112+
% CREATEGAUGE Create a gauge
113+
% G = CREATEGAUGE(M, NAME) creates a gauge
114+
% with the specified name. A gauge's value can increase or
115+
% decrease but it should never be summed in aggregation.
116+
%
117+
% G = CREATEGAUGE(M, NAME, DESCRIPTION, UNIT) also
118+
% specifies a description and a unit.
119+
%
120+
% See also CREATECOUNTER, CREATEUPDOWNCOUNTER, CREATEHISTOGRAM,
121+
% CREATEOBSERVABLEGAUGE
122+
arguments
123+
obj
124+
name
125+
description = ""
126+
unit = ""
127+
end
128+
129+
[name, description, unit] = processSynchronousInputs(name, ...
130+
description, unit);
131+
id = obj.Proxy.createGauge(name, description, unit);
132+
GaugeProxy = libmexclass.proxy.Proxy("Name", ...
133+
"libmexclass.opentelemetry.GaugeProxy", "ID", id);
134+
gauge = opentelemetry.metrics.Gauge(GaugeProxy, name, description, unit);
135+
end
136+
111137
function obscounter = createObservableCounter(obj, callback, name, ...
112138
description, unit, timeout)
113139
% CREATEOBSERVABLECOUNTER Create an observable counter
@@ -205,7 +231,7 @@
205231
% positive duration scalar.
206232
%
207233
% See also OPENTELEMETRY.METRICS.OBSERVABLERESULT,
208-
% CREATEOBSERVABLECOUNTER, CREATEOBSERVABLEUPDOWNCOUNTER
234+
% CREATEGAUGE, CREATEOBSERVABLECOUNTER, CREATEOBSERVABLEUPDOWNCOUNTER
209235
arguments
210236
obj
211237
callback
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
// Copyright 2025 The MathWorks, Inc.
2+
3+
#pragma once
4+
5+
#include "libmexclass/proxy/Proxy.h"
6+
#include "libmexclass/proxy/method/Context.h"
7+
8+
#include "opentelemetry/metrics/meter.h"
9+
#include "opentelemetry/metrics/sync_instruments.h"
10+
11+
#include "opentelemetry-matlab/common/attribute.h"
12+
#include "opentelemetry-matlab/common/ProcessedAttributes.h"
13+
14+
namespace metrics_api = opentelemetry::metrics;
15+
namespace nostd = opentelemetry::nostd;
16+
17+
namespace libmexclass::opentelemetry {
18+
class GaugeProxy : public libmexclass::proxy::Proxy {
19+
public:
20+
GaugeProxy(nostd::shared_ptr<metrics_api::Gauge<double> > g) : CppGauge(g) {
21+
REGISTER_METHOD(GaugeProxy, processValue);
22+
}
23+
24+
void processValue(libmexclass::proxy::method::Context& context);
25+
26+
private:
27+
28+
nostd::shared_ptr<metrics_api::Gauge<double> > CppGauge;
29+
30+
};
31+
} // namespace libmexclass::opentelemetry
32+
33+

api/metrics/include/opentelemetry-matlab/metrics/MeterProxy.h

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Copyright 2023-2024 The MathWorks, Inc.
1+
// Copyright 2023-2025 The MathWorks, Inc.
22

33
#pragma once
44

@@ -8,6 +8,7 @@
88
#include "opentelemetry-matlab/metrics/CounterProxy.h"
99
#include "opentelemetry-matlab/metrics/HistogramProxy.h"
1010
#include "opentelemetry-matlab/metrics/UpDownCounterProxy.h"
11+
#include "opentelemetry-matlab/metrics/GaugeProxy.h"
1112
#include "opentelemetry-matlab/metrics/ObservableCounterProxy.h"
1213
#include "opentelemetry-matlab/metrics/ObservableUpDownCounterProxy.h"
1314
#include "opentelemetry-matlab/metrics/ObservableGaugeProxy.h"
@@ -27,6 +28,7 @@ class MeterProxy : public libmexclass::proxy::Proxy {
2728
REGISTER_METHOD(MeterProxy, createCounter);
2829
REGISTER_METHOD(MeterProxy, createUpDownCounter);
2930
REGISTER_METHOD(MeterProxy, createHistogram);
31+
REGISTER_METHOD(MeterProxy, createGauge);
3032
REGISTER_METHOD(MeterProxy, createObservableCounter);
3133
REGISTER_METHOD(MeterProxy, createObservableUpDownCounter);
3234
REGISTER_METHOD(MeterProxy, createObservableGauge);
@@ -38,6 +40,8 @@ class MeterProxy : public libmexclass::proxy::Proxy {
3840

3941
void createHistogram(libmexclass::proxy::method::Context& context);
4042

43+
void createGauge(libmexclass::proxy::method::Context& context);
44+
4145
void createObservableCounter(libmexclass::proxy::method::Context& context);
4246

4347
void createObservableUpDownCounter(libmexclass::proxy::method::Context& context);

api/metrics/include/opentelemetry-matlab/metrics/SynchronousInstrumentProxyFactory.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Copyright 2023 The MathWorks, Inc.
1+
// Copyright 2023-2025 The MathWorks, Inc.
22

33
#pragma once
44

@@ -11,7 +11,7 @@ namespace nostd = opentelemetry::nostd;
1111

1212
namespace libmexclass::opentelemetry {
1313

14-
enum class SynchronousInstrumentType {Counter, UpDownCounter, Histogram};
14+
enum class SynchronousInstrumentType {Counter, UpDownCounter, Histogram, Gauge};
1515

1616
class SynchronousInstrumentProxyFactory {
1717
public:

api/metrics/src/GaugeProxy.cpp

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
// Copyright 2025 The MathWorks, Inc.
2+
3+
#include "opentelemetry-matlab/metrics/GaugeProxy.h"
4+
5+
#include "MatlabDataArray.hpp"
6+
7+
namespace libmexclass::opentelemetry {
8+
9+
10+
void GaugeProxy::processValue(libmexclass::proxy::method::Context& context){
11+
12+
matlab::data::Array value_mda = context.inputs[0];
13+
double value = static_cast<double>(value_mda[0]);
14+
size_t nin = context.inputs.getNumberOfElements();
15+
if (nin == 1){
16+
CppGauge->Record(value);
17+
}
18+
// add attributes
19+
else {
20+
ProcessedAttributes attrs;
21+
matlab::data::StringArray attrnames_mda = context.inputs[1];
22+
matlab::data::Array attrvalues_mda = context.inputs[2];
23+
size_t nattrs = attrnames_mda.getNumberOfElements();
24+
for (size_t i = 0; i < nattrs; i ++){
25+
std::string attrname = static_cast<std::string>(attrnames_mda[i]);
26+
matlab::data::Array attrvalue = attrvalues_mda[i];
27+
processAttribute(attrname, attrvalue, attrs);
28+
}
29+
CppGauge->Record(value, attrs.Attributes);
30+
}
31+
32+
}
33+
34+
35+
36+
} // namespace libmexclass::opentelemetry

api/metrics/src/MeterProxy.cpp

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Copyright 2023-2024 The MathWorks, Inc.
1+
// Copyright 2023-2025 The MathWorks, Inc.
22

33
#include "opentelemetry-matlab/metrics/MeterProxy.h"
44
#include "opentelemetry-matlab/metrics/MeasurementFetcher.h"
@@ -46,6 +46,10 @@ void MeterProxy::createHistogram(libmexclass::proxy::method::Context& context) {
4646
createSynchronous(context, SynchronousInstrumentType::Histogram);
4747
}
4848

49+
void MeterProxy::createGauge(libmexclass::proxy::method::Context& context) {
50+
createSynchronous(context, SynchronousInstrumentType::Gauge);
51+
}
52+
4953
void MeterProxy::createAsynchronous(libmexclass::proxy::method::Context& context, AsynchronousInstrumentType type) {
5054
// Always assumes 4 inputs
5155
matlab::data::StringArray name_mda = context.inputs[0];

api/metrics/src/SynchronousInstrumentProxyFactory.cpp

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
1-
// Copyright 2023 The MathWorks, Inc.
1+
// Copyright 2023-2025 The MathWorks, Inc.
22

33
#include "opentelemetry-matlab/metrics/SynchronousInstrumentProxyFactory.h"
44
#include "opentelemetry-matlab/metrics/CounterProxy.h"
55
#include "opentelemetry-matlab/metrics/HistogramProxy.h"
66
#include "opentelemetry-matlab/metrics/UpDownCounterProxy.h"
7+
#include "opentelemetry-matlab/metrics/GaugeProxy.h"
78

89

910
namespace libmexclass::opentelemetry {
@@ -29,6 +30,12 @@ std::shared_ptr<libmexclass::proxy::Proxy> SynchronousInstrumentProxyFactory::cr
2930
proxy = std::shared_ptr<libmexclass::proxy::Proxy>(new HistogramProxy(hist));
3031
}
3132
break;
33+
case SynchronousInstrumentType::Gauge:
34+
{
35+
nostd::shared_ptr<metrics_api::Gauge<double> > g = std::move(CppMeter->CreateDoubleGauge(name, description, unit));
36+
proxy = std::shared_ptr<libmexclass::proxy::Proxy>(new GaugeProxy(g));
37+
}
38+
break;
3239
}
3340
return proxy;
3441
}

examples/context_propagation/CMakeLists.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@ if(NOT DEFINED OTEL_CPP_INSTALLED_DIR)
88
add_dependencies(${CONTEXTPROP_EXAMPLE_CPP_TARGET} ${OTEL_CPP_PROJECT_NAME})
99
endif()
1010

11+
# Specify the ABI version, include directories, and libraries of otel-cpp. The C++ code in this example directly calls otel-cpp functions.
12+
target_compile_definitions(${CONTEXTPROP_EXAMPLE_CPP_TARGET} PRIVATE ${OTEL_CPP_ABI_VERSION})
1113
target_include_directories(${CONTEXTPROP_EXAMPLE_CPP_TARGET} PRIVATE ${OTEL_CPP_PREFIX}/include)
1214
target_link_libraries(${CONTEXTPROP_EXAMPLE_CPP_TARGET} PRIVATE ${OPENTELEMETRY_PROXY_LINK_LIBRARIES})
1315
if(UNIX AND NOT APPLE AND NOT CYGWIN)

0 commit comments

Comments
 (0)