Skip to content

Commit 8459290

Browse files
committed
Use a standalone flag to switch deprecated LogRecord APIs
Fixes GetBody::ReadWriteLogRecord
1 parent 6044690 commit 8459290

11 files changed

+62
-41
lines changed

CHANGELOG.md

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -63,13 +63,15 @@ Important changes:
6363
* [SDK] Fix lifetime for sdk::ReadWriteLogRecord
6464
[#3147](https://github.com/open-telemetry/opentelemetry-cpp/pull/3245)
6565

66-
* With ABI version 2, `opentelemetry::sdk::logs::ReadableLogRecord::GetAttributes()`
66+
* `opentelemetry::sdk::logs::ReadableLogRecord::GetAttributes()`
6767
will returns a `std::unordered_map<std::string, opentelemetry::sdk::common::OwnedAttributeValue>`
6868
instead of a `std::unordered_map<std::string, opentelemetry::common::AttributeValue>`
6969
to keep the same type as the `opentelemetry::sdk::trace::SpanData`. And
7070
`opentelemetry::sdk::logs::ReadableLogRecord::GetBody()` will returns a
7171
`const opentelemetry::sdk::common::OwnedAttributeValue &` instead of a
72-
`const opentelemetry::common::AttributeValue &`.
72+
`const opentelemetry::common::AttributeValue &` now, we switch to the old
73+
APIs by add `-DWITH_DEPRECATED_SDK_LOG_RECORD=ON` for cmake or add
74+
`--copt -DOPENTELEMETRY_DEPRECATED_SDK_LOG_RECORD` for bazel.
7375

7476
## [1.20 2025-04-01]
7577

CMakeLists.txt

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -185,6 +185,13 @@ message(STATUS "OPENTELEMETRY_VERSION=${OPENTELEMETRY_VERSION}")
185185

186186
option(WITH_NO_DEPRECATED_CODE "Do not include deprecated code" OFF)
187187

188+
option(WITH_DEPRECATED_SDK_LOG_RECORD "Use deprecated SDK log record" OFF)
189+
190+
if(WITH_DEPRECATED_SDK_LOG_RECORD)
191+
message(
192+
WARNING "WITH_DEPRECATED_SDK_LOG_RECORD=ON is temporary and deprecated")
193+
endif()
194+
188195
set(WITH_STL
189196
"OFF"
190197
CACHE STRING "Which version of the Standard Library for C++ to use")

ci/do_ci.ps1

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,7 @@ switch ($action) {
6767
cmake $SRC_DIR `
6868
-DVCPKG_TARGET_TRIPLET=x64-windows `
6969
-DOPENTELEMETRY_BUILD_DLL=1 `
70+
-DWITH_DEPRECATED_SDK_LOG_RECORD=ON `
7071
"-DCMAKE_TOOLCHAIN_FILE=$VCPKG_DIR/scripts/buildsystems/vcpkg.cmake"
7172
$exit = $LASTEXITCODE
7273
if ($exit -ne 0) {
@@ -221,6 +222,7 @@ switch ($action) {
221222
cmake $SRC_DIR `
222223
-DVCPKG_TARGET_TRIPLET=x64-windows `
223224
-DWITH_ASYNC_EXPORT_PREVIEW=ON `
225+
-DWITH_DEPRECATED_SDK_LOG_RECORD=ON `
224226
"-DCMAKE_TOOLCHAIN_FILE=$VCPKG_DIR/scripts/buildsystems/vcpkg.cmake"
225227
$exit = $LASTEXITCODE
226228
if ($exit -ne 0) {

ci/do_ci.sh

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -228,6 +228,7 @@ elif [[ "$1" == "cmake.with_async_export.test" ]]; then
228228
-DWITH_METRICS_EXEMPLAR_PREVIEW=ON \
229229
-DCMAKE_CXX_FLAGS="-Werror $CXXFLAGS" \
230230
-DWITH_ASYNC_EXPORT_PREVIEW=ON \
231+
-DWITH_DEPRECATED_SDK_LOG_RECORD=ON \
231232
"${SRC_DIR}"
232233
make -j $(nproc)
233234
make test
@@ -281,6 +282,7 @@ elif [[ "$1" == "cmake.c++20.test" ]]; then
281282
-DCMAKE_CXX_FLAGS="-Werror $CXXFLAGS" \
282283
-DWITH_ASYNC_EXPORT_PREVIEW=ON \
283284
-DWITH_STL=CXX20 \
285+
-DWITH_DEPRECATED_SDK_LOG_RECORD=ON \
284286
"${SRC_DIR}"
285287
eval "$MAKE_COMMAND"
286288
make test
@@ -292,6 +294,7 @@ elif [[ "$1" == "cmake.c++23.test" ]]; then
292294
-DCMAKE_CXX_FLAGS="-Werror $CXXFLAGS" \
293295
-DWITH_ASYNC_EXPORT_PREVIEW=ON \
294296
-DWITH_STL=CXX23 \
297+
-DWITH_DEPRECATED_SDK_LOG_RECORD=ON \
295298
"${SRC_DIR}"
296299
eval "$MAKE_COMMAND"
297300
make test
@@ -304,6 +307,7 @@ elif [[ "$1" == "cmake.c++14.stl.test" ]]; then
304307
-DCMAKE_CXX_FLAGS="-Werror $CXXFLAGS" \
305308
-DWITH_ASYNC_EXPORT_PREVIEW=ON \
306309
-DWITH_STL=CXX14 \
310+
-DWITH_DEPRECATED_SDK_LOG_RECORD=ON \
307311
"${SRC_DIR}"
308312
eval "$MAKE_COMMAND"
309313
make test
@@ -316,6 +320,7 @@ elif [[ "$1" == "cmake.c++17.stl.test" ]]; then
316320
-DCMAKE_CXX_FLAGS="-Werror $CXXFLAGS" \
317321
-DWITH_ASYNC_EXPORT_PREVIEW=ON \
318322
-DWITH_STL=CXX17 \
323+
-DWITH_DEPRECATED_SDK_LOG_RECORD=ON \
319324
"${SRC_DIR}"
320325
eval "$MAKE_COMMAND"
321326
make test

sdk/include/opentelemetry/sdk/logs/log_record_data.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ class LogRecordData final : public ReadableLogRecord
8282
* Get body field of this log. \
8383
* @return the body field for this log. \
8484
*/
85-
#if OPENTELEMETRY_ABI_VERSION_NO >= 2
85+
#if !defined(OPENTELEMETRY_DEPRECATED_SDK_LOG_RECORD)
8686
const common::OwnedAttributeValue &
8787
#else
8888
const opentelemetry::common::AttributeValue &
@@ -156,7 +156,7 @@ class LogRecordData final : public ReadableLogRecord
156156
* Get attributes of this log.
157157
* @return the body field of this log
158158
*/
159-
#if OPENTELEMETRY_ABI_VERSION_NO >= 2
159+
#if !defined(OPENTELEMETRY_DEPRECATED_SDK_LOG_RECORD)
160160
const std::unordered_map<std::string, common::OwnedAttributeValue> &
161161
#else
162162
const std::unordered_map<std::string, opentelemetry::common::AttributeValue> &
@@ -196,7 +196,7 @@ class LogRecordData final : public ReadableLogRecord
196196
const opentelemetry::sdk::resource::Resource *resource_;
197197
const opentelemetry::sdk::instrumentationscope::InstrumentationScope *instrumentation_scope_;
198198

199-
#if OPENTELEMETRY_ABI_VERSION_NO >= 2
199+
#if !defined(OPENTELEMETRY_DEPRECATED_SDK_LOG_RECORD)
200200
common::AttributeMap attributes_map_;
201201
// We resue the same utility functions of MixedAttributeMap with key="" for the body field
202202
common::OwnedAttributeValue body_;

sdk/include/opentelemetry/sdk/logs/read_write_log_record.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ class ReadWriteLogRecord final : public ReadableLogRecord
8282
* Get body field of this log.
8383
* @return the body field for this log.
8484
*/
85-
#if OPENTELEMETRY_ABI_VERSION_NO >= 2
85+
#if !defined(OPENTELEMETRY_DEPRECATED_SDK_LOG_RECORD)
8686
const common::OwnedAttributeValue &
8787
#else
8888
const opentelemetry::common::AttributeValue &
@@ -156,7 +156,7 @@ class ReadWriteLogRecord final : public ReadableLogRecord
156156
* Get attributes of this log.
157157
* @return the body field of this log
158158
*/
159-
#if OPENTELEMETRY_ABI_VERSION_NO >= 2
159+
#if !defined(OPENTELEMETRY_DEPRECATED_SDK_LOG_RECORD)
160160
const std::unordered_map<std::string, common::OwnedAttributeValue> &
161161
#else
162162
const std::unordered_map<std::string, opentelemetry::common::AttributeValue> &
@@ -196,7 +196,7 @@ class ReadWriteLogRecord final : public ReadableLogRecord
196196
const opentelemetry::sdk::resource::Resource *resource_;
197197
const opentelemetry::sdk::instrumentationscope::InstrumentationScope *instrumentation_scope_;
198198

199-
#if OPENTELEMETRY_ABI_VERSION_NO >= 2
199+
#if !defined(OPENTELEMETRY_DEPRECATED_SDK_LOG_RECORD)
200200
common::AttributeMap attributes_map_;
201201
// We resue the same utility functions of MixedAttributeMap with key="" for the body field
202202
common::OwnedAttributeValue body_;

sdk/include/opentelemetry/sdk/logs/readable_log_record.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ class ReadableLogRecord : public Recordable
7575
* Get body field of this log.
7676
* @return the body field for this log.
7777
*/
78-
#if OPENTELEMETRY_ABI_VERSION_NO >= 2
78+
#if !defined(OPENTELEMETRY_DEPRECATED_SDK_LOG_RECORD)
7979
virtual const common::OwnedAttributeValue &
8080
#else
8181
virtual const opentelemetry::common::AttributeValue &
@@ -116,7 +116,7 @@ class ReadableLogRecord : public Recordable
116116
* Get attributes of this log.
117117
* @return the body field of this log
118118
*/
119-
#if OPENTELEMETRY_ABI_VERSION_NO >= 2
119+
#if !defined(OPENTELEMETRY_DEPRECATED_SDK_LOG_RECORD)
120120
virtual const std::unordered_map<std::string, common::OwnedAttributeValue> &
121121
#else
122122
virtual const std::unordered_map<std::string, opentelemetry::common::AttributeValue> &

sdk/src/logs/CMakeLists.txt

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,11 @@ target_link_libraries(opentelemetry_logs PUBLIC opentelemetry_resources
3131
opentelemetry_common)
3232
set_target_version(opentelemetry_logs)
3333

34+
if(NOT WITH_NO_DEPRECATED_CODE AND WITH_DEPRECATED_SDK_LOG_RECORD)
35+
target_compile_definitions(opentelemetry_logs
36+
PUBLIC OPENTELEMETRY_DEPRECATED_SDK_LOG_RECORD)
37+
endif()
38+
3439
target_include_directories(
3540
opentelemetry_logs
3641
PUBLIC "$<BUILD_INTERFACE:${PROJECT_SOURCE_DIR}/sdk/include>")

sdk/src/logs/log_record_data.cc

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ LogRecordData::LogRecordData()
3636
event_name_("")
3737
{
3838

39-
#if OPENTELEMETRY_ABI_VERSION_NO >= 2
39+
#if !defined(OPENTELEMETRY_DEPRECATED_SDK_LOG_RECORD)
4040
body_ = std::string();
4141
#else
4242
body_.SetAttribute("", nostd::string_view());
@@ -77,21 +77,21 @@ opentelemetry::logs::Severity LogRecordData::GetSeverity() const noexcept
7777

7878
void LogRecordData::SetBody(const opentelemetry::common::AttributeValue &message) noexcept
7979
{
80-
#if OPENTELEMETRY_ABI_VERSION_NO >= 2
80+
#if !defined(OPENTELEMETRY_DEPRECATED_SDK_LOG_RECORD)
8181
body_ = nostd::visit(attribute_converter_, message);
8282
#else
8383
body_.SetAttribute("", message);
8484
#endif
8585
}
8686

87-
#if OPENTELEMETRY_ABI_VERSION_NO >= 2
87+
#if !defined(OPENTELEMETRY_DEPRECATED_SDK_LOG_RECORD)
8888
const common::OwnedAttributeValue &
8989
#else
9090
const opentelemetry::common::AttributeValue &
9191
#endif
9292
LogRecordData::GetBody() const noexcept
9393
{
94-
#if OPENTELEMETRY_ABI_VERSION_NO >= 2
94+
#if !defined(OPENTELEMETRY_DEPRECATED_SDK_LOG_RECORD)
9595
return body_;
9696
#else
9797
return body_.GetAttributes().begin()->second;
@@ -183,7 +183,7 @@ void LogRecordData::SetAttribute(nostd::string_view key,
183183
attributes_map_.SetAttribute(key, value);
184184
}
185185

186-
#if OPENTELEMETRY_ABI_VERSION_NO >= 2
186+
#if !defined(OPENTELEMETRY_DEPRECATED_SDK_LOG_RECORD)
187187
const std::unordered_map<std::string, opentelemetry::sdk::common::OwnedAttributeValue> &
188188
#else
189189
const std::unordered_map<std::string, opentelemetry::common::AttributeValue> &

sdk/src/logs/read_write_log_record.cc

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ ReadWriteLogRecord::ReadWriteLogRecord()
3030
: severity_(opentelemetry::logs::Severity::kInvalid),
3131
resource_(nullptr),
3232
instrumentation_scope_(nullptr),
33-
#if OPENTELEMETRY_ABI_VERSION_NO >= 2
33+
#if !defined(OPENTELEMETRY_DEPRECATED_SDK_LOG_RECORD)
3434
body_(std::string()),
3535
#else
3636
body_(nostd::string_view()),
@@ -75,14 +75,14 @@ opentelemetry::logs::Severity ReadWriteLogRecord::GetSeverity() const noexcept
7575

7676
void ReadWriteLogRecord::SetBody(const opentelemetry::common::AttributeValue &message) noexcept
7777
{
78-
#if OPENTELEMETRY_ABI_VERSION_NO >= 2
78+
#if !defined(OPENTELEMETRY_DEPRECATED_SDK_LOG_RECORD)
7979
body_ = nostd::visit(attribute_converter_, message);
8080
#else
8181
body_ = message;
8282
#endif
8383
}
8484

85-
#if OPENTELEMETRY_ABI_VERSION_NO >= 2
85+
#if !defined(OPENTELEMETRY_DEPRECATED_SDK_LOG_RECORD)
8686
const common::OwnedAttributeValue &
8787
#else
8888
const opentelemetry::common::AttributeValue &
@@ -174,21 +174,21 @@ const opentelemetry::trace::TraceFlags &ReadWriteLogRecord::GetTraceFlags() cons
174174
void ReadWriteLogRecord::SetAttribute(nostd::string_view key,
175175
const opentelemetry::common::AttributeValue &value) noexcept
176176
{
177-
#if OPENTELEMETRY_ABI_VERSION_NO >= 2
177+
#if !defined(OPENTELEMETRY_DEPRECATED_SDK_LOG_RECORD)
178178
attributes_map_.SetAttribute(key, value);
179179
#else
180180
attributes_map_[std::string(key)] = value;
181181
#endif
182182
}
183183

184-
#if OPENTELEMETRY_ABI_VERSION_NO >= 2
184+
#if !defined(OPENTELEMETRY_DEPRECATED_SDK_LOG_RECORD)
185185
const std::unordered_map<std::string, opentelemetry::sdk::common::OwnedAttributeValue> &
186186
#else
187187
const std::unordered_map<std::string, opentelemetry::common::AttributeValue> &
188188
#endif
189189
ReadWriteLogRecord::GetAttributes() const noexcept
190190
{
191-
#if OPENTELEMETRY_ABI_VERSION_NO >= 2
191+
#if !defined(OPENTELEMETRY_DEPRECATED_SDK_LOG_RECORD)
192192
return attributes_map_.GetAttributes();
193193
#else
194194
return attributes_map_;

0 commit comments

Comments
 (0)