Skip to content

fix(spanner): disable metrics tracer when built-in metrics are disabled#8170

Merged
rahul2393 merged 7 commits intomainfrom
fix-disabled-builtin-metrics-tracer
May 5, 2026
Merged

fix(spanner): disable metrics tracer when built-in metrics are disabled#8170
rahul2393 merged 7 commits intomainfrom
fix-disabled-builtin-metrics-tracer

Conversation

@rahul2393
Copy link
Copy Markdown
Contributor

@rahul2393 rahul2393 commented May 5, 2026

Summary

  • Track built-in metrics enablement on each Spanner instance
  • Sync MetricsTracerFactory.enabled with the instance's computed metrics state during configureMetrics_()
  • Prevent MetricsTracer and metric interceptor creation when built-in metrics are disabled via client option, env var, or insecure credentials

Why

When disableBuiltInMetrics was true, metrics export setup was skipped, but MetricsTracerFactory.enabled could still remain true from its default value or prior state. If projectId_ was set, requests could still create MetricsTracer instances even though built-in metrics were disabled.

This change computes metrics enablement during client setup and uses the instance-level state to guard tracer/
interceptor creation.

Ensure MetricsTracerFactory starts disabled and is synchronized with the built-in metrics configuration. This prevents MetricsTracer instances from being created when metrics are disabled via client options, environment, or insecure credentials.
@rahul2393 rahul2393 requested a review from a team as a code owner May 5, 2026 12:00
@product-auto-label product-auto-label Bot added the api: spanner Issues related to the Spanner API. label May 5, 2026
Copy link
Copy Markdown
Contributor

@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 updates the Spanner client to explicitly set the MetricsTracerFactory.enabled flag based on the instance's configuration and changes the default value of this static property to false. A significant issue was identified where setting a static property based on instance-specific logic creates a global side effect, potentially causing configuration conflicts in applications that utilize multiple Spanner clients with different settings.

Comment thread handwritten/spanner/src/index.ts Outdated
Track built-in metrics enablement on each Spanner instance and use it to guard metric interceptors and MetricsTracer creation. This prevents clients that explicitly disable built-in metrics from creating or retaining metrics tracers without mutating global MetricsTracerFactory state for other clients.
@rahul2393 rahul2393 force-pushed the fix-disabled-builtin-metrics-tracer branch from e3f7913 to 910c701 Compare May 5, 2026 12:26
@rahul2393
Copy link
Copy Markdown
Contributor Author

/gemini review

@rahul2393
Copy link
Copy Markdown
Contributor Author

/gemini summary

Copy link
Copy Markdown
Contributor

@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 introduces an instance-level _metricsEnabled property to the Spanner class, replacing the global MetricsTracerFactory.enabled check to allow for more granular control over metrics collection. Feedback indicates a logic error in configureMetrics_ where _metricsEnabled fails to incorporate the _isInSecureCredentials check. This omission leads to inconsistent behavior when guarding tracer creation in the request and requestStream methods.

Comment thread handwritten/spanner/src/index.ts Outdated
@gemini-code-assist
Copy link
Copy Markdown
Contributor

Summary of Changes

This pull request refactors the metrics configuration logic in the Spanner client to move away from global state management. By introducing an instance-specific flag, it ensures that metrics settings are correctly isolated across different client instances, preventing side effects where one client's configuration could inadvertently affect others. The changes also improve the reliability of the metrics disabling mechanism by ensuring all relevant conditions are consistently evaluated.

Highlights

  • Instance-based Metrics Control: Introduced a private _metricsEnabled property to the Spanner class to manage metrics state per-instance, replacing the problematic global MetricsTracerFactory.enabled flag.
  • Metrics Configuration Logic: Updated configureMetrics_ to centralize the logic for disabling metrics, ensuring it correctly accounts for client options, environment variables, and insecure credentials.
  • Tracer Creation Guard: Added checks for _metricsEnabled in request and requestStream methods to prevent unnecessary MetricsTracer creation when built-in metrics are disabled.
Activity
  • The pull request was created by rahul2393.
  • Gemini Code Assist identified a high-priority issue regarding global state side effects in the initial implementation.
  • The author requested a review and summary.
  • Gemini Code Assist provided further feedback on the implementation of the metrics guard logic.

Comment thread handwritten/spanner/src/index.ts Outdated
Update the per-instance metrics flag to include insecure credentials so metric interceptors and tracers are not created when built-in metrics are disabled for that client. Keep the mock metrics tests using an explicit in-memory metrics override because they intentionally run against insecure test credentials.
@rahul2393
Copy link
Copy Markdown
Contributor Author

/gemini review

@rahul2393
Copy link
Copy Markdown
Contributor Author

/gemini summary

Copy link
Copy Markdown
Contributor

@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 transitions the metrics enablement state from a global static property (MetricsTracerFactory.enabled) to an instance-specific property (_metricsEnabled) within the Spanner class. This change allows for independent metrics configuration across different Spanner client instances, preventing global state side effects. The request and requestStream methods, as well as the interceptor logic, have been updated to reference this instance property. I have no feedback to provide.

@gemini-code-assist
Copy link
Copy Markdown
Contributor

Summary of Changes

This pull request refactors the metrics configuration logic in the Spanner client to eliminate reliance on global state. By moving the metrics-enabled status to an instance-specific property, the changes prevent configuration leakage between multiple Spanner clients and ensure that metrics are consistently disabled when required by client options, environment variables, or insecure credentials.

Highlights

  • Instance-based Metrics Control: Replaced the global MetricsTracerFactory.enabled flag with a private _metricsEnabled property in the Spanner class to ensure metrics configuration is isolated per client instance.
  • Centralized Configuration Logic: Updated configureMetrics_ to consolidate the logic for disabling metrics, ensuring it correctly evaluates client options, environment variables, and insecure credentials.
  • Tracer Creation Guard: Added strict checks for _metricsEnabled in request and requestStream methods to prevent the creation of MetricsTracer instances when built-in metrics are disabled.
Activity
  • The pull request was created by rahul2393.
  • Gemini Code Assist identified a high-priority issue regarding global state side effects.
  • Gemini Code Assist provided feedback on the implementation of the metrics guard logic to ensure insecure credentials are properly handled.
  • User surbhigarg92 requested clarification on the inclusion of the insecure credentials check in the metrics guard.
  • The author updated the implementation to address the feedback and ensure consistent metrics disabling.

@surbhigarg92 surbhigarg92 added do not merge Indicates a pull request not ready for merge, due to either quality or timing. and removed do not merge Indicates a pull request not ready for merge, due to either quality or timing. labels May 5, 2026
rahul2393 added 3 commits May 5, 2026 19:42
Default MetricsTracerFactory.enabled to false so tracer creation stays disabled until metrics configuration explicitly enables the singleton factory.
Move MetricsTracerFactory.enabled assignment outside the metrics setup branch so explicitly disabled or insecure clients update the singleton guard before any request path can create metrics tracers.
@rahul2393 rahul2393 merged commit 0dd9a53 into main May 5, 2026
26 checks passed
@rahul2393 rahul2393 deleted the fix-disabled-builtin-metrics-tracer branch May 5, 2026 15:16
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 Spanner API.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants