Skip to content

impl(storage): add PayloadMetrics recorder for OTel body size histograms - #16712

Open
cpriti-os wants to merge 3 commits into
googleapis:mainfrom
cpriti-os:impl-storage-payload-metrics
Open

cpriti-os wants to merge 3 commits into
googleapis:mainfrom
cpriti-os:impl-storage-payload-metrics

Conversation

@cpriti-os

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

Copy link
Copy Markdown
Contributor

Adds storage_internal::PayloadMetrics (google/cloud/storage/internal/payload_metrics.{h,cc}) to record operation-scoped payload body size OpenTelemetry histograms:

  • gcp.storage.client.request.body.size (UInt64Histogram, unit By)
  • gcp.storage.client.response.body.size (UInt64Histogram, unit By)

Also in this PR:

  • Registers both metric constants in google/cloud/storage/internal/metrics_constants.h and adds them to kStorageSizeHistogramInstruments in google/cloud/storage/internal/grpc/metrics_meter_provider.{h,_test.cc}.
  • Extracts shared metric attribute helpers (MetricAttribute, ToNostd, SemanticVersionString, MakeStorageMetricAttributes) into google/cloud/storage/internal/metrics_attributes.{h,cc} with unit tests in metrics_attributes_test.cc, and updates OperationMetrics and AttemptMetrics to reuse them.
  • Extracts shared test attribute extraction helpers (CapturedMetricAttrs, ExtractMetricAttrs, ExtractStringMetricAttrs) into google/cloud/storage/testing/metrics_test_utils.h under google::cloud::storage::testing with duplicate-key failure checks, and updates attempt_metrics_test.cc, operation_metrics_test.cc, and payload_metrics_test.cc to use them.

PayloadMetrics is not wired into callers yet; caller wiring will follow in a subsequent PR.


Created by cpriti-o11y-agent

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

@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 PayloadMetrics class to record OpenTelemetry metrics for request and response payload body sizes in Google Cloud Storage, along with comprehensive unit tests and build file updates. The review feedback suggests simplifying the implementation and tests by directly using google::cloud::version_string() instead of defining and using a custom SemanticVersionString() helper.

Comment on lines +46 to +50
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

We can simplify the code and avoid manual string formatting by using google::cloud::version_string() directly instead of defining a custom SemanticVersionString() helper. This is also more consistent with how the client version is recorded in other parts of the SDK.

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.

I would keep SemanticVersionString() here. version_string() returns v<major>.<minor>.<patch> plus any pre-release and build metadata, so gcp.client.version would no longer match what OperationMetrics and AttemptMetrics emit. The bare major.minor.patch form is what #16685 and #16686 landed with.

opentelemetry::nostd::shared_ptr<
opentelemetry::metrics::MeterProvider> const& provider,
std::string server_address, std::string rpc_system)
: client_version_(SemanticVersionString()),

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 google::cloud::version_string() directly here to initialize client_version_.

    : client_version_(google::cloud::version_string()),

Comment on lines +148 to +151
std::string const expected_version =
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

Simplify this to use google::cloud::version_string() directly, matching the change in PayloadMetrics.

  std::string const expected_version = google::cloud::version_string();

@codecov

codecov Bot commented Nov 26, 2026 •

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 97.89030% with 5 lines in your changes missing coverage. Please review.
✅ Project coverage is 92.80%. Comparing base (fdcfe38) to head (7b5ce77).
⚠️ Report is 21 commits behind head on main.

Files with missing lines Patch % Lines
google/cloud/storage/testing/metrics_test_utils.h 87.50% 4 Missing ⚠️
google/cloud/storage/internal/payload_metrics.cc 96.15% 1 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main   #16712      +/-   ##
==========================================
+ Coverage   92.78%   92.80%   +0.02%     
==========================================
  Files        2279     2283       +4     
  Lines      228396   228561     +165     
==========================================
+ Hits       211921   212123     +202     
+ Misses      16475    16438      -37     

☔ 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-payload-metrics branch 2 times, most recently from e0324ed to b728eaf Compare November 27, 2026 11:10
opentelemetry::metrics::MeterProvider> const& provider,
std::string server_address, std::string rpc_system);

void RecordRequestBodySize(PayloadLabels const& labels, std::uint64_t bytes,

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.

RecordRequestBodySize does not say what one call stands for: a whole operation, one attempt, or one chunk. The histogram's shape depends on that, and the instrument description (Total logical payload bytes uploaded.) 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.

Good catch! Updated the method comments and instrument descriptions to specify that each call records the total logical payload bytes for one completed logical operation (including 0-byte objects), rather than per-attempt or per-chunk sizes.


Replied by cpriti-o11y-agent

Comment on lines +46 to +50
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
Contributor

Choose a reason for hiding this comment

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

I would keep SemanticVersionString() here. version_string() returns v<major>.<minor>.<patch> plus any pre-release and build metadata, so gcp.client.version would no longer match what OperationMetrics and AttemptMetrics emit. The bare major.minor.patch form is what #16685 and #16686 landed with.

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.

It looks like ToNostd and SemanticVersionString are now in three places: here, operation_metrics.cc and attempt_metrics.cc. The five base attributes in RecordBodySize are repeated across the three recorders too, so a change to gcp.client.version would need three edits. Would it make sense to move the two functions and the base attribute list into internal/metrics_attributes.h, which keeps them in storage_internal and out of the public API?

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.

Agreed, moved ToNostd, SemanticVersionString, and a MakeStorageMetricAttributes helper for the 5 common attributes into internal/metrics_attributes.{h,cc} and updated PayloadMetrics, OperationMetrics, and AttemptMetrics to share them.


Replied by cpriti-o11y-agent

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

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.

It looks like ExtractStringAttrs() is a verbatim copy of the helper in operation_metrics_test.cc, and attempt_metrics_test.cc has a variant.

Would it make sense to move one version into google/cloud/testing_util (or a storage-specific location) and use it from these tests?

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.

Makes sense! Extracted ExtractStringMetricAttrs and ExtractMetricAttrs into google/cloud/storage/testing/metrics_test_utils.h and updated payload_metrics_test.cc, operation_metrics_test.cc, and attempt_metrics_test.cc to reuse them.


Replied by cpriti-o11y-agent

@kalragauri kalragauri left a comment

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.

The PR description still reflects only the initial commit and omits the shared-helper refactor across metrics_attributes.{h,cc}, OperationMetrics, AttemptMetrics, and storage/testing/metrics_test_utils.h. It also leaves out the metrics_constants.h / metrics_meter_provider.{h,_test.cc} updates and that PayloadMetrics is not wired into callers yet.

Could you refresh the description so the squash commit message covers the full diff?

if (auto const* sv =
opentelemetry::nostd::get_if<opentelemetry::nostd::string_view>(
&value)) {
result.strings.emplace(k, std::string(sv->data(), sv->size()));

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.

result.strings.emplace ignores its return value (as do result.ints.emplace and ExtractStringMetricAttrs), so duplicate attribute keys are silently kept first-wins. If a recorder repeats a base key in MakeStorageMetricAttributes, UnorderedElementsAre can pass even though the OTel SDK keeps the last value.

Consider calling ADD_FAILURE() whenever a key appears more than once (including across strings and ints).

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.

Done, both helpers now call ADD_FAILURE() on duplicate keys.


Replied by cpriti-o11y-agent

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

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

ToNostd, SemanticVersionString, and MakeStorageMetricAttributes are now shared helpers here, but metrics_attributes_test.cc has no direct tests for them. Existing recorder tests only pass null-terminated strings, so dropping sv.size() in ToNostd or calling MakeStorageMetricAttributes with zero extra attributes is untested.

Would it make sense to add unit tests for them in metrics_attributes_test.cc, including a non-null-terminated std::string_view slice?

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.

Done, added unit tests in metrics_attributes_test.cc including a non-null-terminated slice.


Replied by cpriti-o11y-agent


namespace google {
namespace cloud {
namespace storage_internal {

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.

This header is in storage_internal, while every other namespaced file under google/cloud/storage/testing/ (such as canonical_errors.h) uses google::cloud::storage::testing without an inline version namespace. Keeping test-only utilities under storage::testing matches the rest of the directory and avoids mixing test symbols into the production internal namespace.

Can we move these helpers to google::cloud::storage::testing?

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.

Done, moved to google::cloud::storage::testing and updated callers.


Replied by cpriti-o11y-agent

void RecordRequestBodySize(PayloadLabels const& labels, std::uint64_t bytes,
opentelemetry::context::Context const& ctx) const;

/// Records the total logical payload bytes downloaded for one completed

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.

This comment says "one completed logical Storage operation (including 0-byte objects)", which reads as skipping early-closed or partial downloads. Like Go's reader close path and Java's description, this metric counts bytes delivered to the application on close, including partial reads, rather than full object size.

Should we reword this (and RecordRequestBodySize for uploads closed early) so callers do not skip partial streams or emit Content-Length?

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.

Done, reworded both doc comments to cover partial and early-closed streams.


Replied by cpriti-o11y-agent

@cpriti-os
cpriti-os requested a review from kalragauri December 5, 2026 05:02

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