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(pubsub): start the publish span as a root span #13285

Merged
merged 8 commits into from
Dec 19, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
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: 2 additions & 3 deletions google/cloud/pubsub/internal/tracing_batch_sink.cc
Original file line number Diff line number Diff line change
Expand Up @@ -64,8 +64,8 @@ auto MakeParent(Links const& links, Spans const& message_spans,
// TODO(#13287): Use the constant instead of the string.
alevenberg marked this conversation as resolved.
Show resolved Hide resolved
// Setting a span as a root span was added in OTel v1.13+. It is a no-op for
// earlier versions.
root_context.SetValue(/*opentelemetry::trace::kIsRootSpanKey=*/"is_root_span",
true);
root_context = root_context.SetValue(
/*opentelemetry::trace::kIsRootSpanKey=*/"is_root_span", true);
options.parent = root_context;
alevenberg marked this conversation as resolved.
Show resolved Hide resolved
options.kind = opentelemetry::trace::SpanKind::kClient;
auto batch_sink_parent = internal::MakeSpan(
Expand All @@ -81,7 +81,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
7 changes: 3 additions & 4 deletions google/cloud/pubsub/internal/tracing_batch_sink_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -217,11 +217,12 @@ TEST(TracingBatchSink, PublishSpanHasAttributes) {
TestTopic().FullName())))));
}

#if OPENTELEMETRY_VERSION_MAJOR >= 1
#if OPENTELEMETRY_VERSION_MINOR >= 13
#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");
alevenberg marked this conversation as resolved.
Show resolved Hide resolved
auto scope = opentelemetry::trace::Scope(message_span);
auto mock = std::make_unique<pubsub_testing::MockBatchSink>();
EXPECT_CALL(*mock, AddMessage(_));
EXPECT_CALL(*mock, AsyncPublish)
Expand All @@ -239,10 +240,8 @@ TEST(TracingBatchSink, PublishSpanIsRoot) {
auto spans = span_catcher->GetSpans();
EXPECT_THAT(spans,
Contains(AllOf(SpanNamed("test-topic publish"), SpanIsRoot())));
EXPECT_THAT(spans, Contains(AllOf(SpanNamed("test span"), SpanIsRoot())));
}
#endif
#endif

TEST(TracingBatchSink, AsyncPublishOnlyIncludeSampledLink) {
namespace sc = ::opentelemetry::trace::SemanticConventions;
Expand Down
5 changes: 2 additions & 3 deletions google/cloud/testing_util/opentelemetry_matchers.h
Original file line number Diff line number Diff line change
Expand Up @@ -150,9 +150,8 @@ MATCHER_P(SpanWithParent, span,
return actual == span->GetContext().span_id();
}

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