feat(spanner): support user-provided OpenTelemetry for client metrics export#13679
feat(spanner): support user-provided OpenTelemetry for client metrics export#13679rahul2393 wants to merge 1 commit into
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
b5fa19a to
1add8f1
Compare
| throws IOException { | ||
| // Only do the check if the emulator environment variable has been set to localhost. | ||
| if (isEmulatorEnabled(options, emulatorHost)) { | ||
| String resolvedEmulatorHost = emulatorHost != null ? emulatorHost : options.getEndpoint(); |
There was a problem hiding this comment.
Why are we doing this?
There was a problem hiding this comment.
Earlier only env was checked, now this is updated to also check if emulator is enabled programmatically via SpannerOptions, to log that in case it's set we are adding this
… export Adds the MetricsProvider family (Default/Noop/CustomOpenTelemetry), SpannerOptions.Builder#setClientMetricsProvider, and SpannerMetrics#configureMeterProviderBuilder so client metrics can be recorded on a caller-owned OpenTelemetry in the spanner/client namespace, decoupled from the Cloud Monitoring built-in metrics flag. Also broadens the emulator gate (SpannerOptions#isEmulatorEnabled, enablegRPCMetrics overload) so no client metrics are recorded against the emulator. Split from PR googleapis#13679 (client-metrics half). Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
af8fd4a to
bfc4aea
Compare
… export Adds the MetricsProvider family (Default/Noop/CustomOpenTelemetry), SpannerOptions.Builder#setClientMetricsProvider, and SpannerMetrics#configureMeterProviderBuilder so client metrics can be recorded on a caller-owned OpenTelemetry in the spanner/client namespace, decoupled from the Cloud Monitoring built-in metrics flag. Also broadens the emulator gate (SpannerOptions#isEmulatorEnabled, enablegRPCMetrics overload) so no client metrics are recorded against the emulator.
bfc4aea to
818893b
Compare
…location-aware routing Adds the ENDPOINT_KEY metric attribute: KeyAwareChannel labels each attempt with the endpoint the location-aware fastpath routed it to, and the custom-export attempt views allow the label through their attribute filter. Cloud Monitoring views never include it. Stacked follow-up to the client metrics export change (googleapis#13679); until that merges this commit also carries the client metrics export work. Merge after googleapis#13679 lands.
Summary
Adds Client Metrics: a new, 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.
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.Emulator handling
Client metrics are not recorded when Spanner is pointed at the emulator. To make that reliable when the emulator is configured programmatically (via
setEmulatorHost(...)) and not only through theSPANNER_EMULATOR_HOSTenvironment variable, this PR broadens emulator detection:SpannerOptions.isEmulatorEnabled()now also recognizes the builder-configured emulator host, and the connection-check error message is generalized to describe both the environment-variable and programmatic configuration paths. This gates both the client and Cloud Monitoring metrics sinks off against the emulator using a single detection predicate. Called out explicitly here for reviewer visibility.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