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 7 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
8 changes: 7 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,13 @@ 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.
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 = root_context.SetValue(
/*opentelemetry::trace::kIsRootSpanKey=*/"is_root_span", true);
options.parent = root_context;
options.kind = opentelemetry::trace::SpanKind::kClient;
auto batch_sink_parent = internal::MakeSpan(
topic.topic_id() + " publish",
Expand All @@ -74,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
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");
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)
.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