diff --git a/google/cloud/internal/async_streaming_write_rpc_tracing.h b/google/cloud/internal/async_streaming_write_rpc_tracing.h index d182d8ef09317..e67096369d5b9 100644 --- a/google/cloud/internal/async_streaming_write_rpc_tracing.h +++ b/google/cloud/internal/async_streaming_write_rpc_tracing.h @@ -48,7 +48,11 @@ class AsyncStreamingWriteRpcTracing } future 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 f) { EndSpan(*ss); @@ -82,7 +86,11 @@ class AsyncStreamingWriteRpcTracing } future> 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> f) { EndSpan(*fs); diff --git a/google/cloud/internal/async_streaming_write_rpc_tracing_test.cc b/google/cloud/internal/async_streaming_write_rpc_tracing_test.cc index 0f0a84accb462..31cb9911a2113 100644 --- a/google/cloud/internal/async_streaming_write_rpc_tracing_test.cc +++ b/google/cloud/internal/async_streaming_write_rpc_tracing_test.cc @@ -34,6 +34,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::_; @@ -78,7 +79,7 @@ TEST(AsyncStreamingWriteRpcTracing, Cancel) { SpanEventsAre( EventNamed("gl-cpp.cancel"), EventNamed("test-only: underlying stream cancel"))), - SpanNamed("Finish"))); + AllOf(SpanNamed("Finish"), SpanWithParent(span)))); } TEST(AsyncStreamingWriteRpcTracing, Start) { @@ -98,10 +99,10 @@ TEST(AsyncStreamingWriteRpcTracing, Start) { auto spans = span_catcher->GetSpans(); EXPECT_THAT( spans, UnorderedElementsAre( - SpanNamed("Start"), + AllOf(SpanNamed("Start"), SpanWithParent(span)), AllOf(SpanNamed("span"), SpanHasAttributes(OTelAttribute( "gl-cpp.stream_started", true))), - SpanNamed("Finish"))); + AllOf(SpanNamed("Finish"), SpanWithParent(span)))); } TEST(AsyncStreamingWriteRpcTracing, Write) { @@ -149,7 +150,7 @@ TEST(AsyncStreamingWriteRpcTracing, Write) { OTelAttribute("message.id", 3), OTelAttribute("message.is_last", true), OTelAttribute("message.success", true))))), - SpanNamed("Finish"))); + AllOf(SpanNamed("Finish"), SpanWithParent(span)))); } TEST(AsyncStreamingWriteRpcTracing, WritesDone) { @@ -171,7 +172,7 @@ TEST(AsyncStreamingWriteRpcTracing, WritesDone) { AllOf(SpanNamed("span"), SpanEventsAre(EventNamed("gl-cpp.first-write"), EventNamed("gl-cpp.writes_done"))), - SpanNamed("Finish"))); + AllOf(SpanNamed("Finish"), SpanWithParent(span)))); } TEST(AsyncStreamingWriteRpcTracing, Finish) { @@ -194,7 +195,7 @@ TEST(AsyncStreamingWriteRpcTracing, Finish) { SpanNamed("span"), SpanHasAttributes(OTelAttribute("grpc.peer", _)), SpanWithStatus(opentelemetry::trace::StatusCode::kError, "fail")), - SpanNamed("Finish"))); + AllOf(SpanNamed("Finish"), SpanWithParent(span)))); } TEST(AsyncStreamingWriteRpcTracing, GetRequestMetadata) {