New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Integration with fastapi #1020
Comments
hi @mr-bjerre This seems interesting, one of the issues that we were facing. Any help would be greatful Thanks |
Shortly speaking they seem similar. The azure example is a minimal example of what I provided (from a quick perspective). If you mean "out of memory" then I don't see how that could be the case. |
If you are using Azure App Insights is also good to add For example: tracer.add_attribute_to_current_span(HTTP_ROUTE, str(request.url.path)) |
Source: Azure Monitor - Tracking FastAPI applications. This Azure example is good the simple implementation they have. @app.middleware("http")
async def middlewareOpencensus(request: Request, call_next):
tracer = Tracer(exporter=AzureExporter(connection_string=f'InstrumentationKey={APPINSIGHTS_INSTRUMENTATIONKEY}'),sampler=ProbabilitySampler(1.0))
with tracer.span("main") as span:
span.span_kind = SpanKind.SERVER
response = await call_next(request)
tracer.add_attribute_to_current_span(
attribute_key=HTTP_STATUS_CODE,
attribute_value=response.status_code)
tracer.add_attribute_to_current_span(
attribute_key=HTTP_URL,
attribute_value=str(request.url))
return response @gautam-ergo, the azure example above has the tracer initiated within the function which initiates a new tracer every time and causes OOM. So, please initiate before the function for your program. A simple example is below. tracer = Tracer(exporter=AzureExporter(connection_string=f'InstrumentationKey={APPINSIGHTS_INSTRUMENTATIONKEY}'),sampler=ProbabilitySampler(1.0))
@app.middleware("http")
async def middlewareOpencensus(request: Request, call_next):
with tracer.span("main") as span:
span.span_kind = SpanKind.SERVER
response = await call_next(request)
tracer.add_attribute_to_current_span(
attribute_key=HTTP_STATUS_CODE,
attribute_value=response.status_code)
tracer.add_attribute_to_current_span(
attribute_key=HTTP_URL,
attribute_value=str(request.url))
return response Hope this helps! |
There is a memory leak in the FastAPI / Tracer sample code. In the code, the Tracer is created on every request, and that leaks hundreds of KB of memory for each request. Each AzureExporer instantiation will spin up worker threads and will remain allocated even after the request is over. We should only instantiate the Tracer once outside of the middleware function. This bug was found separately in the opencensus repo. census-instrumentation/opencensus-python#1020 (comment)
thanks @gkocjan , this was really helpful |
@mr-bjerre Your integration would help much people including me. Could you proceed with a PR? |
I'm not really using this anymore but you should be able to simply use it as is? |
I have used your code in some projects and it would be more useful if it maintained in the contrib dir. If you say so, could I make a PR using your code and my unittests? |
Yes of course. Go ahead - I agree that would be preferable! |
Would this solution also enable to include custom properties (e.g. header information)? If yes, how would one do this? I tried extending the Middleware by adding |
I've created my own FastAPI middleware. I figured it could be useful for others and it would be great to maintain it in the
contrib
library here.Implementation of
opencensus.ext.fastapi
Minimal example using it
Let me know if I should proceed with a PR
The text was updated successfully, but these errors were encountered: