Repository navigation
Conversation
Sending messages to a Spanner Queue via client mutations had Go and Java examples without a Python batch.send sample.
|
Here is the summary of changes. You are about to add 1 region tag.
This comment is generated by snippet-bot.
|
There was a problem hiding this comment.
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.
| LIVE_DATABASE_ID = os.environ.get( | ||
| "SPANNER_DATABASE_ID", os.environ.get("SPANNER_DATABASE_ID", "") | ||
| ) |
There was a problem hiding this comment.
There was a problem hiding this comment.
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: |
There was a problem hiding this comment.
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:There was a problem hiding this comment.
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.
| 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, | ||
| ) |
There was a problem hiding this comment.
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.
| 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, | |
| ) |
There was a problem hiding this comment.
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.
| parser.add_argument( | ||
| "--project-id", | ||
| default=os.environ.get("GOOGLE_CLOUD_PROJECT", "example-project-id"), | ||
| help="Google Cloud project ID (default: $GOOGLE_CLOUD_PROJECT)", | ||
| ) |
There was a problem hiding this comment.
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.
| 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
- 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.
There was a problem hiding this comment.
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.
Description
Cloud Spanner Queues documentation shows Go and Java
Sendmutations without a Python example demonstratingbatch.sendand base64 payload encoding forBYTESqueue columns.Establishes
spanner/queuesand addsspanner_queue_send_message.Checklist
Testing
Compliance & Style
spanner/, which the default repository owners cover.Post-Approval Actions