Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
7 changes: 7 additions & 0 deletions exporters/otlp/src/otlp_http_client.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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)
{
Expand All @@ -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;
}
Expand Down
105 changes: 105 additions & 0 deletions exporters/otlp/test/otlp_http_exporter_custom_client_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,10 @@
// SPDX-License-Identifier: Apache-2.0

#include <gtest/gtest.h>
#include <atomic>
#include <chrono>
#include <functional>
#include <initializer_list>
#include <string>
#include <utility>
#include "gmock/gmock.h"
Expand Down Expand Up @@ -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<std::atomic<int>> &calls,
const std::shared_ptr<sdk::common::ExportResult> &result)
{
auto arena = std::make_unique<google::protobuf::Arena>();
auto *request =
google::protobuf::Arena::Create<proto::collector::trace::v1::ExportTraceServiceRequest>(
arena.get());
auto *response =
google::protobuf::Arena::Create<proto::collector::trace::v1::ExportTraceServiceResponse>(
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)
{
Expand Down Expand Up @@ -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<int>(state));

auto client = http_client::HttpClientTestFactory::Create();
auto no_send_client = std::static_pointer_cast<http_client::nosend::HttpClient>(client);
auto session = std::static_pointer_cast<http_client::nosend::Session>(no_send_client->session_);

std::shared_ptr<opentelemetry::ext::http::client::EventHandler> pending;
EXPECT_CALL(*session, SendRequest)
.WillRepeatedly(
[&pending](std::shared_ptr<opentelemetry::ext::http::client::EventHandler> callback) {
pending = std::move(callback);
});

OtlpHttpClient otlp_client(MakeOtlpHttpClientOptions(std::chrono::seconds{30}), client);

auto calls = std::make_shared<std::atomic<int>>(0);
auto result = std::make_shared<sdk::common::ExportResult>(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<int>(state));

auto client = http_client::HttpClientTestFactory::Create();
auto no_send_client = std::static_pointer_cast<http_client::nosend::HttpClient>(client);
auto session = std::static_pointer_cast<http_client::nosend::Session>(no_send_client->session_);

std::shared_ptr<opentelemetry::ext::http::client::EventHandler> pending;
EXPECT_CALL(*session, SendRequest)
.WillRepeatedly(
[&pending](std::shared_ptr<opentelemetry::ext::http::client::EventHandler> callback) {
pending = std::move(callback);
});

OtlpHttpClient otlp_client(MakeOtlpHttpClientOptions(std::chrono::seconds{30}), client);

auto calls = std::make_shared<std::atomic<int>>(0);
auto result = std::make_shared<sdk::common::ExportResult>(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
Loading