GithubHelp home page GithubHelp logo

Comments (3)

TheUncharted avatar TheUncharted commented on June 2, 2024

I have the same problem I think

I followed the same example
My context : when deployed I use Azure App Service + Fastapi inside a docker container and in local I just run Fastapi
Those are my libs with poetry :

opentelemetry-api = "1.17.0"
opentelemetry-sdk = "1.17.0"
azure-monitor-opentelemetry = {version = "^1.0.0b13", allow-prereleases = true}

I tested the tracing and the metrics
For the metrics, it works as expected and for the tracing when I test my routes there is no display of tracing of my routes.

Also I tested also with opentelemetry + azure-monitor-opentelemetry-exporter directly with those libs and following some tutos

azure-monitor-opentelemetry-exporter = {version = "^1.0.0b14", allow-prereleases = true}
opentelemetry-instrumentation-fastapi = "^0.39b0"
opentelemetry-api = "1.18.0"
opentelemetry-sdk = "1.18.0"
opentelemetry-instrumentation-fastapi = "^0.39b0"

and it works

This is the pseudo code I put to make it works

from fastapi.responses import JSONResponse
from starlette.exceptions import HTTPException as StarletteHTTPException

from opentelemetry.context import get_current as get_current_context
from opentelemetry import trace
from opentelemetry.sdk.trace import TracerProvider, _Span
from opentelemetry.sdk.trace.export import (BatchSpanProcessor,
                                            ConsoleSpanExporter,
                                            SimpleSpanProcessor)
from opentelemetry.instrumentation.fastapi import FastAPIInstrumentor
from azure.monitor.opentelemetry.exporter import AzureMonitorTraceExporter

tracer = TracerProvider()
tracer.add_span_processor(
    SimpleSpanProcessor(ConsoleSpanExporter())
)
exporter = AzureMonitorTraceExporter.from_connection_string(
    "MY_KEY"
)

tracer.add_span_processor(
    BatchSpanProcessor(exporter)
)

app = FastAPI()
FastAPIInstrumentor.instrument_app(app, tracer_provider=tracer)

@app.exception_handler(StarletteHTTPException)
async def http_exception_handler(request: Request, exc):
    span_key = list(get_current_context().keys())[0]
    span = get_current_context().get(span_key)
    assert isinstance(span, _Span)
    return JSONResponse(
        {
            "message": str(exc.detail),
            "trace_id": span.context.trace_id,
            "span_id": span.context.span_id
        },
        status_code=exc.status_code
    )

from applicationinsights-python.

kevinco26 avatar kevinco26 commented on June 2, 2024

Hi! I ended up solving this, but I had to just use the opencensus package.. in the end, the problem was how the instrumentation key was being set...

app.add_middleware(FastAPIMiddleware, exporter=AzureExporter(connection_string=application_insights_connection_string))
application_insights_connection_string= 'InstrumentationKey=XXXX'

this in addition to using from opencensus.ext.azure.trace_exporter import AzureExporter did it for me

from applicationinsights-python.

TheUncharted avatar TheUncharted commented on June 2, 2024

Hey @kevinco26
Thanks for the tips
what is this FastAPIMiddleware ? is it your or someone custom middleware, if yes can you share it ?

from applicationinsights-python.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.