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 5 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
5 changes: 5 additions & 0 deletions google/cloud/pubsub/internal/tracing_batch_sink.cc
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,11 @@ 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
root_context.SetValue(/*opentelemetry::trace::kIsRootSpanKey=*/"is_root_span",
true);
alevenberg marked this conversation as resolved.
Show resolved Hide resolved
options.parent = root_context;
options.kind = opentelemetry::trace::SpanKind::kClient;
auto batch_sink_parent = internal::MakeSpan(
topic.topic_id() + " publish",
Expand Down
23 changes: 23 additions & 0 deletions google/cloud/pubsub/internal/tracing_batch_sink_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ using ::google::cloud::testing_util::SpanHasInstrumentationScope;
using ::google::cloud::testing_util::SpanKindIsClient;
using ::google::cloud::testing_util::SpanLinksSizeIs;
using ::google::cloud::testing_util::SpanNamed;
using ::google::cloud::testing_util::SpanParentIsRoot;
using ::google::cloud::testing_util::ThereIsAnActiveSpan;
using ::testing::_;
using ::testing::AllOf;
Expand Down Expand Up @@ -216,6 +217,28 @@ TEST(TracingBatchSink, PublishSpanHasAttributes) {
TestTopic().FullName())))));
}

TEST(TracingBatchSink, PublishSpanParentIsRoot) {
alevenberg marked this conversation as resolved.
Show resolved Hide resolved
auto span_catcher = InstallSpanCatcher();
auto message_span = MakeSpan("test span");
alevenberg marked this conversation as resolved.
Show resolved Hide resolved
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"),
SpanParentIsRoot())));
alevenberg marked this conversation as resolved.
Show resolved Hide resolved
}

TEST(TracingBatchSink, AsyncPublishOnlyIncludeSampledLink) {
namespace sc = ::opentelemetry::trace::SemanticConventions;
// Create span before the span catcher so it is not sampled.
Expand Down
7 changes: 7 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,13 @@ MATCHER_P(SpanWithParent, span,
return actual == span->GetContext().span_id();
}

MATCHER(SpanParentIsRoot, " is root span") {
alevenberg marked this conversation as resolved.
Show resolved Hide resolved
auto const& actual =
(arg->GetParentSpanId() == opentelemetry::trace::SpanId());
alevenberg marked this conversation as resolved.
Show resolved Hide resolved
*result_listener << "is root span: " << actual;
return actual;
alevenberg marked this conversation as resolved.
Show resolved Hide resolved
}

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