Skip to content

Logging to Application Insights #433

@bwoods89

Description

@bwoods89

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.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions