Skip to content

Commit 3d311aa

Browse files
committed
Add Python tracer API description
1 parent b2e9e09 commit 3d311aa

File tree

1 file changed

+64
-0
lines changed

1 file changed

+64
-0
lines changed

docs/py-api-001.md

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
# Python sys.monitoring Tracer API
2+
3+
## Overview
4+
This document describes the user-facing Python API for the `codetracer` module built on top of `runtime_tracing` and `sys.monitoring`. The API exposes a minimal surface for starting and stopping traces, managing trace sessions, and integrating tracing into scripts or test suites.
5+
6+
## Module `codetracer`
7+
8+
### Constants
9+
- `DEFAULT_FORMAT: str = "binary"`
10+
- `TRACE_BINARY: str = "binary"`
11+
- `TRACE_JSON: str = "json"`
12+
13+
### Session Management
14+
- Start a global trace; returns a `TraceSession`.
15+
```py
16+
def start(path: str | os.PathLike, *, format: str = DEFAULT_FORMAT,
17+
capture_values: bool = True, source_roots: Iterable[str | os.PathLike] | None = None) -> TraceSession
18+
```
19+
- Stop the active trace if any.
20+
```py
21+
def stop() -> None
22+
```
23+
- Query whether tracing is active.
24+
```py
25+
def is_tracing() -> bool
26+
```
27+
- Context manager helper for scoped tracing.
28+
```py
29+
@contextlib.contextmanager
30+
def trace(path: str | os.PathLike, *, format: str = DEFAULT_FORMAT,
31+
capture_values: bool = True, source_roots: Iterable[str | os.PathLike] | None = None):
32+
...
33+
```
34+
- Flush buffered data to disk without ending the session.
35+
```py
36+
def flush() -> None
37+
```
38+
39+
## Class `TraceSession`
40+
Represents a live tracing session returned by `start()` and used by the context manager.
41+
42+
```py
43+
class TraceSession:
44+
path: pathlib.Path
45+
format: str
46+
47+
def stop(self) -> None: ...
48+
def flush(self) -> None: ...
49+
def __enter__(self) -> TraceSession: ...
50+
def __exit__(self, exc_type, exc, tb) -> None: ...
51+
```
52+
53+
## Environment Integration
54+
- Auto-start tracing when `CODETRACER_TRACE` is set; the value is interpreted as the output path.
55+
- When `CODETRACER_FORMAT` is provided, it overrides the default output format.
56+
- `CODETRACER_CAPTURE_VALUES` toggles value recording.
57+
58+
## Usage Example
59+
```py
60+
import codetracer
61+
62+
with codetracer.trace("trace.bin"):
63+
run_application()
64+
```

0 commit comments

Comments
 (0)