prometheus-fastapi-instrumentator icon indicating copy to clipboard operation
prometheus-fastapi-instrumentator copied to clipboard

Add async instrumentation support

Open AndreasPB opened this issue 3 years ago • 4 comments

AndreasPB avatar Aug 02 '21 10:08 AndreasPB

@trallnag should this be merged?

caspervk avatar Jan 11 '22 15:01 caspervk

Async support would be great

Skeen avatar Jun 14 '22 13:06 Skeen

Is this still relevant? From what I understand the new middleware implementation is async. I did not implement this, so I can't say for sure. In addition I know almost nothing about async Python.

https://github.com/trallnag/prometheus-fastapi-instrumentator/pull/139

trallnag avatar Aug 23 '22 17:08 trallnag

We use this branch at work, so it would be awesome to have it on master :)

Edit: I'll try to read through the changes to make sure

AndreasPB avatar Aug 24 '22 13:08 AndreasPB

Codecov Report

Merging #61 (b8491ab) into master (e1b2391) will increase coverage by 0.14%. The diff coverage is 100.00%.

@@            Coverage Diff             @@
##           master      #61      +/-   ##
==========================================
+ Coverage   93.60%   93.75%   +0.14%     
==========================================
  Files           5        5              
  Lines         297      304       +7     
==========================================
+ Hits          278      285       +7     
  Misses         19       19              
Impacted Files Coverage Δ
...ometheus_fastapi_instrumentator/instrumentation.py 95.52% <100.00%> (+0.28%) :arrow_up:
...rc/prometheus_fastapi_instrumentator/middleware.py 95.23% <100.00%> (+0.17%) :arrow_up:

Help us with your feedback. Take ten seconds to tell us how you rate us. Have a feature suggestion? Share it here.

codecov[bot] avatar Feb 23 '23 14:02 codecov[bot]

I managed to rebase your enhancement into the new middleware. I don't have any tests yet. Can you add examples on how you use async_instrumentation? Of course only if it is still relevant for you.

I will go ahead and merge it into master regardless because the changes don't interfere in any way with existing code.

trallnag avatar Feb 23 '23 14:02 trallnag

... Can you add examples on how you use async_instrumentation?

Hi, we use this feature actively.

The use-case is exactly as with the non-async case:

sync_metric = Info("sync_metric", "Documentation")
async_metric = Info("async_metric", "Documentation")

def sync_function(_: InstInfo) -> None:
    value = sync_metric_getter()
    sync_metric.info({"type": value})

async def async_function(_: InstInfo) -> None:
    value = await async_metric_getter()
    async_metric.info({"type": value})

instrumentator = Instrumentator()

instrumentator.add(sync_function)
instrumentator.add(async_function)

This just adds the option of using async def functions.

Skeen avatar Feb 23 '23 16:02 Skeen

@Skeen, thanks for the example. Adapted it into a unit test

trallnag avatar Feb 24 '23 16:02 trallnag

@Skeen, thanks for the example. Adapted it into a unit test

You're awesome, thank you for the work that you do!

Skeen avatar Feb 24 '23 16:02 Skeen

What @Skeen said - Thank you!

One last request, would it be possible to get a release with these changes sometime in the near future, @trallnag?

AndreasPB avatar Feb 24 '23 18:02 AndreasPB

@AndreasPB, I'll release it already this weekend

trallnag avatar Feb 24 '23 22:02 trallnag