|
| 1 | +# wide-event |
| 2 | + |
| 3 | +[](https://crates.io/crates/wide-event) |
| 4 | +[](https://docs.rs/wide-event) |
| 5 | +[](LICENSE-MIT) |
| 6 | + |
| 7 | +Honeycomb-style wide events for Rust. |
| 8 | + |
| 9 | +A wide event accumulates key-value pairs throughout a request (or task) |
| 10 | +lifecycle and emits them as a **single structured event** when the request |
| 11 | +completes. This gives you one row per request in your log aggregator with |
| 12 | +every dimension attached — perfect for high-cardinality exploratory analysis. |
| 13 | + |
| 14 | +## Quick start |
| 15 | + |
| 16 | +```rust |
| 17 | +use wide_event::{WideEventGuard, WideEventLayer}; |
| 18 | +use tracing_subscriber::prelude::*; |
| 19 | + |
| 20 | +// Once at startup: |
| 21 | +tracing_subscriber::registry() |
| 22 | + .with(WideEventLayer::stdout().with_system("myapp")) |
| 23 | + .init(); |
| 24 | + |
| 25 | +// Per request — guard auto-emits on drop: |
| 26 | +{ |
| 27 | + let req = WideEventGuard::new("http"); |
| 28 | + req.set_str("method", "GET"); |
| 29 | + req.set_str("path", "/api/users"); |
| 30 | + req.set_u64("status", 200); |
| 31 | +} // ← emitted here as single JSON line |
| 32 | +``` |
| 33 | + |
| 34 | +## How it works |
| 35 | + |
| 36 | +1. `WideEvent::new` starts a timer and creates an empty field map. |
| 37 | +2. Throughout processing, call setters (`set_str`, `set_u64`, `incr`, etc.) |
| 38 | + to accumulate fields — these are cheap local `Mutex` operations that never |
| 39 | + touch the tracing subscriber. |
| 40 | +3. `WideEvent::emit` (or `WideEventGuard` drop) finalizes the record, pushes |
| 41 | + it to a thread-local stack, and dispatches a structured `tracing::info!` |
| 42 | + event. The `WideEventLayer` pulls the record from the stack, formats the |
| 43 | + timestamp, and serializes in a single pass. |
| 44 | + |
| 45 | +## Features |
| 46 | + |
| 47 | +| Feature | Description | |
| 48 | +|---------|-------------| |
| 49 | +| `opentelemetry` | Attaches `trace_id` and `span_id` from the current OpenTelemetry span context | |
| 50 | +| `tokio` | Provides `context::scope` and `context::current` for async task-local wide event propagation | |
| 51 | + |
| 52 | +```toml |
| 53 | +[dependencies] |
| 54 | +wide-event = { version = "0.1", features = ["tokio"] } |
| 55 | +``` |
| 56 | + |
| 57 | +## Formatter options |
| 58 | + |
| 59 | +- **`JsonFormatter`** (default) — one JSON object per line |
| 60 | +- **`LogfmtFormatter`** — `key=value` pairs per line |
| 61 | + |
| 62 | +```rust |
| 63 | +use wide_event::{WideEventLayer, LogfmtFormatter}; |
| 64 | + |
| 65 | +let layer = WideEventLayer::new(std::io::stdout(), LogfmtFormatter); |
| 66 | +``` |
| 67 | + |
| 68 | +## Performance |
| 69 | + |
| 70 | +- Field keys are `&'static str` — zero allocation on every setter call. Field |
| 71 | + names are almost always string literals, so this is natural and avoids the |
| 72 | + `key.to_string()` overhead entirely. |
| 73 | +- Field setters (`set_str`, `set_u64`, `incr`, …) are cheap local `Mutex` |
| 74 | + operations — they never interact with the tracing subscriber. |
| 75 | +- Timestamp formatting reuses a thread-local buffer — no `String` allocation |
| 76 | + per emit. |
| 77 | +- Serialization happens once at emit time in a single pass over the accumulated |
| 78 | + fields. |
| 79 | +- A thread-local emit stack avoids cross-thread synchronization on the hot path. |
| 80 | + |
| 81 | +## Development |
| 82 | + |
| 83 | +```bash |
| 84 | +# Set up pre-push hook (runs fmt, clippy, tests before each push) |
| 85 | +git config core.hooksPath .githooks |
| 86 | + |
| 87 | +# Release a new version (bumps Cargo.toml, commits, tags, pushes) |
| 88 | +# CI publishes to crates.io automatically on tag push. |
| 89 | +cargo release patch # or: minor, major |
| 90 | +``` |
| 91 | + |
| 92 | +Requires [`cargo-release`](https://crates.io/crates/cargo-release): |
| 93 | +`cargo install cargo-release` |
| 94 | + |
| 95 | +## License |
| 96 | + |
| 97 | +MIT — see [LICENSE-MIT](LICENSE-MIT). |
0 commit comments