Skip to content

Commit 5e754a9

Browse files
committed
[BUGFIX] Complete an OTLP HTTP request on every state its client can end on
The switch that decides whether a request is over named seven states. The client can end a transfer on three more: Destroyed, ReadError and WriteError. The switch below it recognises all three and logs them, so they arrive, are described, and are then dropped. A request ended that way never reaches Unbind, so its result callback never runs and its session is never released. ForceFlush and Shutdown have nothing to wait on but their own deadlines, and the caller is left waiting for a callback that is not coming. The bundled curl client does not currently produce those three states, so this is reached through the installed HttpClient interface by a client the user supplies. That interface is a supported extension point and the states are part of it, which is why the exporter has to answer them rather than assume which ones a client uses. Ordering is unchanged. stopping_ is a compare-exchange and the first writer wins, so one of these arriving after a response has already been reported still reports nothing. Signed-off-by: thc1006 <84045975+thc1006@users.noreply.github.com>
1 parent 57253cd commit 5e754a9

3 files changed

Lines changed: 124 additions & 0 deletions

File tree

‎CHANGELOG.md‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,11 @@ Increment the:
1515

1616
## [Unreleased]
1717

18+
* [BUGFIX] Complete an OTLP HTTP request that its client ends with
19+
`Destroyed`, `ReadError` or `WriteError`, rather than logging the state
20+
and leaving the caller waiting
21+
[#4425](https://github.com/open-telemetry/opentelemetry-cpp/issues/4425)
22+
1823
* [CONFIGURATION] Build the configured resource detectors in SdkBuilder, apply
1924
the `detection.attributes` include/exclude filter to the detected attributes,
2025
and merge the resource per the resource SDK specification.

‎exporters/otlp/src/otlp_http_client.cc‎

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -186,6 +186,12 @@ class ResponseHandler : public http_client::EventHandler
186186
opentelemetry::nostd::string_view reason) noexcept override
187187
{
188188
// need to modify stopping_ under lock before calling callback
189+
//
190+
// Every state a request can end on, because a request that ends on one nobody answers leaves
191+
// the caller waiting for a callback that is not coming. Destroyed, ReadError and WriteError
192+
// are the three the client can finish on that used to be logged and then dropped. Ordering is
193+
// decided by the exchange below rather than here, so one of these arriving after a response
194+
// has already been reported does not report a second time.
189195
bool need_stop = false;
190196
switch (state)
191197
{
@@ -195,6 +201,9 @@ class ResponseHandler : public http_client::EventHandler
195201
case http_client::SessionState::SSLHandshakeFailed:
196202
case http_client::SessionState::TimedOut:
197203
case http_client::SessionState::NetworkError:
204+
case http_client::SessionState::Destroyed:
205+
case http_client::SessionState::ReadError:
206+
case http_client::SessionState::WriteError:
198207
case http_client::SessionState::Cancelled: {
199208
need_stop = true;
200209
}

‎exporters/otlp/test/otlp_http_exporter_custom_client_test.cc‎

Lines changed: 110 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33

44
#ifndef OPENTELEMETRY_STL_VERSION
55

6+
# include <atomic>
67
# include <chrono>
78
# include <memory>
89
# include <utility>
@@ -91,6 +92,32 @@ class OtlpHttpExporterCustomClientTestPeer : public ::testing::Test
9192
return {new OtlpHttpClient(MakeOtlpHttpClientOptions(), http_client), http_client};
9293
}
9394

95+
// The same, with the outcome recorded. A request that never settles calls this no times, and one
96+
// that settles twice calls it twice, so both failures the terminal states can produce are
97+
// visible in the count rather than only in a timeout.
98+
static void ExportOneRequest(OtlpHttpClient &otlp_client,
99+
std::shared_ptr<std::atomic<int>> calls,
100+
std::shared_ptr<sdk::common::ExportResult> result)
101+
{
102+
auto arena = std::make_unique<google::protobuf::Arena>();
103+
auto *request =
104+
google::protobuf::Arena::Create<proto::collector::trace::v1::ExportTraceServiceRequest>(
105+
arena.get());
106+
auto *response =
107+
google::protobuf::Arena::Create<proto::collector::trace::v1::ExportTraceServiceResponse>(
108+
arena.get());
109+
110+
otlp_client.Export(
111+
*request, std::move(arena), response,
112+
[calls, result](opentelemetry::sdk::common::ExportResult outcome,
113+
google::protobuf::Message *) {
114+
calls->fetch_add(1, std::memory_order_release);
115+
*result = outcome;
116+
return true;
117+
},
118+
1);
119+
}
120+
94121
// A non-zero request budget keeps the export asynchronous, so it returns while the session runs.
95122
static void ExportOneRequest(OtlpHttpClient &otlp_client)
96123
{
@@ -235,6 +262,89 @@ TEST_F(OtlpHttpExporterCustomClientTestPeer, ForceFlushReportsSuccessOnceTheSess
235262
EXPECT_TRUE(otlp_client.ForceFlush(std::chrono::milliseconds{50}));
236263
}
237264

265+
// Every state the client can finish a request on has to end that request. These three used to be
266+
// logged and then dropped: the switch that decides whether the request is over did not name them,
267+
// so a client that ended a transfer this way left the caller waiting for a callback that was not
268+
// coming, and ForceFlush and Shutdown could only give up on their own deadlines.
269+
class OtlpHttpTerminalStateTest : public OtlpHttpExporterCustomClientTestPeer,
270+
public ::testing::WithParamInterface<http_client::SessionState>
271+
{};
272+
273+
TEST_P(OtlpHttpTerminalStateTest, ATerminalClientEventEndsTheRequest)
274+
{
275+
auto client = http_client::HttpClientTestFactory::Create();
276+
auto no_send_client = std::static_pointer_cast<http_client::nosend::HttpClient>(client);
277+
auto session = std::static_pointer_cast<http_client::nosend::Session>(no_send_client->session_);
278+
279+
std::shared_ptr<opentelemetry::ext::http::client::EventHandler> pending;
280+
EXPECT_CALL(*session, SendRequest)
281+
.WillRepeatedly(
282+
[&pending](std::shared_ptr<opentelemetry::ext::http::client::EventHandler> callback) {
283+
pending = std::move(callback);
284+
});
285+
286+
OtlpHttpClient otlp_client(MakeOtlpHttpClientOptions(std::chrono::seconds{30}), client);
287+
288+
auto calls = std::make_shared<std::atomic<int>>(0);
289+
auto result = std::make_shared<sdk::common::ExportResult>(sdk::common::ExportResult::kSuccess);
290+
ExportOneRequest(otlp_client, calls, result);
291+
ASSERT_NE(pending, nullptr);
292+
293+
pending->OnEvent(GetParam(), "");
294+
295+
EXPECT_EQ(1, calls->load(std::memory_order_acquire))
296+
<< "the request was not ended by the state the client finished on";
297+
EXPECT_EQ(sdk::common::ExportResult::kFailure, *result);
298+
299+
// The deadline is short on purpose. Without the request being ended above this waits it out.
300+
EXPECT_TRUE(otlp_client.ForceFlush(std::chrono::milliseconds{50}));
301+
}
302+
303+
INSTANTIATE_TEST_SUITE_P(TerminalStates,
304+
OtlpHttpTerminalStateTest,
305+
::testing::Values(http_client::SessionState::ReadError,
306+
http_client::SessionState::WriteError,
307+
http_client::SessionState::Destroyed));
308+
309+
// The other direction. A client is free to report one of those states after it has already
310+
// delivered a response, and the outcome the caller was given must not be replaced or repeated.
311+
TEST_F(OtlpHttpExporterCustomClientTestPeer, ALateTerminalEventDoesNotReportASecondTime)
312+
{
313+
for (const auto state :
314+
{http_client::SessionState::ReadError, http_client::SessionState::WriteError,
315+
http_client::SessionState::Destroyed})
316+
{
317+
SCOPED_TRACE(static_cast<int>(state));
318+
319+
auto client = http_client::HttpClientTestFactory::Create();
320+
auto no_send_client = std::static_pointer_cast<http_client::nosend::HttpClient>(client);
321+
auto session = std::static_pointer_cast<http_client::nosend::Session>(no_send_client->session_);
322+
323+
std::shared_ptr<opentelemetry::ext::http::client::EventHandler> pending;
324+
EXPECT_CALL(*session, SendRequest)
325+
.WillRepeatedly(
326+
[&pending](std::shared_ptr<opentelemetry::ext::http::client::EventHandler> callback) {
327+
pending = std::move(callback);
328+
});
329+
330+
OtlpHttpClient otlp_client(MakeOtlpHttpClientOptions(std::chrono::seconds{30}), client);
331+
332+
auto calls = std::make_shared<std::atomic<int>>(0);
333+
auto result = std::make_shared<sdk::common::ExportResult>(sdk::common::ExportResult::kFailure);
334+
ExportOneRequest(otlp_client, calls, result);
335+
ASSERT_NE(pending, nullptr);
336+
337+
http_client::nosend::Response sent;
338+
sent.Finish(*pending);
339+
ASSERT_EQ(1, calls->load(std::memory_order_acquire));
340+
341+
pending->OnEvent(state, "");
342+
343+
EXPECT_EQ(1, calls->load(std::memory_order_acquire))
344+
<< "a state arriving after the response reported the request a second time";
345+
}
346+
}
347+
238348
} // namespace otlp
239349
} // namespace exporter
240350
OPENTELEMETRY_END_NAMESPACE

0 commit comments

Comments
 (0)