Skip to content

Commit 1327409

Browse files
authored
Merge pull request #134 from mathworks/internal_log_handler
Internal log handler
2 parents 9a6cc4e + ce3bb3f commit 1327409

File tree

13 files changed

+172
-13
lines changed

13 files changed

+172
-13
lines changed

CMakeLists.txt

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ if(APPLE)
5858
set(VCPKG_OTEL_TRIPLET ${MAC_HOST_SYSTEM}-osx-otel-matlab)
5959
set(VCPKG_OVERLAY_TRIPLETS ${CMAKE_SOURCE_DIR}/cmake/vcpkg_triplets)
6060
set(VCPKG_TARGET_TRIPLET ${VCPKG_OTEL_TRIPLET})
61-
set(TRIPLET_DEFINITIONS -DVCPKG_OVERLAY_TRIPLETS="${VCPKG_OVERLAY_TRIPLETS}" -DVCPKG_TARGET_TRIPLET=${VCPKG_TARGET_TRIPLET})
61+
set(TRIPLET_DEFINITIONS -DVCPKG_TARGET_TRIPLET=${VCPKG_TARGET_TRIPLET})
6262
elseif(WIN32)
6363
# On Windows, we use a custom triplet to work around two issues.
6464
# First, there is a conflict between abseil_dll.dll and the version used by Simulink.
@@ -68,7 +68,7 @@ elseif(WIN32)
6868
# See https://github.com/mathworks/OpenTelemetry-Matlab/issues/130
6969
set(VCPKG_OVERLAY_TRIPLETS ${CMAKE_SOURCE_DIR}/cmake/vcpkg_triplets)
7070
set(VCPKG_TARGET_TRIPLET x64-windows-otel-matlab)
71-
set(TRIPLET_DEFINITIONS -DVCPKG_OVERLAY_TRIPLETS="${VCPKG_OVERLAY_TRIPLETS}" -DVCPKG_TARGET_TRIPLET=${VCPKG_TARGET_TRIPLET})
71+
set(TRIPLET_DEFINITIONS -DVCPKG_TARGET_TRIPLET=${VCPKG_TARGET_TRIPLET})
7272
endif()
7373

7474
# ######################################
@@ -304,7 +304,8 @@ set(OPENTELEMETRY_PROXY_SOURCES
304304
${LOGS_SDK_SOURCE_DIR}/LoggerProviderProxy.cpp
305305
${LOGS_SDK_SOURCE_DIR}/SimpleLogRecordProcessorProxy.cpp
306306
${LOGS_SDK_SOURCE_DIR}/BatchLogRecordProcessorProxy.cpp
307-
${COMMON_SDK_SOURCE_DIR}/resource.cpp)
307+
${COMMON_SDK_SOURCE_DIR}/resource.cpp
308+
${COMMON_SDK_SOURCE_DIR}/InternalLogHandlerProxy.cpp)
308309
if(WITH_OTLP_HTTP)
309310
set(OPENTELEMETRY_PROXY_SOURCES
310311
${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
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

test/tlogs_sdk.m

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,9 @@ function testShutdown(testCase)
134134
forceFlush(lp, testCase.ForceFlushTimeout);
135135
verifyTrue(testCase, shutdown(lp));
136136

137+
% suppress internal error logs about log export failure
138+
nologs = SuppressInternalLogs; %#ok<NASGU>
139+
137140
% emit another log record
138141
emitLogRecord(lg, "info", "quux");
139142

@@ -156,6 +159,9 @@ function testCleanupSdk(testCase)
156159
forceFlush(lp, testCase.ForceFlushTimeout);
157160
verifyTrue(testCase, opentelemetry.sdk.common.Cleanup.shutdown(lp));
158161

162+
% suppress internal error logs about log export failure
163+
nologs = SuppressInternalLogs; %#ok<NASGU>
164+
159165
% emit another log record
160166
emitLogRecord(lg, "warn", "quux");
161167

@@ -181,6 +187,9 @@ function testCleanupApi(testCase)
181187
opentelemetry.sdk.common.Cleanup.forceFlush(lp_api, testCase.ForceFlushTimeout);
182188
verifyTrue(testCase, opentelemetry.sdk.common.Cleanup.shutdown(lp_api));
183189

190+
% suppress internal error logs about log export failure
191+
nologs = SuppressInternalLogs; %#ok<NASGU>
192+
184193
% emit another log record
185194
emitLogRecord(lg, "error", "quux");
186195

test/tmetrics.m

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -184,6 +184,9 @@ function testCounterInvalidAdd(testCase)
184184
mt = p.getMeter("foo");
185185
ct = mt.createCounter("bar");
186186

187+
% suppress internal warnings about invalid metric values
188+
nologs = SuppressInternalLogs; %#ok<NASGU>
189+
187190
% add negative value to counter
188191
ct.add(-1);
189192
% add add complex value
@@ -471,6 +474,10 @@ function testHistogramDelta(testCase)
471474

472475
function testGetSetMeterProvider(testCase)
473476
% testGetSetMeterProvider: setting and getting global instance of MeterProvider
477+
478+
% suppress internal warning logs about repeated shutdown
479+
nologs = SuppressInternalLogs; %#ok<NASGU>
480+
474481
mp = opentelemetry.sdk.metrics.MeterProvider(testCase.ShortIntervalReader);
475482
setMeterProvider(mp);
476483

test/tmetrics_sdk.m

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -372,6 +372,9 @@ function testShutdown(testCase)
372372
% of metrics
373373
mp = opentelemetry.sdk.metrics.MeterProvider(testCase.ShortIntervalReader);
374374

375+
% suppress internal warning logs about repeated shutdown
376+
nologs = SuppressInternalLogs; %#ok<NASGU>
377+
375378
% shutdown the meter provider
376379
verifyTrue(testCase, shutdown(mp));
377380

@@ -394,6 +397,9 @@ function testCleanupSdk(testCase)
394397
% Shut down an SDK meter provider instance
395398
mp = opentelemetry.sdk.metrics.MeterProvider(testCase.ShortIntervalReader);
396399

400+
% suppress internal warning logs about repeated shutdown
401+
nologs = SuppressInternalLogs; %#ok<NASGU>
402+
397403
% shutdown the meter provider through the Cleanup class
398404
verifyTrue(testCase, opentelemetry.sdk.common.Cleanup.shutdown(mp));
399405

@@ -412,7 +418,10 @@ function testCleanupSdk(testCase)
412418

413419
function testCleanupApi(testCase)
414420
% testCleanupApi: shutdown an API meter provider through the Cleanup class
415-
421+
422+
% suppress internal warning logs about repeated shutdown
423+
nologs = SuppressInternalLogs; %#ok<NASGU>
424+
416425
% Shut down an API meter provider instance
417426
mp = opentelemetry.sdk.metrics.MeterProvider(testCase.ShortIntervalReader);
418427
setMeterProvider(mp);

0 commit comments

Comments
 (0)