Skip to content

feat(spanner): add ClientContext support#13775

Merged
olavloite merged 4 commits intogoogleapis:mainfrom
aseering:secure_context_support
Feb 10, 2026
Merged

feat(spanner): add ClientContext support#13775
olavloite merged 4 commits intogoogleapis:mainfrom
aseering:secure_context_support

Conversation

@aseering
Copy link
Copy Markdown
Contributor

@aseering aseering commented Feb 4, 2026

Add support for the new ClientContext field to Spanner's Go Client.

This enables support for Secure Parameters, as well as other future features.

@aseering aseering requested a review from a team February 4, 2026 16:05
@aseering aseering requested review from a team as code owners February 4, 2026 16:05
@product-auto-label product-auto-label bot added the api: spanner Issues related to the Spanner API. label Feb 4, 2026
@gemini-code-assist
Copy link
Copy Markdown
Contributor

Summary of Changes

Hello @aseering, 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 introduces robust support for a ClientContext field within the Spanner Go client. This enhancement allows client-owned context information to be attached to and propagated with Spanner requests, which is a foundational step for enabling future features such as Secure Parameters. The implementation ensures that ClientContext can be configured as a default at the client level and then overridden or augmented at the individual operation level, utilizing a clear merging strategy to manage context priorities.

Highlights

  • ClientContext Field Introduction: The ClientContext field has been added to key configuration and option structs, including ClientConfig, TransactionOptions, ReadOptions, QueryOptions, and BatchWriteOptions, as well as internal transaction structs like txReadOnly and writeOnlyTransaction.
  • Comprehensive ClientContext Propagation: Support for ClientContext has been integrated across all major Spanner operations. This ensures that client-owned context information is propagated with requests for reads, queries, partitioned DML, batch writes, and both read-write and read-only transactions.
  • ClientContext Merging Logic: A new helper function, mergeClientContext, has been implemented to handle the combination of default ClientContext values (e.g., from ClientConfig) with operation-specific ClientContext values, ensuring that specific contexts override or augment defaults appropriately.
  • Extensive Unit Testing: A new test file, spanner/secure_parameters_test.go, has been added, containing comprehensive unit tests that verify the correct propagation and merging behavior of ClientContext across various Spanner API calls and scenarios.
Changelog
  • spanner/batch.go
    • Updated createRequestOptions calls in PartitionReadUsingIndexWithOptions and partitionQuery to propagate ClientContext from read/query options.
  • spanner/client.go
    • Added clientContext field to the Client struct.
    • Added ClientContext field to ClientConfig for default client-wide configuration.
    • Initialized Client.clientContext from ClientConfig during client creation.
    • Propagated Client.clientContext to ReadOnlyTransaction and WriteOnlyTransaction instances created by the client.
    • Added ClientContext to BatchWriteOptions and updated its merge method to combine contexts.
    • Modified BatchWriteWithOptions to pass the merged ClientContext to the underlying request.
  • spanner/pdml.go
    • Modified partitionedUpdate to pass ClientContext from query options to createRequestOptions.
  • spanner/secure_parameters_test.go
    • New file added with comprehensive unit tests for ClientContext propagation and merging across various Spanner operations, including queries, reads, read-write transactions, batch writes, partitioned DML, batch read-only transactions, and ApplyAtLeastOnce.
  • spanner/transaction.go
    • Added clientContext fields to txReadOnly and writeOnlyTransaction structs.
    • Added ClientContext fields to TransactionOptions and ReadOptions structs.
    • Updated TransactionOptions.merge and ReadOptions.merge methods to include ClientContext merging logic.
    • Introduced a new mergeClientContext helper function for combining sppb.RequestOptions_ClientContext messages.
    • Modified createRequestOptions to accept and set ClientContext in the sppb.RequestOptions.
    • Integrated ClientContext into txReadOnly.ReadWithOptions, txReadOnly.prepareExecuteSQL, ReadOnlyTransaction.begin, ReadWriteTransaction.batchUpdateWithOptions, beginTransaction, ReadWriteTransaction.commit, and writeOnlyTransaction.applyAtLeastOnce.
Activity
  • No specific activity (comments, reviews, or progress updates) has been recorded for this pull request yet.
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
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 support for ClientContext across the Spanner Go client, enabling features like Secure Parameters. The implementation is thorough, propagating the new context from client configuration down to individual requests. The addition of a comprehensive test suite in secure_parameters_test.go is excellent and covers a wide range of scenarios, ensuring the new functionality is well-tested.

I've identified a couple of areas for improvement in spanner/transaction.go to enhance code clarity and efficiency. Specifically, the createRequestOptions function can be optimized to avoid unnecessary allocations, and the logic for constructing request options within beginTransaction can be simplified. My detailed comments provide specific suggestions for these improvements. Overall, this is a solid contribution.

Comment on lines 1713 to +1723
if opts.txOptions.TransactionTag != "" {
request.RequestOptions = &sppb.RequestOptions{TransactionTag: opts.txOptions.TransactionTag}
}
ro := createRequestOptions(sppb.RequestOptions_PRIORITY_UNSPECIFIED, "", opts.txOptions.TransactionTag, opts.txOptions.ClientContext)
if ro != nil {
if request.RequestOptions == nil {
request.RequestOptions = ro
} else {
request.RequestOptions.ClientContext = ro.ClientContext
}
}
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.

medium

This logic for setting RequestOptions is a bit complex and can be simplified. The TransactionTag is handled in two places, which is confusing. You can replace this entire block with a single call to createRequestOptions.

	request.RequestOptions = createRequestOptions(sppb.RequestOptions_PRIORITY_UNSPECIFIED, "", opts.txOptions.TransactionTag, opts.txOptions.ClientContext)

@olavloite olavloite changed the title feat(spanner): Add ClientContext support to Go Client feat(spanner): add ClientContext support Feb 5, 2026
@olavloite olavloite requested a review from rahul2393 February 9, 2026 14:32
@olavloite olavloite merged commit e85d706 into googleapis:main Feb 10, 2026
10 checks passed
rahul2393 added a commit that referenced this pull request Feb 11, 2026
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/librarian-go@sha256:19bb93e8f1f916c61b597db2bad65dc432f79baaabb210499d7d0e4ad1dffe29
<details><summary>spanner: 1.88.0</summary>

##
[1.88.0](spanner/v1.87.0...spanner/v1.88.0)
(2026-02-11)

### Features

* Adding Send and Ack Mutation Support for Cloud Spanner Queue (#13616)
([1cf600d](1cf600d6))

* include cache updates into the ResultSet response (PiperOrigin-RevId:
865546011)
([6f31019](6f310199))

* add a ClientContext field to Spanner requests (PiperOrigin-RevId:
853323071)
([80379ed](80379edb))

* add Secure Parameters to the ClientContext (PiperOrigin-RevId:
853323071)
([80379ed](80379edb))

* PGNumeric implements Scanner and Valuer (#13722)
([85bc9db](85bc9dbf))

* support struct literal (#13766)
([b4a6f4c](b4a6f4c5))

* Exposing total CPU related fields in AutoscalingConfig
(PiperOrigin-RevId: 845819318)
([db65e79](db65e792))

* add ClientContext support (#13775)
([e85d706](e85d7061))

### Bug Fixes

* disable config logging by default (#13478)
([ad19592](ad19592e))

* decode PG JSONB array to PGJsonB struct (#13602)
([d72d0f4](d72d0f45))

### Performance Improvements

* only create sessions if multiplexed sessions are disabled (#13477)
([e44e58f](e44e58f6))

### Documentation

* A comment for field `param_types` in message
`.google.spanner.v1.PartitionQueryRequest` is changed
(PiperOrigin-RevId: 865546011)
([6f31019](6f310199))

* A comment for field `transaction_tag` in message
`.google.spanner.v1.RequestOptions` is changed (PiperOrigin-RevId:
865546011)
([6f31019](6f310199))

* A comment for field `commit_timestamp` in message
`.google.spanner.v1.BatchWriteResponse` is changed (PiperOrigin-RevId:
865546011)
([6f31019](6f310199))

* A comment for field `params` in message
`.google.spanner.v1.PartitionQueryRequest` is changed
(PiperOrigin-RevId: 865546011)
([6f31019](6f310199))

* Update client side metrics and permission issues in README (#13491)
([ab56892](ab56892e))

* Update high_priority_cpu_utilization_percent in AutoscalingConfig to
be Optional and clarify its behavior when not specified
(PiperOrigin-RevId: 845819318)
([db65e79](db65e792))

</details>
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.

3 participants