-
Notifications
You must be signed in to change notification settings - Fork 13
feat: API-Server - Added OTel trace id auto-instrumentation for FastAPI #82
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
base: master
Are you sure you want to change the base?
Changes from all commits
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,84 @@ | ||
| """ | ||
| OpenTelemetry tracing configuration for FastAPI applications. | ||
|
|
||
| This module sets up distributed tracing with OTLP exporter for sending traces | ||
| to an OpenTelemetry collector endpoint specified via OTEL_EXPORTER_OTLP_ENDPOINT. | ||
| """ | ||
|
|
||
| import logging | ||
| import os | ||
|
|
||
| from fastapi import FastAPI | ||
| from opentelemetry import trace | ||
| from opentelemetry.exporter.otlp.proto.grpc.trace_exporter import ( | ||
| OTLPSpanExporter as GRPCSpanExporter, | ||
|
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. Just curious - Why are we renaming this import? |
||
| ) | ||
| from opentelemetry.instrumentation.fastapi import FastAPIInstrumentor | ||
|
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. Style: Let's import modules, not particular classes/functions. https://google.github.io/styleguide/pyguide.html#22-imports TBH, I dislike the module naming conventions of opentelemetry SDK. Ambiguity like So if the result looks too ugly to you, feel free to skip. |
||
| from opentelemetry.sdk.resources import Resource, SERVICE_NAME | ||
| from opentelemetry.sdk.trace import TracerProvider | ||
| from opentelemetry.sdk.trace.export import BatchSpanProcessor | ||
|
|
||
| logger = logging.getLogger(__name__) | ||
|
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.
|
||
|
|
||
|
|
||
| def setup_api_tracing(app: FastAPI) -> None: | ||
| """ | ||
| Configure OpenTelemetry tracing for a FastAPI application. | ||
|
|
||
| Args: | ||
| app: The FastAPI application instance to instrument | ||
|
|
||
| Environment Variables: | ||
| OTEL_EXPORTER_OTLP_ENDPOINT: The endpoint URL for the OTLP collector | ||
| (e.g., "http://localhost:4317") | ||
| If not set, tracing will not be exported. | ||
| APP_ENV: Optional environment name to include in service name | ||
| (defaults to "development") | ||
| """ | ||
| # Get OTLP endpoint from environment variable | ||
| otlp_endpoint = os.environ.get("OTEL_EXPORTER_OTLP_ENDPOINT") | ||
|
|
||
| if not otlp_endpoint: | ||
| logger.warning( | ||
| "OTEL_EXPORTER_OTLP_ENDPOINT not configured. " | ||
| "Tracing will not be exported. Set the environment variable to enable trace export." | ||
| ) | ||
| return | ||
|
|
||
| try: | ||
| # Build service name with environment suffix | ||
| app_env = os.environ.get("APP_ENV", "development") | ||
|
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. Isn't environement usually |
||
| service_name = f"tangle-{app_env}" | ||
|
|
||
| # Create a resource identifying this service | ||
| resource = Resource(attributes={SERVICE_NAME: service_name}) | ||
|
|
||
| # Create the OTLP exporter | ||
| otlp_exporter = GRPCSpanExporter( | ||
|
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. Is GRPCSpanExporter the only exporter that would work good for Tangle API telemetry?
Collaborator
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. We can actually support multiple. Like HTTP. Good point.
Collaborator
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'll change this to be configurable and support multiple exporters |
||
| endpoint=otlp_endpoint, | ||
| ) | ||
|
|
||
| # Create and configure the tracer provider | ||
| tracer_provider = TracerProvider(resource=resource) | ||
|
|
||
| # Add a batch span processor to export spans in batches | ||
| # This improves performance by reducing network overhead | ||
| span_processor = BatchSpanProcessor(otlp_exporter) | ||
| tracer_provider.add_span_processor(span_processor) | ||
|
|
||
| # Set the global tracer provider | ||
| trace.set_tracer_provider(tracer_provider) | ||
|
|
||
| # Instrument the FastAPI application | ||
| # This automatically creates spans for all incoming HTTP requests | ||
| FastAPIInstrumentor.instrument_app(app) | ||
|
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. What kind of data will this capture and export? |
||
|
|
||
| logger.info( | ||
| f"OpenTelemetry tracing configured successfully. " | ||
| f"Service: {service_name}, Endpoint: {otlp_endpoint}" | ||
| ) | ||
|
|
||
| except Exception as e: | ||
| logger.error(f"Failed to configure OpenTelemetry tracing: {e}", exc_info=True) | ||
|
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. Nit: Can just use |
||
| # Don't raise the exception - we don't want tracing setup failures | ||
| # to prevent the application from starting | ||
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.
Thank you for making it non-invasive.