feat(spanner): support user-provided OpenTelemetry for built-in client metrics and endpoint attribute for location-aware routing#13679
Conversation
There was a problem hiding this comment.
Code Review
This pull request introduces the MetricsProvider interface and its implementations (DefaultMetricsProvider, NoopMetricsProvider, and CustomOpenTelemetryMetricsProvider) to allow customization of how built-in client-side metrics are collected and exported in the Cloud Spanner Java client. This enables recording metrics on a caller-provided OpenTelemetry instance rather than exporting them directly to Google Cloud Monitoring, which is particularly useful for Spanner Omni clients. Additionally, the changes add support for registering built-in metric views on a custom meter provider, introduce an endpoint attribute for location-aware routing, and include comprehensive integration and unit tests to validate these new features. There are no review comments, so I have no feedback to provide.
124a9c7 to
383daa2
Compare
|
/gemini review |
There was a problem hiding this comment.
Code Review
This pull request introduces a customizable MetricsProvider interface for the Cloud Spanner Java client, allowing users to configure how built-in metrics are collected and exported. It adds support for a CustomOpenTelemetryMetricsProvider to record metrics on a user-provided OpenTelemetry instance, alongside the default Google Cloud Monitoring export and a NoopMetricsProvider to disable metrics entirely. The changes also include labeling attempt metrics with routed endpoints on the location-aware fastpath, and extensive integration tests verifying the dual-sink and gRPC-layer metrics behavior. Feedback on the code changes highlights a potential UnsupportedOperationException in BuiltInMetricsConstant.java due to mutating a set returned by Collectors.toSet(), with a suggestion to use Collectors.toCollection(HashSet::new) to guarantee mutability.
f7bd207 to
5a63c9e
Compare
72b0be7 to
03c7825
Compare
…t metrics and endpoint attribute for location-aware routing
b5fa19a to
1add8f1
Compare
Summary
Adds Client Metrics: a new, independent opt-in feature that exports Spanner's client-side metrics to a caller-provided OpenTelemetry pipeline (OTLP, Prometheus, any exporter), including on Spanner Omni where the existing Cloud Monitoring metrics are unavailable. Also adds an optional
endpointattribute on the metrics for the location-aware routing path.Fully opt-in and fully decoupled from the existing built-in (Cloud Monitoring) metrics. Default behavior is unchanged: without a client-metrics provider, nothing new happens, and built-in metrics continue to export to Cloud Monitoring exactly as before.
Two independent features
setBuiltInMetricsEnabledand theSPANNER_DISABLE_BUILTIN_METRICSenvironment variable, exactly as today. Not available on Spanner Omni.setClientMetricsProvider(...)— aCustomOpenTelemetryMetricsProviderturns it on;NoopMetricsProvider(or no provider) turns it off. Works on all instance types, Omni included.The two are decoupled:
setBuiltInMetricsEnabledand the env var affect only the built-in Cloud Monitoring sink and have no effect on client metrics; the client-metrics provider affects only the caller-owned sink and has no effect on built-in metrics. Under the hood these are the same client instruments, exported under the distinctspanner/clientnamespace — the difference is the export path, not the metrics.Motivation
Client metrics currently export only to Cloud Monitoring, which is unavailable on Spanner Omni. Customers running on Omni (or who standardize on their own observability stack) had no way to receive these metrics. This lets them route the metrics to any OpenTelemetry exporter, independently of the built-in Cloud Monitoring configuration.
API
setClientMetricsProvider(MetricsProvider)— controls the client-metrics export destination only.CustomOpenTelemetryMetricsProvider— export client metrics to a caller-provided OpenTelemetry.NoopMetricsProvider— disable client-metrics export.DefaultMetricsProvider(default) — no client-metrics export.SpannerMetrics.configureMeterProviderBuilder(SdkMeterProviderBuilder)— registers the client-metric views on the caller's meter-provider builder (metric names underspanner/client, histogram buckets, attribute filtering). Optional but recommended; without it, metrics record with raw instrument names, default buckets, and unfiltered attributes.Behavior
setBuiltInMetricsEnabledor the env var;NoopMetricsProvider/no provider disables it.setBuiltInMetricsEnabled/env as today; on Omni they never run.spanner/client), separate from the reserved Cloud Monitoring namespace (spanner.googleapis.com/...), so re-exporting these metrics to Cloud Monitoring yourself cannot collide with the reserved Spanner metrics.endpointattribute is added to client attempt metrics only on the Omni location-aware routing path, carrying the resolved endpoint host. It never appears on operation metrics or on the default path, so standard deployments see no new cardinality.Notes
OpenTelemetrySdkinstance; all other client metrics work with any OpenTelemetry implementation.