Skip to content

Commit 511a048

Browse files
committed
Merge branch 'main' into exampletests
2 parents 138b29a + 1327409 commit 511a048

File tree

16 files changed

+230
-34
lines changed

16 files changed

+230
-34
lines changed

CMakeLists.txt

Lines changed: 41 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -25,25 +25,6 @@ else()
2525
set(VCPKG_PREFIX ${CMAKE_BINARY_DIR}/vcpkg)
2626
endif()
2727

28-
# Define custom triplets for vcpkg
29-
if(APPLE)
30-
# On Mac, there is a conflict between libcurl and the version in MATLAB, so
31-
# use libcurl as a shared library and load the MATLAB version at runtime
32-
# run uname -m to determine whether arm64 or x86_64
33-
exec_program(uname ARGS -m OUTPUT_VARIABLE MAC_HOST_SYSTEM)
34-
set(VCPKG_OTEL_TRIPLET ${MAC_HOST_SYSTEM}-osx-otel-matlab)
35-
set(VCPKG_OVERLAY_TRIPLETS ${CMAKE_SOURCE_DIR}/cmake/vcpkg_triplets)
36-
set(VCPKG_TARGET_TRIPLET ${VCPKG_OTEL_TRIPLET})
37-
set(TRIPLET_DEFINITIONS -DVCPKG_OVERLAY_TRIPLETS="${VCPKG_OVERLAY_TRIPLETS}" -DVCPKG_TARGET_TRIPLET=${VCPKG_TARGET_TRIPLET})
38-
elseif(WIN32)
39-
# On Windows, there is a conflict between abseil_dll.dll and the version used by Simulink.
40-
# The shared library doesn't seem ABI stable and different versions cannot be used interchangeably.
41-
# To sidestep the problem, use static library.
42-
set(VCPKG_OVERLAY_TRIPLETS ${CMAKE_SOURCE_DIR}/cmake/vcpkg_triplets)
43-
set(VCPKG_TARGET_TRIPLET x64-windows-otel-matlab)
44-
set(TRIPLET_DEFINITIONS -DVCPKG_OVERLAY_TRIPLETS="${VCPKG_OVERLAY_TRIPLETS}" -DVCPKG_TARGET_TRIPLET=${VCPKG_TARGET_TRIPLET})
45-
endif()
46-
4728
set(VCPKG_FETCH_CONTENT_NAME vcpkg)
4829
set(VCPKG_GIT_REPOSITORY "https://github.com/microsoft/vcpkg.git")
4930
set(VCPKG_GIT_TAG "fba75d0")
@@ -68,6 +49,28 @@ if(NOT DEFINED VCPKG_INSTALLED_DIR)
6849
set(VCPKG_INSTALLED_DIR ${CMAKE_BINARY_DIR}/vcpkg_installed)
6950
endif()
7051

52+
# Define custom triplets for vcpkg
53+
if(APPLE)
54+
# On Mac, there is a conflict between libcurl and the version in MATLAB, so
55+
# use libcurl as a shared library and load the MATLAB version at runtime
56+
# run uname -m to determine whether arm64 or x86_64
57+
exec_program(uname ARGS -m OUTPUT_VARIABLE MAC_HOST_SYSTEM)
58+
set(VCPKG_OTEL_TRIPLET ${MAC_HOST_SYSTEM}-osx-otel-matlab)
59+
set(VCPKG_OVERLAY_TRIPLETS ${CMAKE_SOURCE_DIR}/cmake/vcpkg_triplets)
60+
set(VCPKG_TARGET_TRIPLET ${VCPKG_OTEL_TRIPLET})
61+
set(TRIPLET_DEFINITIONS -DVCPKG_TARGET_TRIPLET=${VCPKG_TARGET_TRIPLET})
62+
elseif(WIN32)
63+
# On Windows, we use a custom triplet to work around two issues.
64+
# First, there is a conflict between abseil_dll.dll and the version used by Simulink.
65+
# The shared library doesn't seem ABI stable and different versions cannot be used interchangeably.
66+
# To sidestep the problem, use static library.
67+
# Second, define a preprocessor macro _DISABLE_CONSTEXPR_MUTEX_CONSTRUCTOR to work around an invalid MEX file issue on GitHub
68+
# See https://github.com/mathworks/OpenTelemetry-Matlab/issues/130
69+
set(VCPKG_OVERLAY_TRIPLETS ${CMAKE_SOURCE_DIR}/cmake/vcpkg_triplets)
70+
set(VCPKG_TARGET_TRIPLET x64-windows-otel-matlab)
71+
set(TRIPLET_DEFINITIONS -DVCPKG_TARGET_TRIPLET=${VCPKG_TARGET_TRIPLET})
72+
endif()
73+
7174
# ######################################
7275
# Options
7376
# ######################################
@@ -184,10 +187,18 @@ if(NOT DEFINED OTEL_CPP_INSTALLED_DIR)
184187

185188
set(OTEL_CPP_CXX_STANDARD 14)
186189

187-
if(NOT APPLE OR SKIP_OTEL_CPP_PATCH)
188-
set(patch_command "")
190+
set(PATCHES_DIR ${CMAKE_SOURCE_DIR}/patches)
191+
if(SKIP_OTEL_CPP_PATCH)
192+
set(patch_comand "")
193+
elseif(WIN32)
194+
# Windows patch to add a preprocessor definition _DISABLE_CONSTEXPR_MUTEX_CONSTRUCTOR
195+
# See https://github.com/mathworks/OpenTelemetry-Matlab/issues/130
196+
set(patch_command git apply ${PATCHES_DIR}/otel-cpp-windows.patch)
197+
elseif(APPLE)
198+
# Mac patch to fix a linker issue when including gRPC exporter
199+
set(patch_command git apply ${PATCHES_DIR}/otel-cpp-mac.patch)
189200
else()
190-
set(patch_command git apply ${CMAKE_SOURCE_DIR}/otel-cpp.patch)
201+
set(patch_command "")
191202
endif()
192203

193204
ExternalProject_Add(
@@ -293,7 +304,8 @@ set(OPENTELEMETRY_PROXY_SOURCES
293304
${LOGS_SDK_SOURCE_DIR}/LoggerProviderProxy.cpp
294305
${LOGS_SDK_SOURCE_DIR}/SimpleLogRecordProcessorProxy.cpp
295306
${LOGS_SDK_SOURCE_DIR}/BatchLogRecordProcessorProxy.cpp
296-
${COMMON_SDK_SOURCE_DIR}/resource.cpp)
307+
${COMMON_SDK_SOURCE_DIR}/resource.cpp
308+
${COMMON_SDK_SOURCE_DIR}/InternalLogHandlerProxy.cpp)
297309
if(WITH_OTLP_HTTP)
298310
set(OPENTELEMETRY_PROXY_SOURCES
299311
${OPENTELEMETRY_PROXY_SOURCES}
@@ -309,6 +321,12 @@ if(WITH_OTLP_GRPC)
309321
${OTLP_EXPORTER_SOURCE_DIR}/OtlpGrpcLogRecordExporterProxy.cpp)
310322
endif()
311323

324+
if(WIN32)
325+
# Add a preprocessor definition _DISABLE_CONSTEXPR_MUTEX_CONSTRUCTOR to fix an invalid mex file issue on Windows.
326+
# See https://github.com/mathworks/OpenTelemetry-Matlab/issues/130
327+
add_compile_definitions(_DISABLE_CONSTEXPR_MUTEX_CONSTRUCTOR)
328+
endif()
329+
312330
libmexclass_client_add_proxy_library(
313331
NAME ${OPENTELEMETRY_PROXY_LIBRARY_NAME}
314332
SOURCES ${OPENTELEMETRY_PROXY_SOURCES}

OtelMatlabProxyFactory.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
#include "opentelemetry-matlab/sdk/logs/LoggerProviderProxy.h"
3030
#include "opentelemetry-matlab/sdk/logs/SimpleLogRecordProcessorProxy.h"
3131
#include "opentelemetry-matlab/sdk/logs/BatchLogRecordProcessorProxy.h"
32+
#include "opentelemetry-matlab/sdk/common/InternalLogHandlerProxy.h"
3233
#ifdef WITH_OTLP_HTTP
3334
#include "opentelemetry-matlab/exporters/otlp/OtlpHttpSpanExporterProxy.h"
3435
#include "opentelemetry-matlab/exporters/otlp/OtlpHttpMetricExporterProxy.h"
@@ -75,6 +76,7 @@ OtelMatlabProxyFactory::make_proxy(const libmexclass::proxy::ClassName& class_na
7576
REGISTER_PROXY(libmexclass.opentelemetry.sdk.LoggerProviderProxy, libmexclass::opentelemetry::sdk::LoggerProviderProxy);
7677
REGISTER_PROXY(libmexclass.opentelemetry.sdk.SimpleLogRecordProcessorProxy, libmexclass::opentelemetry::sdk::SimpleLogRecordProcessorProxy);
7778
REGISTER_PROXY(libmexclass.opentelemetry.sdk.BatchLogRecordProcessorProxy, libmexclass::opentelemetry::sdk::BatchLogRecordProcessorProxy);
79+
REGISTER_PROXY(libmexclass.opentelemetry.sdk.InternalLogHandlerProxy, libmexclass::opentelemetry::sdk::InternalLogHandlerProxy);
7880

7981
#ifdef WITH_OTLP_HTTP
8082
REGISTER_PROXY(libmexclass.opentelemetry.exporters.OtlpHttpSpanExporterProxy, libmexclass::opentelemetry::exporters::OtlpHttpSpanExporterProxy);

api/common/src/attribute.cpp

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -45,23 +45,23 @@ void processAttribute(const std::string& attrname, // input, attribute name
4545
if (valtype == matlab::data::ArrayType::DOUBLE) {
4646
matlab::data::TypedArray<double> attrvalue_mda = attrvalue;
4747
attrs.Attributes.push_back(std::pair<std::string, common::AttributeValue>(attrname,
48-
nostd::span<const double>{&(*attrvalue_mda.cbegin()), &(*attrvalue_mda.cend())}));
48+
nostd::span<const double>{&(*attrvalue_mda.cbegin()), attrvalue_mda.getNumberOfElements()}));
4949
} else if (valtype == matlab::data::ArrayType::INT32) {
5050
matlab::data::TypedArray<int32_t> attrvalue_mda = attrvalue;
5151
attrs.Attributes.push_back(std::pair<std::string, common::AttributeValue>(attrname,
52-
nostd::span<const int32_t>{&(*attrvalue_mda.cbegin()), &(*attrvalue_mda.cend())}));
52+
nostd::span<const int32_t>{&(*attrvalue_mda.cbegin()), attrvalue_mda.getNumberOfElements()}));
5353
} else if (valtype == matlab::data::ArrayType::UINT32) {
5454
matlab::data::TypedArray<uint32_t> attrvalue_mda = attrvalue;
5555
attrs.Attributes.push_back(std::pair<std::string, common::AttributeValue>(attrname,
56-
nostd::span<const uint32_t>{&(*attrvalue_mda.cbegin()), &(*attrvalue_mda.cend())}));
56+
nostd::span<const uint32_t>{&(*attrvalue_mda.cbegin()), attrvalue_mda.getNumberOfElements()}));
5757
} else if (valtype == matlab::data::ArrayType::INT64) {
5858
matlab::data::TypedArray<int64_t> attrvalue_mda = attrvalue;
5959
attrs.Attributes.push_back(std::pair<std::string, common::AttributeValue>(attrname,
60-
nostd::span<const int64_t>{&(*attrvalue_mda.cbegin()), &(*attrvalue_mda.cend())}));
60+
nostd::span<const int64_t>{&(*attrvalue_mda.cbegin()), attrvalue_mda.getNumberOfElements()}));
6161
} else if (valtype == matlab::data::ArrayType::LOGICAL) {
6262
matlab::data::TypedArray<bool> attrvalue_mda = attrvalue;
6363
attrs.Attributes.push_back(std::pair<std::string, common::AttributeValue>(attrname,
64-
nostd::span<const bool>{&(*attrvalue_mda.cbegin()), &(*attrvalue_mda.cend())}));
64+
nostd::span<const bool>{&(*attrvalue_mda.cbegin()), attrvalue_mda.getNumberOfElements()}));
6565
} else if (valtype == matlab::data::ArrayType::MATLAB_STRING) { // string
6666
matlab::data::StringArray attrvalue_mda = attrvalue;
6767
std::vector<nostd::string_view> strarray_attr;
@@ -73,7 +73,7 @@ void processAttribute(const std::string& attrname, // input, attribute name
7373
}
7474
attrs.StringViewBuffer.push_back(strarray_attr);
7575
attrs.Attributes.push_back(std::pair<std::string, common::AttributeValue>(attrname,
76-
nostd::span<const nostd::string_view>{&(*attrs.StringViewBuffer.back().cbegin()), &(*attrs.StringViewBuffer.back().cend())}));
76+
nostd::span<const nostd::string_view>{&(*attrs.StringViewBuffer.back().cbegin()), attrvalue_mda.getNumberOfElements()}));
7777
} else { // ignore all other types
7878
return;
7979
}
@@ -85,8 +85,7 @@ void processAttribute(const std::string& attrname, // input, attribute name
8585
}
8686
attrs.DimensionsBuffer.push_back(attrvalue_dims_buffer);
8787
attrs.Attributes.push_back(std::pair<std::string, common::AttributeValue>(sizeattr,
88-
nostd::span<const double>{&(*attrs.DimensionsBuffer.back().cbegin()),
89-
&(*attrs.DimensionsBuffer.back().cend())}));
88+
nostd::span<const double>{attrs.DimensionsBuffer.back().data(), attrvalue_dims_buffer.size()}));
9089
}
9190
}
9291
} // namespace

api/metrics/src/AsynchronousInstrumentProxy.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,8 @@ void AsynchronousInstrumentProxy::removeCallback(libmexclass::proxy::method::Con
2626
double idx = idx_mda[0] - 1; // adjust index from 1-based in MATLAB to 0-based in C++
2727
auto iter = CallbackInputs.begin();
2828
std::advance(iter, idx);
29-
CallbackInputs.erase(iter);
3029
CppInstrument->RemoveCallback(MeasurementFetcher::Fetcher, static_cast<void*>(&(*iter)));
30+
CallbackInputs.erase(iter);
3131
}
3232

3333
} // namespace libmexclass::opentelemetry

cmake/vcpkg_triplets/x64-windows-otel-matlab.cmake

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,4 +6,7 @@ if(${PORT} MATCHES "abseil")
66
else()
77
set(VCPKG_LIBRARY_LINKAGE dynamic)
88
endif()
9-
9+
# Define a preprocessor macro _DISABLE_CONSTEXPR_MUTEX_CONSTRUCTOR to work around an invalid MEX file issue
10+
# See https://github.com/mathworks/OpenTelemetry-Matlab/issues/130
11+
set(VCPKG_CXX_FLAGS "/D_DISABLE_CONSTEXPR_MUTEX_CONSTRUCTOR")
12+
set(VCPKG_C_FLAGS "/D_DISABLE_CONSTEXPR_MUTEX_CONSTRUCTOR")
File renamed without changes.

patches/otel-cpp-windows.patch

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
diff --git a/CMakeLists.txt b/CMakeLists.txt
2+
index cc085b1a..6c4dc0ae 100644
3+
--- a/CMakeLists.txt
4+
+++ b/CMakeLists.txt
5+
@@ -15,6 +15,12 @@ endif()
6+
7+
project(opentelemetry-cpp)
8+
9+
+if(WIN32)
10+
+ # Add a preprocessor macro _DISABLE_CONSTEXPR_MUTEX_CONSTRUCTOR to work around an invalid MEX file issue
11+
+ # See https://github.com/mathworks/OpenTelemetry-Matlab/issues/130
12+
+ add_compile_definitions(_DISABLE_CONSTEXPR_MUTEX_CONSTRUCTOR)
13+
+endif()
14+
+
15+
# Mark variables as used so cmake doesn't complain about them
16+
mark_as_advanced(CMAKE_TOOLCHAIN_FILE)
17+
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
classdef InternalLogHandler < handle
2+
% Internal log handler enables changes to internal log message display
3+
4+
% Copyright 2024 The MathWorks, Inc.
5+
6+
properties (Access=private)
7+
Proxy % Proxy object to interface C++ code
8+
end
9+
10+
properties (Access=private, Constant)
11+
LogLevels = ["none" "error" "warning" "info" "debug"]
12+
end
13+
14+
properties (Dependent)
15+
LogLevel (1,1) string % Supported levels from highest to lowest are
16+
% "none", "error", "warning", "info",
17+
% "debug". Setting to a higher level displays
18+
% fewer internal log messages.
19+
end
20+
21+
methods
22+
function obj = InternalLogHandler()
23+
% Internal log handler enables changes to internal log message
24+
% display
25+
% H = OPENTELEMETRY.SDK.COMMON.INTERNALLOGHANDLER creates an internal log handler.
26+
obj.Proxy = libmexclass.proxy.Proxy("Name", ...
27+
"libmexclass.opentelemetry.sdk.InternalLogHandlerProxy", ...
28+
"ConstructorArguments", {});
29+
end
30+
31+
function loglevel = get.LogLevel(obj)
32+
loglevel = obj.Proxy.getLogLevel();
33+
end
34+
35+
function set.LogLevel(obj, loglevel)
36+
loglevel = validatestring(loglevel, obj.LogLevels);
37+
obj.Proxy.setLogLevel(loglevel);
38+
end
39+
end
40+
end
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
// Copyright 2024 The MathWorks, Inc.
2+
3+
#pragma once
4+
5+
#include "libmexclass/proxy/Proxy.h"
6+
7+
namespace libmexclass::opentelemetry::sdk {
8+
class InternalLogHandlerProxy: public libmexclass::proxy::Proxy {
9+
public:
10+
InternalLogHandlerProxy() {
11+
REGISTER_METHOD(InternalLogHandlerProxy, setLogLevel);
12+
REGISTER_METHOD(InternalLogHandlerProxy, getLogLevel);
13+
}
14+
15+
static libmexclass::proxy::MakeResult make(const libmexclass::proxy::FunctionArguments& constructor_arguments) {
16+
return std::make_shared<InternalLogHandlerProxy>();
17+
}
18+
19+
void setLogLevel(libmexclass::proxy::method::Context& context);
20+
21+
void getLogLevel(libmexclass::proxy::method::Context& context);
22+
};
23+
} // namespace libmexclass::opentelemetry
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
// Copyright 2024 The MathWorks, Inc.
2+
3+
#include "opentelemetry-matlab/sdk/common/InternalLogHandlerProxy.h"
4+
5+
#include "opentelemetry/sdk/common/global_log_handler.h"
6+
7+
#include "MatlabDataArray.hpp"
8+
9+
namespace internal_log = opentelemetry::sdk::common::internal_log;
10+
11+
namespace libmexclass::opentelemetry::sdk {
12+
void InternalLogHandlerProxy::setLogLevel(libmexclass::proxy::method::Context& context) {
13+
matlab::data::StringArray loglevel_mda = context.inputs[0];
14+
matlab::data::MATLABString loglevelstr = loglevel_mda[0];
15+
16+
internal_log::LogLevel loglevel;
17+
if (loglevelstr->compare(u"none")==0) {
18+
loglevel = internal_log::LogLevel::None;
19+
} else if (loglevelstr->compare(u"error")==0) {
20+
loglevel = internal_log::LogLevel::Error;
21+
} else if (loglevelstr->compare(u"warning")==0) {
22+
loglevel = internal_log::LogLevel::Warning;
23+
} else if (loglevelstr->compare(u"info")==0) {
24+
loglevel = internal_log::LogLevel::Info;
25+
} else {
26+
assert(loglevelstr->compare(u"debug")==0);
27+
loglevel = internal_log::LogLevel::Debug;
28+
}
29+
internal_log::GlobalLogHandler::SetLogLevel(loglevel);
30+
}
31+
32+
void InternalLogHandlerProxy::getLogLevel(libmexclass::proxy::method::Context& context) {
33+
internal_log::LogLevel loglevel = internal_log::GlobalLogHandler::GetLogLevel();
34+
std::string loglevelstr = internal_log::LevelToString(loglevel);
35+
loglevelstr[0] = tolower(loglevelstr[0]); // LevelToString returns first letter capitalized
36+
37+
matlab::data::ArrayFactory factory;
38+
auto loglevelstr_mda = factory.createScalar(loglevelstr);
39+
context.outputs[0] = loglevelstr_mda;
40+
}
41+
} // namespace libmexclass::opentelemetry

0 commit comments

Comments
 (0)