Description
I'm trying to send logs for my app to Azure Application Insights. I've tried following steps from this article: https://github.com/microsoft/ApplicationInsights-Python
Namely the section called "Basic logging configuration (second option)". I've setup the logger, added my instrumentation key, but I don't see anything going to Application Insights, or to the logs of my container. This could be unrelated to FastAPI, but I'm not sure. Here's a snippet of my code
from fastapi import FastAPI
import logging
from applicationinsights.logging import enable, LoggingHandler
from features import FeaturesRequest
from XGB_New_Cust_80_Feature import predict_output_new_customer
from XGB_Rep_Cust_80_Feature import predict_output_repeat_customer
app = FastAPI(openapi_prefix="/risk-model")
instrumentation_key = '<my instrumentation key>'
try:
with open("/keyvault/ApplicationInsights__InstrumentationKey", "r")as f:
instrumentation_key = f.read()
except IOError:
pass
enable(instrumentation_key)
handler = LoggingHandler(instrumentation_key)
logging.basicConfig(handlers=[ handler ], format='%(levelname)s: %(message)s', level=logging.DEBUG)
logger = logging.getLogger("main")
@app.post("/risk-predictions/new-customers")
async def new_customer_risk_prediction(features: FeaturesRequest):
logger.info(features)
prediction = predict_output_new_customer(features)
return {
"prediction": prediction[0][0],
"model": prediction[1]
}
@app.post("/risk-predictions/existing-customers")
async def existing_customer_risk_prediction(features: FeaturesRequest):
logger.info(features)
prediction = predict_output_repeat_customer(features)
return {
"prediction": prediction[0][0],
"model": prediction[1]
}
Additional context
Add any other context or screenshots about the feature request here.
Try using the OpenCensus library. Python Application Insights is deprecated, recommending to use OpenCensus instead. See here. If there are any issues using it, please, open an issue!
Thanks @victoraugustolls. I'll look into that. My initial read is that OpenCensus is a lot more focused on tracing, and doesn't capture stack traces or directly support logging. Does that track with what you know of it?
Hey, it does capture logging and stacktrace on exceptions! OpenTracing was focused more on tracing, and now we have OpenTelemetry, but it is still in specs phase. You can find more of Application Insights integration here
And I would recommend closing this issue as it isn鈥檛 related with FastAPI and would help the developer!!
Thanks for the help here @victoraugustolls! 馃嵃馃殌
And thanks @bwoods89 for reporting back and closing the issue. 馃帀