question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

[QUESTION] Logging to Application Insights

See original GitHub issue

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.

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Comments:6 (1 by maintainers)

github_iconTop GitHub Comments

2reactions
NixBikscommented, Apr 7, 2021

For anyone who might drop by. I’ve created fastapi middleware that can be added for opencencus tracer

import logging

from fastapi import Request
from opencensus.trace import (
    attributes_helper,
    execution_context,
    print_exporter,
    samplers,
)
from opencensus.trace import span as span_module
from opencensus.trace import tracer as tracer_module
from opencensus.trace import utils
from opencensus.trace.propagation import trace_context_http_header_format
from starlette.types import ASGIApp

HTTP_HOST = attributes_helper.COMMON_ATTRIBUTES["HTTP_HOST"]
HTTP_METHOD = attributes_helper.COMMON_ATTRIBUTES["HTTP_METHOD"]
HTTP_PATH = attributes_helper.COMMON_ATTRIBUTES["HTTP_PATH"]
HTTP_ROUTE = attributes_helper.COMMON_ATTRIBUTES["HTTP_ROUTE"]
HTTP_URL = attributes_helper.COMMON_ATTRIBUTES["HTTP_URL"]
HTTP_STATUS_CODE = attributes_helper.COMMON_ATTRIBUTES["HTTP_STATUS_CODE"]

module_logger = logging.getLogger(__name__)


class TracerMiddleware:
    def __init__(
        self,
        app: ASGIApp,
        excludelist_paths=None,
        excludelist_hostnames=None,
        sampler=None,
        exporter=None,
        propagator=None,
    ) -> None:
        self.app = app
        self.excludelist_paths = excludelist_paths
        self.excludelist_hostnames = excludelist_hostnames
        self.sampler = sampler or samplers.AlwaysOnSampler()
        self.exporter = exporter or print_exporter.PrintExporter()
        self.propagator = (
            propagator or trace_context_http_header_format.TraceContextPropagator()
        )

    async def __call__(self, request: Request, call_next):

        # Do not trace if the url is in the exclude list
        if utils.disable_tracing_url(str(request.url), self.excludelist_paths):
            return await call_next(request)

        try:
            span_context = self.propagator.from_headers(request.headers)

            tracer = tracer_module.Tracer(
                span_context=span_context,
                sampler=self.sampler,
                exporter=self.exporter,
                propagator=self.propagator,
            )
        except Exception:  # pragma: NO COVER
            module_logger.error("Failed to trace request", exc_info=True)
            return await call_next(request)

        try:
            span = tracer.start_span()
            span.span_kind = span_module.SpanKind.SERVER
            span.name = "[{}]{}".format(request.method, request.url)
            tracer.add_attribute_to_current_span(HTTP_HOST, request.url.hostname)
            tracer.add_attribute_to_current_span(HTTP_METHOD, request.method)
            tracer.add_attribute_to_current_span(HTTP_PATH, request.url.path)
            tracer.add_attribute_to_current_span(HTTP_URL, str(request.url))
            execution_context.set_opencensus_attr(
                "excludelist_hostnames", self.excludelist_hostnames
            )
        except Exception:  # pragma: NO COVER
            module_logger.error("Failed to trace request", exc_info=True)

        response = await call_next(request)
        try:
            tracer.add_attribute_to_current_span(HTTP_STATUS_CODE, response.status_code)
        except Exception:  # pragma: NO COVER
            module_logger.error("Failed to trace response", exc_info=True)
        finally:
            tracer.end_span()
            return response

It is heavily inspired by the flask middleware.

You can add the middleware by

app.middleware("http")(TracerMiddleware(app))

This thread might get interesting in the future

1reaction
tiangolocommented, Sep 1, 2019

Thanks for the help here @victoraugustolls! 🍰🚀

And thanks @bwoods89 for reporting back and closing the issue. 🎉

Read more comments on GitHub >

github_iconTop Results From Across the Web

Application Insights logging with .NET - Azure Monitor
Application Insights captures and sends ILogger logs by using the same TelemetryConfiguration information that's used for every other telemetry.
Read more >
How can I view logs in Application Insights? - Stack Overflow
Application Insights -> Transaction search Here you can filter data by TRACE type and it is your application logs.
Read more >
Sitecore Identity server logs to Application Insights
I want to write the logs into the Application Insights for the Sitecore Identity server. What I have done till: I checked the...
Read more >
Structured Logging In Microsoft's Azure Application Insights
This becomes especially critical with automatic logging frameworks/SDKs like Application Insights. Azure Application Insights logs all the ...
Read more >
Using Application Insights For Better Application Logging
App Insights can give you a clear look into your browser errors, making it easy to group error logs by various keys like...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found