32 lines
1.1 KiB
Python
32 lines
1.1 KiB
Python
import os
|
|
|
|
from fastapi import FastAPI
|
|
from opentelemetry import trace
|
|
from opentelemetry.exporter.otlp.proto.http.trace_exporter import OTLPSpanExporter
|
|
from opentelemetry.instrumentation.fastapi import FastAPIInstrumentor
|
|
from opentelemetry.sdk.resources import Resource
|
|
from opentelemetry.sdk.trace import TracerProvider
|
|
from opentelemetry.sdk.trace.export import BatchSpanProcessor
|
|
|
|
provider = TracerProvider(resource=Resource.create({"service.name": "gateway-fastapi-demo"}))
|
|
provider.add_span_processor(BatchSpanProcessor(OTLPSpanExporter(
|
|
endpoint=os.getenv("OTEL_EXPORTER_OTLP_TRACES_ENDPOINT", "http://otel-lab-collector.monitoring.svc:4318/v1/traces")
|
|
)))
|
|
trace.set_tracer_provider(provider)
|
|
app = FastAPI(title="Jeannie Gateway API Lab")
|
|
FastAPIInstrumentor.instrument_app(app, excluded_urls="health")
|
|
|
|
|
|
@app.get("/health")
|
|
def health():
|
|
return {"status": "ok"}
|
|
|
|
|
|
@app.get("/api/info")
|
|
def info():
|
|
with trace.get_tracer(__name__).start_as_current_span("demo.compute") as span:
|
|
return {
|
|
"message": "Hello from FastAPI behind Gateway API",
|
|
"trace_id": format(span.get_span_context().trace_id, "032x"),
|
|
}
|