PeekAPI — Rust SDK
Rust SDK for PeekAPI. Thread-based buffered client with feature-gated middleware for Actix Web, Axum, and Rocket.
Install
# Cargo.toml
[]
= "0.1"
# Enable framework middleware (pick one or more)
# peekapi = { version = "0.1", features = ["actix"] }
# peekapi = { version = "0.1", features = ["axum-middleware"] }
# peekapi = { version = "0.1", features = ["rocket-fairing"] }
Quick Start
Actix Web
use ;
use PeekApi;
use ;
use Arc;
async
Axum
use ;
use PeekApiLayer;
use ;
async
Rocket
use ;
use PeekApiFairing;
use ;
async
Standalone Client
use ;
let client = new.unwrap;
client.track;
// Graceful shutdown (flushes remaining events, persists to disk)
client.shutdown;
Configuration
| Field | Type | Default | Description |
|---|---|---|---|
api_key |
String |
required | Your PeekAPI key |
endpoint |
String |
PeekAPI cloud | Ingestion endpoint URL |
flush_interval |
Duration |
10s |
Time between automatic flushes |
batch_size |
usize |
100 |
Events per batch (triggers flush) |
max_buffer_size |
usize |
10,000 |
Max events held in memory |
max_storage_bytes |
u64 |
5MB |
Max disk fallback file size |
max_event_bytes |
usize |
64KB |
Per-event size limit |
storage_path |
Option<String> |
temp dir | JSONL fallback file path |
debug |
bool |
false |
Enable debug logging to stderr |
on_error |
Option<ErrorCallback> |
None |
Callback for background flush errors |
How It Works
- Middleware intercepts every request/response
- Captures method, path, status code, response time, request/response sizes, consumer ID
- Events are buffered in memory and flushed in batches on a background thread
- On network failure: exponential backoff with jitter, up to 5 retries
- After max retries: events are persisted to a JSONL file on disk
- On next startup: persisted events are recovered and re-sent
- On shutdown: remaining buffer is flushed or persisted to disk
Consumer Identification
By default, consumers are identified by:
X-API-Keyheader — stored as-isAuthorizationheader — hashed with SHA-256 (stored ashash_<hex>)
Override with the identify_consumer option to use any header:
let mut opts = new;
opts.identify_consumer = Some;
let client = new.unwrap;
The callback receives a header-getter closure (&dyn Fn(&str) -> Option<String>) and should return an Option<String>.
Features
- Minimal dependencies — serde, serde_json, ureq, sha2 (framework deps are feature-gated)
- Background thread — dedicated flush thread with configurable interval and batch size
- Disk persistence — undelivered events saved to JSONL, recovered on restart
- Exponential backoff — with jitter on network failures
- SSRF protection — private IP blocking, HTTPS enforcement (HTTP only for localhost)
- Input sanitization — path (2048), method (16), consumer_id (256) truncation
- Per-event size limit — strips metadata first, drops if still too large (default 64KB)
- Feature-gated middleware — only compile the framework adapter you need
Feature Flags
| Feature | Framework | Adds |
|---|---|---|
actix |
Actix Web 4 | actix-web, actix-service |
axum-middleware |
Axum 0.8 | axum, tower, tower-layer, http, pin-project-lite |
rocket-fairing |
Rocket 0.5 | rocket |
Requirements
- Rust 2021 edition
Contributing
- Fork & clone the repo
- Run tests —
cargo test - Lint —
cargo clippy --all-features - Format —
cargo fmt - Submit a PR
License
MIT