trojan-analytics
Connection event collection and ClickHouse export for trojan-rs.
Overview
This crate provides detailed per-connection event tracking for traffic analysis, billing, and auditing:
- Event collection — Non-blocking, lock-free event recording with configurable buffer size
- ClickHouse export — Batched writes to ClickHouse for scalable analytics
- Sampling — Configurable sampling rate for high-traffic deployments
- Privacy controls — Options to mask IPs, truncate user IDs, and control SNI recording
Architecture
Connection Handler
│
▼
EventCollector ──▶ Ring Buffer ──▶ Batch Writer ──▶ ClickHouse
│ │
└─ sampling check fallback file
Events are recorded non-blocking via a bounded channel. A background task flushes events in batches to ClickHouse. Failed writes fall back to a local file.
Usage
use ;
// Initialize from config
let collector = init.await?;
// Record a connection event (builder pattern)
let event = collector.connection
.user
.target
.protocol
.transport;
// Event is automatically sent when the builder is dropped
Configuration
[]
= true
= "node-1"
[]
= "http://localhost:8123"
= "trojan"
[]
= 0.1 # 10% sampling
[]
= false
= false
License
GPL-3.0-only