Skip to content

feat(bigtable): add AttemptLatency2 metric and populate peer info labels#16095

Open
elinorli wants to merge 1 commit into
googleapis:mainfrom
elinorli:directpath
Open

feat(bigtable): add AttemptLatency2 metric and populate peer info labels#16095
elinorli wants to merge 1 commit into
googleapis:mainfrom
elinorli:directpath

Conversation

@elinorli
Copy link
Copy Markdown
Contributor

This introduces the AttemptLatency2 metric for DirectPath to record attempt latencies with the fields extracted from the decoded PeerInfo trailing metadata, populating peer_info_labels_ and forwarding them to IntoLabelMap.

Also added
AttemptLatency2Test to test the newly populated peer info labels. Refactored SetClusterZone to use the new helper function CreateServerMetadata.

This introduces the `AttemptLatency2` metric for DirectPath to record attempt latencies with the fields extracted from the decoded `PeerInfo` trailing metadata,
populating `peer_info_labels_` and forwarding them to `IntoLabelMap`.

Also added
`AttemptLatency2Test` to test the newly populated peer info labels. Refactored `SetClusterZone` to use the new helper function `CreateServerMetadata`.
@elinorli elinorli requested a review from a team as a code owner May 13, 2026 23:55
@product-auto-label product-auto-label Bot added the api: bigtable Issues related to the Bigtable API. label May 13, 2026
Copy link
Copy Markdown

@gemini-code-assist gemini-code-assist Bot left a comment

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 AttemptLatency2 metric, which extends latency tracking by including peer information such as transport type, region, and subzone extracted from gRPC trailing metadata. The implementation includes new data structures for peer labels, utility functions for decoding metadata, and the integration of this metric into various Bigtable operations. Review feedback suggests a performance optimization in GetPeerInfoFromTrailingMetadata to use a constant reference when accessing trailing metadata to avoid an unnecessary copy of the multimap.


absl::optional<google::bigtable::v2::PeerInfo> GetPeerInfoFromTrailingMetadata(
grpc::ClientContext const& client_context) {
auto metadata = client_context.GetServerTrailingMetadata();
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 GetServerTrailingMetadata() method returns a reference to a std::multimap. Using auto here results in a copy of the entire map, which is inefficient. Prefer using auto const& to avoid the unnecessary copy.

  auto const& metadata = client_context.GetServerTrailingMetadata();

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Agreed. Let's avoid making a copy if we don't have to.


absl::optional<google::bigtable::v2::PeerInfo> GetPeerInfoFromTrailingMetadata(
grpc::ClientContext const& client_context) {
auto metadata = client_context.GetServerTrailingMetadata();
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

bigtable-peer-info is sent as initialmetadata. so i would check it it initial metadata and if it is not present, check it in GetServerTrailingMetadata

auto iter = metadata.find("bigtable-peer-info");
if (iter == metadata.end()) return absl::nullopt;
std::string decoded;
if (!absl::Base64Unescape(
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

check if we need [WebSafeBase64Unescape]

}

std::string TransportTypeToString(
google::bigtable::v2::PeerInfo::TransportType type) {
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

I think we can use implict proto conversion here. absl::AsciiStrToLower(google::bigtable::v2::PeerInfo::TransportType_Name(type));

@codecov
Copy link
Copy Markdown

codecov Bot commented May 14, 2026

Codecov Report

❌ Patch coverage is 93.84058% with 17 lines in your changes missing coverage. Please review.
✅ Project coverage is 92.70%. Comparing base (cc2ed18) to head (dbd8386).

Files with missing lines Patch % Lines
google/cloud/bigtable/internal/metrics.cc 78.26% 15 Missing ⚠️
...oud/bigtable/internal/operation_context_factory.cc 77.77% 2 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main   #16095      +/-   ##
==========================================
- Coverage   92.70%   92.70%   -0.01%     
==========================================
  Files        2353     2353              
  Lines      218354   218612     +258     
==========================================
+ Hits       202428   202664     +236     
- Misses      15926    15948      +22     

☔ View full report in Codecov by Sentry.
📢 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.

absl::optional<google::bigtable::v2::ResponseParams>
GetResponseParamsFromTrailingMetadata(
grpc::ClientContext const& client_context);
absl::optional<google::bigtable::v2::PeerInfo> GetPeerInfoFromTrailingMetadata(
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Let's use std::optional instead of absl::optional in new code.


absl::optional<google::bigtable::v2::PeerInfo> GetPeerInfoFromTrailingMetadata(
grpc::ClientContext const& client_context) {
auto metadata = client_context.GetServerTrailingMetadata();
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Agreed. Let's avoid making a copy if we don't have to.

auto metadata = client_context.GetServerTrailingMetadata();
// Base64 encoded peer info header key defined by the server.
auto iter = metadata.find("bigtable-peer-info");
if (iter == metadata.end()) return absl::nullopt;
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

std::nullopt instead of absl::nullopt

}
google::bigtable::v2::PeerInfo p;
if (p.ParseFromString(decoded)) return p;
return absl::nullopt;
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

std::nullopt


void AttemptLatency2::PreCall(opentelemetry::context::Context const&,
PreCallParams const& p) {
attempt_start_ = std::move(p.attempt_start);
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

p is passed by const& and cannot be moved from.

SetServerMetadata(client_context, server_metadata);

EXPECT_THAT(GetPeerInfoFromTrailingMetadata(client_context),
Eq(absl::nullopt));
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

std::nullopt

SetServerMetadata(client_context, server_metadata);

EXPECT_THAT(GetPeerInfoFromTrailingMetadata(client_context),
Eq(absl::nullopt));
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

std::nullopt

AttemptLatency2 attempt_latency("my-instrument-scope", mock_provider);
ResourceLabels resource_labels{"my-project-id", "my-instance", "my-table", "",
""};
DataLabels data_labels{"my-method", "my-streaming", "my-client-name",
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

This formatting is unusual. Please remove the extra white space between arguments.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

api: bigtable Issues related to the Bigtable API.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants