feat: API-Server - Added OTel trace id auto-instrumentation for FastAPI#82
feat: API-Server - Added OTel trace id auto-instrumentation for FastAPI#82morgan-wowk wants to merge 1 commit intomasterfrom
Conversation
567fbc0 to
bfea644
Compare
**Changes:** * Install OTel packages for FastAPI * Use OTel FastAPI auto-instrumetation for trace ids on responses
bfea644 to
fd231ed
Compare
Ark-kun
left a comment
There was a problem hiding this comment.
Thank you for implementing this.
| ) | ||
|
|
||
| except Exception as e: | ||
| logger.error(f"Failed to configure OpenTelemetry tracing: {e}", exc_info=True) |
There was a problem hiding this comment.
Nit: Can just use logger.exception. It sets exc_info=True automatically.
| from opentelemetry.sdk.trace import TracerProvider | ||
| from opentelemetry.sdk.trace.export import BatchSpanProcessor | ||
|
|
||
| logger = logging.getLogger(__name__) |
There was a problem hiding this comment.
_logger (to make it private)
| from fastapi import FastAPI | ||
| from opentelemetry import trace | ||
| from opentelemetry.exporter.otlp.proto.grpc.trace_exporter import ( | ||
| OTLPSpanExporter as GRPCSpanExporter, |
There was a problem hiding this comment.
Just curious - Why are we renaming this import?
| from opentelemetry.exporter.otlp.proto.grpc.trace_exporter import ( | ||
| OTLPSpanExporter as GRPCSpanExporter, | ||
| ) | ||
| from opentelemetry.instrumentation.fastapi import FastAPIInstrumentor |
There was a problem hiding this comment.
Style: Let's import modules, not particular classes/functions. https://google.github.io/styleguide/pyguide.html#22-imports
Usage then becomes: module1.Class1 or module1.function1.
Ambiguous module names can be renamed.
import fastapi
#? from opentelemetry.exporter.otlp.proto.grpc import trace_exporter
from opentelemetry import trace as otel_trace
from opentelemetry.sdk import resources as otel_sdk_resources
from opentelemetry.sdk import trace as otel_sdk_trace
from opentelemetry.sdk.trace import export as otel_sdk_trace_export
TBH, I dislike the module naming conventions of opentelemetry SDK. Ambiguity like .trace vs .sdk.trace. I also kind of dislike the module names that look like verbs: trace, export and single nouns like status. These names clash with function names and local variables.
So if the result looks too ugly to you, feel free to skip.
| resource = Resource(attributes={SERVICE_NAME: service_name}) | ||
|
|
||
| # Create the OTLP exporter | ||
| otlp_exporter = GRPCSpanExporter( |
There was a problem hiding this comment.
Is GRPCSpanExporter the only exporter that would work good for Tangle API telemetry?
There was a problem hiding this comment.
We can actually support multiple. Like HTTP. Good point.
There was a problem hiding this comment.
I'll change this to be configurable and support multiple exporters
|
|
||
| try: | ||
| # Build service name with environment suffix | ||
| app_env = os.environ.get("APP_ENV", "development") |
There was a problem hiding this comment.
Isn't environement usually ENV?
| ) | ||
|
|
||
| # Configure OpenTelemetry tracing | ||
| otel_tracing.setup_api_tracing(app) |
There was a problem hiding this comment.
Thank you for making it non-invasive.
|
|
||
| # Instrument the FastAPI application | ||
| # This automatically creates spans for all incoming HTTP requests | ||
| FastAPIInstrumentor.instrument_app(app) |
There was a problem hiding this comment.
What kind of data will this capture and export?

Changes:
otel_tracing.pymodule for configuring distributed tracing