buswatch-sdk
Lightweight instrumentation SDK for emitting message bus metrics.
Add buswatch-sdk to your Rust application to emit metrics that can be consumed by the buswatch TUI or any compatible consumer.
Quick Start
[]
= "0.1"
use ;
use Duration;
async
Output Destinations
The SDK supports multiple output destinations:
File Output
Writes JSON snapshots to a file (useful for local development):
use Output;
let output = file;
TCP Output
Streams newline-delimited JSON to a TCP endpoint:
use Output;
let output = tcp;
Channel Output
Sends snapshots to a tokio channel (for in-process consumers):
use Output;
use mpsc;
let = channel;
let output = channel;
OpenTelemetry OTLP
Exports metrics via OpenTelemetry Protocol (requires otel feature):
use Output;
let output = otlp;
Recording Metrics
Basic Counting
// Record message reads/writes
handle.record_read;
handle.record_write;
// Record batches
handle.record_read;
Tracking Pending Duration
Use guards to automatically track how long operations take:
// Track how long a read operation is pending
let _guard = handle.start_read;
let message = consumer.receive.await; // blocking call
drop; // automatically records the pending duration
Setting Backlog
// Report the current backlog for a topic
handle.set_backlog;
Configuration
Emission Interval
Control how often snapshots are emitted:
use Duration;
let instrumentor = builder
.interval // emit every 5 seconds
.build;
Multiple Outputs
Send metrics to multiple destinations:
let instrumentor = builder
.output
.output
.build;
Features
| Feature | Description |
|---|---|
tokio |
Async runtime support (enabled by default) |
otel |
OpenTelemetry OTLP export |
OpenTelemetry Integration
Enable the otel feature for OTLP export:
[]
= { = "0.1", = ["otel"] }
use ;
let instrumentor = builder
.output
.build;
This allows buswatch metrics to flow into Grafana, Datadog, or any OTLP-compatible backend.
Thread Safety
The SDK is designed for concurrent use:
InstrumentorisSend + SyncModuleHandleisClone + Send + Sync- Metrics are collected using lock-free atomics where possible
let handle = instrumentor.register_module;
// Clone handles for use across threads
let handle2 = handle.clone;
spawn;
Performance
The SDK is designed to have minimal overhead:
- Lock-free atomic counters for counts
- Lazy snapshot collection (only when emitting)
- Configurable emission interval to control I/O frequency
- No allocations on the hot path (record_read/write)