Kevin Cohen
Kevin Cohen

Reputation: 1341

Python Azure opentelemetry - inbound requests not getting logged

I followed the instructions and the sample project here: https://github.com/microsoft/ApplicationInsights-Python/blob/05a8b1dd3556ab5e7c268c22ee30a365eaf5ec7a/azure-monitor-opentelemetry/samples/tracing/http_fastapi.py#L15. I set up my app insights account and I can see exceptions and dependencies properly getting logged.

However, the requests table is not getting populated.. Based on the link above, requests to my FastAPI should automatically be populated. Am I doing something wrong?

Upvotes: 2

Views: 778

Answers (2)

B.C
B.C

Reputation: 587

I have also encountered this issue recently after migrating a FastAPI app from openCensus to OpenTelemetry. The issue can be due to importing the FastAPI or Flask app before you initialize the logger. From the following link, which has plenty of useful other debugging information: https://learn.microsoft.com/en-us/azure/azure-monitor/app/opentelemetry-help-support-feedback?tabs=python

If you are missing Requests table data but not other categories, it is likely that your http framework is not being instrumented. This can occur in FastAPI and Flask apps using the Azure Monitor OpenTelemetry Distro client library for Python if you don't structure your import declarations correctly. You might be importing the fastapi.FastAPI or flask.Flask respectively before you call the configure_azure_monitor function to instrument the FastAPI and Flask libraries. For example, the following code doesn't successfully instrument the FastAPI and Flask apps:


# FastAPI

from azure.monitor.opentelemetry import configure_azure_monitor
from fastapi import FastAPI

configure_azure_monitor()

app = FastAPI()

But doing the following resulted in the requests table successfully being populated:

# FastAPI

from azure.monitor.opentelemetry import configure_azure_monitor

configure_azure_monitor()

from fastapi import FastAPI

app = FastAPI(__name__)

For completeness the article also suggests the following to fix the issue:

Instead, we recommend that you import the fastapi or flask modules as a whole, and then call configure_azure_monitor to configure OpenTelemetry to use Azure Monitor before you access fastapi.FastAPI or flask.Flask:

# FastAPI

from azure.monitor.opentelemetry import configure_azure_monitor

configure_azure_monitor()

from fastapi import FastAPI

app = FastAPI(__name__)

Upvotes: 0

Sumit Kumar Mahto
Sumit Kumar Mahto

Reputation: 1

I faced the same issue. I was also quite confused as to many things which are explained in Azure Documentation do not work as expected. This was one of them.

You will have to add a line to import FastAPIInstrumentor and instrument your app to see the requests coming into your app-insights.

from opentelemetry.instrumentation.fastapi import FastAPIInstrumentor FastAPIInstrumentor.instrument_app(app)

If this does not help, you might have to add a few more dependencies, which will instrument your requests.

For further info, check this out: FastAPI intrumentor

Upvotes: 0

Related Questions