Other languages metrics installation

Note: Metrics is in open alpha. Any team can turn it on — open Metrics and select Enable metrics in the onboarding view. Setup details, including the ingestion endpoint, may change before general availability.

PostHog's application metrics work with any OpenTelemetry-compatible client. If your app or infrastructure already exports OTLP metrics, point the exporter at PostHog. No PostHog packages required.

  1. Install OpenTelemetry packages

    Required

    The key requirements are:

    • Use OTLP (OpenTelemetry Protocol) for metrics export over HTTP
    • Send metrics to your Metrics endpoint (see configuration step below)
    • Include your project token in the Authorization header or as a ?token= query parameter

    Find the OpenTelemetry SDK for your language in the official registry. If you already run an OpenTelemetry Collector, you can add PostHog as an additional exporter without touching application code.

  2. Get your project token

    Required

    You'll need your PostHog project token to authenticate metrics requests. This is the same key you use for capturing events and exceptions with the PostHog SDK.

    Important: Use your project token which starts with phc_. Do not use a personal API key (which starts with phx_).

    You can find your project token in Project Settings.

  3. Configure the exporter

    Required

    Most OpenTelemetry SDKs pick up standard environment variables, so configuration is often no more than:

    Terminal
    OTEL_EXPORTER_OTLP_METRICS_ENDPOINT="https://us.i.posthog.com/i/v1/metrics"
    OTEL_EXPORTER_OTLP_METRICS_HEADERS="Authorization=Bearer <ph_project_token>"
    OTEL_SERVICE_NAME="my-app"

    Set OTEL_SERVICE_NAME so metrics from different systems stay easy to tell apart. It's attached to every series and used by the Metrics UI for filtering.

    If you configure the exporter in code instead:

    Endpoint:

    https://us.i.posthog.com/i/v1/metrics

    Authentication: Include your project token either as an Authorization header:

    Authorization: Bearer <ph_project_token>

    Or as a query parameter on the endpoint:

    https://us.i.posthog.com/i/v1/metrics?token=<ph_project_token>
  4. Send metrics without an SDK

    Optional

    The metrics endpoint is a standard OTLP/HTTP receiver, so you can also POST metrics to it directly, for example from a platform with no OpenTelemetry SDK.

    We recommend an SDK instead where you can. The OTLP payload is verbose, and an SDK handles aggregation, batching, retries, and flushing for you. If your app runs JavaScript, Node.js, or Python, use the posthog.metrics API in the PostHog SDK. For other languages, use an OpenTelemetry SDK.

    To send a request by hand, the body is an ExportMetricsServiceRequest in the OTLP JSON encoding. This example sends one counter data point:

    Terminal
    curl -X POST "https://us.i.posthog.com/i/v1/metrics" \
    -H "Authorization: Bearer <ph_project_token>" \
    -H "Content-Type: application/json" \
    -d '{
    "resourceMetrics": [{
    "resource": {
    "attributes": [
    { "key": "service.name", "value": { "stringValue": "my-app" } }
    ]
    },
    "scopeMetrics": [{
    "scope": { "name": "manual" },
    "metrics": [{
    "name": "orders.created",
    "unit": "1",
    "sum": {
    "aggregationTemporality": 1,
    "isMonotonic": true,
    "dataPoints": [{
    "timeUnixNano": "1767225600000000000",
    "asInt": "3",
    "attributes": [
    { "key": "plan", "value": { "stringValue": "pro" } }
    ]
    }]
    }
    }]
    }]
    }]
    }'

    Replace timeUnixNano with the current time in nanoseconds since the Unix epoch. The main rules of the JSON encoding are:

    • Field names are lowerCamelCase, for example timeUnixNano, not time_unix_nano.
    • Enums are integers. For aggregationTemporality, 1 is delta (the change since the last data point) and 2 is cumulative (the running total).
    • 64-bit integers, such as timeUnixNano and asInt, are strings. Use asDouble for decimal values.
    • Each metric has one data field: sum for counters, gauge for current values, or histogram for distributions. See the metrics data model for the fields of each type.

    A 200 response means that PostHog accepted the data. You can also send the binary protobuf encoding with Content-Type: application/x-protobuf, and compress either body with Content-Encoding: gzip.

  5. Test your setup

    Recommended
    1. Trigger the code path that records a metric
    2. Open Metrics in the PostHog sidebar and pick your metric from the name picker
    3. Data points should appear within a minute of sending

    If nothing shows up, check that the endpoint ends in /i/v1/metrics, that the token starts with phc_, and see the troubleshooting section.

    View your metrics in PostHog
  6. Next steps

    Checkpoint
    What you can do with your metrics

    ActionDescription
    Why you need metricsWhat metrics show you that events and logs don't
    Getting started guidePick the right metric type, add attributes carefully, and chart what matters
    Group and filterGroup by an attribute for one line per value, or filter with key=value chips
    How metrics worksHow metrics are ingested, stored, and queried
    Query with SQLEvery metric lands in the posthog.metrics table, queryable from the SQL tab

    Continue with the getting started guide

Still have questions?

Was this page useful?