Skip to content

Recursive logging from #4799 performance #4866

@aabmass

Description

@aabmass

#4799 is not yet released but it works by walking the stack on each on_emit() call which is pretty hot code.

I think using a contextvar (or OTel context) would be significantly faster since loading a traceback is slow and then we have to walk the stack. Rough estimate of perf difference:

$ python -m timeit -s 'import traceback' 'sum(
                item.name == "on_emit"
                and (
                    item.filename.endswith("export/__init__.py")
                    or item.filename.endswith(
                        r"export\__init__.py"
                    )  # backward slash on windows..
                )
                for item in traceback.extract_stack()
            )'

10000 loops, best of 5: 22.8 usec per loop

$ python -m timeit -s 'import contextvars; c = contextvars.ContextVar("c", default=False)' 'token = c.set("True"); bool(c.get()); c.reset(token)'
2000000 loops, best of 5: 157 nsec per loop

Metadata

Metadata

Assignees

No one assigned

    Labels

    loggingsdkAffects the SDK package.

    Type

    No type

    Projects

    Status

    Todo

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions