Skip to content

Commit

Permalink
impl(pubsub): start the publish span as a root span (#13285)
Browse files Browse the repository at this point in the history
  • Loading branch information
alevenberg authored Dec 19, 2023
1 parent fc01856 commit 916fe8f
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 1 deletion.
7 changes: 6 additions & 1 deletion google/cloud/pubsub/internal/tracing_batch_sink.cc
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,12 @@ auto MakeParent(Links const& links, Spans const& message_spans,
pubsub::Topic const& topic) {
namespace sc = ::opentelemetry::trace::SemanticConventions;
opentelemetry::trace::StartSpanOptions options;
opentelemetry::context::Context root_context;
// TODO(#13287): Use the constant instead of the string.
// Setting a span as a root span was added in OTel v1.13+. It is a no-op for
// earlier versions.
options.parent = root_context.SetValue(
/*opentelemetry::trace::kIsRootSpanKey=*/"is_root_span", true);
options.kind = opentelemetry::trace::SpanKind::kClient;
auto batch_sink_parent = internal::MakeSpan(
topic.topic_id() + " publish",
Expand All @@ -74,7 +80,6 @@ auto MakeParent(Links const& links, Spans const& message_spans,
{sc::kMessagingDestinationTemplate, topic.FullName()}},
/*links*/ std::move(links), options);

// Add metadata to the message spans about the batch sink span.
auto context = batch_sink_parent->GetContext();
auto trace_id = internal::ToString(context.trace_id());
auto span_id = internal::ToString(context.span_id());
Expand Down
27 changes: 27 additions & 0 deletions google/cloud/pubsub/internal/tracing_batch_sink_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ using ::google::cloud::testing_util::SpanEventAttributesAre;
using ::google::cloud::testing_util::SpanHasAttributes;
using ::google::cloud::testing_util::SpanHasEvents;
using ::google::cloud::testing_util::SpanHasInstrumentationScope;
using ::google::cloud::testing_util::SpanIsRoot;
using ::google::cloud::testing_util::SpanKindIsClient;
using ::google::cloud::testing_util::SpanLinksSizeIs;
using ::google::cloud::testing_util::SpanNamed;
Expand Down Expand Up @@ -216,6 +217,32 @@ TEST(TracingBatchSink, PublishSpanHasAttributes) {
TestTopic().FullName())))));
}

#if OPENTELEMETRY_VERSION_MAJOR >= 2 || \
(OPENTELEMETRY_VERSION_MAJOR == 1 && OPENTELEMETRY_VERSION_MINOR >= 13)
TEST(TracingBatchSink, PublishSpanIsRoot) {
auto span_catcher = InstallSpanCatcher();
auto message_span = MakeSpan("test span");
auto scope = opentelemetry::trace::Scope(message_span);
auto mock = std::make_unique<pubsub_testing::MockBatchSink>();
EXPECT_CALL(*mock, AddMessage(_));
EXPECT_CALL(*mock, AsyncPublish)
.WillOnce([](google::pubsub::v1::PublishRequest const& request) {
EXPECT_THAT(request, IsProtoEqual(MakeRequest(1)));
return make_ready_future(make_status_or(MakeResponse(request)));
});
auto batch_sink = MakeTestBatchSink(std::move(mock));
auto initial_spans = {message_span};
AddMessages(initial_spans, batch_sink);

auto response = batch_sink->AsyncPublish(MakeRequest(1)).get();
EXPECT_THAT(response, IsOk());

auto spans = span_catcher->GetSpans();
EXPECT_THAT(spans,
Contains(AllOf(SpanNamed("test-topic publish"), SpanIsRoot())));
}
#endif

TEST(TracingBatchSink, AsyncPublishOnlyIncludeSampledLink) {
namespace sc = ::opentelemetry::trace::SemanticConventions;
// Create span before the span catcher so it is not sampled.
Expand Down
6 changes: 6 additions & 0 deletions google/cloud/testing_util/opentelemetry_matchers.h
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,12 @@ MATCHER_P(SpanWithParent, span,
return actual == span->GetContext().span_id();
}

MATCHER(SpanIsRoot, "is root span") {
auto const actual = arg->GetParentSpanId() == opentelemetry::trace::SpanId();
*result_listener << "is root span: " << (actual ? "true" : "false");
return actual;
}

MATCHER_P(SpanNamed, name, "has name: " + std::string{name}) {
auto const& actual = arg->GetName();
*result_listener << "has name: " << actual;
Expand Down

0 comments on commit 916fe8f

Please sign in to comment.