Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

impl(otel): fewer OTel spans on HTTP requests #14406

Merged
merged 1 commit into from
Jul 1, 2024
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
22 changes: 16 additions & 6 deletions google/cloud/internal/tracing_http_payload.cc
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

#include "google/cloud/internal/tracing_http_payload.h"
#include "google/cloud/internal/opentelemetry.h"
#include <chrono>

namespace google {
namespace cloud {
Expand All @@ -33,16 +34,25 @@ bool TracingHttpPayload::HasUnreadData() const {

StatusOr<std::size_t> TracingHttpPayload::Read(absl::Span<char> buffer) {
auto scope = opentelemetry::trace::Scope(span_);
auto span = internal::MakeSpan("Read");
span->SetAttribute("read.buffer.size",
static_cast<std::int64_t>(buffer.size()));
auto const start = std::chrono::system_clock::now();
auto status = impl_->Read(buffer);
auto const latency = std::chrono::duration_cast<std::chrono::microseconds>(
std::chrono::system_clock::now() - start)
.count();
if (!status) {
internal::EndSpan(*span, status.status());
auto const code = StatusCodeToString(status.status().code());
span_->AddEvent(
"gl-cpp.read", opentelemetry::common::SystemTimestamp(start),
{{"read.status.code", code},
{"read.buffer.size", static_cast<std::int64_t>(buffer.size())},
{"read.latency.us", static_cast<std::int64_t>(latency)}});
return internal::EndSpan(*span_, std::move(status));
}
span->SetAttribute("read.returned.size", static_cast<std::int64_t>(*status));
internal::EndSpan(*span, status.status());
span_->AddEvent(
"gl-cpp.read", opentelemetry::common::SystemTimestamp(start),
{{"read.buffer.size", static_cast<std::int64_t>(buffer.size())},
{"read.returned.size", static_cast<std::int64_t>(*status)},
{"read.latency.us", static_cast<std::int64_t>(latency)}});
if (*status != 0) return status;
return internal::EndSpan(*span_, std::move(status));
}
Expand Down
88 changes: 42 additions & 46 deletions google/cloud/internal/tracing_http_payload_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -28,15 +28,18 @@ namespace rest_internal {
GOOGLE_CLOUD_CPP_INLINE_NAMESPACE_BEGIN
namespace {

using ::google::cloud::testing_util::EventNamed;
using ::google::cloud::testing_util::InstallSpanCatcher;
using ::google::cloud::testing_util::MakeMockHttpPayloadSuccess;
using ::google::cloud::testing_util::MockHttpPayload;
using ::google::cloud::testing_util::OTelAttribute;
using ::google::cloud::testing_util::SpanEventAttributesAre;
using ::google::cloud::testing_util::SpanHasAttributes;
using ::google::cloud::testing_util::SpanHasInstrumentationScope;
using ::google::cloud::testing_util::SpanKindIsClient;
using ::google::cloud::testing_util::SpanNamed;
using ::google::cloud::testing_util::StatusIs;
using ::testing::_;
using ::testing::AllOf;
using ::testing::Return;
using ::testing::UnorderedElementsAre;
Expand All @@ -45,6 +48,23 @@ std::string MockContents() {
return "The quick brown fox jumps over the lazy dog";
}

auto MakeReadMatcher(std::int64_t buffer_size, std::int64_t read_size) {
return AllOf(EventNamed("gl-cpp.read"),
SpanEventAttributesAre(
OTelAttribute<std::int64_t>("read.buffer.size", buffer_size),
OTelAttribute<std::int64_t>("read.returned.size", read_size),
OTelAttribute<std::int64_t>("read.latency.us", _)));
}

auto MakeReadMatcher(std::int64_t buffer_size) {
return AllOf(
EventNamed("gl-cpp.read"),
SpanEventAttributesAre(
OTelAttribute<std::string>("read.status.code", "UNAVAILABLE"),
OTelAttribute<std::int64_t>("read.buffer.size", buffer_size),
OTelAttribute<std::int64_t>("read.latency.us", _)));
}

TEST(TracingHttpPayload, Success) {
namespace sc = ::opentelemetry::trace::SemanticConventions;
auto span_catcher = InstallSpanCatcher();
Expand All @@ -59,23 +79,16 @@ TEST(TracingHttpPayload, Success) {
for (auto s = read(); s && s.value() != 0; s = read()) continue;

auto spans = span_catcher->GetSpans();
auto make_read_matcher = [](auto bs, auto rs) {
return AllOf(SpanNamed("Read"), SpanHasInstrumentationScope(),
SpanKindIsClient(),
SpanHasAttributes(
OTelAttribute<std::int32_t>("gl-cpp.status_code", 0),
OTelAttribute<std::int64_t>("read.buffer.size", bs),
OTelAttribute<std::int64_t>("read.returned.size", rs)));
};
EXPECT_THAT(spans,
UnorderedElementsAre(
AllOf(SpanNamed("HTTP/GET"), SpanHasInstrumentationScope(),
SpanKindIsClient(),
SpanHasAttributes(OTelAttribute<std::string>(
/*sc::kNetworkTransport=*/"network.transport",
sc::NetTransportValues::kIpTcp))),
make_read_matcher(16, 16), make_read_matcher(16, 16),
make_read_matcher(16, 11), make_read_matcher(16, 0)));
EXPECT_THAT(
spans,
UnorderedElementsAre(AllOf(
SpanNamed("HTTP/GET"), SpanHasInstrumentationScope(),
SpanKindIsClient(),
SpanHasAttributes(OTelAttribute<std::string>(
/*sc::kNetworkTransport=*/"network.transport",
sc::NetTransportValues::kIpTcp)),
SpanHasEvents(MakeReadMatcher(16, 16), MakeReadMatcher(16, 16),
MakeReadMatcher(16, 11), MakeReadMatcher(16, 0)))));
}

TEST(TracingHttpPayload, Failure) {
Expand All @@ -99,35 +112,18 @@ TEST(TracingHttpPayload, Failure) {
EXPECT_THAT(status, StatusIs(StatusCode::kUnavailable));

auto spans = span_catcher->GetSpans();
auto make_read_success_matcher = [](auto bs, auto rs) {
return AllOf(SpanNamed("Read"), SpanHasInstrumentationScope(),
SpanKindIsClient(),
SpanHasAttributes(
OTelAttribute<std::int32_t>("gl-cpp.status_code", 0),
OTelAttribute<std::int64_t>("read.buffer.size", bs),
OTelAttribute<std::int64_t>("read.returned.size", rs)));
};
auto make_read_error_matcher = [](auto bs, StatusCode code) {
return AllOf(SpanNamed("Read"), SpanHasInstrumentationScope(),
SpanKindIsClient(),
SpanHasAttributes(
OTelAttribute<std::int32_t>(
"gl-cpp.status_code", static_cast<std::int32_t>(code)),
OTelAttribute<std::int64_t>("read.buffer.size", bs)));
};
EXPECT_THAT(spans,
UnorderedElementsAre(
AllOf(SpanNamed("HTTP/GET"), SpanHasInstrumentationScope(),
SpanKindIsClient(),
SpanHasAttributes(
OTelAttribute<std::string>(
/*sc::kNetworkTransport=*/"network.transport",
sc::NetTransportValues::kIpTcp),
OTelAttribute<int>(
"gl-cpp.status_code",
static_cast<int>(StatusCode::kUnavailable)))),
make_read_success_matcher(16, 16),
make_read_error_matcher(16, StatusCode::kUnavailable)));
EXPECT_THAT(
spans,
UnorderedElementsAre(AllOf(
SpanNamed("HTTP/GET"), SpanHasInstrumentationScope(),
SpanKindIsClient(),
SpanHasAttributes(
OTelAttribute<std::string>(
/*sc::kNetworkTransport=*/"network.transport",
sc::NetTransportValues::kIpTcp),
OTelAttribute<int>("gl-cpp.status_code",
static_cast<int>(StatusCode::kUnavailable))),
SpanHasEvents(MakeReadMatcher(16, 16), MakeReadMatcher(16)))));
}

} // namespace
Expand Down
38 changes: 20 additions & 18 deletions google/cloud/internal/tracing_rest_client_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -117,8 +117,10 @@ TEST(TracingRestClient, Delete) {
OTelAttribute<std::string>(
"http.response.header.x-test-header-1", "value1"),
OTelAttribute<std::string>(
"http.response.header.x-test-header-2", "value2"))),
SpanNamed("SendRequest"), SpanNamed("Read"), SpanNamed("Read")));
"http.response.header.x-test-header-2", "value2")),
SpanHasEvents(EventNamed("gl-cpp.read"),
EventNamed("gl-cpp.read"))),
SpanNamed("SendRequest")));
}

TEST(TracingRestClient, HasScope) {
Expand Down Expand Up @@ -154,14 +156,15 @@ TEST(TracingRestClient, HasScope) {
EXPECT_THAT(contents, IsOkAndHolds(MockContents()));

auto spans = span_catcher->GetSpans();
EXPECT_THAT(
spans,
UnorderedElementsAre(
AllOf(SpanNamed("HTTP/GET"), SpanHasInstrumentationScope(),
SpanKindIsClient(),
SpanHasAttributes(OTelAttribute<std::string>("test.attribute",
"test.value"))),
SpanNamed("SendRequest"), SpanNamed("Read"), SpanNamed("Read")));
EXPECT_THAT(spans,
UnorderedElementsAre(
AllOf(SpanNamed("HTTP/GET"), SpanHasInstrumentationScope(),
SpanKindIsClient(),
SpanHasAttributes(OTelAttribute<std::string>(
"test.attribute", "test.value")),
SpanHasEvents(EventNamed("gl-cpp.read"),
EventNamed("gl-cpp.read"))),
SpanNamed("SendRequest")));
}

TEST(TracingRestClient, PropagatesTraceContext) {
Expand Down Expand Up @@ -201,9 +204,8 @@ TEST(TracingRestClient, PropagatesTraceContext) {
EXPECT_THAT(contents, IsOkAndHolds(MockContents()));

auto spans = span_catcher->GetSpans();
EXPECT_THAT(spans, UnorderedElementsAre(
SpanNamed("HTTP/PATCH"), SpanNamed("SendRequest"),
SpanNamed("Read"), SpanNamed("Read")));
EXPECT_THAT(spans, UnorderedElementsAre(SpanNamed("HTTP/PATCH"),
SpanNamed("SendRequest")));
}

TEST(TracingRestClient, WithRestContextDetails) {
Expand Down Expand Up @@ -265,14 +267,15 @@ TEST(TracingRestClient, WithRestContextDetails) {
OTelAttribute<std::string>(
/*sc::kClientAddress=*/"client.address", "127.0.0.1"),
OTelAttribute<std::int32_t>(/*sc::kClientPort=*/"client.port",
32000))),
32000)),
SpanHasEvents(EventNamed("gl-cpp.read"),
EventNamed("gl-cpp.read"))),
AllOf(SpanNamed("SendRequest"),
SpanHasAttributes(
OTelAttribute<bool>("gl-cpp.cached_connection", false)),
SpanHasEvents(EventNamed("gl-cpp.curl.namelookup"),
EventNamed("gl-cpp.curl.connected"),
EventNamed("gl-cpp.curl.ssl.handshake"))),
SpanNamed("Read"), SpanNamed("Read")));
EventNamed("gl-cpp.curl.ssl.handshake")))));
}

TEST(TracingRestClient, CachedConnection) {
Expand Down Expand Up @@ -312,8 +315,7 @@ TEST(TracingRestClient, CachedConnection) {
AllOf(SpanNamed("SendRequest"),
SpanHasAttributes(OTelAttribute<bool>(
"gl-cpp.cached_connection", true)),
SpanHasEvents(EventNamed("gl-cpp.curl.connected"))),
SpanNamed("Read"), SpanNamed("Read")));
SpanHasEvents(EventNamed("gl-cpp.curl.connected")))));
}

#else
Expand Down
37 changes: 20 additions & 17 deletions google/cloud/internal/tracing_rest_response_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -29,14 +29,17 @@ namespace rest_internal {
GOOGLE_CLOUD_CPP_INLINE_NAMESPACE_BEGIN
namespace {

using ::google::cloud::testing_util::EventNamed;
using ::google::cloud::testing_util::InstallSpanCatcher;
using ::google::cloud::testing_util::MakeMockHttpPayloadSuccess;
using ::google::cloud::testing_util::MockRestResponse;
using ::google::cloud::testing_util::OTelAttribute;
using ::google::cloud::testing_util::SpanEventAttributesAre;
using ::google::cloud::testing_util::SpanHasAttributes;
using ::google::cloud::testing_util::SpanHasInstrumentationScope;
using ::google::cloud::testing_util::SpanKindIsClient;
using ::google::cloud::testing_util::SpanNamed;
using ::testing::_;
using ::testing::AllOf;
using ::testing::Return;
using ::testing::UnorderedElementsAre;
Expand All @@ -45,6 +48,14 @@ std::string MockContents() {
return "The quick brown fox jumps over the lazy dog";
}

auto MakeReadMatcher(std::int64_t buffer_size, std::int64_t read_size) {
return AllOf(EventNamed("gl-cpp.read"),
SpanEventAttributesAre(
OTelAttribute<std::int64_t>("read.buffer.size", buffer_size),
OTelAttribute<std::int64_t>("read.returned.size", read_size),
OTelAttribute<std::int64_t>("read.latency.us", _)));
}

TEST(TracingRestResponseTest, Success) {
namespace sc = ::opentelemetry::trace::SemanticConventions;
auto span_catcher = InstallSpanCatcher();
Expand All @@ -71,24 +82,16 @@ TEST(TracingRestResponseTest, Success) {
EXPECT_EQ(*status, 0);

auto spans = span_catcher->GetSpans();
auto make_read_event_matcher = [](std::int64_t bs, std::int64_t rs) {
return AllOf(SpanNamed("Read"), SpanHasInstrumentationScope(),
SpanKindIsClient(),
SpanHasAttributes(
OTelAttribute<std::int32_t>("gl-cpp.status_code", 0),
OTelAttribute<std::int64_t>("read.buffer.size", bs),
OTelAttribute<std::int64_t>("read.returned.size", rs)));
};
auto const content_size = static_cast<std::int64_t>(MockContents().size());
EXPECT_THAT(spans,
UnorderedElementsAre(
AllOf(SpanNamed("HTTP/GET"), SpanHasInstrumentationScope(),
SpanKindIsClient(),
SpanHasAttributes(OTelAttribute<std::string>(
/*sc::kNetworkTransport=*/"network.transport",
sc::NetTransportValues::kIpTcp))),
make_read_event_matcher(kBufferSize, content_size),
make_read_event_matcher(kBufferSize, 0)));
EXPECT_THAT(
spans, UnorderedElementsAre(
AllOf(SpanNamed("HTTP/GET"), SpanHasInstrumentationScope(),
SpanKindIsClient(),
SpanHasAttributes(OTelAttribute<std::string>(
/*sc::kNetworkTransport=*/"network.transport",
sc::NetTransportValues::kIpTcp)),
SpanHasEvents(MakeReadMatcher(kBufferSize, content_size),
MakeReadMatcher(kBufferSize, 0)))));
}

} // namespace
Expand Down
Loading