Skip to content
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

Is there a way how to add metrics that are not dependent on request/response? #19

Closed
lpravda opened this issue Dec 4, 2020 · 2 comments
Labels
question Further information is requested

Comments

@lpravda
Copy link

lpravda commented Dec 4, 2020

Hello everyone, as title mentions, I wonder if this is something that can be done, if so how?

My present understanding is that the default implementation of metrics works on per requests/response basis. At least those objects are available from the prometheus_fastapi_instrumentator.metrics.Info object. I use them and they work as expected. However, the API I work on triggers fastapi.BackgroundTask with some external computation process, that is executed asynchronously.

What I would like to have is a metrics on how long the BackgroundTask execution took as well as resources consumed. I can extract the values when the task is completed, but I have no idea how should I create metric and pass those values to the instrumentator, so that these values are consumed in the same fashion as the default metrices.

Generally speaking the API contains end point for passing parameters. Response is immediate with info that the job has started (or not). Next, there is another end point to retrieve status / download results. If the job completed successfully no new job is triggered when new request comes in with the same parameters.

Any help would be greatly appreciated. Thank you!

@trallnag
Copy link
Owner

trallnag commented Dec 4, 2020

Hey @lpravda, it sounds like this is not something that can be covered by this project. As you have pointed out it is really only usable for request/response workflows where the response represents the "end" of the task you are interested to instrument. So in your case I'd probably do the instrumentation myself either completely uncoupled from the FastAPI endpoint or extract path info from the request and use that info in combination with custom instrumentation

I wonder if there are other options / existing libraries available for this. But I cannot think of any. Maybe in the future OpenTelemetry initiative will this easy and many different small packages redundant.

Can you post an example or a link that shows using BackgroundTask? It is new to me

@trallnag trallnag added the question Further information is requested label Dec 4, 2020
@lpravda
Copy link
Author

lpravda commented Dec 4, 2020

Hi @trallnag , thanks a lot for reaching back that quickly!

I'll try to think of decoupling the problem from request/response workflow and see if any of the attempts would work.

The documentation for background task is available from here: https://fastapi.tiangolo.com/tutorial/background-tasks/ Let me know if I could provide you with some other use case.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
question Further information is requested
Projects
None yet
Development

No branches or pull requests

2 participants