Skip to content

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

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

cpriti-os wants to merge 2 commits 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.59813% with 3 lines in your changes missing coverage. Please review.
✅ Project coverage is 92.80%. Comparing base (fdcfe38) to head (591e00b).
⚠️ Report is 4 commits behind head on main.

Files with missing lines Patch % Lines
...ud/storage/internal/attempt_timing_metrics_test.cc 98.75% 2 Missing ⚠️
...e/cloud/storage/internal/attempt_timing_metrics.cc 98.14% 1 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main   #16713      +/-   ##
==========================================
+ Coverage   92.78%   92.80%   +0.02%     
==========================================
  Files        2279     2281       +2     
  Lines      228396   228610     +214     
==========================================
+ Hits       211921   212170     +249     
+ Misses      16475    16440      -35     

☔ 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.

@cpriti-os
cpriti-os force-pushed the impl-storage-attempt-timing-metrics branch 2 times, most recently from 6395641 to e5b50c9 Compare November 27, 2026 11:12
opentelemetry::metrics::MeterProvider> const& provider,
std::string server_address);

void RecordTtfb(TtfbLabels const& labels,

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

RecordTtfb does not say where elapsed starts (the start of the attempt, or after the request is sent) or whether it is called for attempts that got no response. The histogram's meaning depends on both, and the instrument description (Time to first byte of a Storage response.) does not settle it either.

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.

Updated the doc comments and instrument description to specify that timing starts when the attempt begins and ends at the first response byte/header (or final response headers for writes), and that callers only invoke it when a server response is received.


Replied by cpriti-o11y-agent

using Attr = std::pair<opentelemetry::nostd::string_view,
opentelemetry::common::AttributeValue>;

opentelemetry::nostd::string_view ToNostd(std::string_view sv) {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Same point as my comment on #16712: ToNostd and SemanticVersionString are now in three places, here, operation_metrics.cc and attempt_metrics.cc.

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.

I moved these into shared metrics_attributes.{h,cc} in #16712. Once #16712 lands, I will rebase this PR onto main and reuse them here.


Replied by cpriti-o11y-agent


/// Labels for recording time-to-first-byte (`gcp.storage.client.ttfb`).
struct TtfbLabels {
std::string_view rpc_system;

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

rpc_system accepts any string here, while AttemptMetrics fixes "grpc" and "http" inside per-transport methods. A typo at a call site would silently start a new rpc.system.name series. Would it make sense to split this into RecordGrpcTtfb() and RecordHttpTtfb() (or take an enum class), so callers cannot pass an arbitrary value?

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.

Split RecordTtfb into RecordGrpcTtfb and RecordHttpTtfb and dropped rpc_system from TtfbLabels, matching AttemptMetrics.


Replied by cpriti-o11y-agent

/// (`rpc.client.call.duration`).
struct RpcCallDurationLabels {
std::string_view rpc_method;
std::string_view status_code;

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

status_code holds the same rpc.response.status_code value that GrpcAttemptLabels calls rpc_status_code. Both structs describe one gRPC attempt, so a single name for the field would be easier to follow. Would it make sense to rename this one to rpc_status_code?

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.

Renamed status_code to rpc_status_code in RpcCallDurationLabels.


Replied by cpriti-o11y-agent

using HistPtr =
opentelemetry::nostd::unique_ptr<opentelemetry::metrics::Histogram<double>>;

std::map<std::string, std::string> ExtractStringAttrs(

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Same point as my comment on #16712: ExtractStringAttrs() here is another verbatim copy of the helper in operation_metrics_test.cc. Could this test use whichever shared location that thread settles on?

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.

I extracted the test attribute helpers into google/cloud/storage/testing/metrics_test_utils.h in #16712. Once #16712 merges, I will rebase and reuse it here.


Replied by cpriti-o11y-agent

@cpriti-os
cpriti-os force-pushed the impl-storage-attempt-timing-metrics branch from e5b50c9 to 591e00b Compare December 1, 2026 05:06
@cpriti-os
cpriti-os requested a review from kalragauri December 1, 2026 05:07

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.

2 participants