Instrumenting custom codeedit

Elastic APM instruments a variety of libraries out of the box, but sometimes you need to know how long a specific function took or how often it gets called.

You can apply the @elasticapm.capture_span() decorator to achieve exactly that. It can also be used as a context manager in the following way:

import elasticapm

@elasticapm.capture_span()
def coffee_maker(strength):
    fetch_water()

    with elasticapm.capture_span('near-to-machine'):
        insert_filter()
        for i in range(strength):
            pour_coffee()

        start_drip()

    fresh_pots()

Similarly, you can use elasticapm.async_capture_span for instrumenting async workloads:

import elasticapm

@elasticapm.async_capture_span()
async def coffee_maker(strength):
    await fetch_water()

    async with elasticapm.async_capture_span('near-to-machine'):
        await insert_filter()
        async for i in range(strength):
            await pour_coffee()

        start_drip()

    fresh_pots()

asyncio support is only available in Python 3.7+.

See the API docs for more information on capture_span.