From 0ac789312fc55504a33e1fc3078ccc37f4544d13 Mon Sep 17 00:00:00 2001 From: cpriti-os Date: Tue, 24 Nov 2026 15:44:42 +0000 Subject: [PATCH 1/3] impl(storage): add retry counter support to AttemptMetrics Extend `storage_internal::AttemptMetrics` with `RetryLabels` and `RecordRetry()` for the `gcp.storage.client.retries` (`{retry}`) OpenTelemetry counter. [Generated-by: AI] --- .../cloud/storage/internal/attempt_metrics.cc | 26 ++++++++- .../cloud/storage/internal/attempt_metrics.h | 16 +++++- .../storage/internal/attempt_metrics_test.cc | 54 ++++++++++++++++++- .../storage/internal/metrics_constants.h | 4 ++ 4 files changed, 96 insertions(+), 4 deletions(-) diff --git a/google/cloud/storage/internal/attempt_metrics.cc b/google/cloud/storage/internal/attempt_metrics.cc index b2d8b10000e9e..e8684be71c2ad 100644 --- a/google/cloud/storage/internal/attempt_metrics.cc +++ b/google/cloud/storage/internal/attempt_metrics.cc @@ -57,13 +57,16 @@ AttemptMetrics::AttemptMetrics( provider->GetMeter(kStorageMeterName, google::cloud::version_string()); if (!meter_) return; attempts_ = meter_->CreateUInt64Counter( - "gcp.storage.client.attempts", + ToNostd(kAttemptsMetric), "Total number of individual backend attempts made during operations.", "{attempt}"); errors_ = meter_->CreateUInt64Counter( - "gcp.storage.client.errors", + ToNostd(kErrorsMetric), "Number of failed backend attempts encountered by the client.", "{error}"); + retries_ = meter_->CreateUInt64Counter( + ToNostd(kRetriesMetric), + "Number of retry attempts initiated by the client.", "{retry}"); } void AttemptMetrics::RecordGrpcAttempt( @@ -115,6 +118,25 @@ void AttemptMetrics::RecordAttemptImpl( } } +void AttemptMetrics::RecordRetry( + RetryLabels const& labels, + opentelemetry::context::Context const& ctx) const { + if (!retries_) return; + std::array const attrs{{ + {"gcp.client.version", ToNostd(client_version_)}, + {"gcp.client.artifact", + opentelemetry::nostd::string_view("google-cloud-cpp")}, + {"gcp.client.service", opentelemetry::nostd::string_view("storage")}, + {"server.address", ToNostd(server_address_)}, + {"rpc.system.name", ToNostd(labels.rpc_system)}, + {"gcp.storage.operation", ToNostd(labels.operation)}, + {"error.type", ToNostd(labels.error_type)}, + {"network.path", ToNostd(labels.network_path)}, + }}; + retries_->Add(std::uint64_t{1}, + opentelemetry::common::MakeKeyValueIterableView(attrs), ctx); +} + GOOGLE_CLOUD_CPP_INLINE_NAMESPACE_END } // namespace storage_internal } // namespace cloud diff --git a/google/cloud/storage/internal/attempt_metrics.h b/google/cloud/storage/internal/attempt_metrics.h index 9b1e44dc8c23f..850b72a13697a 100644 --- a/google/cloud/storage/internal/attempt_metrics.h +++ b/google/cloud/storage/internal/attempt_metrics.h @@ -48,8 +48,16 @@ struct HttpAttemptLabels { std::string_view network_path; }; +/// Labels for recording a client-initiated retry attempt. +struct RetryLabels { + std::string_view rpc_system; + std::string_view operation; + std::string_view error_type; + std::string_view network_path; +}; + /// Thread-safe recorder for attempt-scoped OpenTelemetry Storage counters -/// (`attempts` and `errors`). No-op when `provider` is null. +/// (`attempts`, `errors`, and `retries`). No-op when `provider` is null. class AttemptMetrics { public: AttemptMetrics(opentelemetry::nostd::shared_ptr< @@ -62,6 +70,9 @@ class AttemptMetrics { void RecordHttpAttempt(HttpAttemptLabels const& labels, opentelemetry::context::Context const& ctx) const; + void RecordRetry(RetryLabels const& labels, + opentelemetry::context::Context const& ctx) const; + private: void RecordAttemptImpl(std::string_view rpc_system, std::string_view operation, @@ -79,6 +90,9 @@ class AttemptMetrics { opentelemetry::nostd::unique_ptr< opentelemetry::metrics::Counter> errors_; + opentelemetry::nostd::unique_ptr< + opentelemetry::metrics::Counter> + retries_; }; GOOGLE_CLOUD_CPP_INLINE_NAMESPACE_END diff --git a/google/cloud/storage/internal/attempt_metrics_test.cc b/google/cloud/storage/internal/attempt_metrics_test.cc index 556b12c6247ca..3351ce367f66a 100644 --- a/google/cloud/storage/internal/attempt_metrics_test.cc +++ b/google/cloud/storage/internal/attempt_metrics_test.cc @@ -134,6 +134,9 @@ TEST(AttemptMetricsTest, RecordsGrpcAndHttpAttributes) { EXPECT_CALL(*meter, CreateUInt64Counter(Eq("gcp.storage.client.errors"), _, Eq("{error}"))) .WillOnce(Return(ByMove(CounterPtr(errors.release())))); + EXPECT_CALL(*meter, CreateUInt64Counter(Eq("gcp.storage.client.retries"), _, + Eq("{retry}"))) + .WillOnce(Return(ByMove(CounterPtr(nullptr)))); auto provider = std::make_shared(); EXPECT_CALL(*provider, GetMeter) @@ -223,6 +226,8 @@ TEST(AttemptMetricsTest, NullProviderAndNullMeterAreNoOps) { no_provider.RecordGrpcAttempt({"ReadObject", "OK", "OK", "directpath"}, ctx); no_provider.RecordHttpAttempt({"ReadObject", "UNAVAILABLE", 503, "cloudpath"}, ctx); + no_provider.RecordRetry({"grpc", "ReadObject", "UNAVAILABLE", "cloudpath"}, + ctx); auto provider = std::make_shared(); EXPECT_CALL(*provider, GetMeter) @@ -237,6 +242,7 @@ TEST(AttemptMetricsTest, NullProviderAndNullMeterAreNoOps) { {"ReadObject", "UNAVAILABLE", "UNAVAILABLE", "cloudpath"}, ctx); no_meter.RecordHttpAttempt({"ReadObject", "UNAVAILABLE", 503, "cloudpath"}, ctx); + no_meter.RecordRetry({"grpc", "ReadObject", "UNAVAILABLE", "cloudpath"}, ctx); } TEST(AttemptMetricsTest, NullAndPartialInstruments) { @@ -244,6 +250,7 @@ TEST(AttemptMetricsTest, NullAndPartialInstruments) { opentelemetry::nostd::shared_ptr empty_meter = std::make_shared(); EXPECT_CALL(*empty_meter, CreateUInt64Counter) + .WillOnce(Return(ByMove(CounterPtr(nullptr)))) .WillOnce(Return(ByMove(CounterPtr(nullptr)))) .WillOnce(Return(ByMove(CounterPtr(nullptr)))); @@ -253,7 +260,8 @@ TEST(AttemptMetricsTest, NullAndPartialInstruments) { std::make_shared(); EXPECT_CALL(*no_attempts_meter, CreateUInt64Counter) .WillOnce(Return(ByMove(CounterPtr(nullptr)))) - .WillOnce(Return(ByMove(CounterPtr(errors.release())))); + .WillOnce(Return(ByMove(CounterPtr(errors.release())))) + .WillOnce(Return(ByMove(CounterPtr(nullptr)))); auto attempts = std::make_unique>(); EXPECT_CALL(*attempts, Add(std::uint64_t{1}, _, _)).Times(1); @@ -261,6 +269,7 @@ TEST(AttemptMetricsTest, NullAndPartialInstruments) { std::make_shared(); EXPECT_CALL(*no_errors_meter, CreateUInt64Counter) .WillOnce(Return(ByMove(CounterPtr(attempts.release())))) + .WillOnce(Return(ByMove(CounterPtr(nullptr)))) .WillOnce(Return(ByMove(CounterPtr(nullptr)))); auto provider = std::make_shared(); @@ -275,6 +284,7 @@ TEST(AttemptMetricsTest, NullAndPartialInstruments) { AttemptMetrics none(mp, "storage.googleapis.com"); none.RecordGrpcAttempt( {"ReadObject", "UNAVAILABLE", "UNAVAILABLE", "cloudpath"}, ctx); + none.RecordRetry({"grpc", "ReadObject", "UNAVAILABLE", "cloudpath"}, ctx); AttemptMetrics no_attempts(mp, "storage.googleapis.com"); no_attempts.RecordGrpcAttempt({"ReadObject", "OK", "OK", "directpath"}, ctx); @@ -286,6 +296,48 @@ TEST(AttemptMetricsTest, NullAndPartialInstruments) { {"ReadObject", "UNAVAILABLE", "UNAVAILABLE", "cloudpath"}, ctx); } +TEST(AttemptMetricsTest, RecordsRetryAttributes) { + CapturedAttrs retry_attrs; + auto retries = std::make_unique>(); + EXPECT_CALL(*retries, Add(std::uint64_t{1}, _, _)) + .WillOnce([&](std::uint64_t, + opentelemetry::common::KeyValueIterable const& kv, + opentelemetry::context::Context const& ctx) { + ExpectTraceMarker(ctx); + retry_attrs = ExtractAttrs(kv); + }); + + opentelemetry::nostd::shared_ptr meter = + std::make_shared(); + EXPECT_CALL(*meter, + CreateUInt64Counter(Eq("gcp.storage.client.attempts"), _, _)) + .WillOnce(Return(ByMove(CounterPtr(nullptr)))); + EXPECT_CALL(*meter, + CreateUInt64Counter(Eq("gcp.storage.client.errors"), _, _)) + .WillOnce(Return(ByMove(CounterPtr(nullptr)))); + EXPECT_CALL(*meter, CreateUInt64Counter(Eq("gcp.storage.client.retries"), _, + Eq("{retry}"))) + .WillOnce(Return(ByMove(CounterPtr(retries.release())))); + + auto provider = std::make_shared(); + EXPECT_CALL(*provider, GetMeter).WillOnce(Return(meter)); + opentelemetry::nostd::shared_ptr mp( + std::static_pointer_cast( + provider)); + + AttemptMetrics metrics(mp, "storage.googleapis.com"); + metrics.RecordRetry( + {"http", "DeleteObject", "UNAVAILABLE", "cloudpath"}, + opentelemetry::context::Context("trace_marker", std::int64_t{42})); + EXPECT_THAT(retry_attrs.strings, + ContainerEq(ExpectedStringAttrs( + {{"rpc.system.name", "http"}, + {"gcp.storage.operation", "DeleteObject"}, + {"error.type", "UNAVAILABLE"}, + {"network.path", "cloudpath"}}))); + EXPECT_THAT(retry_attrs.ints, IsEmpty()); +} + } // namespace GOOGLE_CLOUD_CPP_INLINE_NAMESPACE_END } // namespace storage_internal diff --git a/google/cloud/storage/internal/metrics_constants.h b/google/cloud/storage/internal/metrics_constants.h index 11c2e9f135cd9..19396d90fd74d 100644 --- a/google/cloud/storage/internal/metrics_constants.h +++ b/google/cloud/storage/internal/metrics_constants.h @@ -34,10 +34,14 @@ inline constexpr char const* kStorageMeterName = "gl-cpp"; /// Shared OpenTelemetry instrument names referenced by both recorders and /// exporter/view registration so they cannot drift apart. +inline constexpr std::string_view kAttemptsMetric = + "gcp.storage.client.attempts"; +inline constexpr std::string_view kErrorsMetric = "gcp.storage.client.errors"; inline constexpr std::string_view kOperationsMetric = "gcp.storage.client.operations"; inline constexpr std::string_view kRequestDurationMetric = "gcp.client.request.duration"; +inline constexpr std::string_view kRetriesMetric = "gcp.storage.client.retries"; GOOGLE_CLOUD_CPP_INLINE_NAMESPACE_END } // namespace storage_internal From eaacf7c0574c3847b8638241412f3614c4757b2d Mon Sep 17 00:00:00 2001 From: cpriti-os Date: Tue, 1 Dec 2026 05:16:10 +0000 Subject: [PATCH 2/3] Split RecordRetry into RecordGrpcRetry and RecordHttpRetry --- .../cloud/storage/internal/attempt_metrics.cc | 16 ++++++- .../cloud/storage/internal/attempt_metrics.h | 21 ++++++++-- .../storage/internal/attempt_metrics_test.cc | 42 ++++++++++++------- 3 files changed, 58 insertions(+), 21 deletions(-) diff --git a/google/cloud/storage/internal/attempt_metrics.cc b/google/cloud/storage/internal/attempt_metrics.cc index e8684be71c2ad..4de167ff2f570 100644 --- a/google/cloud/storage/internal/attempt_metrics.cc +++ b/google/cloud/storage/internal/attempt_metrics.cc @@ -118,9 +118,21 @@ void AttemptMetrics::RecordAttemptImpl( } } -void AttemptMetrics::RecordRetry( +void AttemptMetrics::RecordGrpcRetry( RetryLabels const& labels, opentelemetry::context::Context const& ctx) const { + RecordRetryImpl("grpc", labels, ctx); +} + +void AttemptMetrics::RecordHttpRetry( + RetryLabels const& labels, + opentelemetry::context::Context const& ctx) const { + RecordRetryImpl("http", labels, ctx); +} + +void AttemptMetrics::RecordRetryImpl( + std::string_view rpc_system, RetryLabels const& labels, + opentelemetry::context::Context const& ctx) const { if (!retries_) return; std::array const attrs{{ {"gcp.client.version", ToNostd(client_version_)}, @@ -128,7 +140,7 @@ void AttemptMetrics::RecordRetry( opentelemetry::nostd::string_view("google-cloud-cpp")}, {"gcp.client.service", opentelemetry::nostd::string_view("storage")}, {"server.address", ToNostd(server_address_)}, - {"rpc.system.name", ToNostd(labels.rpc_system)}, + {"rpc.system.name", ToNostd(rpc_system)}, {"gcp.storage.operation", ToNostd(labels.operation)}, {"error.type", ToNostd(labels.error_type)}, {"network.path", ToNostd(labels.network_path)}, diff --git a/google/cloud/storage/internal/attempt_metrics.h b/google/cloud/storage/internal/attempt_metrics.h index 850b72a13697a..a0ffea16add19 100644 --- a/google/cloud/storage/internal/attempt_metrics.h +++ b/google/cloud/storage/internal/attempt_metrics.h @@ -48,9 +48,9 @@ struct HttpAttemptLabels { std::string_view network_path; }; -/// Labels for recording a client-initiated retry attempt. +/// Labels for recording a client-initiated retry attempt +/// (`gcp.storage.client.retries`). struct RetryLabels { - std::string_view rpc_system; std::string_view operation; std::string_view error_type; std::string_view network_path; @@ -70,8 +70,18 @@ class AttemptMetrics { void RecordHttpAttempt(HttpAttemptLabels const& labels, opentelemetry::context::Context const& ctx) const; - void RecordRetry(RetryLabels const& labels, - opentelemetry::context::Context const& ctx) const; + /// Records a client-initiated gRPC retry (`rpc.system.name = "grpc"`). + /// Callers should invoke this only for actual retries (`attempt > 1`) or + /// proactive write stream rotations, setting `labels.error_type` to the + /// prior failed attempt's error type (or `"SLOW_STREAM"`). + void RecordGrpcRetry(RetryLabels const& labels, + opentelemetry::context::Context const& ctx) const; + + /// Records a client-initiated HTTP retry (`rpc.system.name = "http"`). + /// Callers should invoke this only for actual retries (`attempt > 1`), + /// setting `labels.error_type` to the prior failed attempt's error type. + void RecordHttpRetry(RetryLabels const& labels, + opentelemetry::context::Context const& ctx) const; private: void RecordAttemptImpl(std::string_view rpc_system, @@ -81,6 +91,9 @@ class AttemptMetrics { opentelemetry::common::AttributeValue status_value, opentelemetry::context::Context const& ctx) const; + void RecordRetryImpl(std::string_view rpc_system, RetryLabels const& labels, + opentelemetry::context::Context const& ctx) const; + std::string client_version_; std::string server_address_; opentelemetry::nostd::shared_ptr meter_; diff --git a/google/cloud/storage/internal/attempt_metrics_test.cc b/google/cloud/storage/internal/attempt_metrics_test.cc index 3351ce367f66a..df19674187474 100644 --- a/google/cloud/storage/internal/attempt_metrics_test.cc +++ b/google/cloud/storage/internal/attempt_metrics_test.cc @@ -226,8 +226,8 @@ TEST(AttemptMetricsTest, NullProviderAndNullMeterAreNoOps) { no_provider.RecordGrpcAttempt({"ReadObject", "OK", "OK", "directpath"}, ctx); no_provider.RecordHttpAttempt({"ReadObject", "UNAVAILABLE", 503, "cloudpath"}, ctx); - no_provider.RecordRetry({"grpc", "ReadObject", "UNAVAILABLE", "cloudpath"}, - ctx); + no_provider.RecordGrpcRetry({"ReadObject", "UNAVAILABLE", "cloudpath"}, ctx); + no_provider.RecordHttpRetry({"ReadObject", "UNAVAILABLE", "cloudpath"}, ctx); auto provider = std::make_shared(); EXPECT_CALL(*provider, GetMeter) @@ -242,7 +242,8 @@ TEST(AttemptMetricsTest, NullProviderAndNullMeterAreNoOps) { {"ReadObject", "UNAVAILABLE", "UNAVAILABLE", "cloudpath"}, ctx); no_meter.RecordHttpAttempt({"ReadObject", "UNAVAILABLE", 503, "cloudpath"}, ctx); - no_meter.RecordRetry({"grpc", "ReadObject", "UNAVAILABLE", "cloudpath"}, ctx); + no_meter.RecordGrpcRetry({"ReadObject", "UNAVAILABLE", "cloudpath"}, ctx); + no_meter.RecordHttpRetry({"ReadObject", "UNAVAILABLE", "cloudpath"}, ctx); } TEST(AttemptMetricsTest, NullAndPartialInstruments) { @@ -284,7 +285,8 @@ TEST(AttemptMetricsTest, NullAndPartialInstruments) { AttemptMetrics none(mp, "storage.googleapis.com"); none.RecordGrpcAttempt( {"ReadObject", "UNAVAILABLE", "UNAVAILABLE", "cloudpath"}, ctx); - none.RecordRetry({"grpc", "ReadObject", "UNAVAILABLE", "cloudpath"}, ctx); + none.RecordGrpcRetry({"ReadObject", "UNAVAILABLE", "cloudpath"}, ctx); + none.RecordHttpRetry({"ReadObject", "UNAVAILABLE", "cloudpath"}, ctx); AttemptMetrics no_attempts(mp, "storage.googleapis.com"); no_attempts.RecordGrpcAttempt({"ReadObject", "OK", "OK", "directpath"}, ctx); @@ -296,15 +298,16 @@ TEST(AttemptMetricsTest, NullAndPartialInstruments) { {"ReadObject", "UNAVAILABLE", "UNAVAILABLE", "cloudpath"}, ctx); } -TEST(AttemptMetricsTest, RecordsRetryAttributes) { - CapturedAttrs retry_attrs; +TEST(AttemptMetricsTest, RecordsGrpcAndHttpRetryAttributes) { + std::vector retry_attrs; auto retries = std::make_unique>(); EXPECT_CALL(*retries, Add(std::uint64_t{1}, _, _)) - .WillOnce([&](std::uint64_t, - opentelemetry::common::KeyValueIterable const& kv, - opentelemetry::context::Context const& ctx) { + .Times(2) + .WillRepeatedly([&](std::uint64_t, + opentelemetry::common::KeyValueIterable const& kv, + opentelemetry::context::Context const& ctx) { ExpectTraceMarker(ctx); - retry_attrs = ExtractAttrs(kv); + retry_attrs.push_back(ExtractAttrs(kv)); }); opentelemetry::nostd::shared_ptr meter = @@ -326,16 +329,25 @@ TEST(AttemptMetricsTest, RecordsRetryAttributes) { provider)); AttemptMetrics metrics(mp, "storage.googleapis.com"); - metrics.RecordRetry( - {"http", "DeleteObject", "UNAVAILABLE", "cloudpath"}, - opentelemetry::context::Context("trace_marker", std::int64_t{42})); - EXPECT_THAT(retry_attrs.strings, + opentelemetry::context::Context const ctx("trace_marker", std::int64_t{42}); + metrics.RecordHttpRetry({"DeleteObject", "UNAVAILABLE", "cloudpath"}, ctx); + metrics.RecordGrpcRetry({"WriteObject", "SLOW_STREAM", "directpath"}, ctx); + + ASSERT_EQ(retry_attrs.size(), 2U); + EXPECT_THAT(retry_attrs[0].strings, ContainerEq(ExpectedStringAttrs( {{"rpc.system.name", "http"}, {"gcp.storage.operation", "DeleteObject"}, {"error.type", "UNAVAILABLE"}, {"network.path", "cloudpath"}}))); - EXPECT_THAT(retry_attrs.ints, IsEmpty()); + EXPECT_THAT(retry_attrs[0].ints, IsEmpty()); + EXPECT_THAT( + retry_attrs[1].strings, + ContainerEq(ExpectedStringAttrs({{"rpc.system.name", "grpc"}, + {"gcp.storage.operation", "WriteObject"}, + {"error.type", "SLOW_STREAM"}, + {"network.path", "directpath"}}))); + EXPECT_THAT(retry_attrs[1].ints, IsEmpty()); } } // namespace From 6eae34fb98c68bc18e11be7a5e68ac491a2a1904 Mon Sep 17 00:00:00 2001 From: cpriti-os Date: Tue, 1 Dec 2026 05:36:01 +0000 Subject: [PATCH 3/3] Add missing include and StrictMock sibling counters in retry test --- google/cloud/storage/internal/attempt_metrics.cc | 1 + google/cloud/storage/internal/attempt_metrics_test.cc | 9 +++++++-- 2 files changed, 8 insertions(+), 2 deletions(-) diff --git a/google/cloud/storage/internal/attempt_metrics.cc b/google/cloud/storage/internal/attempt_metrics.cc index 4de167ff2f570..af44a8566612b 100644 --- a/google/cloud/storage/internal/attempt_metrics.cc +++ b/google/cloud/storage/internal/attempt_metrics.cc @@ -21,6 +21,7 @@ #include #include #include +#include #include #include #include diff --git a/google/cloud/storage/internal/attempt_metrics_test.cc b/google/cloud/storage/internal/attempt_metrics_test.cc index df19674187474..63b6950769145 100644 --- a/google/cloud/storage/internal/attempt_metrics_test.cc +++ b/google/cloud/storage/internal/attempt_metrics_test.cc @@ -310,14 +310,19 @@ TEST(AttemptMetricsTest, RecordsGrpcAndHttpRetryAttributes) { retry_attrs.push_back(ExtractAttrs(kv)); }); + auto attempts = + std::make_unique<::testing::StrictMock>>(); + auto errors = + std::make_unique<::testing::StrictMock>>(); + opentelemetry::nostd::shared_ptr meter = std::make_shared(); EXPECT_CALL(*meter, CreateUInt64Counter(Eq("gcp.storage.client.attempts"), _, _)) - .WillOnce(Return(ByMove(CounterPtr(nullptr)))); + .WillOnce(Return(ByMove(CounterPtr(attempts.release())))); EXPECT_CALL(*meter, CreateUInt64Counter(Eq("gcp.storage.client.errors"), _, _)) - .WillOnce(Return(ByMove(CounterPtr(nullptr)))); + .WillOnce(Return(ByMove(CounterPtr(errors.release())))); EXPECT_CALL(*meter, CreateUInt64Counter(Eq("gcp.storage.client.retries"), _, Eq("{retry}"))) .WillOnce(Return(ByMove(CounterPtr(retries.release()))));