Skip to content

impl(storage): add server address and network path helpers for OTel metrics - #16683

Open
cpriti-os wants to merge 7 commits into
googleapis:mainfrom
cpriti-os:impl-storage-metrics-attributes
Open

cpriti-os wants to merge 7 commits into
googleapis:mainfrom
cpriti-os:impl-storage-metrics-attributes

Conversation

@cpriti-os

Copy link
Copy Markdown
Contributor

Adds internal attribute normalization helpers for Google Cloud Storage OpenTelemetry client metrics:

  • storage_internal::NormalizeServerAddress(std::string_view): normalizes a configured endpoint into the server.address attribute by stripping URI schemes (https://, dns:///, google-c2p:///, etc.), paths/queries, IPv6 brackets, and trailing :port.
  • storage_internal::NetworkPathOfPeer(std::string_view): classifies a gRPC peer URI or IP literal into "directpath" (34.126.0.0/18, 2001:4860:8040::/42), "cloudpath", or "unknown" for the network.path attribute.

This PR adds the internal helpers and unit tests; metric instrument wiring lands in follow-up PRs.


Created by cpriti-o11y-agent

@cpriti-os
cpriti-os requested review from a team as code owners November 19, 2026 10:07
@cpriti-os
cpriti-os requested a review from kalragauri November 19, 2026 10:07
@product-auto-label product-auto-label Bot added the api: storage Issues related to the Cloud Storage API. label Nov 19, 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 utility functions NormalizeServerAddress and NetworkPathOfPeer in a new metrics_attributes component to assist with GCS metrics, along with corresponding unit tests and build file updates. The review feedback identifies a bug in ParseIpv6 where malformed IPv6 addresses with out-of-range or unparseable groups are incorrectly validated as true, and suggests fixing this logic and adding a corresponding unit test case to ensure they are correctly classified as unknown.

Comment on lines +70 to +72
if (!ParseUint(ip.substr(0, colon), 16, groups[i]) || groups[i] > 0xFFFF) {
return true;
}

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

In ParseIpv6, if a non-empty group fails to parse or its value exceeds 0xFFFF (e.g., 80400), the function incorrectly returns true (treating it as a valid IPv6 address). This causes malformed IPv6 addresses to be classified as "cloudpath" instead of "unknown".

We should only return true early if the group is empty (which is valid for compressed IPv6 addresses like ::). If the group is not empty and parsing fails or the value is out of range, we should return false.

    auto const part = ip.substr(0, colon);
    if (part.empty()) return true;
    if (!ParseUint(part, 16, groups[i]) || groups[i] > 0xFFFF) {
      return false;
    }

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 ParseIpv6 to validate all hex groups and reject overflowing or malformed IPv6 addresses as "unknown".


Replied by cpriti-o11y-agent

{"", "unknown"},
{"unix:/tmp/sock", "unknown"},
{"not-an-ip", "unknown"},
{"ipv4:999.0.0.1:443", "unknown"},

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

Add a test case to verify that invalid IPv6 addresses with overflowing groups are correctly classified as "unknown".

      {"ipv4:999.0.0.1:443", "unknown"},
      {"ipv6:[2001:4860:80400::1]:443", "unknown"},

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 test cases for overflowing and malformed IPv6 addresses (as well as percent-encoded gRPC IPv6 peers and zone IDs).


Replied by cpriti-o11y-agent

@codecov

codecov Bot commented Nov 19, 2026 •

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 99.35484% with 1 line in your changes missing coverage. Please review.
✅ Project coverage is 92.75%. Comparing base (1ff1740) to head (c2ce663).
⚠️ Report is 13 commits behind head on main.

Files with missing lines Patch % Lines
...oogle/cloud/storage/internal/metrics_attributes.cc 99.30% 1 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main   #16683      +/-   ##
==========================================
+ Coverage   92.73%   92.75%   +0.01%     
==========================================
  Files        2271     2273       +2     
  Lines      226963   227118     +155     
==========================================
+ Hits       210476   210660     +184     
+ Misses      16487    16458      -29     

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

} // namespace

std::string NormalizeServerAddress(std::string_view endpoint) {
std::size_t const scheme_pos = endpoint.find("://");

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.

Matching only "://" here (and only dns, google-c2p, and google-c2p-experimental below) mishandles valid gRPC name-resolution targets without // or under xds:///. For example, dns:storage.googleapis.com normalizes to "dns", dns:storage.googleapis.com:443 stays unchanged, and xds:///storage.googleapis.com becomes "".

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, added xds to the scheme://[authority]/target list and handled dns:host[:port] without //, with test cases for both.


Replied by cpriti-o11y-agent

{"ipv6:[2001:4860:8040:]:443", "unknown"},
{"ipv6:[2001:4860:8040::1", "unknown"},
{"2001::4860::1", "unknown"},
{":::", "unknown"},

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 "unknown" cases ending here miss the unclosed %5B branch in UnwrapBrackets (the one uncovered line in Codecov) and several parser guards. Dropping res.ptr == end in ParseUint, the 8-group bounds check in ParseIpv6Half, or the head_count group-count checks in ParseIpv6 would still leave this test green.

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 "unknown" cases covering unclosed %5B, trailing non-numeric characters, too many IPv6 groups, and too few IPv6 groups without ::.


Replied by cpriti-o11y-agent


bool ParseIpv6(std::string_view ip, bool& is_directpath) {
if (!absl::StrContains(ip, ':')) return false;
if (absl::StartsWithIgnoreCase(ip, "::ffff:") &&

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 "::ffff:" check only unmaps compressed dotted-quad IPv4-mapped addresses, so hex forms like ipv6:[::ffff:227e:1]:443 or 0:0:0:0:0:ffff:227e:1 (34.126.0.1) classify as "cloudpath" instead of "directpath".

Can we combine head and tail into one 8-group array and check groups[0..4] == 0 && groups[5] == 0xffff against 34.126.0.0/18 as well (leaving ::ffff:1 as "cloudpath")?

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 idea, combined head and tail into one 8-group array and checked groups[0..4] == 0 && groups[5] == 0xffff against 34.126.0.0/18, with tests for ::ffff:227e:1 and 0:0:0:0:0:ffff:227e:1.


Replied by cpriti-o11y-agent

"storage.googleapis.com"},
{"https://storage.us-central1.rep.googleapis.com",
"storage.us-central1.rep.googleapis.com"},
{"https://storage.googleapis.com/path#frag", "storage.googleapis.com"},

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: in "https://storage.googleapis.com/path#frag", find_first_of("/?#") stops at '/' before reaching '#', so removing '#' from metrics_attributes.cc would still pass this test.

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 test input to "https://storage.googleapis.com#frag" so # is actually exercised.


Replied by cpriti-o11y-agent

{"ipv6:[::FfFf:34.126.0.1]:443", "directpath"},
{"ipv4:34.125.255.255:443", "cloudpath"},
{"ipv4:34.126.64.0:443", "cloudpath"},
{"ipv4:142.250.190.78:443", "cloudpath"},

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.

Rows like this one differ from the DirectPath ranges in several groups at once, so the "cloudpath" cases have no near-miss on the leading prefix. Dropping the octets[0] == 34, head[0] == 0x2001 or head[1] == 0x4860 comparison in metrics_attributes.cc still passes every row.

Consider adding "ipv4:35.126.0.1:443", "ipv6:[2002:4860:8040::1]:443" and "ipv6:[2001:4861:8040::1]:443" as "cloudpath" cases.

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 "ipv4:35.126.0.1:443", "ipv6:[2002:4860:8040::1]:443", and "ipv6:[2001:4861:8040::1]:443" as "cloudpath" cases.


Replied by cpriti-o11y-agent

return res.ec == std::errc{} && res.ptr == end;
}

bool ParseIpv4(std::string_view ip, bool& is_directpath) {

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.

ParseIpv4 and ParseIpv6 report validity through the return value and the classification through a bool& output parameter. The Google C++ style guide prefers return values over output parameters.

Consider returning std::optional<bool>, with std::nullopt meaning "not an IP address".

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 ParseIpv4 and ParseIpv6 to return std::optional<bool> instead of using a bool& output parameter.


Replied by cpriti-o11y-agent

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.

Running all cases in a loop inside TEST(MetricsAttributes, NetworkPathOfPeer) (and NormalizeServerAddress) collapses failures into a single test result. CI summaries cannot show which or how many cases broke without opening the log, and individual cases cannot be run with --gtest_filter.

Consider converting these to TEST_P suites so each case surfaces and filters independently.

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.

Converted both table tests to TEST_P suites so each case reports and filters independently.


Replied by cpriti-o11y-agent

@cpriti-os
cpriti-os requested a review from kalragauri November 24, 2026 10:10

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