GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — fix(spanner): derive built-in metrics project from database client by rahul2393 · Pull Request #13262 · googleapis/google-cloud-java

Conversation

@rahul2393

@rahul2393 rahul2393 commented May 25, 2026 •
edited
Loading

Uh oh!

There was an error while loading. Please reload this page.

Copy link
Copy Markdown
Contributor

Fixes #13240

Problem

The built-in-metrics Cloud Monitoring exporter can initialize before any DatabaseClient exists, so the OpenTelemetry Resource may carry the host/GKE project instead of the Spanner project — metrics get attributed to the wrong project. A single resource-level project also misattributes metrics for multi-project clients (RPCs to databases in other projects).

Solution

Route each metric by its point-level project_id instead of the resource project:

  1. HeaderInterceptor adds project_id to its existing cached built-in attribute map (cache-miss only, only when parsed from the resource prefix and not the undefined-project sentinel). No new parsing or per-RPC work.
  2. The exporter ignores the resource-level project_id, derives each TimeSeries' project from the point, groups by
    project, and calls createServiceTimeSeries once per project (still batched at the 200 quota limit).
  3. Fallback for points with no usable project is meter-scoped: grpc-java/grpc-gcp core metrics use a fallback project (first DatabaseClient/BatchClient project, set in initializeBuiltInMetrics); Spanner/GAX DB-scoped metrics are skipped rather than exported under an unrelated project. No fallback yet → skip.

Notes / trade-offs

  • gRPC/core metrics for multi-project clients are attributed to the first client's project (they carry no point project).
  • gRPC/core metrics before the first data/batch client, or in admin-only processes, are dropped.
  • Per-project export-failure logging (once per project, with PermissionDeniedException setup guidance + project id; cleared on a successful export).

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

gemini-code-assist[bot] reacted with eyes emoji
All reactions
  • 👀 1 reaction
@rahul2393
fix(spanner): derive built-in metrics project from database client
3ef313c
@rahul2393
rahul2393 requested review from a team as code owners May 25, 2026 09:20
@rahul2393
rahul2393 requested review from sakthivelmanii and surbhigarg92 May 25, 2026 09:20
gemini-code-assist[bot]
gemini-code-assist Bot reviewed May 25, 2026
View reviewed changes

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

Copy link
Copy Markdown
Contributor

There was a problem hiding this 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 refactors the built-in metrics initialization and export logic to use a project ID supplier, ensuring that metrics are correctly associated with the active database project. Key changes include updating SpannerCloudMonitoringExporter to override resource project IDs during export and integrating metric initialization into SpannerImpl and SpannerOptions. Reviewer feedback identifies dead code in the exporter, suggests optimizing convertToSpannerTimeSeries to accept a Collection to avoid redundant list copies, and points out a test case that needs a non-empty collection to properly validate export skipping behavior.

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
Comment thread ...gle-cloud-spanner/src/main/java/com/google/cloud/spanner/SpannerCloudMonitoringExporter.java Resolved

Uh oh!

There was an error while loading. Please reload this page.

Comment thread ...loud-spanner/src/main/java/com/google/cloud/spanner/SpannerCloudMonitoringExporterUtils.java Resolved

Uh oh!

There was an error while loading. Please reload this page.

Comment thread ...loud-spanner/src/main/java/com/google/cloud/spanner/SpannerCloudMonitoringExporterUtils.java Resolved

Uh oh!

There was an error while loading. Please reload this page.

Comment thread ...gle-cloud-spanner/src/main/java/com/google/cloud/spanner/SpannerCloudMonitoringExporter.java Resolved

Uh oh!

There was an error while loading. Please reload this page.

Comment thread ...cloud-spanner/src/test/java/com/google/cloud/spanner/SpannerCloudMonitoringExporterTest.java Resolved

Uh oh!

There was an error while loading. Please reload this page.

gemini-code-assist[bot]
gemini-code-assist Bot reviewed May 25, 2026
View reviewed changes

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

Copy link
Copy Markdown
Contributor

There was a problem hiding this 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 refactors the initialization and export of built-in metrics in the Spanner client by introducing a project ID supplier and triggering initialization upon client access. The logic for filtering metrics based on project ID mismatches in resource attributes has been removed, with the project ID now applied directly to all exported metrics. Review feedback suggests enhancing performance by using Collection instead of List in convertToSpannerTimeSeries to avoid redundant object allocations. Furthermore, the reviewer recommended re-evaluating the lastExportSkippedData flag, as the removal of filtering logic has rendered its current implementation potentially obsolete or incorrect.

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
Comment thread ...loud-spanner/src/main/java/com/google/cloud/spanner/SpannerCloudMonitoringExporterUtils.java Resolved

Uh oh!

There was an error while loading. Please reload this page.

Comment thread ...loud-spanner/src/main/java/com/google/cloud/spanner/SpannerCloudMonitoringExporterUtils.java Resolved

Uh oh!

There was an error while loading. Please reload this page.

Comment thread ...gle-cloud-spanner/src/main/java/com/google/cloud/spanner/SpannerCloudMonitoringExporter.java Resolved

Uh oh!

There was an error while loading. Please reload this page.

Comment thread ...gle-cloud-spanner/src/main/java/com/google/cloud/spanner/SpannerCloudMonitoringExporter.java Resolved

Uh oh!

There was an error while loading. Please reload this page.

@rahul2393
address review comments
52234f4
@rahul2393 rahul2393 mentioned this pull request May 25, 2026
feat(spanner): add setMetricsProjectId to fix metrics export #13241
Closed
surbhigarg92
surbhigarg92 reviewed May 25, 2026
View reviewed changes
Comment thread ...nner/google-cloud-spanner/src/main/java/com/google/cloud/spanner/BuiltInMetricsProvider.java
BuiltInMetricsView.registerBuiltinMetrics(
SpannerCloudMonitoringExporter.create(
projectId, credentials, monitoringHost, universeDomain),
this::getProjectId, credentials, monitoringHost, universeDomain),

@surbhigarg92 surbhigarg92 May 25, 2026

Copy link
Copy Markdown
Contributor

There was a problem hiding this comment.

Choose a reason for hiding this comment

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

@rahul2393 I did not understand this solution. getOrCreateOpenTelemetry is called from GapicSpannerRPC while creating SpannerClient. At the time projectId shared here could be the projectId of GKE instance for example.

So in this case we will be initialising SpannerCloudMonitoringExporter with null projectId ? As by this time setProjectIdIfAbsent won't be called, it is called later during database init.

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions

@rahul2393 rahul2393 May 25, 2026

Copy link
Copy Markdown
Contributor Author

There was a problem hiding this comment.

Choose a reason for hiding this comment

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

So flow is:

  1. SpannerClient init → OpenTelemetry/exporter may be created, project supplier returns null
  2. no export happens yet because no database project is known
  3. getDatabaseClient(DatabaseId) → database project is set once
  4. future metric exports use that database project

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
surbhigarg92
surbhigarg92 reviewed May 25, 2026
View reviewed changes
Comment thread ...nner/google-cloud-spanner/src/main/java/com/google/cloud/spanner/BuiltInMetricsProvider.java
BuiltInMetricsView.registerBuiltinMetrics(
SpannerCloudMonitoringExporter.create(
projectId, credentials, monitoringHost, universeDomain),
this::getProjectId, credentials, monitoringHost, universeDomain),

@surbhigarg92 surbhigarg92 May 25, 2026

Copy link
Copy Markdown
Contributor

There was a problem hiding this comment.

Choose a reason for hiding this comment

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

We are also passing the projectId in next line to create OpenTelemetry Resource

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions

@rahul2393 rahul2393 May 25, 2026

Copy link
Copy Markdown
Contributor Author

There was a problem hiding this comment.

Choose a reason for hiding this comment

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

We also do

monitoredResourceBuilder.putLabels(PROJECT_ID_KEY.getKey(), projectId);

So the resource created during SDK initialization may contain the early/default project, but before sending
createServiceTimeSeries, we overwrite the monitored resource label with the database project from the exporter supplier.

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
@rahul2393
rahul2393 requested a review from surbhigarg92 May 25, 2026 12:39
olavloite
olavloite reviewed May 27, 2026
View reviewed changes
Comment thread ...gle-cloud-spanner/src/main/java/com/google/cloud/spanner/SpannerCloudMonitoringExporter.java

/** Export client built in metrics */
private CompletableResultCode exportSpannerClientMetrics(Collection<MetricData> collection) {
// Filter spanner metrics. Only include metrics that contain a valid project.

@olavloite olavloite May 27, 2026

Copy link
Copy Markdown
Contributor

There was a problem hiding this comment.

Choose a reason for hiding this comment

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

I don't think you can safely remove this code when using this strategy. Or technically you can, but this then changes the current behavior of clients that use multiple different projects, which I don't think is a side-effect that we want from this fix. Previously, metrics with mismatched project IDs would be filtered out and not exported. Now, they are all set to whatever project ID is used by the first DatabaseClient that is created and then exported. I think that the latter is a degradation from the previous behavior, where they were dropped, as now you risk getting metrics from a 'wrong' database into a different project.

An alternative to setting a fixed project ID that is used for all metrics, is to dynamically collect and then batch export the metrics per project ID (which in the vast majority of cases would be just one project). That would remove the requirement to try to set a project ID the first time a DatabaseClient is created, which would simplify the code a bit. See here for an example: https://github.com/googleapis/google-cloud-java/compare/spanner-export-metrics-per-project (Note: That sample has not been further refined, so it might need a bit of polishing before it is ready to use)

The up and downsides of the strategy in https://github.com/googleapis/google-cloud-java/compare/spanner-export-metrics-per-project are:

  • Upside: It correctly exports Spanner metrics to the correct project, even when a client creates multiple DatabaseClients with different project IDs.
  • Downside: It uses the default Project ID of the environment (so for example the GKE project ID) for core gRPC metrics that are not handled by the Spanner interceptor.

The downside mentioned above could partly be mitigated by combining it with the strategy in this pull request, and dynamically setting the project ID that is used for non-Spanner metrics to the project ID of the first DatabaseClient.

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

rahul2393 reacted with thumbs up emoji
All reactions
  • 👍 1 reaction
@rahul2393
incorporate suggestions
592d47d
@rahul2393
rahul2393 requested a review from a team as a code owner May 29, 2026 13:27
@rahul2393
rahul2393 requested a review from olavloite May 29, 2026 13:30
@rahul2393
fix test
f8cfb70
olavloite
olavloite approved these changes May 29, 2026
View reviewed changes
Comment thread ...gle-cloud-spanner/src/main/java/com/google/cloud/spanner/SpannerCloudMonitoringExporter.java Resolved

Uh oh!

There was an error while loading. Please reload this page.

@rahul2393
remove TODO
07599d7
@rahul2393
rahul2393 merged commit a577934 into main May 29, 2026
167 of 168 checks passed

Uh oh!

There was an error while loading. Please reload this page.

@rahul2393
rahul2393 deleted the fix-builtin-metrics-spanner branch May 29, 2026 15:57
This was referenced Jun 3, 2026
chore(main): release 1.87.0 #13291
Closed
chore(main): release 1.87.0 #13346
Merged
blakeli0 added a commit that referenced this pull request Jun 3, 2026
@release-please @cloud-java-bot @blakeli0
chore(main): release 1.87.0 (#13346)
6ce7a49
🤖 I have created a release *beep* *boop*
---


<details><summary>1.87.0</summary>

##
[1.87.0](v1.86.0...v1.87.0)
(2026-06-03)


### ⚠ BREAKING CHANGES

* [container] remove image_family from
* [saasservicemgmt] Changed field behavior for an existing field

### Features

* [admanager] added new API dimension:
([39e93fe](39e93fe))
* [aiplatform] Release
([39e93fe](39e93fe))
* [aiplatform] Release
([39e93fe](39e93fe))
* [asset] add authentication scope for Policy Analyzer MCP tool
([9c90001](9c90001))
* [backstory] new module for backstory
([#13334](#13334))
([fd8f666](fd8f666))
* [bigquerymigration] add support for assessment tasks in BigQuery
([9c90001](9c90001))
* [chat] Support force notify and silent notification option for
([39e93fe](39e93fe))
* [cloudsupport] v2 version for `GetComment` and `GetAttachment`
([39e93fe](39e93fe))
* [compute] Update Compute Engine v1 API to revision 20260422
([9c90001](9c90001))
* [container] add confidential instance type and hyperdisk support
([39e93fe](39e93fe))
* [container] add confidential instance type and hyperdisk support
([39e93fe](39e93fe))
* [container] add custom node image configuration
([9c90001](9c90001))
* [databasecenter] add a AggregateQueryStats API
([9c90001](9c90001))
* [datalineage] A new method SearchLineageStreaming is added
([39e93fe](39e93fe))
* [datamanager] add encrypted_user_id.proto and the EncryptedUserId
([9c90001](9c90001))
* [datamanager] add the coupon_codes field to CartData to track
([9c90001](9c90001))
* [dataproc] add support for Cloud Resource Manager tags for
([39e93fe](39e93fe))
* [developerknowledge] new module for developerknowledge
([#13313](#13313))
([502841b](502841b))
* [dlp] Supporting Conversation Content Type
([9c90001](9c90001))
* [health] Introduce Data Subscription Service for managing webhook
([9c90001](9c90001))
* [health] introduce MedicalDeviceInfo message for Software as
([9c90001](9c90001))
* [maps-places] add transit_station data to Places API (New)
([9c90001](9c90001))
* [network-management] add fields and messages for Cloud Run jobs
([9c90001](9c90001))
* [network-security] add networksecurity v1 client library
([9c90001](9c90001))
* [policy-troubleshooter] add authentication scope for
([9c90001](9c90001))
* [recommender] Support utilization insights and cloud hub
([9c90001](9c90001))
* [security-private-ca] add
([9c90001](9c90001))
* [storage] add support for GCS Storage Control Intelligence Finding
([9c90001](9c90001))
* add duration to health metrics
([#13181](#13181))
([ae86384](ae86384))
* add periodic WARNING metrics to assist in debugging
([#12976](#12976))
([c9e2e6d](c9e2e6d))
* add v1 version support for geminidataanalytics
([#13314](#13314))
([946dfb4](946dfb4))
* **bigquery-jdbc:** implement JDBC getObject type coercion support
([#13257](#13257))
([72d4ab6](72d4ab6))
* **bqjdbc:** Add custom logger to handle ResultSet logs
([#13034](#13034))
([887c67f](887c67f))
* **bqjdbc:** implement Per connection logging with Context proxy
([#13001](#13001))
([293f18f](293f18f))
* **dataplex:** Add `RequestDataProductAccess` method to request access
([9c90001](9c90001))
* migrate grpc-gcp to maven
([#237](#237))
([d787372](d787372))
* **spanner:** add getIsolationLevel and getReadLockMode methods to
CommitResponse
([#13004](#13004))
([d5fc504](d5fc504))
* **spanner:** add option for auto-tagging transactions
([#13214](#13214))
([c8234cf](c8234cf))
* **spanner:** add option for inline begin with multi-use read only txn
([#13233](#13233))
([056abb6](056abb6))


### Bug Fixes

* [container] remove image_family from
([9c90001](9c90001))
* [saasservicemgmt] Changed field behavior for an existing field
([9c90001](9c90001))
* **bigquery-jdbc:** Add escape character support for pattern matching
([#13259](#13259))
([af3b7c5](af3b7c5))
* **bigquery-jdbc:** configure logging early to capture URL parsing
warnings and errors
([#13323](#13323))
([98d8e69](98d8e69))
* **bigquery-jdbc:** fallback to standard precision and scale for
numeric, integer, and temporal types
([#13321](#13321))
([51ba902](51ba902))
* **bigquery-jdbc:** fix manual commit mode failure for non-US regions
([#13285](#13285))
([d28436c](d28436c))
* **bigquery-jdbc:** handle EXPORT DATA, EXPORT MODEL, and LOAD DATA
statements
([#13267](#13267))
([5970866](5970866))
* **bigquery-jdbc:** implement JDBC wrapper interface methods
([#13322](#13322))
([46d6a86](46d6a86))
* **bigquery:** prevent NullPointerException in OTel tracing when JobId
is null
([#13301](#13301))
([dcc2a68](dcc2a68))
* **bigtable:** Don't invoke unknown delegate code while holding a lock
([#13202](#13202))
([a7e859e](a7e859e))
* **bigtable:** Ensure that FallbackChannelPool locks doesnt leak to
alien listeners
([#13195](#13195))
([c8e615e](c8e615e))
* **bigtable:** make heartbeat interval volatile
([#13217](#13217))
([279918e](279918e))
* **bigtable:** make ignoreError volatile
([#13218](#13218))
([e5e5f14](e5e5f14))
* **bigtable:** prune older AFEs first
([#13215](#13215))
([114e77b](114e77b))
* **bigtable:** recycle channel on consecutive new stream failures
([#13245](#13245))
([842f64e](842f64e))
* **bigtable:** resolve check-then-act race condition in VRpcImpl
([#13196](#13196))
([9853dd5](9853dd5))
* **bigtable:** resolve deadlock in ClientConfigurationManager when
notifying callbacks
([#13192](#13192))
([c98e53d](c98e53d))
* **bqjdbc:** avoid reusing statement in DatabaseMetaData
([#13224](#13224))
([b1279ab](b1279ab))
* **bqjdbc:** fallback to RestAPI if ReadAPI is not accessible
([#13018](#13018))
([cb9bdc3](cb9bdc3))
* **bqjdbc:** fix BigDecimal usage in mocks
([#13207](#13207))
([65674c2](65674c2))
* **bqjdbc:** fix Long to java.sql.Time coercion
([#13035](#13035))
([8ab9799](8ab9799))
* **bqjdbc:** optimize meetsReadRatio latency to achieve faster page
counting
([#13090](#13090))
([36af3eb](36af3eb))
* **bqjdbc:** Pass connection ID to DataType class loggers
([#13197](#13197))
([ce9c504](ce9c504))
* **bqjdbc:** resolve JDBC metadata compliance gaps for wrappers,
searchability, and type mappings
([#13242](#13242))
([5678fc2](5678fc2))
* **bqjdbc:** update metadata values for GEOGRAPHY/JSON/INTERVAL types
([#13223](#13223))
([d08691a](d08691a))
* **bqjdbc:** update shading to be more targeted
([#13232](#13232))
([cc691a8](cc691a8))
* **bqjdbc:** validate integer connection properties
([#13174](#13174))
([875ff6a](875ff6a))
* **build:** support generating unversioned libraries in OwlBot config
([#13336](#13336))
([90d6661](90d6661))
* **ci:** correct directory typo in shared-dependencies workflow
([#13225](#13225))
([a327bf2](a327bf2))
* **firestore:** increase ITShutdownTest timeout to 50s
([#13199](#13199))
([d432b35](d432b35))
* **gapic-generator:** allow non-versioned proto paths and correct help
text
([#13329](#13329))
([13717a2](13717a2))
* **generator:** align template and post-processor formatting
([#13028](#13028))
([6a2307f](6a2307f))
* **hermetic_build:** add grpc-gcp-java to module allowlist in root pom
generator
([#13211](#13211))
([f7c8407](f7c8407))
* **hermetic_build:** restore Version.java after postprocessing
entrypoint
([#13328](#13328))
([ee3e9cd](ee3e9cd))
* **hermetic:** exclude showcase modules from versions.txt
([#13030](#13030))
([6ce87b7](6ce87b7))
* **iam-admin:** remove redundant IAM project service to prevent state
conflict
([#13016](#13016))
([95cfd5e](95cfd5e))
* remove google-cloud-bigtable-deps-bom from gapic-libraries-bom
([#13209](#13209))
([ce156f2](ce156f2))
* **spanner:** avoid double grpc-gcp wrapping for directpath fallback
([#13155](#13155))
([fe608fe](fe608fe))
* **spanner:** cache auto-tagging options to avoid system property lock
contention
([#13273](#13273))
([dc2feed](dc2feed))
* **spanner:** derive built-in metrics project from database client
([#13262](#13262))
([a577934](a577934))


### Dependencies

* update dependency com.google.cloud:sdk-platform-java-config to v3.62.0
([#2379](#2379))
([e9ec6e0](e9ec6e0))
* update dependency com.google.cloud:sdk-platform-java-config to v3.62.0
([#2729](#2729))
([af4893b](af4893b))
* update googleapis/google-cloud-java action to v1.86.0
([#2377](#2377))
([f2de1c3](f2de1c3))
* update googleapis/google-cloud-java action to v1.86.0
([#2727](#2727))
([d16eda9](d16eda9))
* update shared dependencies
([#2910](#2910))
([8cc8ae0](8cc8ae0))


### Documentation

* add root-level development.md guide for scoped builds
([#13151](#13151))
([bcec186](bcec186))
* **agents:** add API lifecycle and stability guidelines skill
([#13179](#13179))
([9110ad6](9110ad6))
* Update gRPC spelling in READMEs
([#13264](#13264))
([d6a93ee](d6a93ee))
</details>

---
This PR was generated with [Release
Please](https://github.com/googleapis/release-please). See
[documentation](https://github.com/googleapis/release-please#release-please).

---------

Co-authored-by: release-please[bot] <55107282+release-please[bot]@users.noreply.github.com>
Co-authored-by: cloud-java-bot <[email protected]>
Co-authored-by: blakeli <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Reviewers

@olavloite olavloite olavloite approved these changes
@sakthivelmanii sakthivelmanii Awaiting requested review from sakthivelmanii
@surbhigarg92 surbhigarg92 Awaiting requested review from surbhigarg92
+1 more reviewer
@gemini-code-assist gemini-code-assist[bot] gemini-code-assist[bot] left review comments
Reviewers whose approvals may not affect merge requirements

Uh oh!

There was an error while loading. Please reload this page.

Assignees

No one assigned

Labels

None yet

Projects

None yet

Milestone

No milestone

Development

Successfully merging this pull request may close these issues.

Spanner: client-side metrics export uses wrong project

Uh oh!

There was an error while loading. Please reload this page.

3 participants

@rahul2393 @olavloite @surbhigarg92
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
You are visitor 01402775 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.