Skip to content

impl(storage): add MetricsConnection decorator for operation-level OTel metrics - #16724

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

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

Conversation

@cpriti-os

Copy link
Copy Markdown
Contributor

Adds the internal MetricsConnection StorageConnection decorator (and MakeMetricsConnection() factory) that records operation-level OpenTelemetry metrics (gcp.storage.client.operations and gcp.client.request.duration) via a constructor-injected OperationMetrics recorder:

  • Instruments logical bucket, object, ACL, HMAC key, service account, and notification operations with canonical gcp.storage.operation, gcp.client.method, rpc.method, and error.type (ComputeErrorType) labels, propagating the active opentelemetry::context::Context
  • Wraps streaming ReadObject responses in MetricsObjectReadSource so operation metrics span the full download lifetime (recording once on open failure, EOF, read error, early Close(), or abandonment) while preserving the opening Context
  • Forwards chunk-level resumable upload RPCs, local file/parallel upload helpers, and IAM SignBlob without double-counting logical Storage operations
  • Returns the underlying connection unchanged when metrics is null

Also registers metrics_connection.{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 30, 2026 08:38
@cpriti-os
cpriti-os requested a review from kalragauri November 30, 2026 08:38
@product-auto-label product-auto-label Bot added the api: storage Issues related to the Cloud Storage API. label Nov 30, 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 MetricsConnection, a decorator for StorageConnection that records operation-level OpenTelemetry metrics, along with its corresponding unit tests. The review feedback suggests several improvements: replacing default parameters in the test harness to comply with the repository style guide's restriction on internal namespaces, refactoring the Read method in MetricsObjectReadSource to return early on error, and simplifying a return statement in ReadObject by removing an explicit std::unique_ptr conversion.

Comment on lines +106 to +108
std::unique_ptr<MetricsHarness> MakeHarness(
std::string server_address = "storage.googleapis.com",
std::string rpc_system = "grpc") {

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 disallows default parameters in internal namespaces (including storage_internal and its nested anonymous namespaces), even in unit tests. Please replace the default parameters with explicit function overloads.

std::unique_ptr<MetricsHarness> MakeHarness(
    std::string server_address,
    std::string rpc_system);

std::unique_ptr<MetricsHarness> MakeHarness() {
  return MakeHarness("storage.googleapis.com", "grpc");
}

std::unique_ptr<MetricsHarness> MakeHarness(
    std::string server_address,
    std::string rpc_system) {
References
  1. Disallow Default Parameters in internal Namespaces: Reject default argument values in functions, member functions, and constructors declared in internal namespaces. (link)

Comment on lines +98 to +103
if (!result) {
RecordOnce(result.status());
} else if (!child_->IsOpen()) {
RecordOnce(Status{});
}
return result;

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

Following the 'Return Early' principle from the style guide, we can return immediately when result is an error. This simplifies the control flow and avoids the else if block.

    if (!result) {
      RecordOnce(result.status());
      return result;
    }
    if (!child_->IsOpen()) {
      RecordOnce(Status{});
    }
    return result;
References
  1. Return Early: Avoid unnecessary else blocks to reduce indentation and keep the main logic flow linear. (link)

Comment on lines +238 to +240
return std::unique_ptr<storage::internal::ObjectReadSource>(
std::make_unique<MetricsObjectReadSource>(metrics_, start, ctx,
*std::move(reader)));

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 MetricsObjectReadSource publicly inherits from storage::internal::ObjectReadSource, std::unique_ptr<MetricsObjectReadSource> implicitly converts to std::unique_ptr<storage::internal::ObjectReadSource>. We can simplify this return statement by removing the explicit constructor call.

  return std::make_unique<MetricsObjectReadSource>(metrics_, start, ctx,
                                                   *std::move(reader));

@codecov

codecov Bot commented Nov 30, 2026 •

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 99.63548% with 3 lines in your changes missing coverage. Please review.
✅ Project coverage is 92.83%. Comparing base (fdcfe38) to head (641b45c).
⚠️ Report is 5 commits behind head on main.

Files with missing lines Patch % Lines
...oogle/cloud/storage/internal/metrics_connection.cc 98.70% 3 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main   #16724      +/-   ##
==========================================
+ Coverage   92.78%   92.83%   +0.04%     
==========================================
  Files        2279     2282       +3     
  Lines      228396   229219     +823     
==========================================
+ Hits       211921   212789     +868     
+ Misses      16475    16430      -45     

☔ 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