-
Notifications
You must be signed in to change notification settings - Fork 6.7k
Add opentelemetry instrumentation instructions to cloud run MCP sample #13991
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
5282aab
f4d59e6
271e5ea
ca43f9c
6ce7f8b
7a951ea
e2dc442
cc66ecf
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,67 @@ | ||
| # Copyright 2026 Google LLC | ||
| # | ||
| # Licensed under the Apache License, Version 2.0 (the "License"); | ||
| # you may not use this file except in compliance with the License. | ||
| # You may obtain a copy of the License at | ||
| # | ||
| # http://www.apache.org/licenses/LICENSE-2.0 | ||
| # | ||
| # Unless required by applicable law or agreed to in writing, software | ||
| # distributed under the License is distributed on an "AS IS" BASIS, | ||
| # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| # See the License for the specific language governing permissions and | ||
| # limitations under the License. | ||
|
|
||
| # [START cloudrun_mcpserver_setup_otel] | ||
| import logging | ||
| import google.auth | ||
| import google.auth.transport.requests | ||
| import grpc | ||
| from google.auth.transport.grpc import AuthMetadataPlugin | ||
| from opentelemetry import trace | ||
| from opentelemetry.exporter.otlp.proto.grpc.trace_exporter import ( | ||
| OTLPSpanExporter, | ||
| ) | ||
| from opentelemetry.sdk.resources import SERVICE_NAME, Resource | ||
| from opentelemetry.sdk.trace import TracerProvider | ||
| from opentelemetry.sdk.trace.export import BatchSpanProcessor | ||
|
|
||
| logger = logging.getLogger(__name__) | ||
|
|
||
|
|
||
| def setup_opentelemetry(service_name: str) -> None: | ||
| """Sets up OpenTelemetry to send traces to Google Cloud Observability.""" | ||
| credentials, project_id = google.auth.default() | ||
| if not project_id: | ||
| raise Exception("Could not determine Google Cloud project ID.") | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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."
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I added this check because the returned I think it's OK to let |
||
|
|
||
| resource = Resource.create( | ||
| attributes={ | ||
| SERVICE_NAME: service_name, | ||
| "gcp.project_id": project_id, | ||
| } | ||
| ) | ||
|
|
||
| # Set up OTLP auth | ||
| request = google.auth.transport.requests.Request() | ||
| auth_metadata_plugin = AuthMetadataPlugin(credentials=credentials, request=request) | ||
| channel_creds = grpc.composite_channel_credentials( | ||
| grpc.ssl_channel_credentials(), | ||
| grpc.metadata_call_credentials(auth_metadata_plugin), | ||
| ) | ||
|
|
||
| # Set up OpenTelemetry Python SDK | ||
| tracer_provider = TracerProvider(resource=resource) | ||
| tracer_provider.add_span_processor( | ||
| BatchSpanProcessor( | ||
| OTLPSpanExporter( | ||
| credentials=channel_creds, | ||
| endpoint="https://telemetry.googleapis.com:443/v1/traces", | ||
| ) | ||
| ) | ||
| ) | ||
| trace.set_tracer_provider(tracer_provider) | ||
| logger.info("OpenTelemetry successfully initialized.") | ||
|
|
||
|
|
||
| # [END cloudrun_mcpserver_setup_otel] | ||
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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?