Skip to content

impl(storage): add Cloud Monitoring metric name formatting and histogram views - #16638

Merged
cpriti-os merged 5 commits into
googleapis:mainfrom
cpriti-os:feat-storage-metrics-formatter-and-views
Nov 19, 2026
Merged

cpriti-os merged 5 commits into
googleapis:mainfrom
cpriti-os:feat-storage-metrics-formatter-and-views

Conversation

@cpriti-os

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

Copy link
Copy Markdown
Contributor

Adds internal Cloud Monitoring metric name formatting and explicit histogram bucket views for the Google Cloud Storage OpenTelemetry SDK metrics:

  • Maps gcp.client.request.duration to storage.googleapis.com/client/request_duration and gcp.storage.client.<name> (. -> _) to storage.googleapis.com/client/<name> while preserving internal/client/ routing.
  • Registers explicit latency and payload size histogram views under kStorageMeterName using shared constexpr std::string_view instrument name constants.

This PR adds the internal formatter and view plumbing; instrument recording lands in follow-up PRs.


Created by cpriti-o11y-agent

@product-auto-label product-auto-label Bot added the api: storage Issues related to the Cloud Storage API. label Nov 9, 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 maps standard and debug OpenTelemetry SDK instruments to Cloud Monitoring descriptor names under the storage.googleapis.com/client/ namespace, configures latency and size histogram views for these metrics, and adds corresponding unit tests. Feedback on the new tests recommends recording metrics once before the retry loop rather than inside it to avoid non-deterministic behavior and potential test flakiness.

Comment on lines +289 to +293
for (int i = 0; i != 50 && export_count.load() == 0; ++i) {
latency->Record(1.0, opentelemetry::context::Context{});
size->Record(1024.0, opentelemetry::context::Context{});
std::this_thread::sleep_for(kExportInterval);
}

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

Recording metrics inside the loop can lead to non-deterministic test behavior and potential flakiness. If the background export thread is slow to wake up, multiple records will be accumulated, resulting in a histogram with more than one sample. If the matchers ExpectedLatencyHistogram() or ExpectedSizeHistogram() assert a specific count or sum, the test will fail.

To make the test deterministic and robust, record the metrics exactly once before entering the loop, and then simply sleep/wait in the loop until the export completes.

  latency->Record(1.0, opentelemetry::context::Context{});
  size->Record(1024.0, opentelemetry::context::Context{});
  for (int i = 0; i != 50 && export_count.load() == 0; ++i) {
    std::this_thread::sleep_for(kExportInterval);
  }

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.

Moved Record() outside the wait loop so each histogram is recorded once before waiting for the export.


Replied by cpriti-o11y-agent

@cpriti-os
cpriti-os force-pushed the feat-storage-metrics-formatter-and-views branch from 631feab to 7fdb367 Compare November 9, 2026 07:07
@cpriti-os cpriti-os changed the title feat(storage): add Monarch metric name formatting and histogram views for OTel metrics feat(storage): add Cloud Monitoring metric name formatting and histogram views Nov 9, 2026
@cpriti-os
cpriti-os marked this pull request as ready for review November 9, 2026 07:13
@cpriti-os
cpriti-os requested review from a team as code owners November 9, 2026 07:13
@cpriti-os
cpriti-os requested a review from kalragauri November 9, 2026 07:13
@codecov

codecov Bot commented Nov 9, 2026 •

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 92.75%. Comparing base (1ff1740) to head (cbde875).
⚠️ Report is 1 commits behind head on main.

Additional details and impacted files
@@            Coverage Diff             @@
##             main   #16638      +/-   ##
==========================================
+ Coverage   92.73%   92.75%   +0.01%     
==========================================
  Files        2271     2271              
  Lines      226963   227031      +68     
==========================================
+ Hits       210476   210578     +102     
+ Misses      16487    16453      -34     

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

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

nit: this PR only adds internal formatter and view plumbing in storage_internal, and the instruments themselves are wired up in follow-up PRs. A feat(storage): prefix makes release/changes.sh list it in CHANGELOG.md as a user-visible feature before those instruments ship.

Consider using impl(storage): in the title and noting in the description that instrument recording lands in follow-up PRs.

data) {
EXPECT_THAT(
data.scope_metric_data_,
Contains(WithMetricsData(AllOf(

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 AllOf(...) runs on every non-empty Export(), while PeriodicExportingMetricReader starts its background collection as soon as MakeGrpcMeterProvider() attaches it. If a collection runs between latency->Record(...) and size->Record(...), that export contains only gcp.client.request.duration and fails this assertion.

Consider testing one instrument per provider like ValidateGrpcClientAttemptSize above, or recording matched instrument names across exports until all expected instruments arrive.

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 ValidateStorageClientHistograms to accumulate verified instrument names across exports until all expected instruments arrive with the right buckets, avoiding any race with periodic collection.


Replied by cpriti-o11y-agent


auto provider = MakeGrpcMeterProvider(std::move(mock), TestReaderOptions());
auto meter = provider->GetMeter(kStorageMeterName, version_string());
auto latency = meter->CreateDoubleHistogram("gcp.client.request.duration",

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.

ValidateStorageClientHistograms only records gcp.client.request.duration and gcp.storage.client.request.body.size, leaving the other nine latency instruments and gcp.storage.client.response.body.size unverified. A typo or misplaced entry in MakeGrpcMeterProvider() would silently fall back to default OpenTelemetry histogram buckets without failing a test.

Can we exercise every registered latency and size instrument name against ExpectedLatencyHistogram() and ExpectedSizeHistogram()?

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 ValidateStorageClientHistograms to record and verify every registered latency and size histogram instrument against ExpectedLatencyHistogram() and ExpectedSizeHistogram().


Replied by cpriti-o11y-agent

std::replace(s.begin(), s.end(), '.', '_');
return "storage.googleapis.com/client/" + std::move(s);
}
std::replace(s.begin(), s.end(), '.', '/');

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.

Moving std::replace(s.begin(), s.end(), '.', '/') below the kInternalMetricPrefix check changes how dotted internal metric names are formatted. internal/client/foo.bar now keeps a literal ., and internal.client.foo lands under customer-visible storage.googleapis.com/client/ instead of internal/client/.

Consider moving the kInternalMetricPrefix branch directly below this std::replace and adding a test case so internal-prefix handling stays unchanged.

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—moved the kInternalMetricPrefix check below std::replace and added test cases for dotted internal metric names.


Replied by cpriti-o11y-agent

if (s == "gcp.client.request.duration") {
return "storage.googleapis.com/client/request_duration";
}
constexpr std::string_view kStorageClientPrefix = "gcp.storage.client.";

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.

nit: std::string_view is used here without a direct #include <string_view>, relying on a transitive include.

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.

Added #include <string_view>.


Replied by cpriti-o11y-agent

// accidentally land in the internal namespace.
{"client/internal/client/duration",
"storage.googleapis.com/client/client/internal/client/duration"},
// Standard and debug OTel SDK instruments map to their Cloud Monitoring

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.

Now that these library-owned SDK instruments map to customer-visible storage.googleapis.com/client/, the comments on MetricNameFormatter (line 191) and kInternalMetricPrefix in metrics_exporter_options.h are stale. Both still say instruments created by this library go to internal/client/.

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 comments on kInternalMetricPrefix and MetricNameFormatter to reflect that customer-visible SDK instruments map under storage.googleapis.com/client/.


Replied by cpriti-o11y-agent

"gcp.client.request.duration",
"gcp.storage.client.ttfb",
"rpc.client.call.duration",
"http.client.request.duration",

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.

"http.client.request.duration" measures HTTP/REST requests, while MakeGrpcMeterProvider() lives in storage_grpc and is only wired into gRPC clients with api="GRPC". Will a follow-up PR share this provider setup with a REST metrics exporter, or can this be omitted?

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.

Removed "http.client.request.duration" from MakeGrpcMeterProvider() for now; we'll register it when REST metrics are wired in a follow-up PR.


Replied by cpriti-o11y-agent

AddLatencyHistogramView(*p, StorageMeterScope(),
MeterScope const storage_scope = StorageMeterScope();
for (char const* name : {
"gcp.client.request.duration",

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.

Consider defining shared constexpr std::string_view constants (like ChannelCreationLatencyInstrument() below.

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.

Defined shared constexpr std::string_view instrument name constants in metrics_meter_provider.h so view registration and tests stay in sync.


Replied by cpriti-o11y-agent

@cpriti-os cpriti-os changed the title feat(storage): add Cloud Monitoring metric name formatting and histogram views impl(storage): add Cloud Monitoring metric name formatting and histogram views Nov 19, 2026
@cpriti-os

Copy link
Copy Markdown
Contributor Author

Updated the PR title to impl(storage): and noted in the description that instrument recording lands in follow-up PRs.


Replied by cpriti-o11y-agent

…ram views

Update `MetricNameFormatterOption` in `MetricsExporterOptions()` to map
standard and debug OpenTelemetry SDK instrument names
(`gcp.client.request.duration` and `gcp.storage.client.*`) to their
Cloud Monitoring target descriptors under
`storage.googleapis.com/client/`.

Also register explicit latency (`s`) and size (`By`) histogram views
for the Storage client instruments under the `gl-cpp` meter scope in
`MakeGrpcMeterProvider()`.

[Generated-by: AI]
@cpriti-os
cpriti-os force-pushed the feat-storage-metrics-formatter-and-views branch from ddf6352 to 91e3430 Compare November 19, 2026 10:02

/// Storage SDK latency histogram instruments registered with explicit buckets.
inline constexpr std::array<std::string_view, 9>
kStorageLatencyHistogramInstruments = {{

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.

nit: since ValidateStorageClientHistograms now iterates kStorageLatencyHistogramInstruments too, a misspelled name here would pass the test and only show up as default buckets once the real instrument is created.

Will the follow-up PRs create instruments from named constants that these arrays are built from, rather than repeating the string literals?

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.

Yes—we'll share named constants between the instrument recorders and these view arrays in the follow-up wiring PRs so the names stay in sync.


Replied by cpriti-o11y-agent

std::this_thread::sleep_for(kExportInterval);
}
std::lock_guard<std::mutex> lk(mu);
EXPECT_EQ(verified.size(), total_expected);

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.

nit: this EXPECT_EQ(verified.size(), total_expected) only reports two counts on failure, so it does not say which instrument never arrived.

Consider EXPECT_THAT(verified, UnorderedElementsAreArray(...)) over the two instrument lists so a failure names the missing instrument.

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 final assertion to UnorderedElementsAreArray over the expected instrument names so any missing instrument is named on failure.


Replied by cpriti-o11y-agent


std::mutex mu;
std::set<std::string> verified;
auto const total_expected = expected_latency.size() + expected_size.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.

nit: here auto const total_expected deduces a std::size_t, and auto h on lines 309 and 315 hides the return type of CreateDoubleHistogram(). The style guide asks for explicit scalar types and for not hiding function return types behind auto.

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 auto with explicit std::size_t and pushed CreateDoubleHistogram() directly into instruments.


Replied by cpriti-o11y-agent

constexpr std::string_view kStorageClientPrefix = "gcp.storage.client.";
if (absl::StartsWith(s, kStorageClientPrefix)) {
s.erase(0, kStorageClientPrefix.size());
std::replace(s.begin(), s.end(), '.', '_');

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.

Missed this in the earlier pass: nothing here says why gcp.storage.client.* names are flattened with _ while every other name becomes a / path. A future editor could reasonably unify the two branches and break the mapping to the registered descriptors.

Consider a one-line comment stating that these SDK metrics are registered in Cloud Monitoring under flat underscore names.

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.

Added a comment noting that these SDK metrics are registered in Cloud Monitoring under flat underscore names.


Replied by cpriti-o11y-agent

@cpriti-os
cpriti-os enabled auto-merge (squash) November 19, 2026 13:42
@cpriti-os
cpriti-os merged commit 028b8a0 into googleapis:main Nov 19, 2026
68 checks passed
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