Skip to content

Conversation

@sinhasubham
Copy link
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 as code owners 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

Summary of Changes

Hello @sinhasubham, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed!

This pull request significantly enhances the out-of-the-box observability for Spanner users by enabling built-in OpenTelemetry metrics by default. The change shifts the control mechanism from an opt-in model to an opt-out model, simplifying metric collection for most users while still providing a clear way to disable them if needed.

Highlights

  • Default Metrics Behavior: Built-in OpenTelemetry metrics are now enabled by default, inverting the previous opt-in behavior to provide better out-of-the-box observability.
  • New Opt-Out Mechanism: Users can now disable metrics by setting the new environment variable SPANNER_DISABLE_BUILTIN_METRICS=true.
  • Deprecated Environment Variable: The old ENABLE_SPANNER_METRICS_ENV_VAR is no longer used and has been removed from the codebase.
  • Breaking Change: This change is a breaking change as metrics will automatically be collected and exported for users who previously did not set any environment variables. To restore the previous behavior, the new SPANNER_DISABLE_BUILTIN_METRICS environment variable must be set to true.
Using Gemini Code Assist

The full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips.

Invoking Gemini

You can request assistance from Gemini at any point by creating a comment using either /gemini <command> or @gemini-code-assist <command>. Below is a summary of the supported commands on the current page.

Feature Command Description
Code Review /gemini review Performs a code review for the current pull request in its current state.
Pull Request Summary /gemini summary Provides a summary of the current pull request in its current state.
Comment @gemini-code-assist Responds in comments when explicitly tagged, both in pull request comments and review comments.
Help /gemini help Displays a list of available commands.

Customization

To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a .gemini/ folder in the base of the repository. Detailed instructions can be found here.

Limitations & Feedback

Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for Github and other Google products, sign up here.

You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension.

Footnotes

  1. Review the Privacy Notices, Generative AI Prohibited Use Policy, Terms of Service, and learn how to configure Gemini Code Assist in GitHub here. Gemini can make mistakes, so double check it and use code with caution.

Copy link

@gemini-code-assist gemini-code-assist bot left a comment

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.


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
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
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
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
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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

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.

3 participants