Docker metrics installation
Contents
Note: Metrics is in alpha. Setup details, including the ingestion endpoint, may change before general availability.
If your services already expose Prometheus-format /metrics endpoints, the PostHog metrics agent scrapes them and forwards everything to PostHog. One docker run, no application changes.
Running Kubernetes? Use the Helm chart instead, which also discovers annotated pods automatically.
- 1
Get your project token
RequiredYou'll need your PostHog project token to authenticate metrics requests. This is the same token you use for capturing events with the PostHog SDK.
Important: Use your project token, which starts with
phc_. Do not use a personal API key (which starts withphx_).You can find your project token in Project Settings.
- 2
Run the agent
RequiredPoint the agent at one or more
host:porttargets that expose/metrics:TerminalThe agent scrapes each target every 15 seconds by default. Two common adjustments:
TerminalSet
SCRAPE_JOB_NAMEto control theservice_nameyour metrics arrive under. It defaults toposthog-metrics-agent. - 3
Verify metrics are flowing
Recommended- Check the agent started cleanly:
TerminalYou should see
Everything is ready. Begin running and processing data.Scrape or export errors appear in the same log.- Open Metrics in PostHog and pick a metric from the name picker. Data points should appear within a minute.
The agent also exposes its own health:
:13133answers health probes, and:8888/metricsserves the agent's own metrics, including scrape successes, queue depth, and points sent or dropped. - 4
Keep data through restarts
OptionalBy default, if PostHog is briefly unreachable the agent retries from memory, and a restart during that window drops whatever was buffered. To keep those samples, back the queue with disk:
TerminalSamples scraped during an outage then survive restarts and deliver when PostHog is reachable again.
- 5
Scale out with shards
OptionalOne agent scrapes every target itself, which is enough for most setups. For a large target set, run a fleet: set
SHARD_COUNTon every instance and give each one a distinctSHARD_INDEXfrom0toSHARD_COUNT - 1. Each instance then scrapes only the targets whose address hashes to its shard, so nothing is scraped twice and nothing is missed.Don't scale with plain copies of one agent: two unsharded agents scraping the same targets record every metric twice.
Next steps
CheckpointWhat you can do with your metricsAction Description Why you need metrics What metrics show you that events and logs don't Getting started guide Pick the right metric type, add attributes carefully, and chart what matters Group and filter Group by an attribute for one line per value, or filter with key=valuechipsHow metrics works How metrics are ingested, stored, and queried Query with SQL Every metric lands in the posthog.metricstable, queryable from the SQL tab
Configuration reference
| Variable | Default | Description |
|---|---|---|
POSTHOG_API_KEY | required | Project token (phc_...), sent as a bearer token |
POSTHOG_HOST | https://us.i.posthog.com | PostHog ingestion origin. Set to https://eu.i.posthog.com for EU Cloud |
SCRAPE_TARGETS | required | Comma-separated host:port list to scrape |
SCRAPE_INTERVAL | 15s | How often to scrape each target |
SCRAPE_METRICS_PATH | /metrics | Metrics path on the targets |
SCRAPE_JOB_NAME | posthog-metrics-agent | Becomes service_name on every metric |
SHARD_COUNT | 1 | Fleet size. Above 1, each instance scrapes only its share of the targets |
SHARD_INDEX | from hostname ordinal | This instance's shard, 0 to SHARD_COUNT - 1 |
PERSIST_QUEUE | off | Set to 1 to buffer undelivered batches on disk |
QUEUE_DIR | /var/lib/posthog-agent | Where the persistent queue is stored |
POSTHOG_DEBUG | off | Set to 1 to also log exported batches to stdout |
For custom Prometheus scrape_configs or a full OpenTelemetry Collector config, mount them into the container. The agent README documents both escape hatches.
Exemplars: link metrics to traces
If your Prometheus client attaches exemplars (trace and span IDs on counters and histograms), the agent preserves them, and the metrics viewer links those data points to the matching traces. The agent requests the OpenMetrics format automatically, because it's the only Prometheus format that carries exemplars. If your endpoint only serves classic Prometheus text, metrics still flow, only without trace links.