Repository navigation
Conversation
Tracing documentation embedded an archived python-spanner example that executed an invalid SQL query and omitted clean TracerProvider shutdown.
|
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 client-side and end-to-end Spanner tracing configuration via OTLP gRPC, along with corresponding unit and live tests. The feedback recommends allowing the project_id parameter and CLI argument to be optional so that the Spanner client can automatically infer the project ID from Application Default Credentials (ADC). Additionally, it suggests using client.project in error messages to display the resolved project ID and mocking this attribute in the test suite.
| def configure_otlp_tracing(project_id: str, instance_id: str, database_id: str) -> None: | ||
| """Configures client-side and end-to-end Spanner tracing via OTLP gRPC. | ||
|
|
||
| Args: | ||
| project_id: The Google Cloud project ID. | ||
| instance_id: The Spanner instance ID. | ||
| database_id: The Spanner database ID. | ||
| """ |
There was a problem hiding this comment.
Allow project_id to be optional (None) so that the Spanner client can automatically infer the project ID from Application Default Credentials (ADC) when it is not explicitly provided.
| def configure_otlp_tracing(project_id: str, instance_id: str, database_id: str) -> None: | |
| """Configures client-side and end-to-end Spanner tracing via OTLP gRPC. | |
| Args: | |
| project_id: The Google Cloud project ID. | |
| instance_id: The Spanner instance ID. | |
| database_id: The Spanner database ID. | |
| """ | |
| def configure_otlp_tracing( | |
| project_id: str | None, instance_id: str, database_id: str | |
| ) -> None: | |
| """Configures client-side and end-to-end Spanner tracing via OTLP gRPC. | |
| Args: | |
| project_id: The Google Cloud project ID. If None, it will be inferred from ADC. | |
| instance_id: The Spanner instance ID. | |
| database_id: The Spanner database ID. | |
| """ |
References
- When using
argparsefor a command-line script, the--project_idargument 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.
| except exceptions.NotFound as err: | ||
| print( | ||
| f"error: database '{database_id}' on instance '{instance_id}' " | ||
| f"in project '{project_id}' was not found: {err.message}", | ||
| file=sys.stderr, | ||
| ) | ||
| except exceptions.GoogleAPICallError as err: | ||
| print( | ||
| f"error: OTLP-traced query failed on database '{database_id}' " | ||
| f"in instance '{instance_id}' for project '{project_id}': {err.message}", | ||
| file=sys.stderr, | ||
| ) |
There was a problem hiding this comment.
Use client.project instead of the input project_id parameter in the error messages. This ensures that if project_id was omitted (and thus None), the resolved project ID auto-detected by the Spanner client is correctly printed.
| except exceptions.NotFound as err: | |
| print( | |
| f"error: database '{database_id}' on instance '{instance_id}' " | |
| f"in project '{project_id}' was not found: {err.message}", | |
| file=sys.stderr, | |
| ) | |
| except exceptions.GoogleAPICallError as err: | |
| print( | |
| f"error: OTLP-traced query failed on database '{database_id}' " | |
| f"in instance '{instance_id}' for project '{project_id}': {err.message}", | |
| file=sys.stderr, | |
| ) | |
| except exceptions.NotFound as err: | |
| print( | |
| f"error: database '{database_id}' on instance '{instance_id}' " | |
| f"in project '{client.project}' was not found: {err.message}", | |
| file=sys.stderr, | |
| ) | |
| except exceptions.GoogleAPICallError as err: | |
| print( | |
| f"error: OTLP-traced query failed on database '{database_id}' " | |
| f"in instance '{instance_id}' for project '{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.
| "--project-id", | ||
| default=os.environ.get("GOOGLE_CLOUD_PROJECT", "example-project-id"), | ||
| help="Google Cloud project ID (default: $GOOGLE_CLOUD_PROJECT)", | ||
| ) | ||
| parser.add_argument( |
There was a problem hiding this comment.
Remove the hardcoded fallback "example-project-id" from the --project-id argument default. This allows the argument to default to None (or the value of GOOGLE_CLOUD_PROJECT if set), enabling the Spanner client to automatically infer the project ID from Application Default Credentials (ADC).
parser.add_argument(
"--project-id",
default=os.environ.get("GOOGLE_CLOUD_PROJECT"),
help="Google Cloud project ID (default: $GOOGLE_CLOUD_PROJECT)",
)References
- When using
argparsefor a command-line script, the--project_idargument 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.
| mock_exporter_class.return_value.export.return_value = SpanExportResult.SUCCESS | ||
| database = ( | ||
| mock_client_class.return_value.instance.return_value.database.return_value | ||
| ) |
There was a problem hiding this comment.
Mock the project attribute on the client mock to ensure that the error-handling path tests (which print client.project) can resolve the project ID correctly without raising an AttributeError or returning a mock object.
| mock_exporter_class.return_value.export.return_value = SpanExportResult.SUCCESS | |
| database = ( | |
| mock_client_class.return_value.instance.return_value.database.return_value | |
| ) | |
| mock_exporter_class.return_value.export.return_value = SpanExportResult.SUCCESS | |
| mock_client_class.return_value.project = PROJECT_ID | |
| database = ( | |
| mock_client_class.return_value.instance.return_value.database.return_value | |
| ) |
There was a problem hiding this comment.
Thanks! Because we intentionally omit project identifiers (project_id / client.project) from stdout and stderr output, the sample does not read client.project, so this mock attribute isn't needed.
Avoids printing project identifiers in stdout resource lines or stderr error diagnostics.
Prints short resource identifiers instead of full project-prefixed resource paths and omits project_id from diagnostic messages.
…tions Prints only runtime query results and verifies real spanner.Client options via wraps in live integration tests.
Description
Configures client-side and end-to-end Spanner tracing (
TracerProvider,OTLPSpanExporter,TraceContextTextMapPropagator, andenable_end_to_end_tracing=True), replacing the archivedpython-spannerexample.Adds
spanner_opentelemetry_traces_otlp_usage.Checklist
Testing
Compliance & Style
spanner/, which the default repository owners cover.Post-Approval Actions