PostHog Observability
Overview
Monitor PostHog integration health with four key signals: event ingestion rate (are events flowing?), feature flag evaluation latency (are flags fast enough for hot paths?), event volume by type (detect instrumentation regressions), and API rate limit consumption (are we approaching 429s?).
Prerequisites
- PostHog project with personal API key (
phx_...)
- Application instrumented with PostHog SDK
- Prometheus/Grafana or equivalent monitoring stack (optional)
Instructions
Step 1: Event Ingestion Health Check
set -euo pipefail
# Check if events are flowing (last 24 hours)
curl "https://app.posthog.com/api/projects/$POSTHOG_PROJECT_ID/query/" \
-H "Authorization: Bearer $POSTHOG_PERSONAL_API_KEY" \
-H "Content-Type: application/json" \
-d '{
"query": {
"kind": "HogQLQuery",
"query": "SELECT toStartOfHour(timestamp) AS hour, count() AS events FROM events WHERE timestamp > now() - interval 24 hour GROUP BY hour ORDER BY hour"
}
}' | jq '.results | map({hour: .[0], events: .[1]}) | .[-3:]'
Step 2: Instrument Flag Evaluation Latency
// posthog-instrumented.ts
import { PostHog } from 'posthog-node';
const posthog = new PostHog(process.env.NEXT_PUBLIC_POSTHOG_KEY!, {
host: 'https://us.i.posthog.com',
personalApiKey: process.env.POSTHOG_PERSONAL_API_KEY,
});
// Wrap flag evaluation with timing
async function getFlag(flagKey: string, userId: string): Promise<any> {
const start = performance.now();
const value = await posthog.getFeatureFlag(flagKey, userId);
const durationMs = performance.now() - start;
// Emit metrics to your monitoring system
emitHistogram('posthog_flag_eval_duration_ms', durationMs, { flag: flagKey });
emitCounter('posthog_flag_evals_total', 1, { flag: flagKey, result: String(value) });
// Alert on slow evaluations (likely means local eval not configured)
if (durationMs > 200) {
console.warn(`[PostHog] Slow flag eval: ${flagKey} took ${durationMs.toFixed(0)}ms — check personalApiKey`);
}
return value;
}
// Example: emit to Prometheus via prom-client
import { Histogram, Counter, Gauge } from 'prom-client';
const flagDuration = new Histogram({
name: 'posthog_flag_eval_duration_ms',
help: 'PostHog feature flag evaluation duration',
labelNames: ['flag'],
buckets: [1, 5, 10, 50, 100, 200, 500, 1000],
});
const flagEvals = new Counter({
name: 'posthog_flag_evals_total',
help: 'Total PostHog feature flag evaluations',
labelNames: ['flag', 'result'],
});
function emitHistogram(name: string, value: number, labels: Record<string, string>) {
flagDuration.observe(labels, value);
}
function emitCounter(name: string, value: number, labels: Record<string, string>) {
flagEvals.inc(labels, value);
}