@@ -51,8 +51,11 @@ using ::google::cloud::testing_util::StatusIs;
5151using ::google::cloud::testing_util::ThereIsAnActiveSpan;
5252using ::testing::_;
5353using ::testing::AllOf;
54+ using ::testing::Contains;
5455using ::testing::ElementsAre;
5556using ::testing::Not;
57+ using ::testing::Pair;
58+ using ::testing::StartsWith;
5659
5760TEST (PublisherTracingConnectionTest, PublishSpanOnSuccess) {
5861 namespace sc = ::opentelemetry::trace::SemanticConventions;
@@ -139,6 +142,29 @@ TEST(PublisherTracingConnectionTest, PublishSpanOnError) {
139142 " messaging.message.total_size_bytes" , 45 )))));
140143}
141144
145+ TEST (PublisherTracingConnectionTest, PublishInjectsTraceContext) {
146+ auto mock = std::make_shared<MockPublisherConnection>();
147+ // Need to install the span catcher so ThereIsAnActiveSpan() detects a span.
148+ auto span_catcher = InstallSpanCatcher ();
149+ EXPECT_CALL (*mock, Publish)
150+ .WillOnce ([&](pubsub::PublisherConnection::PublishParams const & p) {
151+ EXPECT_TRUE (ThereIsAnActiveSpan ());
152+ // We need to test the trace context has been injected here, since the
153+ // connection moves the message to the child connection.
154+ EXPECT_THAT (p.message .attributes (),
155+ Contains (Pair (StartsWith (" googclient_" ), _)));
156+ return make_ready_future (StatusOr<std::string>(" test-id-0" ));
157+ });
158+ auto connection = MakePublisherTracingConnection (
159+ Topic (" test-project" , " test-topic" ), std::move (mock));
160+
161+ auto message = pubsub::MessageBuilder{}
162+ .SetData (" test-data-0" )
163+ .SetOrderingKey (" ordering-key-0" )
164+ .Build ();
165+ auto response = connection->Publish ({message}).get ();
166+ }
167+
142168TEST (PublisherTracingConnectionTest, PublishSpanOmitsOrderingKey) {
143169 auto span_catcher = InstallSpanCatcher ();
144170 auto mock = std::make_shared<MockPublisherConnection>();
0 commit comments