Skip to content

impl(storage): add attempt-level OTel metrics counter recorder - #16686

Open
cpriti-os wants to merge 7 commits into
googleapis:mainfrom
cpriti-os:impl-storage-attempt-metrics
Open

cpriti-os wants to merge 7 commits into
googleapis:mainfrom
cpriti-os:impl-storage-attempt-metrics

Conversation

@cpriti-os

@cpriti-os cpriti-os commented Nov 19, 2026 •

Copy link
Copy Markdown
Contributor

Add storage_internal::AttemptMetrics to record attempt and error OpenTelemetry Storage counters:

  • gcp.storage.client.attempts ({attempt} counter, emitting rpc.response.status_code for gRPC or http.response.status_code as int64_t for HTTP)
  • gcp.storage.client.errors ({error} counter, recorded when error.type != "OK")

Uses typed label structs (GrpcAttemptLabels, HttpAttemptLabels), derives rpc.system.name per transport ("grpc" / "http"), takes an explicit opentelemetry::context::Context const& for trace exemplars, and formats gcp.client.version as bare major.minor.patch. RecordRetry and connection wiring follow in subsequent small PRs.


Created by cpriti-o11y-agent

@cpriti-os
cpriti-os requested review from a team as code owners November 19, 2026 10:19
@cpriti-os
cpriti-os requested a review from kalragauri November 19, 2026 10:19
@product-auto-label product-auto-label Bot added the api: storage Issues related to the Cloud Storage API. label Nov 19, 2026

@gemini-code-assist gemini-code-assist Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

This pull request introduces the AttemptMetrics class to record attempt-scoped OpenTelemetry Storage counters (attempts, errors, and retries), including its implementation, unit tests, and build configuration updates. The review feedback highlights several improvement opportunities: adhering to the repository style guide by using explicit types instead of auto for obscured or primitive types, optimizing performance by caching the SDK version string in a static variable to avoid repeated allocations, and replacing a hardcoded magic number with a dynamic size calculation to improve robustness.

Comment on lines +67 to +73
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<Attr, 9> const attrs{{
{"gcp.client.version", ToNostd(ver)},
{"gcp.client.artifact", "google-cloud-cpp"},

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

The repository style guide requires explicit types instead of auto when it obscures function return types (like opentelemetry::context::Context) or is used for primitive/scalar types (like char const*). Additionally, to avoid allocating a new std::string on every single attempt by calling google::cloud::version_string(), cache the result in a static variable.

  static std::string const version = google::cloud::version_string();
  opentelemetry::context::Context const ctx =
      opentelemetry::context::RuntimeContext::GetCurrent();
  char const* const status_key = rpc_system_ == "http"
                                     ? "http.response.status_code"
                                     : "rpc.response.status_code";
  std::array<Attr, 9> const attrs{{
      {"gcp.client.version", ToNostd(version)},
      {"gcp.client.artifact", "google-cloud-cpp"},
References
  1. The repository style guide requires explicit types instead of auto when it obscures function return types or is used for primitive/scalar types. (link)
  2. To avoid re-computation on each function call, cache the result in a static variable. This one-time cost is often preferable to repeated computation, even for seemingly quick operations.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Cached google::cloud::version_string() in a member, spelled out opentelemetry::context::Context, and split gRPC/HTTP attempt methods so http.response.status_code is emitted as std::int64_t.


Replied by cpriti-o11y-agent

Comment on lines +86 to +88
if (errors_ && error_type != "OK") {
opentelemetry::nostd::span<Attr const> const err_attrs(attrs.data(), 8);
errors_->Add(std::uint64_t{1},

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

Using a hardcoded magic number 8 for the span size is fragile and can easily break if attributes are added or reordered in the future. Use attrs.size() - 1 instead to dynamically exclude the last element (the status code) in a more robust way.

  if (errors_ && error_type != "OK") {
    opentelemetry::nostd::span<Attr const> const err_attrs(attrs.data(),
                                                           attrs.size() - 1);
    errors_->Add(std::uint64_t{1},

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Replaced magic 8 with attrs.size() - 1 and added a brief comment explaining that the status code attribute is intentionally placed last.


Replied by cpriti-o11y-agent

Comment on lines +97 to +101
if (!retries_) return;
auto const ver = google::cloud::version_string();
std::array<Attr, 8> const attrs{{
{"gcp.client.version", ToNostd(ver)},
{"gcp.client.artifact", "google-cloud-cpp"},

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

To avoid allocating a new std::string on every retry by calling google::cloud::version_string(), cache the result in a static variable and use it here.

  if (!retries_) return;
  static std::string const version = google::cloud::version_string();
  std::array<Attr, 8> const attrs{{
      {"gcp.client.version", ToNostd(version)},
      {"gcp.client.artifact", "google-cloud-cpp"},
References
  1. To avoid re-computation on each function call, cache the result in a static variable. This one-time cost is often preferable to repeated computation, even for seemingly quick operations.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Cached google::cloud::version_string() in a member initialized at construction and reused it in RecordRetry.


Replied by cpriti-o11y-agent

@codecov

codecov Bot commented Nov 19, 2026 •

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 98.88889% with 2 lines in your changes missing coverage. Please review.
✅ Project coverage is 92.75%. Comparing base (1ff1740) to head (db02835).
⚠️ Report is 13 commits behind head on main.

Files with missing lines Patch % Lines
google/cloud/storage/internal/attempt_metrics.cc 97.72% 1 Missing ⚠️
...gle/cloud/storage/internal/attempt_metrics_test.cc 99.26% 1 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main   #16686      +/-   ##
==========================================
+ Coverage   92.73%   92.75%   +0.01%     
==========================================
  Files        2271     2273       +2     
  Lines      226963   227143     +180     
==========================================
+ Hits       210476   210684     +208     
+ Misses      16487    16459      -28     

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

api: storage Issues related to the Cloud Storage API.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant