Skip to content

impl(storage): add AttemptTimingMetrics recorder for OTel TTFB and RPC duration histograms - #16713

Open
cpriti-os wants to merge 1 commit into
googleapis:mainfrom
cpriti-os:impl-storage-attempt-timing-metrics
Open

cpriti-os wants to merge 1 commit into
googleapis:mainfrom
cpriti-os:impl-storage-attempt-timing-metrics

Conversation

@cpriti-os

Copy link
Copy Markdown
Contributor

Adds the internal AttemptTimingMetrics recorder for per-attempt OpenTelemetry timing histograms:

  • gcp.storage.client.ttfb (DoubleHistogram, unit s) with base attributes (gcp.client.version, gcp.client.artifact, gcp.client.service, server.address) plus rpc.system.name, gcp.storage.operation, and network.path
  • rpc.client.call.duration (DoubleHistogram, unit s) with base attributes plus rpc.system.name ("grpc"), rpc.method, rpc.response.status_code, error.type, and network.path

Both methods accept std::chrono::steady_clock::duration (clamping negative durations to 0.0), propagate the explicit opentelemetry::context::Context, and safely no-op when the provider, meter, or individual instruments are null.

Also registers attempt_timing_metrics.{h,cc,_test.cc} in Bazel and CMake build files.


Created by cpriti-o11y-agent

@cpriti-os
cpriti-os requested review from a team as code owners November 26, 2026 07:50
@cpriti-os
cpriti-os requested a review from kalragauri November 26, 2026 07:50
@product-auto-label product-auto-label Bot added the api: storage Issues related to the Cloud Storage API. label Nov 26, 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 AttemptTimingMetrics class to record OpenTelemetry metrics for storage attempt timings, specifically tracking time-to-first-byte (gcp.storage.client.ttfb) and RPC call duration (rpc.client.call.duration). It also includes corresponding unit tests and updates the Bazel and CMake build configurations. The review feedback suggests optimizing the retrieval of the semantic version string by caching it in a heap-allocated static variable. This avoids re-computation on every call and prevents static destruction order issues, allowing the removal of the client_version_ member variable from the class to reduce object size and avoid unnecessary copies.

Comment on lines +44 to +48
std::string SemanticVersionString() {
return std::to_string(google::cloud::version_major()) + "." +
std::to_string(google::cloud::version_minor()) + "." +
std::to_string(google::cloud::version_patch());
}

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 re-computation on each function call, we should cache the semantic version string in a static variable. To prevent static destruction order issues with non-trivial destructors (like std::string), we can allocate it on the heap using new std::string and return a reference to it.

std::string const& SemanticVersionString() {
  static auto const* const version = new std::string(
      std::to_string(google::cloud::version_major()) + "." +
      std::to_string(google::cloud::version_minor()) + "." +
      std::to_string(google::cloud::version_patch()));
  return *version;
}
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.
  2. Avoid using function-local static variables with non-trivial destructors (such as std::vector or std::string) to prevent static destruction order issues, in accordance with the Google C++ Style Guide.

Comment on lines +69 to +70
std::string client_version_;
std::string server_address_;

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

Since the semantic version string is static and can be cached safely on the heap, we do not need to store client_version_ as a member variable in every instance of AttemptTimingMetrics. This reduces the object size and avoids unnecessary copies.

  std::string server_address_;

Comment on lines +59 to +64
AttemptTimingMetrics::AttemptTimingMetrics(
opentelemetry::nostd::shared_ptr<
opentelemetry::metrics::MeterProvider> const& provider,
std::string server_address)
: client_version_(SemanticVersionString()),
server_address_(std::move(server_address)) {

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

Remove the initialization of client_version_ from the constructor since we can use the cached static version directly.

AttemptTimingMetrics::AttemptTimingMetrics(
    opentelemetry::nostd::shared_ptr<
        opentelemetry::metrics::MeterProvider> const& provider,
    std::string server_address)
    : server_address_(std::move(server_address)) {

Comment on lines +79 to +88
std::array<Attr, 7> 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)},
{"network.path", ToNostd(labels.network_path)},
}};

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

Use the cached static SemanticVersionString() directly instead of the member variable client_version_.

  std::array<Attr, 7> const attrs{{
      {"gcp.client.version", ToNostd(SemanticVersionString())},
      {"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)},
      {"network.path", ToNostd(labels.network_path)},
  }};

Comment on lines +98 to +109
std::array<Attr, 9> 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", opentelemetry::nostd::string_view("grpc")},
{"rpc.method", ToNostd(labels.rpc_method)},
{"rpc.response.status_code", ToNostd(labels.status_code)},
{"error.type", ToNostd(labels.error_type)},
{"network.path", ToNostd(labels.network_path)},
}};

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

Use the cached static SemanticVersionString() directly instead of the member variable client_version_.

  std::array<Attr, 9> const attrs{{
      {"gcp.client.version", ToNostd(SemanticVersionString())},
      {"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", opentelemetry::nostd::string_view("grpc")},
      {"rpc.method", ToNostd(labels.rpc_method)},
      {"rpc.response.status_code", ToNostd(labels.status_code)},
      {"error.type", ToNostd(labels.error_type)},
      {"network.path", ToNostd(labels.network_path)},
  }};

@codecov

codecov Bot commented Nov 26, 2026

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 98.42932% with 3 lines in your changes missing coverage. Please review.
✅ Project coverage is 92.79%. Comparing base (fa26249) to head (174bb40).

Files with missing lines Patch % Lines
...ud/storage/internal/attempt_timing_metrics_test.cc 98.60% 2 Missing ⚠️
...e/cloud/storage/internal/attempt_timing_metrics.cc 97.91% 1 Missing ⚠️
Additional details and impacted files
@@           Coverage Diff            @@
##             main   #16713    +/-   ##
========================================
  Coverage   92.78%   92.79%            
========================================
  Files        2275     2277     +2     
  Lines      227981   228172   +191     
========================================
+ Hits       211537   211726   +189     
- Misses      16444    16446     +2     

☔ 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