Skip to content

Commit 84a1463

Browse files
authored
Merge branch 'main' into fix/reader_data_race
2 parents 549bc41 + 25738f3 commit 84a1463

File tree

14 files changed

+268
-48
lines changed

14 files changed

+268
-48
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@ Increment the:
1717

1818
* [EXPORTER] Gzip compression support for OTLP/HTTP and OTLP/gRPC exporter
1919
[#2530](https://github.com/open-telemetry/opentelemetry-cpp/pull/2530)
20+
* [EXPORTER] Support URL-encoded values for `OTEL_EXPORTER_OTLP_HEADERS`
21+
[#2579](https://github.com/open-telemetry/opentelemetry-cpp/pull/2579)
2022

2123
Important changes:
2224

CMakeLists.txt

Lines changed: 16 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
# Copyright The OpenTelemetry Authors
22
# SPDX-License-Identifier: Apache-2.0
33

4-
cmake_minimum_required(VERSION 3.1)
4+
cmake_minimum_required(VERSION 3.9)
55

66
# See https://cmake.org/cmake/help/v3.3/policy/CMP0057.html required by certain
77
# versions of gtest
@@ -301,15 +301,16 @@ function(install_windows_deps)
301301
set(VCPKG_TARGET_ARCHITECTURE
302302
${ARCH}
303303
PARENT_SCOPE)
304-
message("Installing build tools and dependencies...")
304+
message(STATUS "Installing build tools and dependencies...")
305305
set(ENV{ARCH} ${ARCH})
306306
execute_process(
307307
COMMAND ${CMAKE_CURRENT_SOURCE_DIR}/tools/setup-buildtools.cmd)
308308
set(CMAKE_TOOLCHAIN_FILE
309309
${CMAKE_CURRENT_SOURCE_DIR}/tools/vcpkg/scripts/buildsystems/vcpkg.cmake
310310
CACHE FILEPATH "")
311311
message(
312-
"Make sure that vcpkg.cmake is set as the CMAKE_TOOLCHAIN_FILE at the START of the cmake build process!
312+
STATUS
313+
"Make sure that vcpkg.cmake is set as the CMAKE_TOOLCHAIN_FILE at the START of the cmake build process!
313314
Can be command-line arg (cmake -DCMAKE_TOOLCHAIN_FILE=...) or set in your editor of choice."
314315
)
315316

@@ -347,7 +348,7 @@ include(GNUInstallDirs)
347348
if(WITH_PROMETHEUS)
348349
find_package(prometheus-cpp CONFIG QUIET)
349350
if(NOT prometheus-cpp_FOUND)
350-
message("Trying to use local prometheus-cpp from submodule")
351+
message(STATUS "Trying to use local prometheus-cpp from submodule")
351352
if(EXISTS ${PROJECT_SOURCE_DIR}/third_party/prometheus-cpp/.git)
352353
set(SAVED_ENABLE_TESTING ${ENABLE_TESTING})
353354
set(ENABLE_TESTING OFF)
@@ -361,7 +362,7 @@ if(WITH_PROMETHEUS)
361362
"git submodule update --init --recursive")
362363
endif()
363364
else()
364-
message("Using external prometheus-cpp")
365+
message(STATUS "Using external prometheus-cpp")
365366
endif()
366367
endif()
367368

@@ -389,7 +390,7 @@ if(WITH_OTLP_GRPC OR WITH_OTLP_HTTP)
389390
endif()
390391

391392
if(WIN32 AND (NOT DEFINED CMAKE_TOOLCHAIN_FILE))
392-
message(STATUS_FATAL "Windows dependency installation failed!")
393+
message(FATAL_ERROR "Windows dependency installation failed!")
393394
endif()
394395
if(WIN32)
395396
include(${CMAKE_TOOLCHAIN_FILE})
@@ -485,15 +486,15 @@ endif()
485486

486487
if(OTELCPP_MAINTAINER_MODE)
487488
if(CMAKE_CXX_COMPILER_ID STREQUAL "GNU")
488-
message("Building with gcc in maintainer mode.")
489+
message(STATUS "Building with gcc in maintainer mode.")
489490

490491
add_compile_options(-Wall)
491492
add_compile_options(-Werror)
492493
add_compile_options(-Wextra)
493494

494495
# Tested with GCC 9.4 on github.
495496
if(CMAKE_CXX_COMPILER_VERSION VERSION_GREATER_EQUAL 9.4)
496-
message("Building with additional warnings for gcc.")
497+
message(STATUS "Building with additional warnings for gcc.")
497498

498499
# Relaxed warnings
499500

@@ -518,15 +519,15 @@ if(OTELCPP_MAINTAINER_MODE)
518519
add_compile_options(-Wvla)
519520
endif()
520521
elseif(CMAKE_CXX_COMPILER_ID STREQUAL "Clang")
521-
message("Building with clang in maintainer mode.")
522+
message(STATUS "Building with clang in maintainer mode.")
522523

523524
add_compile_options(-Wall)
524525
add_compile_options(-Werror)
525526
add_compile_options(-Wextra)
526527

527528
# Tested with Clang 11.0 on github.
528529
if(CMAKE_CXX_COMPILER_VERSION VERSION_GREATER_EQUAL 11.0)
529-
message("Building with additional warnings for clang.")
530+
message(STATUS "Building with additional warnings for clang.")
530531

531532
# Relaxed warnings
532533
add_compile_options(-Wno-error=unused-private-field)
@@ -549,7 +550,7 @@ if(OTELCPP_MAINTAINER_MODE)
549550
add_compile_options(-Wold-style-cast)
550551
endif()
551552
elseif(CMAKE_CXX_COMPILER_ID STREQUAL "MSVC")
552-
message("Building with msvc in maintainer mode.")
553+
message(STATUS "Building with msvc in maintainer mode.")
553554

554555
add_compile_options(/WX)
555556
add_compile_options(/W4)
@@ -619,8 +620,8 @@ if(BUILD_TESTING)
619620
if(GTEST_INCLUDE_DIRS)
620621
include_directories(SYSTEM ${GTEST_INCLUDE_DIRS})
621622
endif()
622-
message("GTEST_INCLUDE_DIRS = ${GTEST_INCLUDE_DIRS}")
623-
message("GTEST_BOTH_LIBRARIES = ${GTEST_BOTH_LIBRARIES}")
623+
message(STATUS "GTEST_INCLUDE_DIRS = ${GTEST_INCLUDE_DIRS}")
624+
message(STATUS "GTEST_BOTH_LIBRARIES = ${GTEST_BOTH_LIBRARIES}")
624625
enable_testing()
625626
if(WITH_BENCHMARK)
626627
# Benchmark respects the CMAKE_PREFIX_PATH
@@ -652,7 +653,7 @@ if(WITH_OPENTRACING)
652653
find_package(OpenTracing CONFIG QUIET)
653654
if(NOT OpenTracing_FOUND)
654655
set(OPENTRACING_DIR "third_party/opentracing-cpp")
655-
message("Trying to use local ${OPENTRACING_DIR} from submodule")
656+
message(STATUS "Trying to use local ${OPENTRACING_DIR} from submodule")
656657
if(EXISTS "${PROJECT_SOURCE_DIR}/${OPENTRACING_DIR}/.git")
657658
set(SAVED_BUILD_TESTING ${BUILD_TESTING})
658659
set(BUILD_TESTING OFF)
@@ -666,7 +667,7 @@ if(WITH_OPENTRACING)
666667
"git submodule update --init --recursive")
667668
endif()
668669
else()
669-
message("Using external opentracing-cpp")
670+
message(STATUS "Using external opentracing-cpp")
670671
endif()
671672
add_subdirectory(opentracing-shim)
672673
endif()

api/include/opentelemetry/common/macros.h

Lines changed: 100 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -3,22 +3,112 @@
33

44
#pragma once
55

6-
#if !defined(OPENTELEMETRY_LIKELY_IF) && defined(__cplusplus)
6+
/*
7+
Expected usage pattern:
8+
9+
if OPENTELEMETRY_LIKELY_CONDITION (ptr != nullptr)
10+
{
11+
do_something_likely();
12+
} else {
13+
do_something_unlikely();
14+
}
15+
16+
This pattern works with gcc/clang and __builtin_expect(),
17+
as well as with C++20.
18+
It is unclear if __builtin_expect() will be deprecated
19+
in favor of C++20 [[likely]] or not.
20+
21+
OPENTELEMETRY_LIKELY_CONDITION is preferred over OPENTELEMETRY_LIKELY,
22+
to be revisited when C++20 is required.
23+
*/
24+
25+
#if !defined(OPENTELEMETRY_LIKELY_CONDITION) && defined(__cplusplus)
26+
// Only use likely with C++20
27+
# if __cplusplus >= 202002L
728
// GCC 9 has likely attribute but do not support declare it at the beginning of statement
8-
# if defined(__has_cpp_attribute) && (defined(__clang__) || !defined(__GNUC__) || __GNUC__ > 9)
9-
# if __has_cpp_attribute(likely)
10-
# define OPENTELEMETRY_LIKELY_IF(...) \
11-
if (__VA_ARGS__) \
12-
[[likely]]
29+
# if defined(__has_cpp_attribute) && (defined(__clang__) || !defined(__GNUC__) || __GNUC__ > 9)
30+
# if __has_cpp_attribute(likely)
31+
# define OPENTELEMETRY_LIKELY_CONDITION(C) (C) [[likely]]
32+
# endif
33+
# endif
34+
# endif
35+
#endif
36+
#if !defined(OPENTELEMETRY_LIKELY_CONDITION) && (defined(__clang__) || defined(__GNUC__))
37+
// Only use if supported by the compiler
38+
# define OPENTELEMETRY_LIKELY_CONDITION(C) (__builtin_expect(!!(C), true))
39+
#endif
40+
#ifndef OPENTELEMETRY_LIKELY_CONDITION
41+
// Do not use likely annotations
42+
# define OPENTELEMETRY_LIKELY_CONDITION(C) (C)
43+
#endif
1344

45+
#if !defined(OPENTELEMETRY_UNLIKELY_CONDITION) && defined(__cplusplus)
46+
// Only use unlikely with C++20
47+
# if __cplusplus >= 202002L
48+
// GCC 9 has unlikely attribute but do not support declare it at the beginning of statement
49+
# if defined(__has_cpp_attribute) && (defined(__clang__) || !defined(__GNUC__) || __GNUC__ > 9)
50+
# if __has_cpp_attribute(unlikely)
51+
# define OPENTELEMETRY_UNLIKELY_CONDITION(C) (C) [[unlikely]]
52+
# endif
1453
# endif
1554
# endif
1655
#endif
17-
#if !defined(OPENTELEMETRY_LIKELY_IF) && (defined(__clang__) || defined(__GNUC__))
18-
# define OPENTELEMETRY_LIKELY_IF(...) if (__builtin_expect(!!(__VA_ARGS__), true))
56+
#if !defined(OPENTELEMETRY_UNLIKELY_CONDITION) && (defined(__clang__) || defined(__GNUC__))
57+
// Only use if supported by the compiler
58+
# define OPENTELEMETRY_UNLIKELY_CONDITION(C) (__builtin_expect(!!(C), false))
1959
#endif
20-
#ifndef OPENTELEMETRY_LIKELY_IF
21-
# define OPENTELEMETRY_LIKELY_IF(...) if (__VA_ARGS__)
60+
#ifndef OPENTELEMETRY_UNLIKELY_CONDITION
61+
// Do not use unlikely annotations
62+
# define OPENTELEMETRY_UNLIKELY_CONDITION(C) (C)
63+
#endif
64+
65+
/*
66+
Expected usage pattern:
67+
68+
if (ptr != nullptr)
69+
OPENTELEMETRY_LIKELY
70+
{
71+
do_something_likely();
72+
} else {
73+
do_something_unlikely();
74+
}
75+
76+
This pattern works starting with C++20.
77+
See https://en.cppreference.com/w/cpp/language/attributes/likely
78+
79+
Please use OPENTELEMETRY_LIKELY_CONDITION instead for now.
80+
*/
81+
82+
#if !defined(OPENTELEMETRY_LIKELY) && defined(__cplusplus)
83+
// Only use likely with C++20
84+
# if __cplusplus >= 202002L
85+
// GCC 9 has likely attribute but do not support declare it at the beginning of statement
86+
# if defined(__has_cpp_attribute) && (defined(__clang__) || !defined(__GNUC__) || __GNUC__ > 9)
87+
# if __has_cpp_attribute(likely)
88+
# define OPENTELEMETRY_LIKELY [[likely]]
89+
# endif
90+
# endif
91+
# endif
92+
#endif
93+
94+
#ifndef OPENTELEMETRY_LIKELY
95+
# define OPENTELEMETRY_LIKELY
96+
#endif
97+
98+
#if !defined(OPENTELEMETRY_UNLIKELY) && defined(__cplusplus)
99+
// Only use unlikely with C++20
100+
# if __cplusplus >= 202002L
101+
// GCC 9 has unlikely attribute but do not support declare it at the beginning of statement
102+
# if defined(__has_cpp_attribute) && (defined(__clang__) || !defined(__GNUC__) || __GNUC__ > 9)
103+
# if __has_cpp_attribute(unlikely)
104+
# define OPENTELEMETRY_UNLIKELY [[unlikely]]
105+
# endif
106+
# endif
107+
# endif
108+
#endif
109+
110+
#ifndef OPENTELEMETRY_UNLIKELY
111+
# define OPENTELEMETRY_UNLIKELY
22112
#endif
23113

24114
/// \brief Declare variable as maybe unused

api/include/opentelemetry/logs/logger.h

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -258,13 +258,19 @@ class Logger
258258

259259
inline bool Enabled(Severity severity, const EventId &event_id) const noexcept
260260
{
261-
OPENTELEMETRY_LIKELY_IF(Enabled(severity) == false) { return false; }
261+
if OPENTELEMETRY_LIKELY_CONDITION (!Enabled(severity))
262+
{
263+
return false;
264+
}
262265
return EnabledImplementation(severity, event_id);
263266
}
264267

265268
inline bool Enabled(Severity severity, int64_t event_id) const noexcept
266269
{
267-
OPENTELEMETRY_LIKELY_IF(Enabled(severity) == false) { return false; }
270+
if OPENTELEMETRY_LIKELY_CONDITION (!Enabled(severity))
271+
{
272+
return false;
273+
}
268274
return EnabledImplementation(severity, event_id);
269275
}
270276

examples/grpc/CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
get_filename_component(proto_file "./protos/messages.proto" ABSOLUTE)
66
get_filename_component(proto_file_path "${proto_file}" PATH)
77

8-
message("PATH:${proto_file_path}:${proto_file}")
8+
message(STATUS "PATH:${proto_file_path}:${proto_file}")
99
# Generated sources
1010
set(example_proto_srcs "${CMAKE_CURRENT_BINARY_DIR}/messages.pb.cc")
1111
set(example_proto_hdrs "${CMAKE_CURRENT_BINARY_DIR}/messages.pb.h")

exporters/otlp/src/otlp_grpc_client.cc

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -342,7 +342,8 @@ std::unique_ptr<grpc::ClientContext> OtlpGrpcClient::MakeClientContext(
342342

343343
for (auto &header : options.metadata)
344344
{
345-
context->AddMetadata(header.first, header.second);
345+
context->AddMetadata(header.first,
346+
opentelemetry::ext::http::common::UrlDecoder::Decode(header.second));
346347
}
347348

348349
return context;

exporters/otlp/src/otlp_http_client.cc

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -948,7 +948,8 @@ OtlpHttpClient::createSession(
948948

949949
for (auto &header : options_.http_headers)
950950
{
951-
request->AddHeader(header.first, header.second);
951+
request->AddHeader(header.first,
952+
opentelemetry::ext::http::common::UrlDecoder::Decode(header.second));
952953
}
953954
request->SetUri(http_uri_);
954955
request->SetSslOptions(options_.ssl_options);

exporters/otlp/src/otlp_log_recordable.cc

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,15 +18,21 @@ namespace otlp
1818

1919
const opentelemetry::sdk::resource::Resource &OtlpLogRecordable::GetResource() const noexcept
2020
{
21-
OPENTELEMETRY_LIKELY_IF(nullptr != resource_) { return *resource_; }
21+
if OPENTELEMETRY_LIKELY_CONDITION (nullptr != resource_)
22+
{
23+
return *resource_;
24+
}
2225

2326
return opentelemetry::sdk::logs::ReadableLogRecord::GetDefaultResource();
2427
}
2528

2629
const opentelemetry::sdk::instrumentationscope::InstrumentationScope &
2730
OtlpLogRecordable::GetInstrumentationScope() const noexcept
2831
{
29-
OPENTELEMETRY_LIKELY_IF(nullptr != instrumentation_scope_) { return *instrumentation_scope_; }
32+
if OPENTELEMETRY_LIKELY_CONDITION (nullptr != instrumentation_scope_)
33+
{
34+
return *instrumentation_scope_;
35+
}
3036

3137
return opentelemetry::sdk::logs::ReadableLogRecord::GetDefaultInstrumentationScope();
3238
}

ext/include/opentelemetry/ext/http/common/url_parser.h

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,50 @@ class UrlParser
131131
}
132132
};
133133

134+
class UrlDecoder
135+
{
136+
public:
137+
static std::string Decode(const std::string &encoded)
138+
{
139+
std::string result;
140+
result.reserve(encoded.size());
141+
142+
for (size_t pos = 0; pos < encoded.size(); pos++)
143+
{
144+
if (encoded[pos] == '%')
145+
{
146+
147+
// Invalid input: less than two characters left after '%'
148+
if (encoded.size() < pos + 3)
149+
{
150+
return encoded;
151+
}
152+
153+
char hex[3] = {0};
154+
hex[0] = encoded[++pos];
155+
hex[1] = encoded[++pos];
156+
157+
char *endptr;
158+
long value = strtol(hex, &endptr, 16);
159+
160+
// Invalid input: no valid hex characters after '%'
161+
if (endptr != &hex[2])
162+
{
163+
return encoded;
164+
}
165+
166+
result.push_back(static_cast<char>(value));
167+
}
168+
else
169+
{
170+
result.push_back(encoded[pos]);
171+
}
172+
}
173+
174+
return result;
175+
}
176+
};
177+
134178
} // namespace common
135179

136180
} // namespace http

0 commit comments

Comments
 (0)