Skip to content

Commit

Permalink
fix(otel): async read stream tracing (#14393)
Browse files Browse the repository at this point in the history
  • Loading branch information
dbolduc authored Jun 28, 2024
1 parent c883817 commit 748b395
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 7 deletions.
12 changes: 10 additions & 2 deletions google/cloud/internal/async_streaming_read_rpc_tracing.h
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,11 @@ class AsyncStreamingReadRpcTracing : public AsyncStreamingReadRpc<Response> {
}

future<bool> Start() override {
auto start_span = internal::MakeSpan("Start");
// It is sufficient to set `span_` as the parent of `start_span`, because
// the lower levels do not create any spans.
opentelemetry::trace::StartSpanOptions options;
options.parent = span_->GetContext();
auto start_span = internal::MakeSpan("Start", options);
return impl_->Start().then(
[this, ss = std::move(start_span)](future<bool> f) {
EndSpan(*ss);
Expand All @@ -66,7 +70,11 @@ class AsyncStreamingReadRpcTracing : public AsyncStreamingReadRpc<Response> {
}

future<Status> Finish() override {
auto finish_span = internal::MakeSpan("Finish");
// It is sufficient to set `span_` as the parent of `finish_span`, because
// the lower levels do not create any spans.
opentelemetry::trace::StartSpanOptions options;
options.parent = span_->GetContext();
auto finish_span = internal::MakeSpan("Finish", options);
return impl_->Finish().then(
[this, fs = std::move(finish_span)](future<Status> f) {
EndSpan(*fs);
Expand Down
11 changes: 6 additions & 5 deletions google/cloud/internal/async_streaming_read_rpc_tracing_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ using ::google::cloud::testing_util::SetServerMetadata;
using ::google::cloud::testing_util::SpanEventAttributesAre;
using ::google::cloud::testing_util::SpanHasAttributes;
using ::google::cloud::testing_util::SpanNamed;
using ::google::cloud::testing_util::SpanWithParent;
using ::google::cloud::testing_util::SpanWithStatus;
using ::google::cloud::testing_util::StatusIs;
using ::testing::_;
Expand Down Expand Up @@ -79,7 +80,7 @@ TEST(AsyncStreamingReadRpcTracing, Cancel) {
SpanEventsAre(
EventNamed("gl-cpp.cancel"),
EventNamed("test-only: underlying stream cancel"))),
SpanNamed("Finish")));
AllOf(SpanNamed("Finish"), SpanWithParent(span))));
}

TEST(AsyncStreamingReadRpcTracing, Start) {
Expand All @@ -98,10 +99,10 @@ TEST(AsyncStreamingReadRpcTracing, Start) {
auto spans = span_catcher->GetSpans();
EXPECT_THAT(
spans, UnorderedElementsAre(
SpanNamed("Start"),
AllOf(SpanNamed("Start"), SpanWithParent(span)),
AllOf(SpanNamed("span"), SpanHasAttributes(OTelAttribute<bool>(
"gl-cpp.stream_started", true))),
SpanNamed("Finish")));
AllOf(SpanNamed("Finish"), SpanWithParent(span))));
}

TEST(AsyncStreamingReadRpcTracing, Read) {
Expand Down Expand Up @@ -146,7 +147,7 @@ TEST(AsyncStreamingReadRpcTracing, Read) {
OTelAttribute<std::string>(
"message.type", "RECEIVED"),
OTelAttribute<int>("message.id", 3))))),
SpanNamed("Finish")));
AllOf(SpanNamed("Finish"), SpanWithParent(span))));
}

TEST(AsyncStreamingReadRpcTracing, Finish) {
Expand All @@ -168,7 +169,7 @@ TEST(AsyncStreamingReadRpcTracing, Finish) {
SpanNamed("span"),
SpanHasAttributes(OTelAttribute<std::string>("grpc.peer", _)),
SpanWithStatus(opentelemetry::trace::StatusCode::kError, "fail")),
SpanNamed("Finish")));
AllOf(SpanNamed("Finish"), SpanWithParent(span))));
}

TEST(AsyncStreamingReadRpcTracing, GetRequestMetadata) {
Expand Down

0 comments on commit 748b395

Please sign in to comment.