Skip to content

Add opentelemetry instrumentation instructions to cloud run MCP sample#13991

Merged
iennae merged 8 commits intoGoogleCloudPlatform:mainfrom
aabmass:push-mklxmwyxyzkt
Apr 21, 2026
Merged

Add opentelemetry instrumentation instructions to cloud run MCP sample#13991
iennae merged 8 commits intoGoogleCloudPlatform:mainfrom
aabmass:push-mklxmwyxyzkt

Conversation

@aabmass
Copy link
Copy Markdown
Contributor

@aabmass aabmass commented Apr 7, 2026

Description

Adds OpenTelemetry SDK setup to the Cloud Run MCP sample. FastMCP has instrumentation built in, so this starts sending it to Cloud Trace
image

Checklist

@product-auto-label product-auto-label Bot added samples Issues that are directly related to samples. api: run Issues related to the Cloud Run API. labels Apr 7, 2026
Copy link
Copy Markdown
Contributor

@gemini-code-assist gemini-code-assist Bot left a comment

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 integrates OpenTelemetry into the MCP server sample to enable observability for traces, logs, and metrics. It includes the necessary dependencies in pyproject.toml and uv.lock, adds a new setup script for OpenTelemetry, and updates the server and test files to initialize it. I have added a suggestion to include a note about the experimental status of the OpenTelemetry Logging SDK in the README to better inform users regarding production readiness.

Comment thread run/mcp-server/README.md Outdated
@aabmass aabmass force-pushed the push-mklxmwyxyzkt branch from 8b476c9 to 567084e Compare April 7, 2026 23:03
Comment thread run/mcp-server/otel_setup.py Outdated
"""Sets up OpenTelemetry to send traces, logs, and metrics to GCP Observability."""
credentials, project_id = google.auth.default()
if not project_id:
raise Exception("Could not determine Google Cloud project 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.

Could we refine the error handling here? It would be great to add import google.api_core.exceptions and catch the more specific google.auth.exceptions.DefaultCredentialsError. This helps make the error handling more robust and explicit."

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.

I added this check because the returned project_id can be None even without an exception being raised IIUC.

I think it's OK to let DefaultCredentialsError bubble up from the sample right?

logger = logging.getLogger(__name__)


def setup_opentelemetry(service_name: 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.

can you add a try...except block and catch not just the credentials issue but whether it successfully was able to connect to the telemetry endpoint?

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.

Unfortunately there's not a great way to catch it, the connection happens lazily/asynchronously.

Would you prefer if I split this into another sample duplicating most of the code to decouple the samples?

Copy link
Copy Markdown
Contributor Author

@aabmass aabmass left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the review!

logger = logging.getLogger(__name__)


def setup_opentelemetry(service_name: 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.

Unfortunately there's not a great way to catch it, the connection happens lazily/asynchronously.

Would you prefer if I split this into another sample duplicating most of the code to decouple the samples?

"""Sets up OpenTelemetry to send traces, logs, and metrics to GCP Observability."""
credentials, project_id = google.auth.default()
if not project_id:
raise Exception("Could not determine Google Cloud project 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.

I added this check because the returned project_id can be None even without an exception being raised IIUC.

I think it's OK to let DefaultCredentialsError bubble up from the sample right?

@aabmass aabmass force-pushed the push-mklxmwyxyzkt branch from 567084e to 7d4cb65 Compare April 8, 2026 14:18
@aabmass aabmass marked this pull request as ready for review April 8, 2026 14:18
@aabmass aabmass requested review from a team as code owners April 8, 2026 14:18
@snippet-bot
Copy link
Copy Markdown

snippet-bot Bot commented Apr 8, 2026

Here is the summary of changes.

You are about to add 3 region tags.

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

@aabmass aabmass force-pushed the push-mklxmwyxyzkt branch 5 times, most recently from cc6e585 to cf7a1c3 Compare April 10, 2026 21:45
Comment thread run/mcp-server/Dockerfile.otel Outdated
Comment thread run/mcp-server/pyproject.toml Outdated
Comment thread run/mcp-server/server.py Outdated
Comment thread run/mcp-server/server_otel.py Outdated
Comment thread run/mcp-server/server_otel.py Outdated
Comment thread run/mcp-server/test_server_otel.py Outdated
@aabmass aabmass force-pushed the push-mklxmwyxyzkt branch from 50bfc4f to 9d3e86d Compare April 13, 2026 20:07
Copy link
Copy Markdown
Contributor

@jackwotherspoon jackwotherspoon left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM ✅

@iennae iennae merged commit 46bb4cc into GoogleCloudPlatform:main Apr 21, 2026
11 checks passed
@aabmass aabmass deleted the push-mklxmwyxyzkt branch April 22, 2026 02:27
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

api: run Issues related to the Cloud Run API. samples Issues that are directly related to samples.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants