Skip to content

Commit ee8b00b

Browse files
authored
Merge pull request #723 from open-telemetry/main
merged from upstream
2 parents 5b5ce45 + c5ad161 commit ee8b00b

8 files changed

Lines changed: 86 additions & 27 deletions

File tree

CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,9 @@ Increment the:
5151
* [CODE HEALTH] Fix clang-tidy performance enum size warnings
5252
[#3923](https://github.com/open-telemetry/opentelemetry-cpp/pull/3923)
5353

54+
* [EXPORTER] Allow custom HttpClient in OTLP HTTP
55+
[#3930](https://github.com/open-telemetry/opentelemetry-cpp/pull/3930)
56+
5457
Important changes:
5558

5659
* [BUILD] Revisit EventLogger deprecation

api/include/opentelemetry/nostd/function_ref.h

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,9 @@ class function_ref<R(Args...)>
3737
void BindTo(F &f) noexcept
3838
{
3939
callable_ = static_cast<void *>(std::addressof(f));
40-
invoker_ = [](void *callable, Args... args) -> R {
40+
// Args... matches the referenced signature and is forwarded as such.
41+
// NOLINTNEXTLINE(performance-unnecessary-value-param)
42+
invoker_ = [](void *callable, Args... args) -> R {
4143
return (*static_cast<FunctionPointer<F>>(callable))(std::forward<Args>(args)...);
4244
};
4345
}
@@ -78,14 +80,21 @@ class function_ref<R(Args...)>
7880
int>::type = 0>
7981
function_ref(F &&f)
8082
{
83+
// Binding by named variable here intentionally keeps function_ref non-owning.
84+
// NOLINTNEXTLINE(cppcoreguidelines-missing-std-forward)
8185
BindTo(f); // not forward
8286
}
8387

8488
function_ref(std::nullptr_t) {}
8589

86-
function_ref(const function_ref &) noexcept = default;
87-
function_ref(function_ref &&) noexcept = default;
90+
~function_ref() = default;
91+
function_ref(const function_ref &) noexcept = default;
92+
function_ref(function_ref &&) noexcept = default;
93+
function_ref &operator=(const function_ref &) noexcept = default;
94+
function_ref &operator=(function_ref &&) noexcept = default;
8895

96+
// Args... is part of the function signature and forwarded to invoker_.
97+
// NOLINTNEXTLINE(performance-unnecessary-value-param)
8998
R operator()(Args... args) const { return invoker_(callable_, std::forward<Args>(args)...); }
9099

91100
explicit operator bool() const { return invoker_; }

api/include/opentelemetry/nostd/shared_ptr.h

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,11 @@ class shared_ptr
4848

4949
shared_ptr_wrapper(std::shared_ptr<T> &&ptr) noexcept : ptr_{std::move(ptr)} {}
5050

51+
shared_ptr_wrapper(const shared_ptr_wrapper &) = default;
52+
shared_ptr_wrapper &operator=(const shared_ptr_wrapper &) = default;
53+
shared_ptr_wrapper(shared_ptr_wrapper &&) = default;
54+
shared_ptr_wrapper &operator=(shared_ptr_wrapper &&) = default;
55+
5156
virtual ~shared_ptr_wrapper() {}
5257

5358
virtual void CopyTo(PlacementBuffer &buffer) const noexcept
@@ -99,20 +104,20 @@ class shared_ptr
99104
typename std::enable_if<std::is_convertible<U *, pointer>::value>::type * = nullptr>
100105
shared_ptr(shared_ptr<U> &&other) noexcept
101106
{
102-
other.wrapper().template MoveTo<T>(buffer_);
107+
std::move(other).wrapper().template MoveTo<T>(buffer_);
103108
}
104109

105110
shared_ptr(const shared_ptr &other) noexcept { other.wrapper().CopyTo(buffer_); }
106111

107112
shared_ptr(unique_ptr<T> &&other) noexcept
108113
{
109-
std::shared_ptr<T> ptr_(other.release());
114+
std::shared_ptr<T> ptr_(std::move(other).release());
110115
new (buffer_.data) shared_ptr_wrapper{std::move(ptr_)};
111116
}
112117

113118
shared_ptr(std::unique_ptr<T> &&other) noexcept
114119
{
115-
std::shared_ptr<T> ptr_(other.release());
120+
std::shared_ptr<T> ptr_(std::move(other).release());
116121
new (buffer_.data) shared_ptr_wrapper{std::move(ptr_)};
117122
}
118123

@@ -156,10 +161,15 @@ class shared_ptr
156161

157162
void swap(shared_ptr<T> &other) noexcept
158163
{
159-
shared_ptr<T> tmp{std::move(other)};
164+
if (this == &other)
165+
{
166+
return;
167+
}
160168

161-
wrapper().MoveTo(other.buffer_);
162-
tmp.wrapper().MoveTo(buffer_);
169+
// Swap the live wrapper objects (object-level swap), not the raw
170+
// PlacementBuffer bytes. This preserves object lifetime correctness and
171+
// avoids moving `other` as an object.
172+
std::swap(wrapper(), other.wrapper());
163173
}
164174

165175
template <typename U>

api/include/opentelemetry/nostd/span.h

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -166,8 +166,14 @@ class span
166166

167167
span(const span &) noexcept = default;
168168

169+
~span() noexcept = default;
170+
171+
span(span &&) noexcept = default;
172+
169173
span &operator=(const span &) noexcept = default;
170174

175+
span &operator=(span &&) noexcept = default;
176+
171177
bool empty() const noexcept { return Extent == 0; }
172178

173179
T *data() const noexcept { return data_; }
@@ -248,8 +254,14 @@ class span<T, dynamic_extent>
248254

249255
span(const span &) noexcept = default;
250256

257+
~span() noexcept = default;
258+
259+
span(span &&) noexcept = default;
260+
251261
span &operator=(const span &) noexcept = default;
252262

263+
span &operator=(span &&) noexcept = default;
264+
253265
bool empty() const noexcept { return extent_ == 0; }
254266

255267
T *data() const noexcept { return data_; }

api/include/opentelemetry/nostd/unique_ptr.h

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -56,14 +56,17 @@ class unique_ptr
5656

5757
unique_ptr(unique_ptr &&other) noexcept : ptr_{other.release()} {}
5858

59+
unique_ptr(const unique_ptr &) = delete;
60+
unique_ptr &operator=(const unique_ptr &) = delete;
61+
5962
template <class U,
6063
typename std::enable_if<std::is_convertible<U *, pointer>::value>::type * = nullptr>
61-
unique_ptr(unique_ptr<U> &&other) noexcept : ptr_{other.release()}
64+
unique_ptr(unique_ptr<U> &&other) noexcept : ptr_{std::move(other).release()}
6265
{}
6366

6467
template <class U,
6568
typename std::enable_if<std::is_convertible<U *, pointer>::value>::type * = nullptr>
66-
unique_ptr(std::unique_ptr<U> &&other) noexcept : ptr_{other.release()}
69+
unique_ptr(std::unique_ptr<U> &&other) noexcept : ptr_{std::move(other).release()}
6770
{}
6871

6972
~unique_ptr() { reset(); }
@@ -84,15 +87,15 @@ class unique_ptr
8487
typename std::enable_if<std::is_convertible<U *, pointer>::value>::type * = nullptr>
8588
unique_ptr &operator=(unique_ptr<U> &&other) noexcept
8689
{
87-
reset(other.release());
90+
reset(std::move(other).release());
8891
return *this;
8992
}
9093

9194
template <class U,
9295
typename std::enable_if<std::is_convertible<U *, pointer>::value>::type * = nullptr>
9396
unique_ptr &operator=(std::unique_ptr<U> &&other) noexcept
9497
{
95-
reset(other.release());
98+
reset(std::move(other).release());
9699
return *this;
97100
}
98101

api/include/opentelemetry/nostd/variant.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,8 @@ OPENTELEMETRY_END_NAMESPACE
5151

5252
# include "opentelemetry/nostd/internal/absl/base/options.h"
5353

54+
// Forward declarations needed by the local Abseil snapshot bridge.
55+
// NOLINTBEGIN(abseil-no-namespace)
5456
namespace absl
5557
{
5658
namespace OTABSL_OPTION_NAMESPACE_NAME
@@ -61,6 +63,7 @@ template <typename... Ts>
6163
class variant;
6264
} // namespace OTABSL_OPTION_NAMESPACE_NAME
6365
} // namespace absl
66+
// NOLINTEND(abseil-no-namespace)
6467

6568
# include "opentelemetry/nostd/internal/absl/types/variant.h"
6669

api/test/nostd/shared_ptr_test.cc

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -166,6 +166,31 @@ TEST(SharedPtrTest, Swap)
166166
EXPECT_EQ(ptr2.get(), value1);
167167
}
168168

169+
TEST(SharedPtrTest, SwapSelfNoOp)
170+
{
171+
struct TestStruct
172+
{
173+
explicit TestStruct(int &destruct_count) noexcept : destruct_count_{&destruct_count} {}
174+
175+
~TestStruct() { ++(*destruct_count_); }
176+
177+
int *destruct_count_;
178+
};
179+
180+
int destruct_count{0};
181+
182+
{
183+
shared_ptr<TestStruct> ptr{std::make_shared<TestStruct>(destruct_count)};
184+
auto *ptr_before = ptr.get();
185+
186+
ptr.swap(ptr);
187+
188+
EXPECT_EQ(ptr.get(), ptr_before);
189+
}
190+
191+
EXPECT_EQ(destruct_count, 1);
192+
}
193+
169194
TEST(SharedPtrTest, Comparison)
170195
{
171196
shared_ptr<int> ptr1{new int{123}};

exporters/otlp/include/opentelemetry/exporters/otlp/otlp_http_client.h

Lines changed: 8 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -158,6 +158,14 @@ class OPENTELEMETRY_EXPORT_TYPE OtlpHttpClient
158158
*/
159159
explicit OtlpHttpClient(OtlpHttpClientOptions &&options);
160160

161+
/**
162+
* Create an OtlpHttpClient using the specified http client.
163+
* @param options the Otlp http client options to be used for exporting
164+
* @param http_client the http client to be used for exporting
165+
*/
166+
OtlpHttpClient(OtlpHttpClientOptions &&options,
167+
std::shared_ptr<ext::http::client::HttpClient> http_client);
168+
161169
~OtlpHttpClient();
162170
OtlpHttpClient(const OtlpHttpClient &) = delete;
163171
OtlpHttpClient &operator=(const OtlpHttpClient &) = delete;
@@ -266,20 +274,6 @@ class OPENTELEMETRY_EXPORT_TYPE OtlpHttpClient
266274
*/
267275
bool cleanupGCSessions() noexcept;
268276

269-
// For testing
270-
friend class OtlpHttpExporterTestPeer;
271-
friend class OtlpHttpLogRecordExporterTestPeer;
272-
friend class OtlpHttpMetricExporterTestPeer;
273-
274-
/**
275-
* Create an OtlpHttpClient using the specified http client.
276-
* Only tests can call this constructor directly.
277-
* @param options the Otlp http client options to be used for exporting
278-
* @param http_client the http client to be used for exporting
279-
*/
280-
OtlpHttpClient(OtlpHttpClientOptions &&options,
281-
std::shared_ptr<ext::http::client::HttpClient> http_client);
282-
283277
// Stores if this HTTP client had its Shutdown() method called
284278
std::atomic<bool> is_shutdown_;
285279

0 commit comments

Comments
 (0)