autometrics-dev / autometrics-py

Easily add metrics to your code that actually help you spot and debug issues in production. Built on Prometheus and OpenTelemetry.
https://autometrics.dev
Apache License 2.0
214 stars 7 forks source link

Add support for async functions #29

Closed flenter closed 1 year ago

flenter commented 1 year ago

Right now the decorator assumes that the function it is wrapping when it returns it is also finished. This is not the case with async functions.

async def async_function():
    """This is an async function."""
    await asyncio.sleep(0.1)
    return True

When calling this function it will return quickly but when you use await it suddenly takes ~0.1s longer.

brettimus commented 1 year ago

Adding a note here: Decorating fastapi path operation functions with @autometrics breaks the API

So, this throws an error:

@app.get("/async-test")
@autometrics
async def async_test_route():
    message = await my_async_function()
    return {"Hello": message}

But this works:

@app.get("/async-test")
async def async_test_route():
    message = await my_async_function()
    return {"Hello": message}