feat(spanner): make built-in metrics enabled by default by sinhasubham · Pull Request #1459 · googleapis/python-spanner · GitHub
Skip to content
This repository was archived by the owner on Jun 8, 2026. It is now read-only.

feat(spanner): make built-in metrics enabled by default#1459

Merged
sinhasubham merged 2 commits into
mainfrom
fix/builtin-metrics-env-var
Dec 4, 2025
Merged

feat(spanner): make built-in metrics enabled by default#1459
sinhasubham merged 2 commits into
mainfrom
fix/builtin-metrics-env-var

Conversation

@sinhasubham

Copy link
Copy Markdown
Contributor

Make built-in metrics enabled by default

This change inverts the logic for enabling built-in OpenTelemetry metrics. Previously, metrics were disabled by default and could be enabled by setting ENABLE_SPANNER_METRICS_ENV_VAR=true.

With this update, metrics are now enabled by default to provide better out-of-the-box observability for users.

To disable metrics, users must now set the new environment variable:
SPANNER_DISABLE_BUILTIN_METRICS=true

The old ENABLE_SPANNER_METRICS_ENV_VAR is no longer used. Unit tests have been updated to reflect this new opt-out behavior.

BREAKING CHANGE: Built-in metrics are now enabled by default. Users who previously did not set any environment variables will have metrics collection and export turned on automatically after upgrading. To restore the previous behavior and disable metrics, thry have to set the SPANNER_DISABLE_BUILTIN_METRICS environment variable to true.

@sinhasubham sinhasubham requested review from a team November 24, 2025 04:18
@product-auto-label product-auto-label Bot added size: s Pull request size is small. api: spanner Issues related to the googleapis/python-spanner API. labels Nov 24, 2025
@gemini-code-assist

Copy link
Copy Markdown
Contributor

@gemini-code-assist gemini-code-assist Bot 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.

Code Review

This pull request correctly inverts the logic for enabling built-in metrics, making them on by default, which is a great improvement for out-of-the-box observability. The changes to the environment variable and the corresponding updates in the client logic and unit tests are clear and well-executed. I've noted one minor point about a leftover constant that could be cleaned up to improve maintainability.

Comment thread google/cloud/spanner_v1/client.py

def _get_spanner_enable_builtin_metrics():
return os.getenv(ENABLE_SPANNER_METRICS_ENV_VAR) == "true"
return os.getenv(SPANNER_DISABLE_BUILTIN_METRICS_ENV_VAR) != "true"

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.

Since we are making Metrics default enabled with this changes, please make sure that metrics are not enabled when emulator is enabled and when customers use NoCredentials

Check https://github.com/googleapis/java-spanner/blob/main/google-cloud-spanner/src/main/java/com/google/cloud/spanner/SpannerOptions.java#L2088

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

yes these cases are getting handled.
Emulator condition: https://github.com/googleapis/python-spanner/blob/main/google/cloud/spanner_v1/client.py#L257

NoCredential user condition: https://github.com/googleapis/python-spanner/blob/main/google/cloud/spanner_v1/client.py#L224-L225
This sets emulator setting for NoCredentials users making them act like emulator which means disabled metrics.

@surbhigarg92

Copy link
Copy Markdown
Contributor

@product-auto-label product-auto-label Bot added size: m Pull request size is medium. and removed size: s Pull request size is small. labels Dec 2, 2025
@sinhasubham sinhasubham force-pushed the fix/builtin-metrics-env-var branch from 28abdfd to b13ea7b Compare December 2, 2025 10:20
@sinhasubham sinhasubham force-pushed the fix/builtin-metrics-env-var branch from b13ea7b to 63ca819 Compare December 2, 2025 11:19
@sinhasubham

Copy link
Copy Markdown
Contributor Author

Got a confirmation from @surbhigarg92 that unexpected enabling of metrics for customers who did not set ENABLE_SPANNER_METRICS_ENV_VAR previously is not a breaking change and can be merged. Merging this PR.

@sinhasubham sinhasubham merged commit 64aebe7 into main Dec 4, 2025
22 checks passed
@sinhasubham sinhasubham deleted the fix/builtin-metrics-env-var branch December 4, 2025 05:57
@surbhigarg92 surbhigarg92 added the release-please:force-run To run release-please label Dec 9, 2025
@release-please release-please Bot removed the release-please:force-run To run release-please label Dec 9, 2025
@surbhigarg92 surbhigarg92 added the release-please:force-run To run release-please label Dec 9, 2025
@release-please release-please Bot removed the release-please:force-run To run release-please label Dec 9, 2025
chalmerlowe pushed a commit that referenced this pull request Dec 9, 2025
Make built-in metrics enabled by default

This change inverts the logic for enabling built-in OpenTelemetry
metrics. Previously, metrics were disabled by default and could be
enabled by setting `ENABLE_SPANNER_METRICS_ENV_VAR=true`.

With this update, metrics are now enabled by default to provide better
out-of-the-box observability for users.

To disable metrics, users must now set the new environment variable:
`SPANNER_DISABLE_BUILTIN_METRICS=true`

The old `ENABLE_SPANNER_METRICS_ENV_VAR` is no longer used. Unit tests
have been updated to reflect this new opt-out behavior.

**BREAKING CHANGE**: Built-in metrics are now enabled by default. Users
who previously did not set any environment variables will have metrics
collection and export turned on automatically after upgrading. To
restore the previous behavior and disable metrics, thry have to set the
`SPANNER_DISABLE_BUILTIN_METRICS` environment variable to `true`.
gcf-merge-on-green Bot pushed a commit that referenced this pull request Dec 10, 2025
PR created by the Librarian CLI to initialize a release. Merging this PR will auto trigger a release.

Librarian Version: v1.0.0
Language Image: us-central1-docker.pkg.dev/cloud-sdk-librarian-prod/images-prod/python-librarian-generator@sha256:8e2c32496077054105bd06c54a59d6a6694287bc053588e24debe6da6920ad91
<details><summary>google-cloud-spanner: 3.60.0</summary>

## [3.60.0](https://togithub.com/googleapis/python-spanner/compare/v3.59.0...v3.60.0) (2025-12-10)

### Features

* make built-in metrics enabled by default (#1459) ([64aebe7](https://togithub.com/googleapis/python-spanner/commit/64aebe7e))

* Exposing AutoscalingConfig in InstancePartition ([8b6f154](https://togithub.com/googleapis/python-spanner/commit/8b6f1540))

* add support for experimental host (#1452) ([9535e5e](https://togithub.com/googleapis/python-spanner/commit/9535e5e0))

* enable OpenTelemetry metrics and tracing by default (#1410) ([bb5095d](https://togithub.com/googleapis/python-spanner/commit/bb5095df))

* add cloud.region, request_tag and transaction_tag in span attributes (#1449) ([d37fb80](https://togithub.com/googleapis/python-spanner/commit/d37fb80a))

* Add QueryAdvisorResult for query plan (PiperOrigin-RevId: 832425466) ([e08260f](https://togithub.com/googleapis/python-spanner/commit/e08260fe))

* Add Send and Ack mutations for Queues (PiperOrigin-RevId: 832425466) ([e08260f](https://togithub.com/googleapis/python-spanner/commit/e08260fe))

* Add Spanner location API (#1457) (PiperOrigin-RevId: 833474957) ([e08260f](https://togithub.com/googleapis/python-spanner/commit/e08260fe))

### Bug Fixes

* Deprecate credentials_file argument ([8b6f154](https://togithub.com/googleapis/python-spanner/commit/8b6f1540))

* configure keepAlive time for gRPC TCP connections (#1448) ([efb2833](https://togithub.com/googleapis/python-spanner/commit/efb2833e))

* Provide Spanner Option to disable metrics (#1460) ([f1ebc43](https://togithub.com/googleapis/python-spanner/commit/f1ebc43b))

### Documentation

* Update description for the BatchCreateSessionsRequest and Session (PiperOrigin-RevId: 832425466) ([e08260f](https://togithub.com/googleapis/python-spanner/commit/e08260fe))

* Update description for the IsolationLevel (PiperOrigin-RevId: 832425466) ([e08260f](https://togithub.com/googleapis/python-spanner/commit/e08260fe))

</details>
@ericzundel

Copy link
Copy Markdown

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

Labels

api: spanner Issues related to the googleapis/python-spanner API. size: m Pull request size is medium.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants