Skip to content

agkphysics/python-simple-tracer

Repository files navigation

Simple Tracer

A simple tracer for Python programs using the sys.monitoring namespace introduced in Python 3.12. The tracer exports to the Chrome Trace event format.

Installation

pip install simple-tracer

Usage

As a context manager:

from simple_tracer import tracer

with tracer("trace.json"):
    func()

as a function decorator:

from simple_tracer import traceable

@traceable
def square(x: int) -> int:
    return x**2

or as a runnable script:

simple-tracer -o trace.json.gz -m mymodule
# or
simple-tracer -o trace.json.gz myscript.py

Examples

There are some example scripts in the examples directory. Some can be run as a python script, while others are to be launched using the simple-tracer command.

Limitations

Currently the tracer does not trace built-in dunder methods (e.g. __add__()), due to the way monitoring is implemented. This means that code such as

import numpy as np

x = np.zeros(10)
y = x + x

will not record the addition operation as a call to numpy.ndarray.__add__().

Releases

No releases published

Languages