Skip to content

fix(bigtable): ensure metrics exporter honors ud - #16752

Merged
scotthart merged 5 commits into
googleapis:mainfrom
scotthart:bigtable_metrics_export_ud
Dec 4, 2026
Merged

scotthart merged 5 commits into
googleapis:mainfrom
scotthart:bigtable_metrics_export_ud

Conversation

@scotthart

Copy link
Copy Markdown
Member

Align Cloud Bigtable's built-in OpenTelemetry metrics exporter with the fail-closed endpoint and credential validation recently introduced for Cloud Storage (#16708), and extract the shared validation and connection-option allowlisting helpers into google/cloud/opentelemetry/internal/.

Previously, bigtable::internal::MetricsExporterConnectionOptions() only unset EndpointOption and AuthorityOption while forwarding all remaining client options (including live credentials) to monitoring_v3::MakeMetricServiceConnection(). If a workload configured a non-default universe or custom endpoint using only EndpointOption / DataEndpointOption (without setting UniverseDomainOption or GOOGLE_CLOUD_UNIVERSE_DOMAIN), the exporter fell back to monitoring.googleapis.com using the client's credentials and metadata. It also dropped private.<ud> / restricted.<ud> VIP endpoints and forwarded unrelated Bigtable-specific options (such as GrpcNumChannelsOption and UserProjectOption).

@scotthart
scotthart requested review from a team as code owners December 3, 2026 18:20
@product-auto-label product-auto-label Bot added the api: bigtable Issues related to the Bigtable API. label Dec 3, 2026
@scotthart
scotthart requested a review from kalragauri December 3, 2026 18:20

@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 centralizes and refactors the logic for detecting OpenTelemetry metrics export compatibility and configuring connection options across the SDK, specifically integrating it into Bigtable and Storage. It introduces shared implementations for DetectMetricsExportMode and MetricsExporterConnectionOptions in the otel_internal namespace, accompanied by comprehensive unit tests and documentation updates. The feedback suggests a performance optimization in MetricsExporterConnectionOptions to avoid temporary string allocations by stripping the endpoint prefix once upfront instead of inside the lambda.

Comment on lines +311 to +314
std::string const& ep = options.get<EndpointOption>();
auto matches = [&ep](std::string const& candidate) {
return ep == candidate || ep == absl::StrCat(kC2pPrefix, candidate);
};

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 matches lambda currently constructs a new string using absl::StrCat on every invocation to check if the endpoint starts with kC2pPrefix. Since matches can be called multiple times, we can optimize this by stripping the prefix from the endpoint view once upfront. This avoids any temporary string allocations and simplifies the comparison logic. This also correctly utilizes std::string_view instead of absl::string_view.

  std::string_view ep_view = options.get<EndpointOption>();
  if (absl::StartsWith(ep_view, kC2pPrefix)) {
    ep_view.remove_prefix(kC2pPrefix.size());
  }
  auto matches = [ep_view](std::string_view candidate) {
    return ep_view == candidate;
  };
References
  1. Prefer using std::string_view instead of absl::string_view in C++ code.

@scotthart scotthart left a comment

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Thanks for aligning Bigtable's metrics exporter with #16708, extracting the shared helpers into google/cloud/opentelemetry/internal/, and fixing the range-loop warning in 328a5e2a8b! A few remaining items to consider before merging:

  1. Host resource detection on kWithoutHostResource (google/cloud/bigtable/data_connection.cc:73-91, google/cloud/bigtable/internal/defaults.cc:463-474):
    ShouldExportMetrics() collapses otel_internal::DetectMetricsExportMode() to a bool, so MetricsOperationContextFactory (operation_context_factory.cc:196-198) and EnableGrpcMetrics() (grpc_metrics_exporter.cc:401-407) still unconditionally call otel::MakeResourceDetector()->Detect(). On a GDU GCE/GKE VM targeting a TPC universe with TPC service account credentials, this exports the GDU host's client_project, host_id, host_name, and region to TPC Cloud Monitoring (and incurs metadata-server retry latency off-GCP). Consider propagating MetricsExportMode and passing Resource::GetEmpty() unless mode == MetricsExportMode::kWithHostResource, matching Cloud Storage (storage/internal/grpc/enable_metrics.cc:43-49).

  2. VIP matching & env precedence in otel_internal::MetricsExporterConnectionOptions() (google/cloud/opentelemetry/internal/monitoring_exporter.cc:295-319):
    Unlike DetectMetricsExportMode(), MetricsExporterConnectionOptions() does not use EffectiveUniverseDomain(options) or EndpointHost(). Endpoints like "restricted.googleapis.com:443" or "private.tpc.invalid:443" pass DetectMetricsExportMode() but fail matches(...), silently dropping EndpointOption and falling back to monitoring.<ud>. Using EffectiveUniverseDomain(options) and absl::EqualsIgnoreCase(EndpointHost(ep), ...) keeps both helpers consistent.

  3. Existing metrics tests silently bypassed (google/cloud/bigtable/data_connection_test.cc:131-187):
    TestingOtelCollectorEnvVar and DirectPathMetricsModeOptionEnabled use TestOptions() (which sets MakeInsecureCredentials() and localhost:1), so ShouldExportMetrics() now returns false and skips MetricsExporterConnectionOptions(), EnableGrpcMetrics(), and MetricsOperationContextFactory while still passing EXPECT_NE(conn, nullptr). Updating them to use in-universe options (e.g., MakeAccessTokenCredentials + GOOGLE_CLOUD_CPP_METRIC_SERVICE_ENDPOINT=localhost:1) and asserting metrics export is not disabled will preserve coverage.

  4. DataEndpointOption fallback symmetry (google/cloud/bigtable/internal/defaults.cc:463-487):
    ShouldExportMetrics() falls back to DataEndpointOption when EndpointOption is unset, while MetricsExporterConnectionOptions() does not. Sharing a small helper keeps direct calls consistent.

  5. Nit: canonical_service parameter (google/cloud/opentelemetry/internal/monitoring_exporter.cc:308-318):
    if (matches(ep_canonical)) return result; produces the same return value as falling through when neither private.<ud> nor restricted.<ud> matches. Dropping canonical_service simplifies the helper signature.

@codecov

codecov Bot commented Dec 3, 2026 •

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 98.43049% with 7 lines in your changes missing coverage. Please review.
✅ Project coverage is 92.94%. Comparing base (261271c) to head (4af7a20).
⚠️ Report is 6 commits behind head on main.

Files with missing lines Patch % Lines
...oud/bigtable/internal/operation_context_factory.cc 66.66% 4 Missing ⚠️
google/cloud/bigtable/data_connection_test.cc 99.11% 1 Missing ⚠️
google/cloud/bigtable/internal/defaults.cc 95.83% 1 Missing ⚠️
google/cloud/bigtable/internal/defaults_test.cc 99.56% 1 Missing ⚠️
Additional details and impacted files
@@           Coverage Diff            @@
##             main   #16752    +/-   ##
========================================
  Coverage   92.93%   92.94%            
========================================
  Files        2282     2282            
  Lines      232838   233105   +267     
========================================
+ Hits       216399   216663   +264     
- Misses      16439    16442     +3     

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

std::string const ep_private = absl::StrCat("private.", ud);
std::string const ep_restricted = absl::StrCat("restricted.", ud);
std::string const& ep = options.get<EndpointOption>();
auto matches = [&ep](std::string const& candidate) {

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 matches lambda compares the raw EndpointOption string against candidate and google-c2p:/// without EndpointHost() or EffectiveUniverseDomain(options). IIUC, VIP endpoints that DetectMetricsExportMode accepts, such as restricted.googleapis.com:443, mixed-case hosts, or a VIP matching GOOGLE_CLOUD_UNIVERSE_DOMAIN without UniverseDomainOption, silently drop EndpointOption and fall back to monitoring.<ud>.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Fixed.

};

/// How Cloud Monitoring metrics export should be configured for a client.
enum class MetricsExportMode {

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: MetricsExportMode, DetectMetricsExportMode(), and MetricsExporterConnectionOptions() only depend on Options, whereas monitoring_exporter.h pulls in metric_client.h, metric_connection.h, and OpenTelemetry SDK headers. That forces storage/internal/grpc/metrics_exporter_options.h to keep a duplicate MetricsExportMode enum and a 1:1 translation switch in metrics_exporter_options.cc.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Fixed.

Eq(MetricsExportMode::kWithoutHostResource));
}

TEST_F(DetectMetricsExportModeTest, ClassifiesCredentials) {

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: ClassifiesCredentials (and the surrounding DetectMetricsExportModeTest cases) do not directly cover MakeUserAccountCredentials() (AuthorizedUserConfig), invalid universe_domain values in credentials JSON (empty or non-string), malformed or unsupported ADC files, or empty DNS labels such as storage..googleapis.com. Those branches in monitoring_exporter.cc are currently only exercised indirectly through metrics_exporter_impl_test.cc in Storage, not when testing opentelemetry in isolation.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Fixed.

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

LGTM from Storage perspective with minor nits. Deferring to Bigtable owners on the Bigtable-specific changes.

@scotthart
scotthart merged commit 32df30b into googleapis:main Dec 4, 2026
68 checks passed
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