Skip to content

Commit

Permalink
Merge pull request #72 from pamelafox/fapifix
Browse files Browse the repository at this point in the history
Fix FastAPI otel issue
  • Loading branch information
pamelafox authored Apr 7, 2024
2 parents 1153a72 + efabe48 commit 984fd0f
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 19 deletions.
17 changes: 0 additions & 17 deletions function/__init__.py
Original file line number Diff line number Diff line change
@@ -1,20 +1,3 @@
import os

from azure.monitor.opentelemetry.exporter import AzureMonitorTraceExporter
from opentelemetry.instrumentation.fastapi import FastAPIInstrumentor
from opentelemetry.sdk.resources import SERVICE_NAME, Resource
from opentelemetry.sdk.trace import TracerProvider
from opentelemetry.sdk.trace.export import BatchSpanProcessor

from .fastapi_app import create_app

fastapi_app = create_app()


@fastapi_app.on_event("startup")
async def startup_event():
if conn_str := os.getenv("APPLICATIONINSIGHTS_CONNECTION_STRING"):
exporter = AzureMonitorTraceExporter.from_connection_string(conn_str)
tracer = TracerProvider(resource=Resource({SERVICE_NAME: "api"}))
tracer.add_span_processor(BatchSpanProcessor(exporter))
FastAPIInstrumentor.instrument_app(fastapi_app, tracer_provider=tracer)
14 changes: 14 additions & 0 deletions function/fastapi_app.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,23 @@
import os

import fastapi
from azure.monitor.opentelemetry.exporter import AzureMonitorTraceExporter
from opentelemetry.instrumentation.fastapi import FastAPIInstrumentor
from opentelemetry.sdk.resources import SERVICE_NAME, Resource
from opentelemetry.sdk.trace import TracerProvider
from opentelemetry.sdk.trace.export import BatchSpanProcessor

from . import fastapi_routes


def create_app():
app = fastapi.FastAPI(docs_url="/")
if conn_str := os.getenv("APPLICATIONINSIGHTS_CONNECTION_STRING"):
exporter = AzureMonitorTraceExporter.from_connection_string(conn_str)
tracer = TracerProvider(resource=Resource({SERVICE_NAME: "api"}))
tracer.add_span_processor(BatchSpanProcessor(exporter))
FastAPIInstrumentor.instrument_app(app, tracer_provider=tracer)

app.include_router(fastapi_routes.router)

return app
4 changes: 2 additions & 2 deletions requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,5 @@ py-staticmaps==0.4.0
fastapi==0.110.0
Pillow==9.5.0
nest-asyncio==1.6.0
opentelemetry-instrumentation-fastapi==0.39b0
azure-monitor-opentelemetry-exporter==1.0.0b14
opentelemetry-instrumentation-fastapi==0.45b0
azure-monitor-opentelemetry-exporter==1.0.0b24

0 comments on commit 984fd0f

Please sign in to comment.