Repository navigation
Conversation
Exporting client-side and server-side Spanner traces directly to Google Cloud Trace lacked a standalone tested sample in python-docs-samples.
|
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 a new module and tests for exporting Google Cloud Spanner OpenTelemetry traces to Cloud Trace. The feedback suggests improving flexibility by making the project_id parameter optional in both the configuration function and the CLI, allowing it to be automatically resolved from the environment or Application Default Credentials. To support this, the reviewer recommends using client.project in error messages to print the resolved project ID, and updating the mock tests to set the project attribute on the mocked Spanner client.
| def configure_cloud_trace_tracing( | ||
| project_id: str, instance_id: str, database_id: str | ||
| ) -> None: |
There was a problem hiding this comment.
Update the configure_cloud_trace_tracing function signature to accept project_id as an optional string (str | None). This allows the project ID to be inferred from the environment or Application Default Credentials (ADC) when not explicitly provided.
| def configure_cloud_trace_tracing( | |
| project_id: str, instance_id: str, database_id: str | |
| ) -> None: | |
| def configure_cloud_trace_tracing( | |
| 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.
| print( | ||
| f"error: database '{database_id}' on instance '{instance_id}' " | ||
| f"in project '{project_id}' was not found: {err.message}", | ||
| file=sys.stderr, | ||
| ) |
There was a problem hiding this comment.
Use client.project instead of project_id in the error message. When project_id is not provided (i.e., None), the Spanner client automatically resolves the project ID from the environment. Using client.project ensures the actual resolved project ID is printed.
| print( | |
| f"error: database '{database_id}' on instance '{instance_id}' " | |
| f"in project '{project_id}' was not found: {err.message}", | |
| file=sys.stderr, | |
| ) | |
| print( | |
| f"error: database '{database_id}' on instance '{instance_id}' " | |
| f"in project '{client.project}' was not found: {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.
| except exceptions.GoogleAPICallError as err: | ||
| print( | ||
| f"error: Cloud Trace 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 project_id in the error message to ensure the actual resolved project ID is printed when project_id is inferred from the environment.
| except exceptions.GoogleAPICallError as err: | |
| print( | |
| f"error: Cloud Trace query failed on database '{database_id}' " | |
| f"in instance '{instance_id}' for project '{project_id}': {err.message}", | |
| file=sys.stderr, | |
| ) | |
| except exceptions.GoogleAPICallError as err: | |
| print( | |
| f"error: Cloud Trace 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.
Make the --project-id argument optional by defaulting to os.environ.get("GOOGLE_CLOUD_PROJECT") (which is None if not set) instead of a hardcoded string like "example-project-id". This allows the project ID to be automatically inferred from Application Default Credentials (ADC) or the environment when not provided.
parser.add_argument(
"--project-id",
default=os.environ.get("GOOGLE_CLOUD_PROJECT"),
help="Google Cloud project ID (default: $GOOGLE_CLOUD_PROJECT)",
)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.
| mock_exporter_class.return_value.export.return_value = SpanExportResult.SUCCESS | ||
| database = ( | ||
| mock_client_class.return_value.instance.return_value.database.return_value | ||
| ) | ||
| database.name = DATABASE_NAME |
There was a problem hiding this comment.
Set the project attribute on the mocked client to PROJECT_ID to ensure the mock behaves realistically and matches the actual client behavior.
| mock_exporter_class.return_value.export.return_value = SpanExportResult.SUCCESS | |
| database = ( | |
| mock_client_class.return_value.instance.return_value.database.return_value | |
| ) | |
| database.name = DATABASE_NAME | |
| 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 | |
| ) | |
| database.name = DATABASE_NAME |
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.
| 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.
Set the project attribute on the mocked client to PROJECT_ID to ensure the mock behaves realistically and matches the actual client behavior.
| 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.
| mock_exporter_class.return_value.export.return_value = SpanExportResult.SUCCESS | ||
| database = ( | ||
| mock_client_class.return_value.instance.return_value.database.return_value | ||
| ) | ||
| database.name = DATABASE_NAME |
There was a problem hiding this comment.
Set the project attribute on the mocked client to PROJECT_ID to ensure the mock behaves realistically and matches the actual client behavior.
| mock_exporter_class.return_value.export.return_value = SpanExportResult.SUCCESS | |
| database = ( | |
| mock_client_class.return_value.instance.return_value.database.return_value | |
| ) | |
| database.name = DATABASE_NAME | |
| 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 | |
| ) | |
| database.name = DATABASE_NAME |
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
Demonstrates exporting client-side and end-to-end Cloud Spanner traces directly to Google Cloud Trace via
CloudTraceSpanExporterand W3CTraceContextTextMapPropagator.Adds
spanner_opentelemetry_traces_cloudtrace_usage.Checklist
Testing
Compliance & Style
spanner/, which the default repository owners cover.Post-Approval Actions