diff --git a/google/cloud/storage/internal/attempt_metrics.cc b/google/cloud/storage/internal/attempt_metrics.cc index b2d8b10000e9e..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 @@ -57,13 +58,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 +119,37 @@ void AttemptMetrics::RecordAttemptImpl( } } +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_)}, + {"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(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..a0ffea16add19 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 +/// (`gcp.storage.client.retries`). +struct RetryLabels { + 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,19 @@ class AttemptMetrics { void RecordHttpAttempt(HttpAttemptLabels 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, std::string_view operation, @@ -70,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_; @@ -79,6 +103,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..63b6950769145 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.RecordGrpcRetry({"ReadObject", "UNAVAILABLE", "cloudpath"}, ctx); + no_provider.RecordHttpRetry({"ReadObject", "UNAVAILABLE", "cloudpath"}, ctx); auto provider = std::make_shared(); EXPECT_CALL(*provider, GetMeter) @@ -237,6 +242,8 @@ TEST(AttemptMetricsTest, NullProviderAndNullMeterAreNoOps) { {"ReadObject", "UNAVAILABLE", "UNAVAILABLE", "cloudpath"}, ctx); no_meter.RecordHttpAttempt({"ReadObject", "UNAVAILABLE", 503, "cloudpath"}, ctx); + no_meter.RecordGrpcRetry({"ReadObject", "UNAVAILABLE", "cloudpath"}, ctx); + no_meter.RecordHttpRetry({"ReadObject", "UNAVAILABLE", "cloudpath"}, ctx); } TEST(AttemptMetricsTest, NullAndPartialInstruments) { @@ -244,6 +251,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 +261,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 +270,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 +285,8 @@ TEST(AttemptMetricsTest, NullAndPartialInstruments) { AttemptMetrics none(mp, "storage.googleapis.com"); none.RecordGrpcAttempt( {"ReadObject", "UNAVAILABLE", "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); @@ -286,6 +298,63 @@ TEST(AttemptMetricsTest, NullAndPartialInstruments) { {"ReadObject", "UNAVAILABLE", "UNAVAILABLE", "cloudpath"}, ctx); } +TEST(AttemptMetricsTest, RecordsGrpcAndHttpRetryAttributes) { + std::vector retry_attrs; + auto retries = std::make_unique>(); + EXPECT_CALL(*retries, Add(std::uint64_t{1}, _, _)) + .Times(2) + .WillRepeatedly([&](std::uint64_t, + opentelemetry::common::KeyValueIterable const& kv, + opentelemetry::context::Context const& ctx) { + ExpectTraceMarker(ctx); + 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(attempts.release())))); + EXPECT_CALL(*meter, + CreateUInt64Counter(Eq("gcp.storage.client.errors"), _, _)) + .WillOnce(Return(ByMove(CounterPtr(errors.release())))); + 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"); + 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[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 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