Skip to content

Commit ab7a6df

Browse files
committed
address review comments
1 parent 8834436 commit ab7a6df

3 files changed

Lines changed: 39 additions & 38 deletions

File tree

google/cloud/storage/internal/async/connection_tracing.cc

Lines changed: 17 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -55,13 +55,8 @@ class AsyncConnectionTracing : public storage::AsyncConnection {
5555
EnrichSpan(*span, p.options,
5656
p.request.write_object_spec().resource().bucket());
5757
internal::OTelScope scope(span);
58-
return impl_->InsertObject(std::move(p))
59-
.then([oc = opentelemetry::context::RuntimeContext::GetCurrent(),
60-
span = std::move(span)](auto f) {
61-
auto result = f.get();
62-
internal::DetachOTelContext(oc);
63-
return internal::EndSpan(*span, std::move(result));
64-
});
58+
return internal::EndSpan(std::move(span),
59+
impl_->InsertObject(std::move(p)));
6560
}
6661

6762
future<StatusOr<std::shared_ptr<storage::ObjectDescriptorConnection>>> Open(
@@ -107,13 +102,8 @@ class AsyncConnectionTracing : public storage::AsyncConnection {
107102
auto span = internal::MakeSpan("storage::AsyncConnection::ReadObjectRange");
108103
EnrichSpan(*span, p.options, p.request.bucket());
109104
internal::OTelScope scope(span);
110-
return impl_->ReadObjectRange(std::move(p))
111-
.then([oc = opentelemetry::context::RuntimeContext::GetCurrent(),
112-
span = std::move(span)](auto f) {
113-
auto result = f.get();
114-
internal::DetachOTelContext(oc);
115-
return internal::EndSpan(*span, std::move(result));
116-
});
105+
return internal::EndSpan(std::move(span),
106+
impl_->ReadObjectRange(std::move(p)));
117107
}
118108

119109
future<StatusOr<std::unique_ptr<storage::AsyncWriterConnection>>>
@@ -233,26 +223,16 @@ class AsyncConnectionTracing : public storage::AsyncConnection {
233223
auto span = internal::MakeSpan("storage::AsyncConnection::ComposeObject");
234224
EnrichSpan(*span, p.options, p.request.destination().bucket());
235225
internal::OTelScope scope(span);
236-
return impl_->ComposeObject(std::move(p))
237-
.then([oc = opentelemetry::context::RuntimeContext::GetCurrent(),
238-
span = std::move(span)](auto f) {
239-
auto result = f.get();
240-
internal::DetachOTelContext(oc);
241-
return internal::EndSpan(*span, std::move(result));
242-
});
226+
return internal::EndSpan(std::move(span),
227+
impl_->ComposeObject(std::move(p)));
243228
}
244229

245230
future<Status> DeleteObject(DeleteObjectParams p) override {
246231
auto span = internal::MakeSpan("storage::AsyncConnection::DeleteObject");
247232
EnrichSpan(*span, p.options, p.request.bucket());
248233
internal::OTelScope scope(span);
249-
return impl_->DeleteObject(std::move(p))
250-
.then([oc = opentelemetry::context::RuntimeContext::GetCurrent(),
251-
span = std::move(span)](auto f) {
252-
auto result = f.get();
253-
internal::DetachOTelContext(oc);
254-
return internal::EndSpan(*span, std::move(result));
255-
});
234+
return internal::EndSpan(std::move(span),
235+
impl_->DeleteObject(std::move(p)));
256236
}
257237

258238
std::shared_ptr<storage::AsyncRewriterConnection> RewriteObject(
@@ -270,15 +250,15 @@ class AsyncConnectionTracing : public storage::AsyncConnection {
270250
auto const options = p.options;
271251
return impl_->GetBucket(std::move(p))
272252
.then([oc = opentelemetry::context::RuntimeContext::GetCurrent(),
273-
span = std::move(span), this, bucket_name,
253+
span = std::move(span), cache = cache_, bucket_name,
274254
options](future<StatusOr<google::storage::v2::Bucket>> f)
275255
-> StatusOr<google::storage::v2::Bucket> {
276256
auto result = f.get();
277257
internal::DetachOTelContext(oc);
278258
if (result.ok()) {
279-
EnrichSpan(*span, options, *result, bucket_name);
259+
EnrichSpan(*span, options, *result, bucket_name, *cache);
280260
} else {
281-
cache().MaybeInvalidate(result, bucket_name);
261+
cache->MaybeInvalidate(result, bucket_name);
282262
}
283263
return internal::EndSpan(*span, std::move(result));
284264
});
@@ -327,9 +307,11 @@ class AsyncConnectionTracing : public storage::AsyncConnection {
327307
span.SetAttribute("gcp.resource.destination.location", entry.location);
328308
}
329309

330-
void EnrichSpan(opentelemetry::trace::Span& span, Options const& options,
331-
google::storage::v2::Bucket const& bucket,
332-
std::string const& bucket_name) {
310+
static void EnrichSpan(opentelemetry::trace::Span& span,
311+
Options const& options,
312+
google::storage::v2::Bucket const& bucket,
313+
std::string const& bucket_name,
314+
BucketMetadataCache& cache) {
333315
auto const enabled = options.get<
334316
google::cloud::storage_experimental::OTelSpanEnrichmentOption>();
335317
if (!enabled) return;
@@ -338,7 +320,7 @@ class AsyncConnectionTracing : public storage::AsyncConnection {
338320
BucketMetadataCache::NormalizeBucketName(bucket_name),
339321
bucket.location(), bucket.location_type());
340322
EnrichSpan(span, entry);
341-
cache().Put(bucket_name, std::move(entry));
323+
cache.Put(bucket_name, std::move(entry));
342324
}
343325

344326
void EnrichSpan(opentelemetry::trace::Span& span, Options const& options,

google/cloud/storage/internal/bucket_metadata_cache.cc

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
#include "google/cloud/storage/bucket_metadata.h"
1717
#include "absl/strings/match.h"
1818
#include <mutex>
19+
#include <string_view>
1920
#include <utility>
2021

2122
namespace google {
@@ -40,9 +41,13 @@ BucketCacheEntry BucketCacheEntry::FromMetadata(
4041

4142
std::string BucketMetadataCache::NormalizeBucketName(
4243
std::string const& bucket) {
43-
auto const prefix = std::string("projects/_/buckets/");
44-
if (absl::StartsWith(bucket, prefix)) {
45-
return bucket.substr(prefix.size());
44+
auto constexpr kPrefix = "projects/";
45+
auto constexpr kMiddle = "/buckets/";
46+
if (absl::StartsWith(bucket, kPrefix)) {
47+
auto pos = bucket.find(kMiddle);
48+
if (pos != std::string::npos) {
49+
return bucket.substr(pos + std::string_view(kMiddle).size());
50+
}
4651
}
4752
return bucket;
4853
}

google/cloud/storage/internal/bucket_metadata_cache_test.cc

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,20 @@ using ::testing::Eq;
2525
using ::testing::IsFalse;
2626
using ::testing::IsTrue;
2727

28+
TEST(BucketMetadataCacheTest, NormalizeBucketName) {
29+
EXPECT_THAT(BucketMetadataCache::NormalizeBucketName("test-bucket"),
30+
Eq("test-bucket"));
31+
EXPECT_THAT(BucketMetadataCache::NormalizeBucketName(
32+
"projects/_/buckets/test-bucket"),
33+
Eq("test-bucket"));
34+
EXPECT_THAT(BucketMetadataCache::NormalizeBucketName(
35+
"projects/123456/buckets/test-bucket"),
36+
Eq("test-bucket"));
37+
EXPECT_THAT(BucketMetadataCache::NormalizeBucketName(
38+
"projects/my-project/buckets/test-bucket"),
39+
Eq("test-bucket"));
40+
}
41+
2842
TEST(BucketMetadataCacheTest, HitAndMiss) {
2943
BucketMetadataCache cache(10);
3044
EXPECT_FALSE(cache.Get("test-bucket").has_value());

0 commit comments

Comments
 (0)