oughtinc/ice

Unclear how to use tracing within applications

Opened this issue · 2 comments

I tried running an example recipe with python hello.py - and it works perfectly.

However, I can not find any instructions on how to use tracing functionality not as a standalone demonstration, but inside other applications.

A simplest example would be:

hello.py:

from ice.recipe import recipe

async def say_hello():
    return "Hello world!"

recipe.main(say_hello)

test.py:

from hello import say_hello
import asyncio

if __name__ == '__main__':
    print(asyncio.run(say_hello()))

Which does not work when run with python test.py - no traces being recorded.

Could anyone please point me towards a way to achieve this?

For anyone stumbling into this issue later:

After digging into the code of ICE I came up with this SOLUTION:

hello.py:

from ice.trace import trace

@trace
async def say_hello():
    return "Hello world!"

test.py:

from hello import say_hello
import asyncio
from ice.trace import trace, enable_trace

if __name__ == '__main__':
    enable_trace()
    @trace
    async def main_wrapper():
        # A traced function cannot be called until the event loop is running.
        return await say_hello()

    print(asyncio.run(main_wrapper()))

This code performs tracing when started with python test.py

Despite the solution being found - it may be benificial to mention this method of running tracing in the README.
Hence I am leaving the issue open.