Skip to content
Open
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: 4 additions & 1 deletion google/cloud/storage/internal/async/connection_tracing.cc
Original file line number Diff line number Diff line change
Expand Up @@ -227,8 +227,11 @@ class AsyncConnectionTracing : public storage::AsyncConnection {
std::shared_ptr<storage::AsyncRewriterConnection> RewriteObject(
RewriteObjectParams p) override {
auto const enabled = internal::TracingEnabled(p.options);
if (!enabled) return impl_->RewriteObject(std::move(p));
auto span = internal::MakeSpan("storage::AsyncConnection::RewriteObject");
EnrichSpan(*span, p.options, p.request.destination_bucket());
return MakeTracingAsyncRewriterConnection(
impl_->RewriteObject(std::move(p)), enabled);
impl_->RewriteObject(std::move(p)), std::move(span));
}

future<StatusOr<google::storage::v2::Bucket>> GetBucket(
Expand Down
53 changes: 53 additions & 0 deletions google/cloud/storage/internal/async/connection_tracing_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -539,6 +539,59 @@ TEST(ConnectionTracing, RewriteObject) {
SpanHasEvents(EventNamed("gl-cpp.storage.rewrite.iterate")))));
}

TEST(ConnectionTracing, RewriteObjectSpanEnrichment) {
auto span_catcher = InstallSpanCatcher();
PromiseWithOTelContext<StatusOr<google::storage::v2::Bucket>> p;

auto options = TracingEnabled().set<
google::cloud::storage_experimental::OTelSpanEnrichmentOption>(true);
auto mock = std::make_unique<MockAsyncConnection>();
EXPECT_CALL(*mock, options).WillRepeatedly(Return(options));

EXPECT_CALL(*mock, GetBucket).WillOnce(expect_context(p));
EXPECT_CALL(*mock, RewriteObject).WillOnce([] {
auto rewriter = std::make_shared<MockAsyncRewriterConnection>();
EXPECT_CALL(*rewriter, Iterate).WillOnce([] {
return make_ready_future(make_status_or(MakeRewriteResponse()));
});
return rewriter;
});

auto connection = MakeTracingAsyncConnection(std::move(mock));

// 1st call populates the cache
google::storage::v2::GetBucketRequest req;
req.set_name("projects/_/buckets/test-bucket");
auto res1 = connection->GetBucket({req, options}).then(expect_no_context);
google::storage::v2::Bucket bucket_meta;
bucket_meta.set_project("projects/123456");
bucket_meta.set_location("us-east1");
bucket_meta.set_location_type("regional");
p.set_value(make_status_or(std::move(bucket_meta)));
ASSERT_STATUS_OK(res1.get());

(void)span_catcher->GetSpans();

// 2nd call: RewriteObject uses cached bucket metadata for span enrichment
google::storage::v2::RewriteObjectRequest rewrite_req;
rewrite_req.set_destination_bucket("projects/_/buckets/test-bucket");
auto rewriter = connection->RewriteObject({rewrite_req, options});
auto r1 = rewriter->Iterate().get();
ASSERT_STATUS_OK(r1);

auto spans = span_catcher->GetSpans();
EXPECT_THAT(
spans,
ElementsAre(AllOf(
SpanNamed("storage::AsyncConnection::RewriteObject"),
SpanWithStatus(opentelemetry::trace::StatusCode::kOk),
SpanHasAttributes(
OTelAttribute<std::string>("gcp.resource.destination.id",
"projects/123456/buckets/test-bucket"),
OTelAttribute<std::string>("gcp.resource.destination.location",
"us-east1")))));
}

TEST(ConnectionTracing, OpenError) {
auto span_catcher = InstallSpanCatcher();
PromiseWithOTelContext<
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,13 @@ MakeTracingAsyncRewriterConnection(
std::shared_ptr<storage::AsyncRewriterConnection> impl, bool enabled) {
if (!enabled) return impl;
auto span = internal::MakeSpan("storage::AsyncConnection::RewriteObject");
return MakeTracingAsyncRewriterConnection(std::move(impl), std::move(span));
}

std::shared_ptr<storage::AsyncRewriterConnection>
MakeTracingAsyncRewriterConnection(
std::shared_ptr<storage::AsyncRewriterConnection> impl,
opentelemetry::nostd::shared_ptr<opentelemetry::trace::Span> span) {
return std::make_shared<AsyncRewriterTracingConnection>(std::move(impl),
std::move(span));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
#define GOOGLE_CLOUD_CPP_GOOGLE_CLOUD_STORAGE_INTERNAL_ASYNC_REWRITER_CONNECTION_TRACING_H

#include "google/cloud/storage/async/rewriter_connection.h"
#include "google/cloud/internal/opentelemetry.h"
#include "google/cloud/options.h"
#include "google/cloud/version.h"
#include <memory>
Expand All @@ -29,6 +30,11 @@ std::shared_ptr<storage::AsyncRewriterConnection>
MakeTracingAsyncRewriterConnection(
std::shared_ptr<storage::AsyncRewriterConnection> impl, bool enabled);

std::shared_ptr<storage::AsyncRewriterConnection>
MakeTracingAsyncRewriterConnection(
std::shared_ptr<storage::AsyncRewriterConnection> impl,
opentelemetry::nostd::shared_ptr<opentelemetry::trace::Span> span);

GOOGLE_CLOUD_CPP_INLINE_NAMESPACE_END
} // namespace storage_internal
} // namespace cloud
Expand Down
Loading