From e7cc5f3b3403bd8b71a97243a4fbc102f4c778d3 Mon Sep 17 00:00:00 2001 From: cpriti-os Date: Thu, 19 Nov 2026 10:18:48 +0000 Subject: [PATCH 1/7] impl(storage): add attempt-level OTel metrics counter recorder --- .../storage/google_cloud_cpp_storage.bzl | 2 + .../storage/google_cloud_cpp_storage.cmake | 3 + .../cloud/storage/internal/attempt_metrics.cc | 117 ++++++++++++++++++ .../cloud/storage/internal/attempt_metrics.h | 68 ++++++++++ .../storage/internal/attempt_metrics_test.cc | 81 ++++++++++++ .../storage/storage_client_unit_tests.bzl | 1 + 6 files changed, 272 insertions(+) create mode 100644 google/cloud/storage/internal/attempt_metrics.cc create mode 100644 google/cloud/storage/internal/attempt_metrics.h create mode 100644 google/cloud/storage/internal/attempt_metrics_test.cc diff --git a/google/cloud/storage/google_cloud_cpp_storage.bzl b/google/cloud/storage/google_cloud_cpp_storage.bzl index a30997d7b3a15..b797636be809c 100644 --- a/google/cloud/storage/google_cloud_cpp_storage.bzl +++ b/google/cloud/storage/google_cloud_cpp_storage.bzl @@ -45,6 +45,7 @@ google_cloud_cpp_storage_hdrs = [ "iam_policy.h", "idempotency_policy.h", "include_folders_as_prefixes.h", + "internal/attempt_metrics.h", "internal/base64.h", "internal/binary_data_as_debug_string.h", "internal/bucket_access_control_parser.h", @@ -168,6 +169,7 @@ google_cloud_cpp_storage_srcs = [ "hmac_key_metadata.cc", "iam_policy.cc", "idempotency_policy.cc", + "internal/attempt_metrics.cc", "internal/base64.cc", "internal/bucket_access_control_parser.cc", "internal/bucket_acl_requests.cc", diff --git a/google/cloud/storage/google_cloud_cpp_storage.cmake b/google/cloud/storage/google_cloud_cpp_storage.cmake index 6801cbc9674fe..8a46af5a815cb 100644 --- a/google/cloud/storage/google_cloud_cpp_storage.cmake +++ b/google/cloud/storage/google_cloud_cpp_storage.cmake @@ -67,6 +67,8 @@ add_library( idempotency_policy.cc idempotency_policy.h include_folders_as_prefixes.h + internal/attempt_metrics.cc + internal/attempt_metrics.h internal/base64.cc internal/base64.h internal/binary_data_as_debug_string.h @@ -429,6 +431,7 @@ if (BUILD_TESTING) hashing_options_test.cc hmac_key_metadata_test.cc idempotency_policy_test.cc + internal/attempt_metrics_test.cc internal/base64_test.cc internal/bucket_acl_requests_test.cc internal/bucket_metadata_cache_test.cc diff --git a/google/cloud/storage/internal/attempt_metrics.cc b/google/cloud/storage/internal/attempt_metrics.cc new file mode 100644 index 0000000000000..9cb51f63399cd --- /dev/null +++ b/google/cloud/storage/internal/attempt_metrics.cc @@ -0,0 +1,117 @@ +// Copyright 2026 Google LLC +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// https://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +#include "google/cloud/storage/internal/attempt_metrics.h" +#include "google/cloud/version.h" +#include +#include +#include +#include +#include +#include +#include + +namespace google { +namespace cloud { +namespace storage_internal { +GOOGLE_CLOUD_CPP_INLINE_NAMESPACE_BEGIN +namespace { + +using Attr = std::pair; + +opentelemetry::nostd::string_view ToNostd(std::string_view sv) { + return opentelemetry::nostd::string_view(sv.data(), sv.size()); +} + +} // namespace + +AttemptMetrics::AttemptMetrics( + opentelemetry::nostd::shared_ptr< + opentelemetry::metrics::MeterProvider> const& provider, + std::string server_address, std::string rpc_system) + : server_address_(std::move(server_address)), + rpc_system_(std::move(rpc_system)) { + if (!provider) return; + meter_ = provider->GetMeter("gl-cpp", google::cloud::version_string()); + if (!meter_) return; + attempts_ = meter_->CreateUInt64Counter( + "gcp.storage.client.attempts", + "Total number of individual backend attempts made during operations.", + "{attempt}"); + errors_ = meter_->CreateUInt64Counter( + "gcp.storage.client.errors", + "Number of failed backend attempts encountered by the client.", + "{error}"); + retries_ = meter_->CreateUInt64Counter( + "gcp.storage.client.retries", + "Number of retry attempts initiated by the client.", "{retry}"); +} + +void AttemptMetrics::RecordAttempt(std::string_view operation, + std::string_view error_type, + std::string_view status_code, + std::string_view network_path) const { + if (!attempts_ && !errors_) return; + auto const ctx = opentelemetry::context::RuntimeContext::GetCurrent(); + auto const ver = google::cloud::version_string(); + auto const status_key = rpc_system_ == "http" ? "http.response.status_code" + : "rpc.response.status_code"; + std::array const attrs{{ + {"gcp.client.version", ToNostd(ver)}, + {"gcp.client.artifact", "google-cloud-cpp"}, + {"gcp.client.service", "storage"}, + {"server.address", ToNostd(server_address_)}, + {"rpc.system.name", ToNostd(rpc_system_)}, + {"gcp.storage.operation", ToNostd(operation)}, + {"error.type", ToNostd(error_type)}, + {"network.path", ToNostd(network_path)}, + {status_key, ToNostd(status_code)}, + }}; + if (attempts_) { + attempts_->Add(std::uint64_t{1}, + opentelemetry::common::MakeKeyValueIterableView(attrs), ctx); + } + if (errors_ && error_type != "OK") { + opentelemetry::nostd::span const err_attrs(attrs.data(), 8); + errors_->Add(std::uint64_t{1}, + opentelemetry::common::MakeKeyValueIterableView(err_attrs), + ctx); + } +} + +void AttemptMetrics::RecordRetry(std::string_view operation, + std::string_view error_type, + std::string_view network_path) const { + if (!retries_) return; + auto const ver = google::cloud::version_string(); + std::array const attrs{{ + {"gcp.client.version", ToNostd(ver)}, + {"gcp.client.artifact", "google-cloud-cpp"}, + {"gcp.client.service", "storage"}, + {"server.address", ToNostd(server_address_)}, + {"rpc.system.name", ToNostd(rpc_system_)}, + {"gcp.storage.operation", ToNostd(operation)}, + {"error.type", ToNostd(error_type)}, + {"network.path", ToNostd(network_path)}, + }}; + retries_->Add(std::uint64_t{1}, + opentelemetry::common::MakeKeyValueIterableView(attrs), + opentelemetry::context::RuntimeContext::GetCurrent()); +} + +GOOGLE_CLOUD_CPP_INLINE_NAMESPACE_END +} // namespace storage_internal +} // namespace cloud +} // namespace google diff --git a/google/cloud/storage/internal/attempt_metrics.h b/google/cloud/storage/internal/attempt_metrics.h new file mode 100644 index 0000000000000..3a99ff6aadb20 --- /dev/null +++ b/google/cloud/storage/internal/attempt_metrics.h @@ -0,0 +1,68 @@ +// Copyright 2026 Google LLC +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// https://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +#ifndef GOOGLE_CLOUD_CPP_GOOGLE_CLOUD_STORAGE_INTERNAL_ATTEMPT_METRICS_H +#define GOOGLE_CLOUD_CPP_GOOGLE_CLOUD_STORAGE_INTERNAL_ATTEMPT_METRICS_H + +#include "google/cloud/storage/version.h" +#include +#include +#include +#include +#include +#include +#include +#include + +namespace google { +namespace cloud { +namespace storage_internal { +GOOGLE_CLOUD_CPP_INLINE_NAMESPACE_BEGIN + +/// Thread-safe recorder for attempt-scoped OpenTelemetry Storage counters +/// (`attempts`, `errors`, and `retries`). No-op when `provider` is null. +class AttemptMetrics { + public: + AttemptMetrics(opentelemetry::nostd::shared_ptr< + opentelemetry::metrics::MeterProvider> const& provider, + std::string server_address, std::string rpc_system); + + void RecordAttempt(std::string_view operation, std::string_view error_type, + std::string_view status_code, + std::string_view network_path) const; + + void RecordRetry(std::string_view operation, std::string_view error_type, + std::string_view network_path) const; + + private: + std::string server_address_; + std::string rpc_system_; + opentelemetry::nostd::shared_ptr meter_; + opentelemetry::nostd::unique_ptr< + opentelemetry::metrics::Counter> + attempts_; + opentelemetry::nostd::unique_ptr< + opentelemetry::metrics::Counter> + errors_; + opentelemetry::nostd::unique_ptr< + opentelemetry::metrics::Counter> + retries_; +}; + +GOOGLE_CLOUD_CPP_INLINE_NAMESPACE_END +} // namespace storage_internal +} // namespace cloud +} // namespace google + +#endif // GOOGLE_CLOUD_CPP_GOOGLE_CLOUD_STORAGE_INTERNAL_ATTEMPT_METRICS_H diff --git a/google/cloud/storage/internal/attempt_metrics_test.cc b/google/cloud/storage/internal/attempt_metrics_test.cc new file mode 100644 index 0000000000000..bacb81ba64e9a --- /dev/null +++ b/google/cloud/storage/internal/attempt_metrics_test.cc @@ -0,0 +1,81 @@ +// Copyright 2026 Google LLC +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// https://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +#include "google/cloud/storage/internal/attempt_metrics.h" +#include "google/cloud/testing_util/mock_opentelemetry_metrics.h" +#include +#include +#include + +namespace google { +namespace cloud { +namespace storage_internal { +GOOGLE_CLOUD_CPP_INLINE_NAMESPACE_BEGIN +namespace { + +using ::google::cloud::testing_util::MockCounter; +using ::google::cloud::testing_util::MockMeter; +using ::google::cloud::testing_util::MockMeterProvider; +using ::testing::_; +using ::testing::ByMove; +using ::testing::Eq; +using ::testing::Return; + +using CounterPtr = opentelemetry::nostd::unique_ptr< + opentelemetry::metrics::Counter>; + +TEST(AttemptMetricsTest, RecordsAttemptsErrorsAndRetries) { + auto attempts = std::make_unique>(); + EXPECT_CALL(*attempts, Add(std::uint64_t{1}, _, _)).Times(2); + auto errors = std::make_unique>(); + EXPECT_CALL(*errors, Add(std::uint64_t{1}, _, _)).Times(1); + auto retries = std::make_unique>(); + EXPECT_CALL(*retries, Add(std::uint64_t{1}, _, _)).Times(1); + + opentelemetry::nostd::shared_ptr meter = + std::make_shared(); + EXPECT_CALL(*meter, CreateUInt64Counter(Eq("gcp.storage.client.attempts"), _, + Eq("{attempt}"))) + .WillOnce(Return(ByMove(CounterPtr(attempts.release())))); + 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(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", "grpc"); + metrics.RecordAttempt("ReadObject", "OK", "OK", "directpath"); + metrics.RecordAttempt("ReadObject", "UNAVAILABLE", "UNAVAILABLE", + "cloudpath"); + metrics.RecordRetry("ReadObject", "UNAVAILABLE", "cloudpath"); +} + +TEST(AttemptMetricsTest, NullProviderIsNoOp) { + AttemptMetrics metrics(nullptr, "storage.googleapis.com", "http"); + metrics.RecordAttempt("ReadObject", "UNAVAILABLE", "503", "cloudpath"); + metrics.RecordRetry("ReadObject", "UNAVAILABLE", "cloudpath"); +} + +} // namespace +GOOGLE_CLOUD_CPP_INLINE_NAMESPACE_END +} // namespace storage_internal +} // namespace cloud +} // namespace google diff --git a/google/cloud/storage/storage_client_unit_tests.bzl b/google/cloud/storage/storage_client_unit_tests.bzl index d0511b00c9bc2..336454cff76f2 100644 --- a/google/cloud/storage/storage_client_unit_tests.bzl +++ b/google/cloud/storage/storage_client_unit_tests.bzl @@ -41,6 +41,7 @@ storage_client_unit_tests = [ "hashing_options_test.cc", "hmac_key_metadata_test.cc", "idempotency_policy_test.cc", + "internal/attempt_metrics_test.cc", "internal/base64_test.cc", "internal/bucket_acl_requests_test.cc", "internal/bucket_metadata_cache_test.cc", From 45f22d055cbf536de044646a0ca5ca2135b2e403 Mon Sep 17 00:00:00 2001 From: cpriti-os Date: Thu, 19 Nov 2026 11:13:39 +0000 Subject: [PATCH 2/7] fix(storage): emit int64 HTTP status code, cache version string, and verify attributes --- .../cloud/storage/internal/attempt_metrics.cc | 58 ++++++++---- .../cloud/storage/internal/attempt_metrics.h | 19 +++- .../storage/internal/attempt_metrics_test.cc | 92 +++++++++++++++++-- 3 files changed, 138 insertions(+), 31 deletions(-) diff --git a/google/cloud/storage/internal/attempt_metrics.cc b/google/cloud/storage/internal/attempt_metrics.cc index 9cb51f63399cd..efd5e3d2eb99c 100644 --- a/google/cloud/storage/internal/attempt_metrics.cc +++ b/google/cloud/storage/internal/attempt_metrics.cc @@ -14,7 +14,6 @@ #include "google/cloud/storage/internal/attempt_metrics.h" #include "google/cloud/version.h" -#include #include #include #include @@ -41,10 +40,11 @@ AttemptMetrics::AttemptMetrics( opentelemetry::nostd::shared_ptr< opentelemetry::metrics::MeterProvider> const& provider, std::string server_address, std::string rpc_system) - : server_address_(std::move(server_address)), + : version_(google::cloud::version_string()), + server_address_(std::move(server_address)), rpc_system_(std::move(rpc_system)) { if (!provider) return; - meter_ = provider->GetMeter("gl-cpp", google::cloud::version_string()); + meter_ = provider->GetMeter("gl-cpp", version_); if (!meter_) return; attempts_ = meter_->CreateUInt64Counter( "gcp.storage.client.attempts", @@ -59,32 +59,50 @@ AttemptMetrics::AttemptMetrics( "Number of retry attempts initiated by the client.", "{retry}"); } -void AttemptMetrics::RecordAttempt(std::string_view operation, - std::string_view error_type, - std::string_view status_code, - std::string_view network_path) const { +void AttemptMetrics::RecordGrpcAttempt(std::string_view operation, + std::string_view error_type, + std::string_view rpc_status_code, + std::string_view network_path) const { + RecordAttemptImpl(operation, error_type, network_path, + "rpc.response.status_code", ToNostd(rpc_status_code)); +} + +void AttemptMetrics::RecordHttpAttempt(std::string_view operation, + std::string_view error_type, + std::int64_t http_status_code, + std::string_view network_path) const { + RecordAttemptImpl(operation, error_type, network_path, + "http.response.status_code", http_status_code); +} + +void AttemptMetrics::RecordAttemptImpl( + std::string_view operation, std::string_view error_type, + std::string_view network_path, char const* status_key, + opentelemetry::common::AttributeValue status_value) const { if (!attempts_ && !errors_) return; - auto const ctx = opentelemetry::context::RuntimeContext::GetCurrent(); - auto const ver = google::cloud::version_string(); - auto const status_key = rpc_system_ == "http" ? "http.response.status_code" - : "rpc.response.status_code"; + opentelemetry::context::Context const ctx = + opentelemetry::context::RuntimeContext::GetCurrent(); + // The status code attribute is placed last so `errors_` can exclude it via a + // prefix span of size `attrs.size() - 1`. std::array const attrs{{ - {"gcp.client.version", ToNostd(ver)}, - {"gcp.client.artifact", "google-cloud-cpp"}, - {"gcp.client.service", "storage"}, + {"gcp.client.version", ToNostd(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(operation)}, {"error.type", ToNostd(error_type)}, {"network.path", ToNostd(network_path)}, - {status_key, ToNostd(status_code)}, + {status_key, status_value}, }}; if (attempts_) { attempts_->Add(std::uint64_t{1}, opentelemetry::common::MakeKeyValueIterableView(attrs), ctx); } if (errors_ && error_type != "OK") { - opentelemetry::nostd::span const err_attrs(attrs.data(), 8); + opentelemetry::nostd::span const err_attrs(attrs.data(), + attrs.size() - 1); errors_->Add(std::uint64_t{1}, opentelemetry::common::MakeKeyValueIterableView(err_attrs), ctx); @@ -95,11 +113,11 @@ void AttemptMetrics::RecordRetry(std::string_view operation, std::string_view error_type, std::string_view network_path) const { if (!retries_) return; - auto const ver = google::cloud::version_string(); std::array const attrs{{ - {"gcp.client.version", ToNostd(ver)}, - {"gcp.client.artifact", "google-cloud-cpp"}, - {"gcp.client.service", "storage"}, + {"gcp.client.version", ToNostd(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(operation)}, diff --git a/google/cloud/storage/internal/attempt_metrics.h b/google/cloud/storage/internal/attempt_metrics.h index 3a99ff6aadb20..17ad54fec1093 100644 --- a/google/cloud/storage/internal/attempt_metrics.h +++ b/google/cloud/storage/internal/attempt_metrics.h @@ -16,6 +16,7 @@ #define GOOGLE_CLOUD_CPP_GOOGLE_CLOUD_STORAGE_INTERNAL_ATTEMPT_METRICS_H #include "google/cloud/storage/version.h" +#include #include #include #include @@ -38,14 +39,26 @@ class AttemptMetrics { opentelemetry::metrics::MeterProvider> const& provider, std::string server_address, std::string rpc_system); - void RecordAttempt(std::string_view operation, std::string_view error_type, - std::string_view status_code, - std::string_view network_path) const; + void RecordGrpcAttempt(std::string_view operation, + std::string_view error_type, + std::string_view rpc_status_code, + std::string_view network_path) const; + + void RecordHttpAttempt(std::string_view operation, + std::string_view error_type, + std::int64_t http_status_code, + std::string_view network_path) const; void RecordRetry(std::string_view operation, std::string_view error_type, std::string_view network_path) const; private: + void RecordAttemptImpl( + std::string_view operation, std::string_view error_type, + std::string_view network_path, char const* status_key, + opentelemetry::common::AttributeValue status_value) const; + + std::string version_; std::string server_address_; std::string rpc_system_; 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 bacb81ba64e9a..1ccb69b177488 100644 --- a/google/cloud/storage/internal/attempt_metrics_test.cc +++ b/google/cloud/storage/internal/attempt_metrics_test.cc @@ -14,9 +14,14 @@ #include "google/cloud/storage/internal/attempt_metrics.h" #include "google/cloud/testing_util/mock_opentelemetry_metrics.h" +#include "google/cloud/version.h" #include #include +#include +#include #include +#include +#include namespace google { namespace cloud { @@ -30,18 +35,68 @@ using ::google::cloud::testing_util::MockMeterProvider; using ::testing::_; using ::testing::ByMove; using ::testing::Eq; +using ::testing::Pair; using ::testing::Return; +using ::testing::UnorderedElementsAre; using CounterPtr = opentelemetry::nostd::unique_ptr< opentelemetry::metrics::Counter>; -TEST(AttemptMetricsTest, RecordsAttemptsErrorsAndRetries) { +struct CapturedAttrs { + std::map strings; + std::map ints; +}; + +CapturedAttrs ExtractAttrs( + opentelemetry::common::KeyValueIterable const& iterable) { + CapturedAttrs out; + iterable.ForEachKeyValue( + [&](opentelemetry::nostd::string_view key, + opentelemetry::common::AttributeValue value) noexcept { + std::string k(key.data(), key.size()); + if (auto const* sv = + opentelemetry::nostd::get_if( + &value)) { + out.strings.emplace(std::move(k), + std::string(sv->data(), sv->size())); + } else if (auto const* i = + opentelemetry::nostd::get_if(&value)) { + out.ints.emplace(std::move(k), *i); + } + return true; + }); + return out; +} + +TEST(AttemptMetricsTest, RecordsGrpcAndHttpAttributes) { + std::vector attempt_attrs; auto attempts = std::make_unique>(); - EXPECT_CALL(*attempts, Add(std::uint64_t{1}, _, _)).Times(2); + EXPECT_CALL(*attempts, Add(std::uint64_t{1}, _, _)) + .Times(3) + .WillRepeatedly([&](std::uint64_t, + opentelemetry::common::KeyValueIterable const& kv, + opentelemetry::context::Context const&) { + attempt_attrs.push_back(ExtractAttrs(kv)); + }); + + std::vector error_attrs; auto errors = std::make_unique>(); - EXPECT_CALL(*errors, Add(std::uint64_t{1}, _, _)).Times(1); + EXPECT_CALL(*errors, Add(std::uint64_t{1}, _, _)) + .Times(2) + .WillRepeatedly([&](std::uint64_t, + opentelemetry::common::KeyValueIterable const& kv, + opentelemetry::context::Context const&) { + error_attrs.push_back(ExtractAttrs(kv)); + }); + + CapturedAttrs retry_attrs; auto retries = std::make_unique>(); - EXPECT_CALL(*retries, Add(std::uint64_t{1}, _, _)).Times(1); + EXPECT_CALL(*retries, Add(std::uint64_t{1}, _, _)) + .WillOnce([&](std::uint64_t, + opentelemetry::common::KeyValueIterable const& kv, + opentelemetry::context::Context const&) { + retry_attrs = ExtractAttrs(kv); + }); opentelemetry::nostd::shared_ptr meter = std::make_shared(); @@ -62,15 +117,36 @@ TEST(AttemptMetricsTest, RecordsAttemptsErrorsAndRetries) { provider)); AttemptMetrics metrics(mp, "storage.googleapis.com", "grpc"); - metrics.RecordAttempt("ReadObject", "OK", "OK", "directpath"); - metrics.RecordAttempt("ReadObject", "UNAVAILABLE", "UNAVAILABLE", - "cloudpath"); + metrics.RecordGrpcAttempt("ReadObject", "OK", "OK", "directpath"); + metrics.RecordGrpcAttempt("ReadObject", "UNAVAILABLE", "UNAVAILABLE", + "cloudpath"); + metrics.RecordHttpAttempt("ReadObject", "UNAVAILABLE", 503, "cloudpath"); metrics.RecordRetry("ReadObject", "UNAVAILABLE", "cloudpath"); + + ASSERT_EQ(attempt_attrs.size(), 3U); + EXPECT_EQ(attempt_attrs[0].strings["rpc.response.status_code"], "OK"); + EXPECT_EQ(attempt_attrs[1].strings["rpc.response.status_code"], + "UNAVAILABLE"); + EXPECT_THAT(attempt_attrs[2].ints, + UnorderedElementsAre(Pair("http.response.status_code", 503))); + ASSERT_EQ(error_attrs.size(), 2U); + EXPECT_EQ(error_attrs[0].strings.count("rpc.response.status_code"), 0U); + EXPECT_EQ(error_attrs[1].ints.count("http.response.status_code"), 0U); + EXPECT_THAT(retry_attrs.strings, + UnorderedElementsAre( + Pair("gcp.client.version", google::cloud::version_string()), + Pair("gcp.client.artifact", "google-cloud-cpp"), + Pair("gcp.client.service", "storage"), + Pair("server.address", "storage.googleapis.com"), + Pair("rpc.system.name", "grpc"), + Pair("gcp.storage.operation", "ReadObject"), + Pair("error.type", "UNAVAILABLE"), + Pair("network.path", "cloudpath"))); } TEST(AttemptMetricsTest, NullProviderIsNoOp) { AttemptMetrics metrics(nullptr, "storage.googleapis.com", "http"); - metrics.RecordAttempt("ReadObject", "UNAVAILABLE", "503", "cloudpath"); + metrics.RecordHttpAttempt("ReadObject", "UNAVAILABLE", 503, "cloudpath"); metrics.RecordRetry("ReadObject", "UNAVAILABLE", "cloudpath"); } From df04463e0296779489574bf145dcfac921e54b79 Mon Sep 17 00:00:00 2001 From: cpriti-os Date: Fri, 20 Nov 2026 07:11:28 +0000 Subject: [PATCH 3/7] test(storage): verify full attempt/error attributes, HTTP OK, and null meter --- .../storage/internal/attempt_metrics_test.cc | 104 ++++++++++++++---- 1 file changed, 85 insertions(+), 19 deletions(-) diff --git a/google/cloud/storage/internal/attempt_metrics_test.cc b/google/cloud/storage/internal/attempt_metrics_test.cc index 1ccb69b177488..55c3822f69a5d 100644 --- a/google/cloud/storage/internal/attempt_metrics_test.cc +++ b/google/cloud/storage/internal/attempt_metrics_test.cc @@ -21,6 +21,7 @@ #include #include #include +#include #include namespace google { @@ -35,6 +36,7 @@ using ::google::cloud::testing_util::MockMeterProvider; using ::testing::_; using ::testing::ByMove; using ::testing::Eq; +using ::testing::IsEmpty; using ::testing::Pair; using ::testing::Return; using ::testing::UnorderedElementsAre; @@ -62,6 +64,8 @@ CapturedAttrs ExtractAttrs( } else if (auto const* i = opentelemetry::nostd::get_if(&value)) { out.ints.emplace(std::move(k), *i); + } else { + ADD_FAILURE() << "Unexpected attribute type for key: " << k; } return true; }); @@ -72,7 +76,7 @@ TEST(AttemptMetricsTest, RecordsGrpcAndHttpAttributes) { std::vector attempt_attrs; auto attempts = std::make_unique>(); EXPECT_CALL(*attempts, Add(std::uint64_t{1}, _, _)) - .Times(3) + .Times(4) .WillRepeatedly([&](std::uint64_t, opentelemetry::common::KeyValueIterable const& kv, opentelemetry::context::Context const&) { @@ -111,7 +115,22 @@ TEST(AttemptMetricsTest, RecordsGrpcAndHttpAttributes) { .WillOnce(Return(ByMove(CounterPtr(retries.release())))); auto provider = std::make_shared(); - EXPECT_CALL(*provider, GetMeter).WillOnce(Return(meter)); + EXPECT_CALL(*provider, GetMeter) +#if OPENTELEMETRY_ABI_VERSION_NO >= 2 + .WillOnce([&](opentelemetry::nostd::string_view scope, + opentelemetry::nostd::string_view scope_version, + opentelemetry::nostd::string_view, + opentelemetry::common::KeyValueIterable const*) { +#else + .WillOnce([&](opentelemetry::nostd::string_view scope, + opentelemetry::nostd::string_view scope_version, + opentelemetry::nostd::string_view) { +#endif + EXPECT_EQ(std::string(scope.data(), scope.size()), "gl-cpp"); + EXPECT_EQ(std::string(scope_version.data(), scope_version.size()), + google::cloud::version_string()); + return meter; + }); opentelemetry::nostd::shared_ptr mp( std::static_pointer_cast( provider)); @@ -120,34 +139,81 @@ TEST(AttemptMetricsTest, RecordsGrpcAndHttpAttributes) { metrics.RecordGrpcAttempt("ReadObject", "OK", "OK", "directpath"); metrics.RecordGrpcAttempt("ReadObject", "UNAVAILABLE", "UNAVAILABLE", "cloudpath"); + metrics.RecordHttpAttempt("ReadObject", "OK", 200, "cloudpath"); metrics.RecordHttpAttempt("ReadObject", "UNAVAILABLE", 503, "cloudpath"); metrics.RecordRetry("ReadObject", "UNAVAILABLE", "cloudpath"); - ASSERT_EQ(attempt_attrs.size(), 3U); - EXPECT_EQ(attempt_attrs[0].strings["rpc.response.status_code"], "OK"); - EXPECT_EQ(attempt_attrs[1].strings["rpc.response.status_code"], - "UNAVAILABLE"); - EXPECT_THAT(attempt_attrs[2].ints, - UnorderedElementsAre(Pair("http.response.status_code", 503))); - ASSERT_EQ(error_attrs.size(), 2U); - EXPECT_EQ(error_attrs[0].strings.count("rpc.response.status_code"), 0U); - EXPECT_EQ(error_attrs[1].ints.count("http.response.status_code"), 0U); - EXPECT_THAT(retry_attrs.strings, + std::string const ver = google::cloud::version_string(); + ASSERT_EQ(attempt_attrs.size(), 4U); + EXPECT_THAT(attempt_attrs[0].strings, UnorderedElementsAre( - Pair("gcp.client.version", google::cloud::version_string()), + Pair("gcp.client.version", ver), Pair("gcp.client.artifact", "google-cloud-cpp"), Pair("gcp.client.service", "storage"), Pair("server.address", "storage.googleapis.com"), Pair("rpc.system.name", "grpc"), Pair("gcp.storage.operation", "ReadObject"), - Pair("error.type", "UNAVAILABLE"), - Pair("network.path", "cloudpath"))); + Pair("error.type", "OK"), Pair("network.path", "directpath"), + Pair("rpc.response.status_code", "OK"))); + EXPECT_THAT(attempt_attrs[0].ints, IsEmpty()); + EXPECT_THAT( + attempt_attrs[3].strings, + UnorderedElementsAre(Pair("gcp.client.version", ver), + Pair("gcp.client.artifact", "google-cloud-cpp"), + Pair("gcp.client.service", "storage"), + Pair("server.address", "storage.googleapis.com"), + Pair("rpc.system.name", "grpc"), + Pair("gcp.storage.operation", "ReadObject"), + Pair("error.type", "UNAVAILABLE"), + Pair("network.path", "cloudpath"))); + EXPECT_THAT(attempt_attrs[3].ints, + UnorderedElementsAre(Pair("http.response.status_code", 503))); + + ASSERT_EQ(error_attrs.size(), 2U); + for (auto const& err : error_attrs) { + EXPECT_THAT( + err.strings, + UnorderedElementsAre(Pair("gcp.client.version", ver), + Pair("gcp.client.artifact", "google-cloud-cpp"), + Pair("gcp.client.service", "storage"), + Pair("server.address", "storage.googleapis.com"), + Pair("rpc.system.name", "grpc"), + Pair("gcp.storage.operation", "ReadObject"), + Pair("error.type", "UNAVAILABLE"), + Pair("network.path", "cloudpath"))); + EXPECT_THAT(err.ints, IsEmpty()); + } + EXPECT_THAT( + retry_attrs.strings, + UnorderedElementsAre(Pair("gcp.client.version", ver), + Pair("gcp.client.artifact", "google-cloud-cpp"), + Pair("gcp.client.service", "storage"), + Pair("server.address", "storage.googleapis.com"), + Pair("rpc.system.name", "grpc"), + Pair("gcp.storage.operation", "ReadObject"), + Pair("error.type", "UNAVAILABLE"), + Pair("network.path", "cloudpath"))); + EXPECT_THAT(retry_attrs.ints, IsEmpty()); } -TEST(AttemptMetricsTest, NullProviderIsNoOp) { - AttemptMetrics metrics(nullptr, "storage.googleapis.com", "http"); - metrics.RecordHttpAttempt("ReadObject", "UNAVAILABLE", 503, "cloudpath"); - metrics.RecordRetry("ReadObject", "UNAVAILABLE", "cloudpath"); +TEST(AttemptMetricsTest, NullProviderAndNullMeterAreNoOps) { + AttemptMetrics no_provider(nullptr, "storage.googleapis.com", "http"); + no_provider.RecordGrpcAttempt("ReadObject", "OK", "OK", "directpath"); + no_provider.RecordHttpAttempt("ReadObject", "UNAVAILABLE", 503, "cloudpath"); + no_provider.RecordRetry("ReadObject", "UNAVAILABLE", "cloudpath"); + + auto provider = std::make_shared(); + EXPECT_CALL(*provider, GetMeter) + .WillOnce(Return( + opentelemetry::nostd::shared_ptr{ + nullptr})); + opentelemetry::nostd::shared_ptr mp( + std::static_pointer_cast( + provider)); + AttemptMetrics no_meter(mp, "storage.googleapis.com", "grpc"); + no_meter.RecordGrpcAttempt("ReadObject", "UNAVAILABLE", "UNAVAILABLE", + "cloudpath"); + no_meter.RecordRetry("ReadObject", "UNAVAILABLE", "cloudpath"); } } // namespace From dcc133efd80edd0f062e84c69bdc47daf8c5e36d Mon Sep 17 00:00:00 2001 From: cpriti-os Date: Tue, 24 Nov 2026 10:05:55 +0000 Subject: [PATCH 4/7] Verify RuntimeContext propagation, distinct attempts, null instruments, and direct includes --- .../cloud/storage/internal/attempt_metrics.cc | 1 + .../storage/internal/attempt_metrics_test.cc | 139 +++++++++++++++--- 2 files changed, 117 insertions(+), 23 deletions(-) diff --git a/google/cloud/storage/internal/attempt_metrics.cc b/google/cloud/storage/internal/attempt_metrics.cc index efd5e3d2eb99c..e8219afba3ea0 100644 --- a/google/cloud/storage/internal/attempt_metrics.cc +++ b/google/cloud/storage/internal/attempt_metrics.cc @@ -15,6 +15,7 @@ #include "google/cloud/storage/internal/attempt_metrics.h" #include "google/cloud/version.h" #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 55c3822f69a5d..4876a2fc63224 100644 --- a/google/cloud/storage/internal/attempt_metrics_test.cc +++ b/google/cloud/storage/internal/attempt_metrics_test.cc @@ -17,6 +17,13 @@ #include "google/cloud/version.h" #include #include +#include +#include +#include +#include +#include +#include +#include #include #include #include @@ -72,6 +79,13 @@ CapturedAttrs ExtractAttrs( return out; } +void ExpectTraceMarker(opentelemetry::context::Context const& ctx) { + opentelemetry::context::ContextValue const val = ctx.GetValue("trace_marker"); + auto const* marker = opentelemetry::nostd::get_if(&val); + ASSERT_NE(marker, nullptr); + EXPECT_EQ(*marker, 42); +} + TEST(AttemptMetricsTest, RecordsGrpcAndHttpAttributes) { std::vector attempt_attrs; auto attempts = std::make_unique>(); @@ -79,7 +93,8 @@ TEST(AttemptMetricsTest, RecordsGrpcAndHttpAttributes) { .Times(4) .WillRepeatedly([&](std::uint64_t, opentelemetry::common::KeyValueIterable const& kv, - opentelemetry::context::Context const&) { + opentelemetry::context::Context const& ctx) { + ExpectTraceMarker(ctx); attempt_attrs.push_back(ExtractAttrs(kv)); }); @@ -89,7 +104,8 @@ TEST(AttemptMetricsTest, RecordsGrpcAndHttpAttributes) { .Times(2) .WillRepeatedly([&](std::uint64_t, opentelemetry::common::KeyValueIterable const& kv, - opentelemetry::context::Context const&) { + opentelemetry::context::Context const& ctx) { + ExpectTraceMarker(ctx); error_attrs.push_back(ExtractAttrs(kv)); }); @@ -98,7 +114,8 @@ TEST(AttemptMetricsTest, RecordsGrpcAndHttpAttributes) { EXPECT_CALL(*retries, Add(std::uint64_t{1}, _, _)) .WillOnce([&](std::uint64_t, opentelemetry::common::KeyValueIterable const& kv, - opentelemetry::context::Context const&) { + opentelemetry::context::Context const& ctx) { + ExpectTraceMarker(ctx); retry_attrs = ExtractAttrs(kv); }); @@ -136,12 +153,16 @@ TEST(AttemptMetricsTest, RecordsGrpcAndHttpAttributes) { provider)); AttemptMetrics metrics(mp, "storage.googleapis.com", "grpc"); + opentelemetry::nostd::unique_ptr const token = + opentelemetry::context::RuntimeContext::Attach( + opentelemetry::context::RuntimeContext::GetCurrent().SetValue( + "trace_marker", std::int64_t{42})); metrics.RecordGrpcAttempt("ReadObject", "OK", "OK", "directpath"); - metrics.RecordGrpcAttempt("ReadObject", "UNAVAILABLE", "UNAVAILABLE", - "cloudpath"); - metrics.RecordHttpAttempt("ReadObject", "OK", 200, "cloudpath"); - metrics.RecordHttpAttempt("ReadObject", "UNAVAILABLE", 503, "cloudpath"); - metrics.RecordRetry("ReadObject", "UNAVAILABLE", "cloudpath"); + metrics.RecordGrpcAttempt("WriteObject", "DEADLINE_EXCEEDED", + "DEADLINE_EXCEEDED", "directpath"); + metrics.RecordHttpAttempt("GetObjectMetadata", "OK", 200, "cloudpath"); + metrics.RecordHttpAttempt("DeleteObject", "UNAVAILABLE", 503, "cloudpath"); + metrics.RecordRetry("DeleteObject", "UNAVAILABLE", "cloudpath"); std::string const ver = google::cloud::version_string(); ASSERT_EQ(attempt_attrs.size(), 4U); @@ -156,6 +177,29 @@ TEST(AttemptMetricsTest, RecordsGrpcAndHttpAttributes) { Pair("error.type", "OK"), Pair("network.path", "directpath"), Pair("rpc.response.status_code", "OK"))); EXPECT_THAT(attempt_attrs[0].ints, IsEmpty()); + EXPECT_THAT(attempt_attrs[1].strings, + UnorderedElementsAre( + Pair("gcp.client.version", ver), + Pair("gcp.client.artifact", "google-cloud-cpp"), + Pair("gcp.client.service", "storage"), + Pair("server.address", "storage.googleapis.com"), + Pair("rpc.system.name", "grpc"), + Pair("gcp.storage.operation", "WriteObject"), + Pair("error.type", "DEADLINE_EXCEEDED"), + Pair("network.path", "directpath"), + Pair("rpc.response.status_code", "DEADLINE_EXCEEDED"))); + EXPECT_THAT(attempt_attrs[1].ints, IsEmpty()); + EXPECT_THAT(attempt_attrs[2].strings, + UnorderedElementsAre( + Pair("gcp.client.version", ver), + Pair("gcp.client.artifact", "google-cloud-cpp"), + Pair("gcp.client.service", "storage"), + Pair("server.address", "storage.googleapis.com"), + Pair("rpc.system.name", "grpc"), + Pair("gcp.storage.operation", "GetObjectMetadata"), + Pair("error.type", "OK"), Pair("network.path", "cloudpath"))); + EXPECT_THAT(attempt_attrs[2].ints, + UnorderedElementsAre(Pair("http.response.status_code", 200))); EXPECT_THAT( attempt_attrs[3].strings, UnorderedElementsAre(Pair("gcp.client.version", ver), @@ -163,26 +207,35 @@ TEST(AttemptMetricsTest, RecordsGrpcAndHttpAttributes) { Pair("gcp.client.service", "storage"), Pair("server.address", "storage.googleapis.com"), Pair("rpc.system.name", "grpc"), - Pair("gcp.storage.operation", "ReadObject"), + Pair("gcp.storage.operation", "DeleteObject"), Pair("error.type", "UNAVAILABLE"), Pair("network.path", "cloudpath"))); EXPECT_THAT(attempt_attrs[3].ints, UnorderedElementsAre(Pair("http.response.status_code", 503))); ASSERT_EQ(error_attrs.size(), 2U); - for (auto const& err : error_attrs) { - EXPECT_THAT( - err.strings, - UnorderedElementsAre(Pair("gcp.client.version", ver), - Pair("gcp.client.artifact", "google-cloud-cpp"), - Pair("gcp.client.service", "storage"), - Pair("server.address", "storage.googleapis.com"), - Pair("rpc.system.name", "grpc"), - Pair("gcp.storage.operation", "ReadObject"), - Pair("error.type", "UNAVAILABLE"), - Pair("network.path", "cloudpath"))); - EXPECT_THAT(err.ints, IsEmpty()); - } + EXPECT_THAT( + error_attrs[0].strings, + UnorderedElementsAre(Pair("gcp.client.version", ver), + Pair("gcp.client.artifact", "google-cloud-cpp"), + Pair("gcp.client.service", "storage"), + Pair("server.address", "storage.googleapis.com"), + Pair("rpc.system.name", "grpc"), + Pair("gcp.storage.operation", "WriteObject"), + Pair("error.type", "DEADLINE_EXCEEDED"), + Pair("network.path", "directpath"))); + EXPECT_THAT(error_attrs[0].ints, IsEmpty()); + EXPECT_THAT( + error_attrs[1].strings, + UnorderedElementsAre(Pair("gcp.client.version", ver), + Pair("gcp.client.artifact", "google-cloud-cpp"), + Pair("gcp.client.service", "storage"), + Pair("server.address", "storage.googleapis.com"), + Pair("rpc.system.name", "grpc"), + Pair("gcp.storage.operation", "DeleteObject"), + Pair("error.type", "UNAVAILABLE"), + Pair("network.path", "cloudpath"))); + EXPECT_THAT(error_attrs[1].ints, IsEmpty()); EXPECT_THAT( retry_attrs.strings, UnorderedElementsAre(Pair("gcp.client.version", ver), @@ -190,7 +243,7 @@ TEST(AttemptMetricsTest, RecordsGrpcAndHttpAttributes) { Pair("gcp.client.service", "storage"), Pair("server.address", "storage.googleapis.com"), Pair("rpc.system.name", "grpc"), - Pair("gcp.storage.operation", "ReadObject"), + Pair("gcp.storage.operation", "DeleteObject"), Pair("error.type", "UNAVAILABLE"), Pair("network.path", "cloudpath"))); EXPECT_THAT(retry_attrs.ints, IsEmpty()); @@ -213,9 +266,49 @@ TEST(AttemptMetricsTest, NullProviderAndNullMeterAreNoOps) { AttemptMetrics no_meter(mp, "storage.googleapis.com", "grpc"); no_meter.RecordGrpcAttempt("ReadObject", "UNAVAILABLE", "UNAVAILABLE", "cloudpath"); + no_meter.RecordHttpAttempt("ReadObject", "UNAVAILABLE", 503, "cloudpath"); no_meter.RecordRetry("ReadObject", "UNAVAILABLE", "cloudpath"); } +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)))); + + auto errors = std::make_unique>(); + EXPECT_CALL(*errors, Add(std::uint64_t{1}, _, _)).Times(1); + auto retries = std::make_unique>(); + EXPECT_CALL(*retries, Add(std::uint64_t{1}, _, _)).Times(1); + opentelemetry::nostd::shared_ptr partial_meter = + std::make_shared(); + EXPECT_CALL(*partial_meter, CreateUInt64Counter) + .WillOnce(Return(ByMove(CounterPtr(nullptr)))) + .WillOnce(Return(ByMove(CounterPtr(errors.release())))) + .WillOnce(Return(ByMove(CounterPtr(retries.release())))); + + auto provider = std::make_shared(); + EXPECT_CALL(*provider, GetMeter) + .WillOnce(Return(empty_meter)) + .WillOnce(Return(partial_meter)); + opentelemetry::nostd::shared_ptr mp( + std::static_pointer_cast( + provider)); + + AttemptMetrics none(mp, "storage.googleapis.com", "grpc"); + none.RecordGrpcAttempt("ReadObject", "UNAVAILABLE", "UNAVAILABLE", + "cloudpath"); + none.RecordRetry("ReadObject", "UNAVAILABLE", "cloudpath"); + + AttemptMetrics partial(mp, "storage.googleapis.com", "grpc"); + partial.RecordGrpcAttempt("ReadObject", "OK", "OK", "directpath"); + partial.RecordGrpcAttempt("ReadObject", "UNAVAILABLE", "UNAVAILABLE", + "cloudpath"); + partial.RecordRetry("ReadObject", "UNAVAILABLE", "cloudpath"); +} + } // namespace GOOGLE_CLOUD_CPP_INLINE_NAMESPACE_END } // namespace storage_internal From 5b388a966ed1d63d9908ae9a68229cc8edfc97df Mon Sep 17 00:00:00 2001 From: cpriti-os Date: Tue, 24 Nov 2026 13:19:25 +0000 Subject: [PATCH 5/7] Use label structs, explicit Context, bare semver, and per-transport rpc.system.name --- .../cloud/storage/internal/attempt_metrics.cc | 73 +++++++------- .../cloud/storage/internal/attempt_metrics.h | 56 +++++++---- .../storage/internal/attempt_metrics_test.cc | 99 ++++++++++++------- 3 files changed, 138 insertions(+), 90 deletions(-) diff --git a/google/cloud/storage/internal/attempt_metrics.cc b/google/cloud/storage/internal/attempt_metrics.cc index e8219afba3ea0..a6e92d14f3194 100644 --- a/google/cloud/storage/internal/attempt_metrics.cc +++ b/google/cloud/storage/internal/attempt_metrics.cc @@ -14,9 +14,9 @@ #include "google/cloud/storage/internal/attempt_metrics.h" #include "google/cloud/version.h" +#include "absl/strings/str_cat.h" #include #include -#include #include #include #include @@ -35,17 +35,22 @@ opentelemetry::nostd::string_view ToNostd(std::string_view sv) { return opentelemetry::nostd::string_view(sv.data(), sv.size()); } +std::string SemanticVersionString() { + return absl::StrCat(google::cloud::version_major(), ".", + google::cloud::version_minor(), ".", + google::cloud::version_patch()); +} + } // namespace AttemptMetrics::AttemptMetrics( opentelemetry::nostd::shared_ptr< opentelemetry::metrics::MeterProvider> const& provider, - std::string server_address, std::string rpc_system) - : version_(google::cloud::version_string()), - server_address_(std::move(server_address)), - rpc_system_(std::move(rpc_system)) { + std::string server_address) + : client_version_(SemanticVersionString()), + server_address_(std::move(server_address)) { if (!provider) return; - meter_ = provider->GetMeter("gl-cpp", version_); + meter_ = provider->GetMeter("gl-cpp", google::cloud::version_string()); if (!meter_) return; attempts_ = meter_->CreateUInt64Counter( "gcp.storage.client.attempts", @@ -60,38 +65,37 @@ AttemptMetrics::AttemptMetrics( "Number of retry attempts initiated by the client.", "{retry}"); } -void AttemptMetrics::RecordGrpcAttempt(std::string_view operation, - std::string_view error_type, - std::string_view rpc_status_code, - std::string_view network_path) const { - RecordAttemptImpl(operation, error_type, network_path, - "rpc.response.status_code", ToNostd(rpc_status_code)); +void AttemptMetrics::RecordGrpcAttempt( + GrpcAttemptLabels const& labels, + opentelemetry::context::Context const& ctx) const { + RecordAttemptImpl("grpc", labels.operation, labels.error_type, + labels.network_path, "rpc.response.status_code", + ToNostd(labels.rpc_status_code), ctx); } -void AttemptMetrics::RecordHttpAttempt(std::string_view operation, - std::string_view error_type, - std::int64_t http_status_code, - std::string_view network_path) const { - RecordAttemptImpl(operation, error_type, network_path, - "http.response.status_code", http_status_code); +void AttemptMetrics::RecordHttpAttempt( + HttpAttemptLabels const& labels, + opentelemetry::context::Context const& ctx) const { + RecordAttemptImpl("http", labels.operation, labels.error_type, + labels.network_path, "http.response.status_code", + labels.http_status_code, ctx); } void AttemptMetrics::RecordAttemptImpl( - std::string_view operation, std::string_view error_type, - std::string_view network_path, char const* status_key, - opentelemetry::common::AttributeValue status_value) const { + std::string_view rpc_system, std::string_view operation, + std::string_view error_type, std::string_view network_path, + char const* status_key, opentelemetry::common::AttributeValue status_value, + opentelemetry::context::Context const& ctx) const { if (!attempts_ && !errors_) return; - opentelemetry::context::Context const ctx = - opentelemetry::context::RuntimeContext::GetCurrent(); // The status code attribute is placed last so `errors_` can exclude it via a // prefix span of size `attrs.size() - 1`. std::array const attrs{{ - {"gcp.client.version", ToNostd(version_)}, + {"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_)}, + {"rpc.system.name", ToNostd(rpc_system)}, {"gcp.storage.operation", ToNostd(operation)}, {"error.type", ToNostd(error_type)}, {"network.path", ToNostd(network_path)}, @@ -110,24 +114,23 @@ void AttemptMetrics::RecordAttemptImpl( } } -void AttemptMetrics::RecordRetry(std::string_view operation, - std::string_view error_type, - std::string_view network_path) const { +void AttemptMetrics::RecordRetry( + RetryLabels const& labels, + opentelemetry::context::Context const& ctx) const { if (!retries_) return; std::array const attrs{{ - {"gcp.client.version", ToNostd(version_)}, + {"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(operation)}, - {"error.type", ToNostd(error_type)}, - {"network.path", ToNostd(network_path)}, + {"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), - opentelemetry::context::RuntimeContext::GetCurrent()); + opentelemetry::common::MakeKeyValueIterableView(attrs), ctx); } GOOGLE_CLOUD_CPP_INLINE_NAMESPACE_END diff --git a/google/cloud/storage/internal/attempt_metrics.h b/google/cloud/storage/internal/attempt_metrics.h index 17ad54fec1093..850b72a13697a 100644 --- a/google/cloud/storage/internal/attempt_metrics.h +++ b/google/cloud/storage/internal/attempt_metrics.h @@ -17,6 +17,7 @@ #include "google/cloud/storage/version.h" #include +#include #include #include #include @@ -31,36 +32,57 @@ namespace cloud { namespace storage_internal { GOOGLE_CLOUD_CPP_INLINE_NAMESPACE_BEGIN +/// Labels for recording a single gRPC backend attempt. +struct GrpcAttemptLabels { + std::string_view operation; + std::string_view error_type; + std::string_view rpc_status_code; + std::string_view network_path; +}; + +/// Labels for recording a single HTTP backend attempt. +struct HttpAttemptLabels { + std::string_view operation; + std::string_view error_type; + std::int64_t http_status_code = 0; + 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`, `errors`, and `retries`). No-op when `provider` is null. class AttemptMetrics { public: AttemptMetrics(opentelemetry::nostd::shared_ptr< opentelemetry::metrics::MeterProvider> const& provider, - std::string server_address, std::string rpc_system); + std::string server_address); - void RecordGrpcAttempt(std::string_view operation, - std::string_view error_type, - std::string_view rpc_status_code, - std::string_view network_path) const; + void RecordGrpcAttempt(GrpcAttemptLabels const& labels, + opentelemetry::context::Context const& ctx) const; - void RecordHttpAttempt(std::string_view operation, - std::string_view error_type, - std::int64_t http_status_code, - std::string_view network_path) const; + void RecordHttpAttempt(HttpAttemptLabels const& labels, + opentelemetry::context::Context const& ctx) const; - void RecordRetry(std::string_view operation, std::string_view error_type, - std::string_view network_path) const; + void RecordRetry(RetryLabels const& labels, + opentelemetry::context::Context const& ctx) const; private: - void RecordAttemptImpl( - std::string_view operation, std::string_view error_type, - std::string_view network_path, char const* status_key, - opentelemetry::common::AttributeValue status_value) const; + void RecordAttemptImpl(std::string_view rpc_system, + std::string_view operation, + std::string_view error_type, + std::string_view network_path, char const* status_key, + opentelemetry::common::AttributeValue status_value, + opentelemetry::context::Context const& ctx) const; - std::string version_; + std::string client_version_; std::string server_address_; - std::string rpc_system_; opentelemetry::nostd::shared_ptr meter_; opentelemetry::nostd::unique_ptr< opentelemetry::metrics::Counter> diff --git a/google/cloud/storage/internal/attempt_metrics_test.cc b/google/cloud/storage/internal/attempt_metrics_test.cc index 4876a2fc63224..2920ec97932b0 100644 --- a/google/cloud/storage/internal/attempt_metrics_test.cc +++ b/google/cloud/storage/internal/attempt_metrics_test.cc @@ -15,12 +15,12 @@ #include "google/cloud/storage/internal/attempt_metrics.h" #include "google/cloud/testing_util/mock_opentelemetry_metrics.h" #include "google/cloud/version.h" +#include "absl/strings/str_cat.h" #include #include #include #include #include -#include #include #include #include @@ -152,19 +152,22 @@ TEST(AttemptMetricsTest, RecordsGrpcAndHttpAttributes) { std::static_pointer_cast( provider)); - AttemptMetrics metrics(mp, "storage.googleapis.com", "grpc"); - opentelemetry::nostd::unique_ptr const token = - opentelemetry::context::RuntimeContext::Attach( - opentelemetry::context::RuntimeContext::GetCurrent().SetValue( - "trace_marker", std::int64_t{42})); - metrics.RecordGrpcAttempt("ReadObject", "OK", "OK", "directpath"); - metrics.RecordGrpcAttempt("WriteObject", "DEADLINE_EXCEEDED", - "DEADLINE_EXCEEDED", "directpath"); - metrics.RecordHttpAttempt("GetObjectMetadata", "OK", 200, "cloudpath"); - metrics.RecordHttpAttempt("DeleteObject", "UNAVAILABLE", 503, "cloudpath"); - metrics.RecordRetry("DeleteObject", "UNAVAILABLE", "cloudpath"); + AttemptMetrics metrics(mp, "storage.googleapis.com"); + opentelemetry::context::Context const ctx = + opentelemetry::context::Context("trace_marker", std::int64_t{42}); + metrics.RecordGrpcAttempt({"ReadObject", "OK", "OK", "directpath"}, ctx); + metrics.RecordGrpcAttempt( + {"WriteObject", "DEADLINE_EXCEEDED", "DEADLINE_EXCEEDED", "directpath"}, + ctx); + metrics.RecordHttpAttempt({"GetObjectMetadata", "OK", 200, "cloudpath"}, ctx); + metrics.RecordHttpAttempt({"DeleteObject", "UNAVAILABLE", 503, "cloudpath"}, + ctx); + metrics.RecordRetry({"http", "DeleteObject", "UNAVAILABLE", "cloudpath"}, + ctx); - std::string const ver = google::cloud::version_string(); + std::string const ver = absl::StrCat(google::cloud::version_major(), ".", + google::cloud::version_minor(), ".", + google::cloud::version_patch()); ASSERT_EQ(attempt_attrs.size(), 4U); EXPECT_THAT(attempt_attrs[0].strings, UnorderedElementsAre( @@ -195,7 +198,7 @@ TEST(AttemptMetricsTest, RecordsGrpcAndHttpAttributes) { Pair("gcp.client.artifact", "google-cloud-cpp"), Pair("gcp.client.service", "storage"), Pair("server.address", "storage.googleapis.com"), - Pair("rpc.system.name", "grpc"), + Pair("rpc.system.name", "http"), Pair("gcp.storage.operation", "GetObjectMetadata"), Pair("error.type", "OK"), Pair("network.path", "cloudpath"))); EXPECT_THAT(attempt_attrs[2].ints, @@ -206,7 +209,7 @@ TEST(AttemptMetricsTest, RecordsGrpcAndHttpAttributes) { Pair("gcp.client.artifact", "google-cloud-cpp"), Pair("gcp.client.service", "storage"), Pair("server.address", "storage.googleapis.com"), - Pair("rpc.system.name", "grpc"), + Pair("rpc.system.name", "http"), Pair("gcp.storage.operation", "DeleteObject"), Pair("error.type", "UNAVAILABLE"), Pair("network.path", "cloudpath"))); @@ -231,7 +234,7 @@ TEST(AttemptMetricsTest, RecordsGrpcAndHttpAttributes) { Pair("gcp.client.artifact", "google-cloud-cpp"), Pair("gcp.client.service", "storage"), Pair("server.address", "storage.googleapis.com"), - Pair("rpc.system.name", "grpc"), + Pair("rpc.system.name", "http"), Pair("gcp.storage.operation", "DeleteObject"), Pair("error.type", "UNAVAILABLE"), Pair("network.path", "cloudpath"))); @@ -242,7 +245,7 @@ TEST(AttemptMetricsTest, RecordsGrpcAndHttpAttributes) { Pair("gcp.client.artifact", "google-cloud-cpp"), Pair("gcp.client.service", "storage"), Pair("server.address", "storage.googleapis.com"), - Pair("rpc.system.name", "grpc"), + Pair("rpc.system.name", "http"), Pair("gcp.storage.operation", "DeleteObject"), Pair("error.type", "UNAVAILABLE"), Pair("network.path", "cloudpath"))); @@ -250,10 +253,13 @@ TEST(AttemptMetricsTest, RecordsGrpcAndHttpAttributes) { } TEST(AttemptMetricsTest, NullProviderAndNullMeterAreNoOps) { - AttemptMetrics no_provider(nullptr, "storage.googleapis.com", "http"); - no_provider.RecordGrpcAttempt("ReadObject", "OK", "OK", "directpath"); - no_provider.RecordHttpAttempt("ReadObject", "UNAVAILABLE", 503, "cloudpath"); - no_provider.RecordRetry("ReadObject", "UNAVAILABLE", "cloudpath"); + opentelemetry::context::Context const ctx{}; + AttemptMetrics no_provider(nullptr, "storage.googleapis.com"); + no_provider.RecordGrpcAttempt({"ReadObject", "OK", "OK", "directpath"}, ctx); + no_provider.RecordHttpAttempt({"ReadObject", "UNAVAILABLE", 503, "cloudpath"}, + ctx); + no_provider.RecordRetry({"http", "ReadObject", "UNAVAILABLE", "cloudpath"}, + ctx); auto provider = std::make_shared(); EXPECT_CALL(*provider, GetMeter) @@ -263,14 +269,16 @@ TEST(AttemptMetricsTest, NullProviderAndNullMeterAreNoOps) { opentelemetry::nostd::shared_ptr mp( std::static_pointer_cast( provider)); - AttemptMetrics no_meter(mp, "storage.googleapis.com", "grpc"); - no_meter.RecordGrpcAttempt("ReadObject", "UNAVAILABLE", "UNAVAILABLE", - "cloudpath"); - no_meter.RecordHttpAttempt("ReadObject", "UNAVAILABLE", 503, "cloudpath"); - no_meter.RecordRetry("ReadObject", "UNAVAILABLE", "cloudpath"); + AttemptMetrics no_meter(mp, "storage.googleapis.com"); + no_meter.RecordGrpcAttempt( + {"ReadObject", "UNAVAILABLE", "UNAVAILABLE", "cloudpath"}, ctx); + no_meter.RecordHttpAttempt({"ReadObject", "UNAVAILABLE", 503, "cloudpath"}, + ctx); + no_meter.RecordRetry({"grpc", "ReadObject", "UNAVAILABLE", "cloudpath"}, ctx); } TEST(AttemptMetricsTest, NullAndPartialInstruments) { + opentelemetry::context::Context const ctx{}; opentelemetry::nostd::shared_ptr empty_meter = std::make_shared(); EXPECT_CALL(*empty_meter, CreateUInt64Counter) @@ -282,31 +290,46 @@ TEST(AttemptMetricsTest, NullAndPartialInstruments) { EXPECT_CALL(*errors, Add(std::uint64_t{1}, _, _)).Times(1); auto retries = std::make_unique>(); EXPECT_CALL(*retries, Add(std::uint64_t{1}, _, _)).Times(1); - opentelemetry::nostd::shared_ptr partial_meter = + opentelemetry::nostd::shared_ptr no_attempts_meter = std::make_shared(); - EXPECT_CALL(*partial_meter, CreateUInt64Counter) + EXPECT_CALL(*no_attempts_meter, CreateUInt64Counter) .WillOnce(Return(ByMove(CounterPtr(nullptr)))) .WillOnce(Return(ByMove(CounterPtr(errors.release())))) .WillOnce(Return(ByMove(CounterPtr(retries.release())))); + auto attempts = std::make_unique>(); + EXPECT_CALL(*attempts, Add(std::uint64_t{1}, _, _)).Times(1); + opentelemetry::nostd::shared_ptr no_errors_meter = + 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(); EXPECT_CALL(*provider, GetMeter) .WillOnce(Return(empty_meter)) - .WillOnce(Return(partial_meter)); + .WillOnce(Return(no_attempts_meter)) + .WillOnce(Return(no_errors_meter)); opentelemetry::nostd::shared_ptr mp( std::static_pointer_cast( provider)); - AttemptMetrics none(mp, "storage.googleapis.com", "grpc"); - none.RecordGrpcAttempt("ReadObject", "UNAVAILABLE", "UNAVAILABLE", - "cloudpath"); - none.RecordRetry("ReadObject", "UNAVAILABLE", "cloudpath"); + 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); + no_attempts.RecordGrpcAttempt( + {"ReadObject", "UNAVAILABLE", "UNAVAILABLE", "cloudpath"}, ctx); + no_attempts.RecordRetry({"grpc", "ReadObject", "UNAVAILABLE", "cloudpath"}, + ctx); - AttemptMetrics partial(mp, "storage.googleapis.com", "grpc"); - partial.RecordGrpcAttempt("ReadObject", "OK", "OK", "directpath"); - partial.RecordGrpcAttempt("ReadObject", "UNAVAILABLE", "UNAVAILABLE", - "cloudpath"); - partial.RecordRetry("ReadObject", "UNAVAILABLE", "cloudpath"); + AttemptMetrics no_errors(mp, "storage.googleapis.com"); + no_errors.RecordGrpcAttempt( + {"ReadObject", "UNAVAILABLE", "UNAVAILABLE", "cloudpath"}, ctx); } } // namespace From 42c23ccd9a5ca1181abce14073d715e72bd20664 Mon Sep 17 00:00:00 2001 From: cpriti-os Date: Tue, 24 Nov 2026 15:11:35 +0000 Subject: [PATCH 6/7] Use std::to_string for semver and add direct shared_ptr include --- google/cloud/storage/internal/attempt_metrics.cc | 8 ++++---- google/cloud/storage/internal/attempt_metrics_test.cc | 8 ++++---- 2 files changed, 8 insertions(+), 8 deletions(-) diff --git a/google/cloud/storage/internal/attempt_metrics.cc b/google/cloud/storage/internal/attempt_metrics.cc index a6e92d14f3194..686372af560ed 100644 --- a/google/cloud/storage/internal/attempt_metrics.cc +++ b/google/cloud/storage/internal/attempt_metrics.cc @@ -14,12 +14,12 @@ #include "google/cloud/storage/internal/attempt_metrics.h" #include "google/cloud/version.h" -#include "absl/strings/str_cat.h" #include #include #include #include #include +#include #include namespace google { @@ -36,9 +36,9 @@ opentelemetry::nostd::string_view ToNostd(std::string_view sv) { } std::string SemanticVersionString() { - return absl::StrCat(google::cloud::version_major(), ".", - google::cloud::version_minor(), ".", - google::cloud::version_patch()); + return std::to_string(google::cloud::version_major()) + "." + + std::to_string(google::cloud::version_minor()) + "." + + std::to_string(google::cloud::version_patch()); } } // namespace diff --git a/google/cloud/storage/internal/attempt_metrics_test.cc b/google/cloud/storage/internal/attempt_metrics_test.cc index 2920ec97932b0..537d1bfa40ccb 100644 --- a/google/cloud/storage/internal/attempt_metrics_test.cc +++ b/google/cloud/storage/internal/attempt_metrics_test.cc @@ -15,12 +15,12 @@ #include "google/cloud/storage/internal/attempt_metrics.h" #include "google/cloud/testing_util/mock_opentelemetry_metrics.h" #include "google/cloud/version.h" -#include "absl/strings/str_cat.h" #include #include #include #include #include +#include #include #include #include @@ -165,9 +165,9 @@ TEST(AttemptMetricsTest, RecordsGrpcAndHttpAttributes) { metrics.RecordRetry({"http", "DeleteObject", "UNAVAILABLE", "cloudpath"}, ctx); - std::string const ver = absl::StrCat(google::cloud::version_major(), ".", - google::cloud::version_minor(), ".", - google::cloud::version_patch()); + std::string const ver = std::to_string(google::cloud::version_major()) + "." + + std::to_string(google::cloud::version_minor()) + "." + + std::to_string(google::cloud::version_patch()); ASSERT_EQ(attempt_attrs.size(), 4U); EXPECT_THAT(attempt_attrs[0].strings, UnorderedElementsAre( From db02835bc312632fe1f7fdc16c9f111f986aa95d Mon Sep 17 00:00:00 2001 From: cpriti-os Date: Tue, 24 Nov 2026 15:43:12 +0000 Subject: [PATCH 7/7] Split RecordRetry out to follow-up PR and compact test expectations --- .../cloud/storage/internal/attempt_metrics.cc | 22 --- .../cloud/storage/internal/attempt_metrics.h | 16 +- .../storage/internal/attempt_metrics_test.cc | 149 ++++++------------ 3 files changed, 53 insertions(+), 134 deletions(-) diff --git a/google/cloud/storage/internal/attempt_metrics.cc b/google/cloud/storage/internal/attempt_metrics.cc index 686372af560ed..6099851d5d058 100644 --- a/google/cloud/storage/internal/attempt_metrics.cc +++ b/google/cloud/storage/internal/attempt_metrics.cc @@ -60,9 +60,6 @@ AttemptMetrics::AttemptMetrics( "gcp.storage.client.errors", "Number of failed backend attempts encountered by the client.", "{error}"); - retries_ = meter_->CreateUInt64Counter( - "gcp.storage.client.retries", - "Number of retry attempts initiated by the client.", "{retry}"); } void AttemptMetrics::RecordGrpcAttempt( @@ -114,25 +111,6 @@ 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 850b72a13697a..9b1e44dc8c23f 100644 --- a/google/cloud/storage/internal/attempt_metrics.h +++ b/google/cloud/storage/internal/attempt_metrics.h @@ -48,16 +48,8 @@ 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`, `errors`, and `retries`). No-op when `provider` is null. +/// (`attempts` and `errors`). No-op when `provider` is null. class AttemptMetrics { public: AttemptMetrics(opentelemetry::nostd::shared_ptr< @@ -70,9 +62,6 @@ 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, @@ -90,9 +79,6 @@ 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 537d1bfa40ccb..fc3b93ee32044 100644 --- a/google/cloud/storage/internal/attempt_metrics_test.cc +++ b/google/cloud/storage/internal/attempt_metrics_test.cc @@ -25,6 +25,7 @@ #include #include #include +#include #include #include #include @@ -42,6 +43,7 @@ using ::google::cloud::testing_util::MockMeter; using ::google::cloud::testing_util::MockMeterProvider; using ::testing::_; using ::testing::ByMove; +using ::testing::ContainerEq; using ::testing::Eq; using ::testing::IsEmpty; using ::testing::Pair; @@ -86,6 +88,21 @@ void ExpectTraceMarker(opentelemetry::context::Context const& ctx) { EXPECT_EQ(*marker, 42); } +std::map ExpectedStringAttrs( + std::initializer_list> extra) { + std::map attrs{ + {"gcp.client.version", + std::to_string(google::cloud::version_major()) + "." + + std::to_string(google::cloud::version_minor()) + "." + + std::to_string(google::cloud::version_patch())}, + {"gcp.client.artifact", "google-cloud-cpp"}, + {"gcp.client.service", "storage"}, + {"server.address", "storage.googleapis.com"}, + }; + attrs.insert(extra.begin(), extra.end()); + return attrs; +} + TEST(AttemptMetricsTest, RecordsGrpcAndHttpAttributes) { std::vector attempt_attrs; auto attempts = std::make_unique>(); @@ -109,16 +126,6 @@ TEST(AttemptMetricsTest, RecordsGrpcAndHttpAttributes) { error_attrs.push_back(ExtractAttrs(kv)); }); - 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"), _, @@ -127,9 +134,6 @@ 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(retries.release())))); auto provider = std::make_shared(); EXPECT_CALL(*provider, GetMeter) @@ -162,94 +166,56 @@ TEST(AttemptMetricsTest, RecordsGrpcAndHttpAttributes) { metrics.RecordHttpAttempt({"GetObjectMetadata", "OK", 200, "cloudpath"}, ctx); metrics.RecordHttpAttempt({"DeleteObject", "UNAVAILABLE", 503, "cloudpath"}, ctx); - metrics.RecordRetry({"http", "DeleteObject", "UNAVAILABLE", "cloudpath"}, - ctx); - std::string const ver = std::to_string(google::cloud::version_major()) + "." + - std::to_string(google::cloud::version_minor()) + "." + - std::to_string(google::cloud::version_patch()); ASSERT_EQ(attempt_attrs.size(), 4U); - EXPECT_THAT(attempt_attrs[0].strings, - UnorderedElementsAre( - Pair("gcp.client.version", ver), - Pair("gcp.client.artifact", "google-cloud-cpp"), - Pair("gcp.client.service", "storage"), - Pair("server.address", "storage.googleapis.com"), - Pair("rpc.system.name", "grpc"), - Pair("gcp.storage.operation", "ReadObject"), - Pair("error.type", "OK"), Pair("network.path", "directpath"), - Pair("rpc.response.status_code", "OK"))); + EXPECT_THAT( + attempt_attrs[0].strings, + ContainerEq(ExpectedStringAttrs({{"rpc.system.name", "grpc"}, + {"gcp.storage.operation", "ReadObject"}, + {"error.type", "OK"}, + {"network.path", "directpath"}, + {"rpc.response.status_code", "OK"}}))); EXPECT_THAT(attempt_attrs[0].ints, IsEmpty()); EXPECT_THAT(attempt_attrs[1].strings, - UnorderedElementsAre( - Pair("gcp.client.version", ver), - Pair("gcp.client.artifact", "google-cloud-cpp"), - Pair("gcp.client.service", "storage"), - Pair("server.address", "storage.googleapis.com"), - Pair("rpc.system.name", "grpc"), - Pair("gcp.storage.operation", "WriteObject"), - Pair("error.type", "DEADLINE_EXCEEDED"), - Pair("network.path", "directpath"), - Pair("rpc.response.status_code", "DEADLINE_EXCEEDED"))); + ContainerEq(ExpectedStringAttrs( + {{"rpc.system.name", "grpc"}, + {"gcp.storage.operation", "WriteObject"}, + {"error.type", "DEADLINE_EXCEEDED"}, + {"network.path", "directpath"}, + {"rpc.response.status_code", "DEADLINE_EXCEEDED"}}))); EXPECT_THAT(attempt_attrs[1].ints, IsEmpty()); EXPECT_THAT(attempt_attrs[2].strings, - UnorderedElementsAre( - Pair("gcp.client.version", ver), - Pair("gcp.client.artifact", "google-cloud-cpp"), - Pair("gcp.client.service", "storage"), - Pair("server.address", "storage.googleapis.com"), - Pair("rpc.system.name", "http"), - Pair("gcp.storage.operation", "GetObjectMetadata"), - Pair("error.type", "OK"), Pair("network.path", "cloudpath"))); + ContainerEq(ExpectedStringAttrs( + {{"rpc.system.name", "http"}, + {"gcp.storage.operation", "GetObjectMetadata"}, + {"error.type", "OK"}, + {"network.path", "cloudpath"}}))); EXPECT_THAT(attempt_attrs[2].ints, UnorderedElementsAre(Pair("http.response.status_code", 200))); - EXPECT_THAT( - attempt_attrs[3].strings, - UnorderedElementsAre(Pair("gcp.client.version", ver), - Pair("gcp.client.artifact", "google-cloud-cpp"), - Pair("gcp.client.service", "storage"), - Pair("server.address", "storage.googleapis.com"), - Pair("rpc.system.name", "http"), - Pair("gcp.storage.operation", "DeleteObject"), - Pair("error.type", "UNAVAILABLE"), - Pair("network.path", "cloudpath"))); + EXPECT_THAT(attempt_attrs[3].strings, + ContainerEq(ExpectedStringAttrs( + {{"rpc.system.name", "http"}, + {"gcp.storage.operation", "DeleteObject"}, + {"error.type", "UNAVAILABLE"}, + {"network.path", "cloudpath"}}))); EXPECT_THAT(attempt_attrs[3].ints, UnorderedElementsAre(Pair("http.response.status_code", 503))); ASSERT_EQ(error_attrs.size(), 2U); EXPECT_THAT( error_attrs[0].strings, - UnorderedElementsAre(Pair("gcp.client.version", ver), - Pair("gcp.client.artifact", "google-cloud-cpp"), - Pair("gcp.client.service", "storage"), - Pair("server.address", "storage.googleapis.com"), - Pair("rpc.system.name", "grpc"), - Pair("gcp.storage.operation", "WriteObject"), - Pair("error.type", "DEADLINE_EXCEEDED"), - Pair("network.path", "directpath"))); + ContainerEq(ExpectedStringAttrs({{"rpc.system.name", "grpc"}, + {"gcp.storage.operation", "WriteObject"}, + {"error.type", "DEADLINE_EXCEEDED"}, + {"network.path", "directpath"}}))); EXPECT_THAT(error_attrs[0].ints, IsEmpty()); - EXPECT_THAT( - error_attrs[1].strings, - UnorderedElementsAre(Pair("gcp.client.version", ver), - Pair("gcp.client.artifact", "google-cloud-cpp"), - Pair("gcp.client.service", "storage"), - Pair("server.address", "storage.googleapis.com"), - Pair("rpc.system.name", "http"), - Pair("gcp.storage.operation", "DeleteObject"), - Pair("error.type", "UNAVAILABLE"), - Pair("network.path", "cloudpath"))); + EXPECT_THAT(error_attrs[1].strings, + ContainerEq(ExpectedStringAttrs( + {{"rpc.system.name", "http"}, + {"gcp.storage.operation", "DeleteObject"}, + {"error.type", "UNAVAILABLE"}, + {"network.path", "cloudpath"}}))); EXPECT_THAT(error_attrs[1].ints, IsEmpty()); - EXPECT_THAT( - retry_attrs.strings, - UnorderedElementsAre(Pair("gcp.client.version", ver), - Pair("gcp.client.artifact", "google-cloud-cpp"), - Pair("gcp.client.service", "storage"), - Pair("server.address", "storage.googleapis.com"), - Pair("rpc.system.name", "http"), - Pair("gcp.storage.operation", "DeleteObject"), - Pair("error.type", "UNAVAILABLE"), - Pair("network.path", "cloudpath"))); - EXPECT_THAT(retry_attrs.ints, IsEmpty()); } TEST(AttemptMetricsTest, NullProviderAndNullMeterAreNoOps) { @@ -258,8 +224,6 @@ TEST(AttemptMetricsTest, NullProviderAndNullMeterAreNoOps) { no_provider.RecordGrpcAttempt({"ReadObject", "OK", "OK", "directpath"}, ctx); no_provider.RecordHttpAttempt({"ReadObject", "UNAVAILABLE", 503, "cloudpath"}, ctx); - no_provider.RecordRetry({"http", "ReadObject", "UNAVAILABLE", "cloudpath"}, - ctx); auto provider = std::make_shared(); EXPECT_CALL(*provider, GetMeter) @@ -274,7 +238,6 @@ 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) { @@ -282,20 +245,16 @@ 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)))); auto errors = std::make_unique>(); EXPECT_CALL(*errors, Add(std::uint64_t{1}, _, _)).Times(1); - auto retries = std::make_unique>(); - EXPECT_CALL(*retries, Add(std::uint64_t{1}, _, _)).Times(1); opentelemetry::nostd::shared_ptr no_attempts_meter = std::make_shared(); EXPECT_CALL(*no_attempts_meter, CreateUInt64Counter) .WillOnce(Return(ByMove(CounterPtr(nullptr)))) - .WillOnce(Return(ByMove(CounterPtr(errors.release())))) - .WillOnce(Return(ByMove(CounterPtr(retries.release())))); + .WillOnce(Return(ByMove(CounterPtr(errors.release())))); auto attempts = std::make_unique>(); EXPECT_CALL(*attempts, Add(std::uint64_t{1}, _, _)).Times(1); @@ -303,7 +262,6 @@ 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(); @@ -318,14 +276,11 @@ 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); no_attempts.RecordGrpcAttempt( {"ReadObject", "UNAVAILABLE", "UNAVAILABLE", "cloudpath"}, ctx); - no_attempts.RecordRetry({"grpc", "ReadObject", "UNAVAILABLE", "cloudpath"}, - ctx); AttemptMetrics no_errors(mp, "storage.googleapis.com"); no_errors.RecordGrpcAttempt(