Skip to content

Commit 24452df

Browse files
authored
Merge pull request #145 from mathworks/OtlpFileExporter
Otlp file exporters
2 parents 34c0f03 + e0563e8 commit 24452df

29 files changed

+976
-45
lines changed

Diff for: CMakeLists.txt

+30-4
Original file line numberDiff line numberDiff line change
@@ -78,8 +78,9 @@ endif()
7878

7979
option(WITH_OTLP_HTTP "Whether to include the OTLP HTTP exporter" ON)
8080
option(WITH_OTLP_GRPC "Whether to include the OTLP gRPC exporter" OFF)
81-
if(NOT WITH_OTLP_HTTP AND NOT WITH_OTLP_GRPC)
82-
message(FATAL_ERROR "At least one of WITH_OTLP_HTTP and WITH_OTLP_GRPC must be ON")
81+
option(WITH_OTLP_FILE "Whether to include the OTLP file exporter" OFF)
82+
if(NOT WITH_OTLP_HTTP AND NOT WITH_OTLP_GRPC AND NOT WITH_OTLP_FILE)
83+
message(FATAL_ERROR "At least one of WITH_OTLP_HTTP, WITH_OTLP_GRPC, or WITH_OTLP_FILE must be ON")
8384
endif()
8485
if(APPLE)
8586
option(SKIP_OTEL_CPP_PATCH "Whether to skip patching OpenTelemetry-cpp" OFF)
@@ -178,6 +179,12 @@ if(WITH_OTLP_GRPC)
178179
${OTEL_CPP_PREFIX}/lib/${CMAKE_STATIC_LIBRARY_PREFIX}opentelemetry_exporter_otlp_grpc_log${CMAKE_STATIC_LIBRARY_SUFFIX}
179180
${OTEL_CPP_PREFIX}/lib/${CMAKE_SHARED_LIBRARY_PREFIX}opentelemetry_proto_grpc${OTEL_PROTO_LIBRARY_SUFFIX})
180181
endif()
182+
if(WITH_OTLP_FILE)
183+
set(OTEL_CPP_LIBRARIES ${OTEL_CPP_LIBRARIES} ${OTEL_CPP_PREFIX}/lib/${CMAKE_STATIC_LIBRARY_PREFIX}opentelemetry_exporter_otlp_file${CMAKE_STATIC_LIBRARY_SUFFIX}
184+
${OTEL_CPP_PREFIX}/lib/${CMAKE_STATIC_LIBRARY_PREFIX}opentelemetry_exporter_otlp_file_client${CMAKE_STATIC_LIBRARY_SUFFIX}
185+
${OTEL_CPP_PREFIX}/lib/${CMAKE_STATIC_LIBRARY_PREFIX}opentelemetry_exporter_otlp_file_metric${CMAKE_STATIC_LIBRARY_SUFFIX}
186+
${OTEL_CPP_PREFIX}/lib/${CMAKE_STATIC_LIBRARY_PREFIX}opentelemetry_exporter_otlp_file_log${CMAKE_STATIC_LIBRARY_SUFFIX})
187+
endif()
181188

182189
if(NOT DEFINED OTEL_CPP_INSTALLED_DIR)
183190
# No installed location supplied. Fetch it as an external project
@@ -209,7 +216,7 @@ if(NOT DEFINED OTEL_CPP_INSTALLED_DIR)
209216
PREFIX ${OTEL_CPP_PREFIX}
210217
UPDATE_DISCONNECTED 1
211218
PATCH_COMMAND ${patch_command}
212-
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}
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}
213220
BUILD_BYPRODUCTS ${OTEL_CPP_LIBRARIES}
214221
INSTALL_DIR ${OTEL_CPP_PREFIX}
215222
INSTALL_COMMAND ${CMAKE_COMMAND} --install . --prefix ${OTEL_CPP_PREFIX} --config $<CONFIG>
@@ -321,6 +328,13 @@ if(WITH_OTLP_GRPC)
321328
${OTLP_EXPORTER_SOURCE_DIR}/OtlpGrpcMetricExporterProxy.cpp
322329
${OTLP_EXPORTER_SOURCE_DIR}/OtlpGrpcLogRecordExporterProxy.cpp)
323330
endif()
331+
if(WITH_OTLP_FILE)
332+
set(OPENTELEMETRY_PROXY_SOURCES
333+
${OPENTELEMETRY_PROXY_SOURCES}
334+
${OTLP_EXPORTER_SOURCE_DIR}/OtlpFileSpanExporterProxy.cpp
335+
${OTLP_EXPORTER_SOURCE_DIR}/OtlpFileMetricExporterProxy.cpp
336+
${OTLP_EXPORTER_SOURCE_DIR}/OtlpFileLogRecordExporterProxy.cpp)
337+
endif()
324338

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

341-
# Additional compiler flags for HTTP/gRPC exporters
355+
# Additional compiler flags for HTTP/gRPC/file exporters
342356
if(WITH_OTLP_HTTP)
343357
target_compile_definitions(${OPENTELEMETRY_PROXY_LIBRARY_NAME} PRIVATE WITH_OTLP_HTTP)
344358
endif()
@@ -347,6 +361,10 @@ if(WITH_OTLP_GRPC)
347361
target_compile_definitions(${OPENTELEMETRY_PROXY_LIBRARY_NAME} PRIVATE WITH_OTLP_GRPC)
348362
endif()
349363

364+
if(WITH_OTLP_FILE)
365+
target_compile_definitions(${OPENTELEMETRY_PROXY_LIBRARY_NAME} PRIVATE WITH_OTLP_FILE)
366+
endif()
367+
350368
# pass in version number
351369
target_compile_definitions(${OPENTELEMETRY_PROXY_LIBRARY_NAME} PRIVATE OTEL_MATLAB_VERSION="${OTEL_MATLAB_VERSION}")
352370
if(WIN32)
@@ -472,6 +490,11 @@ set(OTLP_GRPC_EXPORTER_MATLAB_SOURCES
472490
${CMAKE_CURRENT_SOURCE_DIR}/exporters/otlp/+opentelemetry/+exporters/+otlp/OtlpGrpcMetricExporter.m
473491
${CMAKE_CURRENT_SOURCE_DIR}/exporters/otlp/+opentelemetry/+exporters/+otlp/OtlpGrpcLogRecordExporter.m
474492
${CMAKE_CURRENT_SOURCE_DIR}/exporters/otlp/+opentelemetry/+exporters/+otlp/OtlpGrpcValidator.m)
493+
set(OTLP_FILE_EXPORTER_MATLAB_SOURCES
494+
${CMAKE_CURRENT_SOURCE_DIR}/exporters/otlp/+opentelemetry/+exporters/+otlp/OtlpFileSpanExporter.m
495+
${CMAKE_CURRENT_SOURCE_DIR}/exporters/otlp/+opentelemetry/+exporters/+otlp/OtlpFileMetricExporter.m
496+
${CMAKE_CURRENT_SOURCE_DIR}/exporters/otlp/+opentelemetry/+exporters/+otlp/OtlpFileLogRecordExporter.m
497+
${CMAKE_CURRENT_SOURCE_DIR}/exporters/otlp/+opentelemetry/+exporters/+otlp/OtlpFileValidator.m)
475498
set(OTLP_MISC_FILES ${CMAKE_CURRENT_SOURCE_DIR}/LICENSE)
476499

477500
set(OTLP_EXPORTERS_DIR +opentelemetry/+exporters/+otlp)
@@ -493,6 +516,9 @@ endif()
493516
if(WITH_OTLP_GRPC)
494517
install(FILES ${OTLP_GRPC_EXPORTER_MATLAB_SOURCES} DESTINATION ${OTLP_EXPORTERS_DIR})
495518
endif()
519+
if(WITH_OTLP_FILE)
520+
install(FILES ${OTLP_FILE_EXPORTER_MATLAB_SOURCES} DESTINATION ${OTLP_EXPORTERS_DIR})
521+
endif()
496522
# Install license. The only reason it is needed is to work around a packaging bug where if
497523
# all files are in packages (+xxx directories), matlab.addons.toolbox.packageToolbox refuses to add the
498524
# toolbox path into MATLAB path. This issue is fixed in R2024a

Diff for: OtelMatlabProxyFactory.cpp

+10
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,11 @@
4040
#include "opentelemetry-matlab/exporters/otlp/OtlpGrpcMetricExporterProxy.h"
4141
#include "opentelemetry-matlab/exporters/otlp/OtlpGrpcLogRecordExporterProxy.h"
4242
#endif
43+
#ifdef WITH_OTLP_FILE
44+
#include "opentelemetry-matlab/exporters/otlp/OtlpFileSpanExporterProxy.h"
45+
#include "opentelemetry-matlab/exporters/otlp/OtlpFileMetricExporterProxy.h"
46+
#include "opentelemetry-matlab/exporters/otlp/OtlpFileLogRecordExporterProxy.h"
47+
#endif
4348

4449
libmexclass::proxy::MakeResult
4550
OtelMatlabProxyFactory::make_proxy(const libmexclass::proxy::ClassName& class_name,
@@ -88,5 +93,10 @@ OtelMatlabProxyFactory::make_proxy(const libmexclass::proxy::ClassName& class_na
8893
REGISTER_PROXY(libmexclass.opentelemetry.exporters.OtlpGrpcMetricExporterProxy, libmexclass::opentelemetry::exporters::OtlpGrpcMetricExporterProxy);
8994
REGISTER_PROXY(libmexclass.opentelemetry.exporters.OtlpGrpcLogRecordExporterProxy, libmexclass::opentelemetry::exporters::OtlpGrpcLogRecordExporterProxy);
9095
#endif
96+
#ifdef WITH_OTLP_FILE
97+
REGISTER_PROXY(libmexclass.opentelemetry.exporters.OtlpFileSpanExporterProxy, libmexclass::opentelemetry::exporters::OtlpFileSpanExporterProxy);
98+
REGISTER_PROXY(libmexclass.opentelemetry.exporters.OtlpFileMetricExporterProxy, libmexclass::opentelemetry::exporters::OtlpFileMetricExporterProxy);
99+
REGISTER_PROXY(libmexclass.opentelemetry.exporters.OtlpFileLogRecordExporterProxy, libmexclass::opentelemetry::exporters::OtlpFileLogRecordExporterProxy);
100+
#endif
91101
return nullptr;
92102
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,124 @@
1+
classdef OtlpFileLogRecordExporter < opentelemetry.sdk.logs.LogRecordExporter
2+
% OtlpFileLogRecordExporter exports log records in OpenTelemetry Protocol format to
3+
% one or more files.
4+
5+
% Copyright 2024 The MathWorks, Inc.
6+
7+
properties
8+
FileName (1,1) string = "logs-%N.jsonl" % Output file name
9+
AliasName (1,1) string = "logs-latest.jsonl" % Alias file name, which is the latest file in rotation
10+
FlushInterval (1,1) duration = seconds(30) % Time interval between log record exports
11+
FlushRecordCount (1,1) double = 256 % Maximum number of records before exporting
12+
MaxFileSize (1,1) double = 20971520 % Maximum output file size
13+
MaxFileCount (1,1) double = 10 % Maximum number of output files, written to in rotation.
14+
end
15+
16+
properties (Access=private, Constant)
17+
Validator = opentelemetry.exporters.otlp.OtlpFileValidator
18+
end
19+
20+
methods
21+
function obj = OtlpFileLogRecordExporter(optionnames, optionvalues)
22+
% OtlpFileLogRecordExporter exports log records in OpenTelemetry
23+
% Protocol format to one or more files.
24+
% EXP = OPENTELEMETRY.EXPORTERS.OTLP.OTLPFILELOGRECORDEXPORTER
25+
% creates an exporter that uses default configurations.
26+
%
27+
% EXP =
28+
% OPENTELEMETRY.EXPORTERS.OTLP.OTLPFILELOGRECORDEXPORTER(PARAM1,
29+
% VALUE1, PARAM2, VALUE2, ...) specifies optional parameter
30+
% name/value pairs. Parameters are:
31+
% "FileName" - Output file name. Can contain
32+
% pattern placeholders. Default
33+
% name is "logs-%N.jsonl"
34+
% "AliasName" - Alias file name, which is the
35+
% latest file in rotation. Can
36+
% contain pattern placeholders.
37+
% Default name is "logs-latest.jsonl"
38+
% "FlushInterval" - Time interval between log record
39+
% exports, represented as a
40+
% duration. Default is 30 seconds.
41+
% "FlushRecordCount" - Maximum number of records before
42+
% exporting. When the number of
43+
% records exceed this value, an
44+
% export will be started. Default
45+
% is 256.
46+
% "MaxFileSize" - Maximum output file size
47+
% "MaxFileCount" - Maximum number of output files,
48+
% written to in rotation. Default
49+
% is 10.
50+
%
51+
% Supported pattern placeholders in FileName and AliasName are:
52+
% %Y: year as a 4 digit decimal number
53+
% %y: last 2 digits of year as a decimal number (range [00,99])
54+
% %m: month as a decimal number (range [01,12])
55+
% %j: day of the year as a decimal number (range [001,366])
56+
% %d: day of the month as a decimal number (range [01,31])
57+
% %w: weekday as a decimal number, where Sunday is 0 (range [0-6])
58+
% %H: hour as a decimal number, 24 hour clock (range [00-23])
59+
% %I: hour as a decimal number, 12 hour clock (range [01,12])
60+
% %M: minute as a decimal number (range [00,59])
61+
% %S: second as a decimal number (range [00,60])
62+
% %F: equivalent to "%Y-%m-%d" (the ISO 8601 date format)
63+
% %T: equivalent to "%H:%M:%S" (the ISO 8601 time format)
64+
% %R: equivalent to "%H:%M"
65+
% %N: rotate index, starting from 0
66+
% %n: rotate index, starting from 1
67+
%
68+
% See also
69+
% OPENTELEMETRY.EXPORTERS.OTLP.OTLPHTTPLOGRECORDEXPORTER,
70+
% OPENTELEMETRY.EXPORTERS.OTLP.OTLPGRPCLOGRECORDEXPORTER
71+
arguments (Repeating)
72+
optionnames (1,:) {mustBeTextScalar}
73+
optionvalues
74+
end
75+
76+
77+
"libmexclass.opentelemetry.exporters.OtlpFileLogRecordExporterProxy");
78+
79+
validnames = ["FileName", "AliasName", "FlushInterval", ...
80+
"FlushRecordCount", "MaxFileSize", "MaxFileCount"];
81+
for i = 1:length(optionnames)
82+
namei = validatestring(optionnames{i}, validnames);
83+
valuei = optionvalues{i};
84+
obj.(namei) = valuei;
85+
end
86+
end
87+
88+
function obj = set.FileName(obj, fn)
89+
fn = obj.Validator.validateName(fn, "FileName");
90+
obj.Proxy.setFileName(fn);
91+
obj.FileName = fn;
92+
end
93+
94+
function obj = set.AliasName(obj, alias)
95+
alias = obj.Validator.validateName(alias, "AliasName");
96+
obj.Proxy.setAliasName(alias);
97+
obj.AliasName = alias;
98+
end
99+
100+
function obj = set.FlushInterval(obj, interval)
101+
obj.Validator.validateFlushInterval(interval);
102+
obj.Proxy.setFlushInterval(milliseconds(interval));
103+
obj.FlushInterval = interval;
104+
end
105+
106+
function obj = set.FlushRecordCount(obj, count)
107+
count = obj.Validator.validateScalarPositiveInteger(count, "FlushRecordCount");
108+
obj.Proxy.setFlushRecordCount(count);
109+
obj.FlushRecordCount = count;
110+
end
111+
112+
function obj = set.MaxFileSize(obj, maxsize)
113+
maxsize = obj.Validator.validateScalarPositiveInteger(maxsize, "MaxFileSize");
114+
obj.Proxy.setMaxFileSize(maxsize);
115+
obj.MaxFileSize = maxsize;
116+
end
117+
118+
function obj = set.MaxFileCount(obj, maxfilecount)
119+
maxfilecount = obj.Validator.validateScalarPositiveInteger(maxfilecount, "MaxFileCount");
120+
obj.Proxy.setMaxFileCount(maxfilecount);
121+
obj.MaxFileCount = maxfilecount;
122+
end
123+
end
124+
end

0 commit comments

Comments
 (0)