diff --git a/CHANGELOG.md b/CHANGELOG.md index 59f7d6529b..3ff4a5224f 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -40,6 +40,11 @@ Increment the: * [CONFIGURATION] Cleanup build targets and docs [#4486](https://github.com/open-telemetry/opentelemetry-cpp/pull/4486) +* [BUGFIX] Complete an OTLP HTTP request that its client ends with + `Destroyed`, `ReadError` or `WriteError`, rather than logging the state + and leaving the caller waiting + [#4425](https://github.com/open-telemetry/opentelemetry-cpp/issues/4425) + * [CONFIGURATION] Build the configured resource detectors in SdkBuilder, apply the `detection.attributes` include/exclude filter to the detected attributes, and merge the resource per the resource SDK specification. diff --git a/exporters/otlp/src/otlp_http_client.cc b/exporters/otlp/src/otlp_http_client.cc index 1f2bf98071..ee902e930a 100644 --- a/exporters/otlp/src/otlp_http_client.cc +++ b/exporters/otlp/src/otlp_http_client.cc @@ -186,6 +186,10 @@ class ResponseHandler : public http_client::EventHandler opentelemetry::nostd::string_view reason) noexcept override { // need to modify stopping_ under lock before calling callback + // + // Every state the client can end a request on. A state missing from here leaves the caller + // waiting for a callback that never comes. The exchange below decides ordering, so a state + // arriving after a response does not report a second time. bool need_stop = false; switch (state) { @@ -195,6 +199,9 @@ class ResponseHandler : public http_client::EventHandler case http_client::SessionState::SSLHandshakeFailed: case http_client::SessionState::TimedOut: case http_client::SessionState::NetworkError: + case http_client::SessionState::Destroyed: + case http_client::SessionState::ReadError: + case http_client::SessionState::WriteError: case http_client::SessionState::Cancelled: { need_stop = true; } diff --git a/exporters/otlp/test/otlp_http_exporter_custom_client_test.cc b/exporters/otlp/test/otlp_http_exporter_custom_client_test.cc index 8eee6fcd00..b9c0157508 100644 --- a/exporters/otlp/test/otlp_http_exporter_custom_client_test.cc +++ b/exporters/otlp/test/otlp_http_exporter_custom_client_test.cc @@ -2,8 +2,10 @@ // SPDX-License-Identifier: Apache-2.0 #include +#include #include #include +#include #include #include #include "gmock/gmock.h" @@ -104,6 +106,31 @@ class OtlpHttpExporterCustomClientTestPeer : public ::testing::Test return {new OtlpHttpClient(MakeOtlpHttpClientOptions(), http_client), http_client}; } + // Records the outcome. A request that never settles calls back no times and one that settles + // twice calls back twice, so both failures show in the count rather than only in a timeout. + static void ExportOneRequest(OtlpHttpClient &otlp_client, + const std::shared_ptr> &calls, + const std::shared_ptr &result) + { + auto arena = std::make_unique(); + auto *request = + google::protobuf::Arena::Create( + arena.get()); + auto *response = + google::protobuf::Arena::Create( + arena.get()); + + otlp_client.Export( + *request, std::move(arena), response, + [calls, result](opentelemetry::sdk::common::ExportResult outcome, + google::protobuf::Message *) { + calls->fetch_add(1, std::memory_order_release); + *result = outcome; + return true; + }, + 1); + } + // A non-zero request budget keeps the export asynchronous, so it returns while the session runs. static void ExportOneRequest(OtlpHttpClient &otlp_client) { @@ -248,6 +275,84 @@ TEST_F(OtlpHttpExporterCustomClientTestPeer, ForceFlushReportsSuccessOnceTheSess EXPECT_TRUE(otlp_client.ForceFlush(std::chrono::milliseconds{50})); } +// A client that finishes a transfer on ReadError, WriteError or Destroyed ends the request: the +// result callback runs once, and ForceFlush returns rather than waiting out its deadline. +TEST_F(OtlpHttpExporterCustomClientTestPeer, ATerminalClientEventEndsTheRequest) +{ + for (const auto state : + {http_client::SessionState::ReadError, http_client::SessionState::WriteError, + http_client::SessionState::Destroyed}) + { + SCOPED_TRACE(static_cast(state)); + + auto client = http_client::HttpClientTestFactory::Create(); + auto no_send_client = std::static_pointer_cast(client); + auto session = std::static_pointer_cast(no_send_client->session_); + + std::shared_ptr pending; + EXPECT_CALL(*session, SendRequest) + .WillRepeatedly( + [&pending](std::shared_ptr callback) { + pending = std::move(callback); + }); + + OtlpHttpClient otlp_client(MakeOtlpHttpClientOptions(std::chrono::seconds{30}), client); + + auto calls = std::make_shared>(0); + auto result = std::make_shared(sdk::common::ExportResult::kSuccess); + ExportOneRequest(otlp_client, calls, result); + ASSERT_NE(pending, nullptr); + + pending->OnEvent(state, ""); + + EXPECT_EQ(1, calls->load(std::memory_order_acquire)) + << "the request was not ended by the state the client finished on"; + EXPECT_EQ(sdk::common::ExportResult::kFailure, *result); + + // The deadline is short on purpose. Without the request being ended above this waits it out. + EXPECT_TRUE(otlp_client.ForceFlush(std::chrono::milliseconds{50})); + } +} + +// The other direction. A client is free to report one of those states after it has already +// delivered a response, and the outcome the caller was given must not be replaced or repeated. +TEST_F(OtlpHttpExporterCustomClientTestPeer, ALateTerminalEventDoesNotReportASecondTime) +{ + for (const auto state : + {http_client::SessionState::ReadError, http_client::SessionState::WriteError, + http_client::SessionState::Destroyed}) + { + SCOPED_TRACE(static_cast(state)); + + auto client = http_client::HttpClientTestFactory::Create(); + auto no_send_client = std::static_pointer_cast(client); + auto session = std::static_pointer_cast(no_send_client->session_); + + std::shared_ptr pending; + EXPECT_CALL(*session, SendRequest) + .WillRepeatedly( + [&pending](std::shared_ptr callback) { + pending = std::move(callback); + }); + + OtlpHttpClient otlp_client(MakeOtlpHttpClientOptions(std::chrono::seconds{30}), client); + + auto calls = std::make_shared>(0); + auto result = std::make_shared(sdk::common::ExportResult::kFailure); + ExportOneRequest(otlp_client, calls, result); + ASSERT_NE(pending, nullptr); + + http_client::nosend::Response sent; + sent.Finish(*pending); + ASSERT_EQ(1, calls->load(std::memory_order_acquire)); + + pending->OnEvent(state, ""); + + EXPECT_EQ(1, calls->load(std::memory_order_acquire)) + << "a state arriving after the response reported the request a second time"; + } +} + } // namespace otlp } // namespace exporter OPENTELEMETRY_END_NAMESPACE