@@ -56,6 +56,12 @@ void TracingConnection::CleanupCompletedTasks() {
5656 bg_tasks_.end ());
5757}
5858
59+ void TracingConnection::EnrichSpan (opentelemetry::trace::Span& span,
60+ BucketCacheEntry const & entry) {
61+ span.SetAttribute (" gcp.resource.destination.id" , entry.id );
62+ span.SetAttribute (" gcp.resource.destination.location" , entry.location );
63+ }
64+
5965void TracingConnection::MaybeTriggerBackgroundFetch (
6066 std::string const & bucket_name) {
6167 CleanupCompletedTasks ();
@@ -65,30 +71,20 @@ void TracingConnection::MaybeTriggerBackgroundFetch(
6571 }
6672
6773 auto current_options = google::cloud::internal::SaveCurrentOptions ();
68- auto f =
69- std::async (std::launch::async, [this , bucket_name, current_options]() {
70- google::cloud::internal::OptionsSpan span (current_options);
71- storage::internal::GetBucketMetadataRequest request (bucket_name);
72- auto result = impl_->GetBucketMetadata (request);
73-
74- BucketCacheEntry entry;
75- if (result.ok ()) {
76- entry.id = " projects/" + std::to_string (result->project_number ()) +
77- " /buckets/" + result->name ();
78- entry.location = result->location ();
79- if (result->location_type () == " multi-region" ||
80- result->location_type () == " dual-region" ) {
81- entry.location = " global" ;
82- }
83- cache ().Put (bucket_name, std::move (entry));
84- } else if (result.status ().code () == StatusCode::kPermissionDenied ) {
85- entry.id = " projects/_/buckets/" + bucket_name;
86- entry.location = " global" ;
87- cache ().Put (bucket_name, std::move (entry));
88- }
89-
90- cache ().EndFetch (bucket_name);
91- });
74+ auto f = std::async (std::launch::async, [this , bucket_name,
75+ current_options]() {
76+ google::cloud::internal::OptionsSpan span (current_options);
77+ storage::internal::GetBucketMetadataRequest request (bucket_name);
78+ auto result = impl_->GetBucketMetadata (request);
79+
80+ if (result.ok ()) {
81+ cache ().Put (bucket_name, BucketCacheEntry::FromMetadata (*result));
82+ } else if (result.status ().code () == StatusCode::kPermissionDenied ) {
83+ cache ().Put (bucket_name, {" projects/_/buckets/" + bucket_name, " global" });
84+ }
85+
86+ cache ().EndFetch (bucket_name);
87+ });
9288
9389 bg_tasks_.push_back (std::move (f));
9490}
@@ -98,27 +94,17 @@ void TracingConnection::EnrichSpan(opentelemetry::trace::Span& span,
9894 if (bucket_name.empty ()) return ;
9995 auto entry = cache ().Get (bucket_name);
10096 if (entry.has_value ()) {
101- span.SetAttribute (" gcp.resource.destination.id" , entry->id );
102- span.SetAttribute (" gcp.resource.destination.location" , entry->location );
97+ EnrichSpan (span, *entry);
10398 } else {
10499 MaybeTriggerBackgroundFetch (bucket_name);
105100 }
106101}
107102
108103void TracingConnection::EnrichSpan (opentelemetry::trace::Span& span,
109104 storage::BucketMetadata const & metadata) {
110- std::string id = " projects/" + std::to_string (metadata.project_number ()) +
111- " /buckets/" + metadata.name ();
112- std::string location = metadata.location ();
113- if (metadata.location_type () == " multi-region" ||
114- metadata.location_type () == " dual-region" ) {
115- location = " global" ;
116- }
117- span.SetAttribute (" gcp.resource.destination.id" , id);
118- span.SetAttribute (" gcp.resource.destination.location" , location);
119-
120- // Populate cache since we have metadata!
121- cache ().Put (metadata.name (), {id, location});
105+ auto entry = BucketCacheEntry::FromMetadata (metadata);
106+ EnrichSpan (span, entry);
107+ cache ().Put (metadata.name (), std::move (entry));
122108}
123109
124110StatusOr<storage::internal::ListBucketsResponse> TracingConnection::ListBuckets (
@@ -143,7 +129,11 @@ StatusOr<storage::BucketMetadata> TracingConnection::GetBucketMetadata(
143129 auto span = internal::MakeSpan (" storage::Client::GetBucketMetadata" );
144130 auto scope = opentelemetry::trace::Scope (span);
145131 auto result = impl_->GetBucketMetadata (request);
146- if (result.ok ()) EnrichSpan (*span, *result);
132+ if (result.ok ()) {
133+ EnrichSpan (*span, *result);
134+ } else {
135+ MaybeInvalidate (result, request.bucket_name ());
136+ }
147137 return internal::EndSpan (*span, std::move (result));
148138}
149139
@@ -153,7 +143,9 @@ StatusOr<storage::internal::EmptyResponse> TracingConnection::DeleteBucket(
153143 auto scope = opentelemetry::trace::Scope (span);
154144 EnrichSpan (*span, request.bucket_name ());
155145 auto result = impl_->DeleteBucket (request);
156- MaybeInvalidate (result, request.bucket_name ());
146+ if (result.ok () || result.status ().code () == StatusCode::kNotFound ) {
147+ cache ().Invalidate (request.bucket_name ());
148+ }
157149 return internal::EndSpan (*span, std::move (result));
158150}
159151
@@ -162,7 +154,11 @@ StatusOr<storage::BucketMetadata> TracingConnection::UpdateBucket(
162154 auto span = internal::MakeSpan (" storage::Client::UpdateBucket" );
163155 auto scope = opentelemetry::trace::Scope (span);
164156 auto result = impl_->UpdateBucket (request);
165- if (result.ok ()) EnrichSpan (*span, *result);
157+ if (result.ok ()) {
158+ EnrichSpan (*span, *result);
159+ } else {
160+ MaybeInvalidate (result, request.metadata ().name ());
161+ }
166162 return internal::EndSpan (*span, std::move (result));
167163}
168164
@@ -171,7 +167,11 @@ StatusOr<storage::BucketMetadata> TracingConnection::PatchBucket(
171167 auto span = internal::MakeSpan (" storage::Client::PatchBucket" );
172168 auto scope = opentelemetry::trace::Scope (span);
173169 auto result = impl_->PatchBucket (request);
174- if (result.ok ()) EnrichSpan (*span, *result);
170+ if (result.ok ()) {
171+ EnrichSpan (*span, *result);
172+ } else {
173+ MaybeInvalidate (result, request.bucket ());
174+ }
175175 return internal::EndSpan (*span, std::move (result));
176176}
177177
0 commit comments