Repository navigation
fix(bigtable): ensure metrics exporter honors ud - #16752
Conversation
There was a problem hiding this comment.
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.
| std::string const& ep = options.get<EndpointOption>(); | ||
| auto matches = [&ep](std::string const& candidate) { | ||
| return ep == candidate || ep == absl::StrCat(kC2pPrefix, candidate); | ||
| }; |
There was a problem hiding this comment.
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
- Prefer using
std::string_viewinstead ofabsl::string_viewin C++ code.
scotthart
left a comment
There was a problem hiding this comment.
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:
-
Host resource detection on
kWithoutHostResource(google/cloud/bigtable/data_connection.cc:73-91,google/cloud/bigtable/internal/defaults.cc:463-474):
ShouldExportMetrics()collapsesotel_internal::DetectMetricsExportMode()to abool, soMetricsOperationContextFactory(operation_context_factory.cc:196-198) andEnableGrpcMetrics()(grpc_metrics_exporter.cc:401-407) still unconditionally callotel::MakeResourceDetector()->Detect(). On a GDU GCE/GKE VM targeting a TPC universe with TPC service account credentials, this exports the GDU host'sclient_project,host_id,host_name, andregionto TPC Cloud Monitoring (and incurs metadata-server retry latency off-GCP). Consider propagatingMetricsExportModeand passingResource::GetEmpty()unlessmode == MetricsExportMode::kWithHostResource, matching Cloud Storage (storage/internal/grpc/enable_metrics.cc:43-49). -
VIP matching & env precedence in
otel_internal::MetricsExporterConnectionOptions()(google/cloud/opentelemetry/internal/monitoring_exporter.cc:295-319):
UnlikeDetectMetricsExportMode(),MetricsExporterConnectionOptions()does not useEffectiveUniverseDomain(options)orEndpointHost(). Endpoints like"restricted.googleapis.com:443"or"private.tpc.invalid:443"passDetectMetricsExportMode()but failmatches(...), silently droppingEndpointOptionand falling back tomonitoring.<ud>. UsingEffectiveUniverseDomain(options)andabsl::EqualsIgnoreCase(EndpointHost(ep), ...)keeps both helpers consistent. -
Existing metrics tests silently bypassed (
google/cloud/bigtable/data_connection_test.cc:131-187):
TestingOtelCollectorEnvVarandDirectPathMetricsModeOptionEnableduseTestOptions()(which setsMakeInsecureCredentials()andlocalhost:1), soShouldExportMetrics()now returnsfalseand skipsMetricsExporterConnectionOptions(),EnableGrpcMetrics(), andMetricsOperationContextFactorywhile still passingEXPECT_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. -
DataEndpointOptionfallback symmetry (google/cloud/bigtable/internal/defaults.cc:463-487):
ShouldExportMetrics()falls back toDataEndpointOptionwhenEndpointOptionis unset, whileMetricsExporterConnectionOptions()does not. Sharing a small helper keeps direct calls consistent. -
Nit:
canonical_serviceparameter (google/cloud/opentelemetry/internal/monitoring_exporter.cc:308-318):
if (matches(ep_canonical)) return result;produces the same return value as falling through when neitherprivate.<ud>norrestricted.<ud>matches. Droppingcanonical_servicesimplifies the helper signature.
Codecov Report❌ Patch coverage is 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. 🚀 New features to boost your workflow:
|
| 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) { |
There was a problem hiding this comment.
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>.
| }; | ||
|
|
||
| /// How Cloud Monitoring metrics export should be configured for a client. | ||
| enum class MetricsExportMode { |
There was a problem hiding this comment.
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.
| Eq(MetricsExportMode::kWithoutHostResource)); | ||
| } | ||
|
|
||
| TEST_F(DetectMetricsExportModeTest, ClassifiesCredentials) { |
There was a problem hiding this comment.
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.
kalragauri
left a comment
There was a problem hiding this comment.
LGTM from Storage perspective with minor nits. Deferring to Bigtable owners on the Bigtable-specific changes.
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 unsetEndpointOptionandAuthorityOptionwhile forwarding all remaining client options (including live credentials) tomonitoring_v3::MakeMetricServiceConnection(). If a workload configured a non-default universe or custom endpoint using onlyEndpointOption/DataEndpointOption(without settingUniverseDomainOptionorGOOGLE_CLOUD_UNIVERSE_DOMAIN), the exporter fell back tomonitoring.googleapis.comusing the client's credentials and metadata. It also droppedprivate.<ud>/restricted.<ud>VIP endpoints and forwarded unrelated Bigtable-specific options (such asGrpcNumChannelsOptionandUserProjectOption).