Replies: 7 comments 1 reply
-
|
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! |
Beta Was this translation helpful? Give feedback.
-
|
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? |
Beta Was this translation helpful? Give feedback.
-
|
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 |
Beta Was this translation helpful? Give feedback.
-
|
And I would recommend closing this issue as it isn’t related with FastAPI and would help the developer!! |
Beta Was this translation helpful? Give feedback.
-
|
Thanks for the help here @victoraugustolls! 🍰🚀 And thanks @bwoods89 for reporting back and closing the issue. 🎉 |
Beta Was this translation helpful? Give feedback.
-
|
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 responseIt 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 |
Beta Was this translation helpful? Give feedback.
-
|
Microsoft does have Middleware for FastAPI: However, I saw that they have kept the note as below: Looking for help to push my production FastAPI logs, traces on Azure Application Insights using OpenTelemetry. Can anyone help me? |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
-
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
Additional context
Add any other context or screenshots about the feature request here.
Beta Was this translation helpful? Give feedback.
All reactions