Building a RUM Pipeline: Sampling, Privacy Gates, and Dashboards
As Sherlock, I'll deduce and construct a complete, production-ready Real User Monitoring (RUM) pipeline from first principles. This pipeline collects browser performance metrics (e.g., Core Web Vitals: LCP, FID, CLS, plus custom metrics), applies sampling to control costs/volume, enforces privacy gates (GDPR/CCPA compliant: PII scrubbing, consent checks), stores data efficiently, and exposes dashboards for analysis.
Key Design Decisions
High-volume optimized: Uses ClickHouse for storage (OLAP, handles billions of rows cheaply).
Client-side: Lightweight JS snippet using navigator.sendBeacon for reliable delivery.
Sampling:
Client-side: 10% probabilistic (head sampling).
Server-side: Tail sampling (e.g., drop slow sessions >99th percentile if quota hit).
Privacy Gates:
IP anonymization (last octet zeroed).
User ID hashing (SHA-256).
PII regex scrubbing (emails, phones).
Consent flag required.
Tech Stack (Open-source, Dockerized):
Component
Tool
Client
Vanilla JS + Web Vitals
Ingestion
Fastify (Node.js)
Sampling/Privacy
Custom processors
Storage
ClickHouse
Dashboards
Grafana
Cost: Runs on a $10/mo VPS; scales to 1M sessions/day.
Deployment: Docker Compose (one-command setup).
Architecture (ASCII Diagram)
code
Browsers (Users)
|
| Beacon POST (JSON payloads)
v
Fastify Server (Ingestion)
├── Client Sampling (10%)
├── Privacy Gate (Scrub/Halt)
└── Server Sampling (Tail/Quota)
|
v
ClickHouse (Storage)
|
v
Grafana (Dashboards + Alerts)
Step 1: Client-Side Instrumentation
Embed this JS snippet in your <head> (or via GTM). It measures Core Web Vitals + custom metrics, samples 10% of sessions, checks consent, and sends via beacon.
{
"title": "RUM Overview",
"panels": [
{
"title": "LCP P95",
"type": "stat",
"targets": [{
"rawSql": "SELECT quantile(0.95)(metric_value) FROM rum_metrics WHERE metric_name='largest-contentful-paint' AND $timeFilter GROUP BY toStartOfDay(timestamp)"
}]
},
{
"title": "Error Rate",
"type": "timeseries",
"targets": [{
"rawSql": "SELECT timestamp, count() as errors FROM rum_metrics WHERE error IS NOT NULL AND $timeFilter GROUP BY toStartOfHour(timestamp)"
}]
},
{
"title": "Top Slow URLs",
"type": "table",
"targets": [{
"rawSql": "SELECT url, avg(metric_value) as avg_lcp FROM rum_metrics WHERE metric_name='largest-contentful-paint' AND $timeFilter GROUP BY url ORDER BY avg_lcp DESC LIMIT 10"
}]
}
],
"time": { "from": "now-24h", "to": "now" },
"refresh": "5m"
}