Skip to content

feat(spanner): Add OpenTelemetry OTLP end-to-end tracing sample - #14870

Open
iennae wants to merge 4 commits into
feat/spanner-disable-builtin-metricsfrom
feat/spanner-opentelemetry-traces-otlp
Open

iennae wants to merge 4 commits into
feat/spanner-disable-builtin-metricsfrom
feat/spanner-opentelemetry-traces-otlp

Conversation

@iennae

@iennae iennae commented Nov 17, 2026

Copy link
Copy Markdown
Contributor

Description

Configures client-side and end-to-end Spanner tracing (TracerProvider, OTLPSpanExporter, TraceContextTextMapPropagator, and enable_end_to_end_tracing=True), replacing the archived python-spanner example.

Adds spanner_opentelemetry_traces_otlp_usage.

Note: Stacked on #14857 (feat/spanner-disable-builtin-metrics) so this PR shows only this sample's diff. It will merge into main after #14857.

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

Tracing documentation embedded an archived python-spanner example that executed an invalid SQL query and omitted clean TracerProvider shutdown.
@iennae
iennae requested review from a team as code owners November 17, 2026 23:48
@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 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.

Comment on lines +28 to +35
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.
"""

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

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.

Suggested change
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
  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.

Comment on lines +57 to +68
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,
)

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

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.

Suggested change
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,
)

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 +89 to +93
"--project-id",
default=os.environ.get("GOOGLE_CLOUD_PROJECT", "example-project-id"),
help="Google Cloud project ID (default: $GOOGLE_CLOUD_PROJECT)",
)
parser.add_argument(

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

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

Comment on lines +88 to +91
mock_exporter_class.return_value.export.return_value = SpanExportResult.SUCCESS
database = (
mock_client_class.return_value.instance.return_value.database.return_value
)

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

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.

Suggested change
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
)

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! 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.
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