|
| 1 | +"""High-level tracing API built on a Rust backend. |
| 2 | +
|
| 3 | +This module exposes a minimal interface for starting and stopping |
| 4 | +runtime traces. The heavy lifting is delegated to the |
| 5 | +`codetracer_python_recorder` Rust extension which will eventually hook |
| 6 | +into `runtime_tracing` and `sys.monitoring`. For now the Rust side only |
| 7 | +maintains placeholder state and performs no actual tracing. |
| 8 | +""" |
| 9 | +from __future__ import annotations |
| 10 | + |
| 11 | +import contextlib |
| 12 | +import os |
| 13 | +from pathlib import Path |
| 14 | +from typing import Iterable, Iterator, Optional |
| 15 | + |
| 16 | +from codetracer_python_recorder import ( |
| 17 | + flush_tracing as _flush_backend, |
| 18 | + is_tracing as _is_tracing_backend, |
| 19 | + start_tracing as _start_backend, |
| 20 | + stop_tracing as _stop_backend, |
| 21 | +) |
| 22 | + |
| 23 | +TRACE_BINARY: str = "binary" |
| 24 | +TRACE_JSON: str = "json" |
| 25 | +DEFAULT_FORMAT: str = TRACE_BINARY |
| 26 | + |
| 27 | +_active_session: Optional["TraceSession"] = None |
| 28 | + |
| 29 | + |
| 30 | +def _normalize_source_roots(source_roots: Iterable[os.PathLike | str] | None) -> Optional[list[str]]: |
| 31 | + if source_roots is None: |
| 32 | + return None |
| 33 | + return [str(Path(p)) for p in source_roots] |
| 34 | + |
| 35 | + |
| 36 | +def start( |
| 37 | + path: os.PathLike | str, |
| 38 | + *, |
| 39 | + format: str = DEFAULT_FORMAT, |
| 40 | + capture_values: bool = True, |
| 41 | + source_roots: Iterable[os.PathLike | str] | None = None, |
| 42 | +) -> "TraceSession": |
| 43 | + """Start a global trace session. |
| 44 | +
|
| 45 | + Parameters mirror the design document. The current implementation |
| 46 | + merely records the active state on the Rust side and performs no |
| 47 | + tracing. |
| 48 | + """ |
| 49 | + global _active_session |
| 50 | + if _is_tracing_backend(): |
| 51 | + raise RuntimeError("tracing already active") |
| 52 | + |
| 53 | + trace_path = Path(path) |
| 54 | + _start_backend(str(trace_path), format, capture_values, _normalize_source_roots(source_roots)) |
| 55 | + session = TraceSession(path=trace_path, format=format) |
| 56 | + _active_session = session |
| 57 | + return session |
| 58 | + |
| 59 | + |
| 60 | +def stop() -> None: |
| 61 | + """Stop the active trace session if one is running.""" |
| 62 | + global _active_session |
| 63 | + if not _is_tracing_backend(): |
| 64 | + return |
| 65 | + _stop_backend() |
| 66 | + _active_session = None |
| 67 | + |
| 68 | + |
| 69 | +def is_tracing() -> bool: |
| 70 | + """Return ``True`` when a trace session is active.""" |
| 71 | + return _is_tracing_backend() |
| 72 | + |
| 73 | + |
| 74 | +def flush() -> None: |
| 75 | + """Flush buffered trace data. |
| 76 | +
|
| 77 | + With the current placeholder implementation this is a no-op but the |
| 78 | + function is provided to match the planned public API. |
| 79 | + """ |
| 80 | + if _is_tracing_backend(): |
| 81 | + _flush_backend() |
| 82 | + |
| 83 | + |
| 84 | +@contextlib.contextmanager |
| 85 | +def trace( |
| 86 | + path: os.PathLike | str, |
| 87 | + *, |
| 88 | + format: str = DEFAULT_FORMAT, |
| 89 | + capture_values: bool = True, |
| 90 | + source_roots: Iterable[os.PathLike | str] | None = None, |
| 91 | +) -> Iterator["TraceSession"]: |
| 92 | + """Context manager helper for scoped tracing.""" |
| 93 | + session = start( |
| 94 | + path, |
| 95 | + format=format, |
| 96 | + capture_values=capture_values, |
| 97 | + source_roots=source_roots, |
| 98 | + ) |
| 99 | + try: |
| 100 | + yield session |
| 101 | + finally: |
| 102 | + session.stop() |
| 103 | + |
| 104 | + |
| 105 | +class TraceSession: |
| 106 | + """Handle representing a live tracing session.""" |
| 107 | + |
| 108 | + path: Path |
| 109 | + format: str |
| 110 | + |
| 111 | + def __init__(self, path: Path, format: str) -> None: |
| 112 | + self.path = path |
| 113 | + self.format = format |
| 114 | + |
| 115 | + def stop(self) -> None: |
| 116 | + """Stop this trace session.""" |
| 117 | + if _active_session is self: |
| 118 | + stop() |
| 119 | + |
| 120 | + def flush(self) -> None: |
| 121 | + """Flush buffered trace data for this session.""" |
| 122 | + flush() |
| 123 | + |
| 124 | + def __enter__(self) -> "TraceSession": |
| 125 | + return self |
| 126 | + |
| 127 | + def __exit__(self, exc_type, exc, tb) -> None: # pragma: no cover - thin wrapper |
| 128 | + self.stop() |
| 129 | + |
| 130 | + |
| 131 | +def _auto_start_from_env() -> None: |
| 132 | + path = os.getenv("CODETRACER_TRACE") |
| 133 | + if not path: |
| 134 | + return |
| 135 | + fmt = os.getenv("CODETRACER_FORMAT", DEFAULT_FORMAT) |
| 136 | + capture_env = os.getenv("CODETRACER_CAPTURE_VALUES") |
| 137 | + capture = True |
| 138 | + if capture_env is not None: |
| 139 | + capture = capture_env.lower() not in {"0", "false", "no"} |
| 140 | + start(path, format=fmt, capture_values=capture) |
| 141 | + |
| 142 | + |
| 143 | +_auto_start_from_env() |
| 144 | + |
| 145 | +__all__ = [ |
| 146 | + "TraceSession", |
| 147 | + "DEFAULT_FORMAT", |
| 148 | + "TRACE_BINARY", |
| 149 | + "TRACE_JSON", |
| 150 | + "start", |
| 151 | + "stop", |
| 152 | + "is_tracing", |
| 153 | + "trace", |
| 154 | + "flush", |
| 155 | +] |
0 commit comments