Skip to content

Otlp file exporters #145

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Aug 16, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 30 additions & 4 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -78,8 +78,9 @@ endif()

option(WITH_OTLP_HTTP "Whether to include the OTLP HTTP exporter" ON)
option(WITH_OTLP_GRPC "Whether to include the OTLP gRPC exporter" OFF)
if(NOT WITH_OTLP_HTTP AND NOT WITH_OTLP_GRPC)
message(FATAL_ERROR "At least one of WITH_OTLP_HTTP and WITH_OTLP_GRPC must be ON")
option(WITH_OTLP_FILE "Whether to include the OTLP file exporter" OFF)
if(NOT WITH_OTLP_HTTP AND NOT WITH_OTLP_GRPC AND NOT WITH_OTLP_FILE)
message(FATAL_ERROR "At least one of WITH_OTLP_HTTP, WITH_OTLP_GRPC, or WITH_OTLP_FILE must be ON")
endif()
if(APPLE)
option(SKIP_OTEL_CPP_PATCH "Whether to skip patching OpenTelemetry-cpp" OFF)
Expand Down Expand Up @@ -178,6 +179,12 @@ if(WITH_OTLP_GRPC)
${OTEL_CPP_PREFIX}/lib/${CMAKE_STATIC_LIBRARY_PREFIX}opentelemetry_exporter_otlp_grpc_log${CMAKE_STATIC_LIBRARY_SUFFIX}
${OTEL_CPP_PREFIX}/lib/${CMAKE_SHARED_LIBRARY_PREFIX}opentelemetry_proto_grpc${OTEL_PROTO_LIBRARY_SUFFIX})
endif()
if(WITH_OTLP_FILE)
set(OTEL_CPP_LIBRARIES ${OTEL_CPP_LIBRARIES} ${OTEL_CPP_PREFIX}/lib/${CMAKE_STATIC_LIBRARY_PREFIX}opentelemetry_exporter_otlp_file${CMAKE_STATIC_LIBRARY_SUFFIX}
${OTEL_CPP_PREFIX}/lib/${CMAKE_STATIC_LIBRARY_PREFIX}opentelemetry_exporter_otlp_file_client${CMAKE_STATIC_LIBRARY_SUFFIX}
${OTEL_CPP_PREFIX}/lib/${CMAKE_STATIC_LIBRARY_PREFIX}opentelemetry_exporter_otlp_file_metric${CMAKE_STATIC_LIBRARY_SUFFIX}
${OTEL_CPP_PREFIX}/lib/${CMAKE_STATIC_LIBRARY_PREFIX}opentelemetry_exporter_otlp_file_log${CMAKE_STATIC_LIBRARY_SUFFIX})
endif()

if(NOT DEFINED OTEL_CPP_INSTALLED_DIR)
# No installed location supplied. Fetch it as an external project
Expand Down Expand Up @@ -209,7 +216,7 @@ if(NOT DEFINED OTEL_CPP_INSTALLED_DIR)
PREFIX ${OTEL_CPP_PREFIX}
UPDATE_DISCONNECTED 1
PATCH_COMMAND ${patch_command}
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} -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}
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}
BUILD_BYPRODUCTS ${OTEL_CPP_LIBRARIES}
INSTALL_DIR ${OTEL_CPP_PREFIX}
INSTALL_COMMAND ${CMAKE_COMMAND} --install . --prefix ${OTEL_CPP_PREFIX} --config $<CONFIG>
Expand Down Expand Up @@ -321,6 +328,13 @@ if(WITH_OTLP_GRPC)
${OTLP_EXPORTER_SOURCE_DIR}/OtlpGrpcMetricExporterProxy.cpp
${OTLP_EXPORTER_SOURCE_DIR}/OtlpGrpcLogRecordExporterProxy.cpp)
endif()
if(WITH_OTLP_FILE)
set(OPENTELEMETRY_PROXY_SOURCES
${OPENTELEMETRY_PROXY_SOURCES}
${OTLP_EXPORTER_SOURCE_DIR}/OtlpFileSpanExporterProxy.cpp
${OTLP_EXPORTER_SOURCE_DIR}/OtlpFileMetricExporterProxy.cpp
${OTLP_EXPORTER_SOURCE_DIR}/OtlpFileLogRecordExporterProxy.cpp)
endif()

if(WIN32)
# Add a preprocessor definition _DISABLE_CONSTEXPR_MUTEX_CONSTRUCTOR to fix an invalid mex file issue on Windows.
Expand All @@ -338,7 +352,7 @@ if(NOT DEFINED OTEL_CPP_INSTALLED_DIR)
add_dependencies(${OPENTELEMETRY_PROXY_LIBRARY_NAME} ${OTEL_CPP_PROJECT_NAME})
endif()

# Additional compiler flags for HTTP/gRPC exporters
# Additional compiler flags for HTTP/gRPC/file exporters
if(WITH_OTLP_HTTP)
target_compile_definitions(${OPENTELEMETRY_PROXY_LIBRARY_NAME} PRIVATE WITH_OTLP_HTTP)
endif()
Expand All @@ -347,6 +361,10 @@ if(WITH_OTLP_GRPC)
target_compile_definitions(${OPENTELEMETRY_PROXY_LIBRARY_NAME} PRIVATE WITH_OTLP_GRPC)
endif()

if(WITH_OTLP_FILE)
target_compile_definitions(${OPENTELEMETRY_PROXY_LIBRARY_NAME} PRIVATE WITH_OTLP_FILE)
endif()

# pass in version number
target_compile_definitions(${OPENTELEMETRY_PROXY_LIBRARY_NAME} PRIVATE OTEL_MATLAB_VERSION="${OTEL_MATLAB_VERSION}")
if(WIN32)
Expand Down Expand Up @@ -472,6 +490,11 @@ set(OTLP_GRPC_EXPORTER_MATLAB_SOURCES
${CMAKE_CURRENT_SOURCE_DIR}/exporters/otlp/+opentelemetry/+exporters/+otlp/OtlpGrpcMetricExporter.m
${CMAKE_CURRENT_SOURCE_DIR}/exporters/otlp/+opentelemetry/+exporters/+otlp/OtlpGrpcLogRecordExporter.m
${CMAKE_CURRENT_SOURCE_DIR}/exporters/otlp/+opentelemetry/+exporters/+otlp/OtlpGrpcValidator.m)
set(OTLP_FILE_EXPORTER_MATLAB_SOURCES
${CMAKE_CURRENT_SOURCE_DIR}/exporters/otlp/+opentelemetry/+exporters/+otlp/OtlpFileSpanExporter.m
${CMAKE_CURRENT_SOURCE_DIR}/exporters/otlp/+opentelemetry/+exporters/+otlp/OtlpFileMetricExporter.m
${CMAKE_CURRENT_SOURCE_DIR}/exporters/otlp/+opentelemetry/+exporters/+otlp/OtlpFileLogRecordExporter.m
${CMAKE_CURRENT_SOURCE_DIR}/exporters/otlp/+opentelemetry/+exporters/+otlp/OtlpFileValidator.m)
set(OTLP_MISC_FILES ${CMAKE_CURRENT_SOURCE_DIR}/LICENSE)

set(OTLP_EXPORTERS_DIR +opentelemetry/+exporters/+otlp)
Expand All @@ -493,6 +516,9 @@ endif()
if(WITH_OTLP_GRPC)
install(FILES ${OTLP_GRPC_EXPORTER_MATLAB_SOURCES} DESTINATION ${OTLP_EXPORTERS_DIR})
endif()
if(WITH_OTLP_FILE)
install(FILES ${OTLP_FILE_EXPORTER_MATLAB_SOURCES} DESTINATION ${OTLP_EXPORTERS_DIR})
endif()
# Install license. The only reason it is needed is to work around a packaging bug where if
# all files are in packages (+xxx directories), matlab.addons.toolbox.packageToolbox refuses to add the
# toolbox path into MATLAB path. This issue is fixed in R2024a
Expand Down
10 changes: 10 additions & 0 deletions OtelMatlabProxyFactory.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,11 @@
#include "opentelemetry-matlab/exporters/otlp/OtlpGrpcMetricExporterProxy.h"
#include "opentelemetry-matlab/exporters/otlp/OtlpGrpcLogRecordExporterProxy.h"
#endif
#ifdef WITH_OTLP_FILE
#include "opentelemetry-matlab/exporters/otlp/OtlpFileSpanExporterProxy.h"
#include "opentelemetry-matlab/exporters/otlp/OtlpFileMetricExporterProxy.h"
#include "opentelemetry-matlab/exporters/otlp/OtlpFileLogRecordExporterProxy.h"
#endif

libmexclass::proxy::MakeResult
OtelMatlabProxyFactory::make_proxy(const libmexclass::proxy::ClassName& class_name,
Expand Down Expand Up @@ -88,5 +93,10 @@ OtelMatlabProxyFactory::make_proxy(const libmexclass::proxy::ClassName& class_na
REGISTER_PROXY(libmexclass.opentelemetry.exporters.OtlpGrpcMetricExporterProxy, libmexclass::opentelemetry::exporters::OtlpGrpcMetricExporterProxy);
REGISTER_PROXY(libmexclass.opentelemetry.exporters.OtlpGrpcLogRecordExporterProxy, libmexclass::opentelemetry::exporters::OtlpGrpcLogRecordExporterProxy);
#endif
#ifdef WITH_OTLP_FILE
REGISTER_PROXY(libmexclass.opentelemetry.exporters.OtlpFileSpanExporterProxy, libmexclass::opentelemetry::exporters::OtlpFileSpanExporterProxy);
REGISTER_PROXY(libmexclass.opentelemetry.exporters.OtlpFileMetricExporterProxy, libmexclass::opentelemetry::exporters::OtlpFileMetricExporterProxy);
REGISTER_PROXY(libmexclass.opentelemetry.exporters.OtlpFileLogRecordExporterProxy, libmexclass::opentelemetry::exporters::OtlpFileLogRecordExporterProxy);
#endif
return nullptr;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,124 @@
classdef OtlpFileLogRecordExporter < opentelemetry.sdk.logs.LogRecordExporter
% OtlpFileLogRecordExporter exports log records in OpenTelemetry Protocol format to
% one or more files.

% Copyright 2024 The MathWorks, Inc.

properties
FileName (1,1) string = "logs-%N.jsonl" % Output file name
AliasName (1,1) string = "logs-latest.jsonl" % Alias file name, which is the latest file in rotation
FlushInterval (1,1) duration = seconds(30) % Time interval between log record exports
FlushRecordCount (1,1) double = 256 % Maximum number of records before exporting
MaxFileSize (1,1) double = 20971520 % Maximum output file size
MaxFileCount (1,1) double = 10 % Maximum number of output files, written to in rotation.
end

properties (Access=private, Constant)
Validator = opentelemetry.exporters.otlp.OtlpFileValidator
end

methods
function obj = OtlpFileLogRecordExporter(optionnames, optionvalues)
% OtlpFileLogRecordExporter exports log records in OpenTelemetry
% Protocol format to one or more files.
% EXP = OPENTELEMETRY.EXPORTERS.OTLP.OTLPFILELOGRECORDEXPORTER
% creates an exporter that uses default configurations.
%
% EXP =
% OPENTELEMETRY.EXPORTERS.OTLP.OTLPFILELOGRECORDEXPORTER(PARAM1,
% VALUE1, PARAM2, VALUE2, ...) specifies optional parameter
% name/value pairs. Parameters are:
% "FileName" - Output file name. Can contain
% pattern placeholders. Default
% name is "logs-%N.jsonl"
% "AliasName" - Alias file name, which is the
% latest file in rotation. Can
% contain pattern placeholders.
% Default name is "logs-latest.jsonl"
% "FlushInterval" - Time interval between log record
% exports, represented as a
% duration. Default is 30 seconds.
% "FlushRecordCount" - Maximum number of records before
% exporting. When the number of
% records exceed this value, an
% export will be started. Default
% is 256.
% "MaxFileSize" - Maximum output file size
% "MaxFileCount" - Maximum number of output files,
% written to in rotation. Default
% is 10.
%
% Supported pattern placeholders in FileName and AliasName are:
% %Y: year as a 4 digit decimal number
% %y: last 2 digits of year as a decimal number (range [00,99])
% %m: month as a decimal number (range [01,12])
% %j: day of the year as a decimal number (range [001,366])
% %d: day of the month as a decimal number (range [01,31])
% %w: weekday as a decimal number, where Sunday is 0 (range [0-6])
% %H: hour as a decimal number, 24 hour clock (range [00-23])
% %I: hour as a decimal number, 12 hour clock (range [01,12])
% %M: minute as a decimal number (range [00,59])
% %S: second as a decimal number (range [00,60])
% %F: equivalent to "%Y-%m-%d" (the ISO 8601 date format)
% %T: equivalent to "%H:%M:%S" (the ISO 8601 time format)
% %R: equivalent to "%H:%M"
% %N: rotate index, starting from 0
% %n: rotate index, starting from 1
%
% See also
% OPENTELEMETRY.EXPORTERS.OTLP.OTLPHTTPLOGRECORDEXPORTER,
% OPENTELEMETRY.EXPORTERS.OTLP.OTLPGRPCLOGRECORDEXPORTER
arguments (Repeating)
optionnames (1,:) {mustBeTextScalar}
optionvalues
end

obj = [email protected](...
"libmexclass.opentelemetry.exporters.OtlpFileLogRecordExporterProxy");

validnames = ["FileName", "AliasName", "FlushInterval", ...
"FlushRecordCount", "MaxFileSize", "MaxFileCount"];
for i = 1:length(optionnames)
namei = validatestring(optionnames{i}, validnames);
valuei = optionvalues{i};
obj.(namei) = valuei;
end
end

function obj = set.FileName(obj, fn)
fn = obj.Validator.validateName(fn, "FileName");
obj.Proxy.setFileName(fn);
obj.FileName = fn;
end

function obj = set.AliasName(obj, alias)
alias = obj.Validator.validateName(alias, "AliasName");
obj.Proxy.setAliasName(alias);
obj.AliasName = alias;
end

function obj = set.FlushInterval(obj, interval)
obj.Validator.validateFlushInterval(interval);
obj.Proxy.setFlushInterval(milliseconds(interval));
obj.FlushInterval = interval;
end

function obj = set.FlushRecordCount(obj, count)
count = obj.Validator.validateScalarPositiveInteger(count, "FlushRecordCount");
obj.Proxy.setFlushRecordCount(count);
obj.FlushRecordCount = count;
end

function obj = set.MaxFileSize(obj, maxsize)
maxsize = obj.Validator.validateScalarPositiveInteger(maxsize, "MaxFileSize");
obj.Proxy.setMaxFileSize(maxsize);
obj.MaxFileSize = maxsize;
end

function obj = set.MaxFileCount(obj, maxfilecount)
maxfilecount = obj.Validator.validateScalarPositiveInteger(maxfilecount, "MaxFileCount");
obj.Proxy.setMaxFileCount(maxfilecount);
obj.MaxFileCount = maxfilecount;
end
end
end
Loading
Loading