Skip to main content

Stream log files and extract context/blocks around spanId matches.

Project description

spectre

A small CLI tool that finds a span id in a large log file and prints either:

  • context around each match (like grep -C), or
  • the full logical block for the matched log event (including multiline stack traces)

This is useful for troubleshooting and correlation workflows (e.g., following a Dynatrace span/trace through services).

Features

  • Python 3.11+
  • Streams files line-by-line (safe for large logs)
  • Fixed-size ring buffer (deque) for before-context
  • Supported formats (auto-detected or forced):
    • KV / plain text (spanId=..., span_id=..., span-id=..., spanid=...)
    • JSON-per-line (extracts spanId, span_id, spanid, span-id, etc.)
    • W3C traceparent (traceparent="00-<trace-id>-<parent-id>-<flags>")
      The parent-id (16 hex chars) is treated as the span id for matching.

Install (local)

python -m venv .venv
source .venv/bin/activate
pip install -e .

Run:

spectre --logfile examples/sample_kv.log --spanid F76281848BD8288C --mode block --context 20

Install + Run (Docker)

Build:

docker build -t spectre .

Run (mount current folder as /work):

docker run --rm -v "$PWD:/work" spectre \
  --logfile /work/examples/sample_kv.log \
  --spanid F76281848BD8288C \
  --mode block \
  --context 20

Usage

spectre --logfile PATH --spanid VALUE [options]

Options:

  • --help: print this help message
  • --context N : number of lines before/after match (default: 20)
  • --mode {context,block} :
    • context: print N before + match + N after (like grep -C)
    • block: print the full “logical block” (including multiline stack traces) and also includes --context lines before the match
  • --output PATH : write to a file (default: stdout)
  • --format {auto,kv,json,traceparent} : force parsing mode (default: auto)
  • --ignore-case (default): case-insensitive matching
  • --case-sensitive: disable ignore-case
  • --interactive: prompt interactively for missing arguments or this is the default mode if no arguments are provided

Examples

KV / plain text log

spectre --logfile examples/sample_kv.log --spanid F76281848BD8288C --mode context --context 10

JSON-per-line log

spectre --logfile examples/sample_json.log --spanid a1b2c3d4e5f60718 --format json --mode block

traceparent log

In W3C traceparent, the third field is the parent-id, which is 16 hex chars and corresponds to a span id:

traceparent="00-<trace-id>-<parent-id>-<flags>"

spectre --logfile examples/sample_traceparent.log --spanid 6f35a0c9d2d3b4a1 --format traceparent --mode block

Output format

For each occurrence:

===== MATCH <index> START =====
line: <file line number>
spanId: <matching span id>
<extracted lines...>
===== MATCH <index> END =====

If no matches are found, the tool prints NOT FOUND and exits with code 1. If matches are found, it exits with code 0. File errors (not found / permission) exit with code 2.

Performance notes

  • The scanner streams the log file line-by-line.
  • Only a fixed-size deque(maxlen=context) is kept for “before” lines.
  • Output is written in a streaming way (no huge strings built in memory).

Standalone Build (No Python Required)

You can package spectre as a single executable using PyInstaller.
This allows users to run the tool without installing Python.

⚠️ Important: You must build on each target OS (Windows builds Windows exe, Linux builds Linux binary, macOS builds macOS binary).


1) Local build on Windows

In PowerShell:

py -3.14 -m venv .venv
.\.venv\Scripts\Activate.ps1
python -m pip install -U pip
python -m pip install -e .
python -m pip install pyinstaller

pyinstaller --onefile --console --name spectre -m spectre.cli

Output:

dist\spectre.exe

Test:

.\dist\spectre.exe --help

If you need to include the examples/ folder inside the executable:

pyinstaller --onefile --console --name spectre -m spectre.cli --add-data "examples;examples"

2) Local build on Linux (machine or VM)

python3 -m venv .venv
source .venv/bin/activate
python -m pip install -U pip
python -m pip install -e .
python -m pip install pyinstaller

pyinstaller --onefile --console --name spectre -m spectre.cli

Output:

dist/spectre

Test:

./dist/spectre --help

Include examples/:

pyinstaller --onefile --console --name spectre -m spectre.cli --add-data "examples:examples"

3) Local build on macOS (requires a Mac)

python3 -m venv .venv
source .venv/bin/activate
python -m pip install -U pip
python -m pip install -e .
python -m pip install pyinstaller

pyinstaller --onefile --console --name spectre -m spectre.cli

Output:

dist/spectre

Test:

./dist/spectre --help

Distribution

You can publish the generated binaries:

  • spectre.exe → Windows
  • spectre → Linux
  • spectre → macOS

For clarity when distributing, you may rename them to:

  • spectre-windows.exe
  • spectre-linux
  • spectre-macos

Users can then download the correct binary for their OS and run it directly.


Notes

  • macOS may show a security warning for unsigned binaries.
  • Cross-compilation is not recommended; build on each OS.
  • If PyInstaller has compatibility issues with the latest Python version, building with Python 3.13 is generally safe and reliable.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

logs_spectre-0.1.1.tar.gz (14.4 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

logs_spectre-0.1.1-py3-none-any.whl (3.3 kB view details)

Uploaded Python 3

File details

Details for the file logs_spectre-0.1.1.tar.gz.

File metadata

  • Download URL: logs_spectre-0.1.1.tar.gz
  • Upload date:
  • Size: 14.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.14.2

File hashes

Hashes for logs_spectre-0.1.1.tar.gz
Algorithm Hash digest
SHA256 ea5b463b121d2e5cc1122169db6f5435bcf06628e88a9d6253635582d0a1e72a
MD5 0a7c5c4b6036356743bfc504a15047a2
BLAKE2b-256 8a0344708a348f9d6a39e640e47d7c1f891678a358f33b84b3a33785462e477e

See more details on using hashes here.

File details

Details for the file logs_spectre-0.1.1-py3-none-any.whl.

File metadata

  • Download URL: logs_spectre-0.1.1-py3-none-any.whl
  • Upload date:
  • Size: 3.3 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.14.2

File hashes

Hashes for logs_spectre-0.1.1-py3-none-any.whl
Algorithm Hash digest
SHA256 45496fc4dbd357d5bd3abc67d9c142f6c5eb58c84a26fc96577b70fbca490bce
MD5 00d3c1a127179b1c6dd83373d719be3d
BLAKE2b-256 225c1b6722136b190ad37707cae41b4f7d774d143609d7289d9160262cf00f96

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page