Skip to content

feat(spanner): Add Spanner Queue Send mutation sample - #14856

Open
iennae wants to merge 2 commits into
mainfrom
feat/spanner-queue-send-message
Open

iennae wants to merge 2 commits into
mainfrom
feat/spanner-queue-send-message

Conversation

@iennae

@iennae iennae commented Nov 17, 2026

Copy link
Copy Markdown
Contributor

Description

Cloud Spanner Queues documentation shows Go and Java Send mutations without a Python example demonstrating batch.send and base64 payload encoding for BYTES queue columns.

Establishes spanner/queues and adds spanner_queue_send_message.

Checklist

Testing

  • I have tested this change on a live environment and verified it works as intended. Unit and live integration tests pass on Python 3.13.

Compliance & Style


Post-Approval Actions

  • Please merge this PR for me once it is approved

Sending messages to a Spanner Queue via client mutations had Go and Java examples without a Python batch.send sample.
@iennae
iennae requested review from a team as code owners November 17, 2026 23:47
@product-auto-label product-auto-label Bot added api: spanner Issues related to the Spanner API. samples Issues that are directly related to samples. labels Nov 17, 2026
@snippet-bot

snippet-bot Bot commented Nov 17, 2026 •

Copy link
Copy Markdown

Here is the summary of changes.

You are about to add 1 region tag.

This comment is generated by snippet-bot.
If you find problems with this result, please file an issue at:
https://github.com/googleapis/repo-automation-bots/issues.
To update this comment, add snippet-bot:force-run label or use the checkbox below:

  • Refresh this comment

@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 introduces Python samples for Cloud Spanner queues, including a script to send messages, configuration files, and unit/integration tests. The review feedback suggests simplifying a redundant nested os.environ.get call in the test configuration. Additionally, it recommends improving the command-line interface and the message-sending function to support project ID inference via Application Default Credentials (ADC) by making the project ID parameter optional and using the resolved project ID from the Spanner client in error messages.

Comment thread spanner/queues/tests/conftest.py Outdated
Comment on lines +23 to +25
LIVE_DATABASE_ID = os.environ.get(
"SPANNER_DATABASE_ID", os.environ.get("SPANNER_DATABASE_ID", "")
)

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

The nested os.environ.get call is redundant and can be simplified to a single os.environ.get call.

Suggested change
LIVE_DATABASE_ID = os.environ.get(
"SPANNER_DATABASE_ID", os.environ.get("SPANNER_DATABASE_ID", "")
)
LIVE_DATABASE_ID = os.environ.get(
"SPANNER_DATABASE_ID", ""
)

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.

Thanks! This comment refers to an earlier revision of this file; the code here has since been reworked in later commits, so this thread no longer applies to the current diff.

from google.cloud import spanner


def send_queue_message(project_id: str, instance_id: str, database_id: str) -> None:

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

Update the function signature to accept project_id as an optional string (str | None = None). This allows the Spanner client to automatically infer the project ID from Application Default Credentials (ADC) when it is not explicitly provided.

def send_queue_message(
    project_id: str | None, instance_id: str, database_id: str
) -> None:

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.

Thanks for the suggestion! We are intentionally keeping project_id: str explicit in the sample signature so callers pass configuration directly rather than relying on implicit ADC resolution, and we intentionally omit project identifiers (project_id / client.project) from stdout and stderr output so project metadata is not echoed in logs.

Comment on lines +50 to +63
except exceptions.NotFound as err:
print(
f"error: queue '{queue_name}' or parent row on database "
f"'{database_id}' in instance '{instance_id}' for project "
f"'{project_id}' was not found: {err.message}",
file=sys.stderr,
)
except exceptions.GoogleAPICallError as err:
print(
f"error: failed to send message to queue '{queue_name}' on "
f"database '{database_id}' in instance '{instance_id}' for project "
f"'{project_id}': {err.message}",
file=sys.stderr,
)

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

When project_id is not provided (i.e., it is None to allow ADC inference), printing project_id will output None. Using client.project instead ensures the actual resolved project ID is printed in the error messages.

Suggested change
except exceptions.NotFound as err:
print(
f"error: queue '{queue_name}' or parent row on database "
f"'{database_id}' in instance '{instance_id}' for project "
f"'{project_id}' was not found: {err.message}",
file=sys.stderr,
)
except exceptions.GoogleAPICallError as err:
print(
f"error: failed to send message to queue '{queue_name}' on "
f"database '{database_id}' in instance '{instance_id}' for project "
f"'{project_id}': {err.message}",
file=sys.stderr,
)
except exceptions.NotFound as err:
print(
f"error: queue '{queue_name}' or parent row on database "
f"'{database_id}' in instance '{instance_id}' for project "
f"'{client.project}' was not found: {err.message}",
file=sys.stderr,
)
except exceptions.GoogleAPICallError as err:
print(
f"error: failed to send message to queue '{queue_name}' on "
f"database '{database_id}' in instance '{instance_id}' for project "
f"'{client.project}': {err.message}",
file=sys.stderr,
)

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.

Thanks for the suggestion! We are intentionally keeping project_id: str explicit in the sample signature so callers pass configuration directly rather than relying on implicit ADC resolution, and we intentionally omit project identifiers (project_id / client.project) from stdout and stderr output so project metadata is not echoed in logs.

Comment on lines +81 to +85
parser.add_argument(
"--project-id",
default=os.environ.get("GOOGLE_CLOUD_PROJECT", "example-project-id"),
help="Google Cloud project ID (default: $GOOGLE_CLOUD_PROJECT)",
)

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

According to the general rules, the --project-id argument should be optional and allow inferring the project from Application Default Credentials (ADC) if not provided. Currently, defaulting to 'example-project-id' prevents ADC from automatically inferring the correct project when the argument and environment variable are omitted.

Setting the default to None (or omitting the default so it defaults to None) allows the Spanner client to fall back to ADC.

Suggested change
parser.add_argument(
"--project-id",
default=os.environ.get("GOOGLE_CLOUD_PROJECT", "example-project-id"),
help="Google Cloud project ID (default: $GOOGLE_CLOUD_PROJECT)",
)
parser.add_argument(
"--project-id",
default=os.environ.get("GOOGLE_CLOUD_PROJECT"),
help="Google Cloud project ID (default: inferred from ADC)",
)
References
  1. When using argparse for a command-line script, the --project_id argument can be optional, as the project can be inferred from Application Default Credentials (ADC) if not provided.

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.

Thanks for the suggestion! We are intentionally keeping project_id: str explicit in the sample signature so callers pass configuration directly rather than relying on implicit ADC resolution, and we intentionally omit project identifiers (project_id / client.project) from stdout and stderr output so project metadata is not echoed in logs.

Avoids printing project identifiers in stdout or stderr and simplifies duplicate environment variable lookup in test configuration.
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. samples Issues that are directly related to samples.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants