generated from pamelafox/icon-writer-function
-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #72 from pamelafox/fapifix
Fix FastAPI otel issue
- Loading branch information
Showing
3 changed files
with
16 additions
and
19 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters