Skip to content

:hedgehog: PostHog is the leading platform for building self-driving products. Our developer tools – AI observability, analytics, session replay, flags, experiments, error tracking, logs, and more – capture all the context agents need to diagnose problems, uncover opportunities, and ship fixes. Steer it all from Slack, web, desktop, or the MCP.

2.6 KB
Raw Blame

Return a scalar count of trace spans matching a filter set. Use this as a cheap pre-flight before query-apm-spans — if the count is large, narrow the filters (or set excludeAttributes) before pulling rows.

All parameters go inside query — top-level fields are rejected:

{ "query": { "serviceNames": ["api"], "dateRange": { "date_from": "-1h" } } }

When to use

  • Before query-apm-spans, to confirm the filter set returns a tractable number of spans rather than pulling rows blind.
  • When the user asks "how many X spans are there?" and you don't need to see individual spans.
  • To check whether a filter combination matches anything at all before committing to a full query.

count is the number of matching spans. traceCount is the number of distinct traces whose root span matches. A trace contains many spans, so count is usually larger.

If the count would scan too much data (a wide date range with no filters), the tool returns a 400 asking you to narrow the window or add filters — narrow the dateRange or add serviceNames / statusCodes / filterGroup, then retry.

Parameters

query.dateRange

Date range for the count. Defaults to the last hour (-1h).

  • date_from: Start of the range. Accepts ISO 8601 timestamps or relative formats: -1h, -6h, -1d, -7d, -30d.
  • date_to: End of the range. Same format. Omit or null for "now".

query.serviceNames

Filter by service names. Unlike query-apm-spans, an unfiltered count is cheap and useful for sizing up a filter before narrowing. Use apm-services-list to discover services.

query.statusCodes

Filter by OTel span status codes (list of integers: 0 Unset, 1 OK, 2 Error) — not HTTP status codes. Use [2] to select error spans.

query.filterGroup

Property filters to narrow the count. Same format as query-apm-spans filters — each filter specifies key, operator, type (span/span_attribute/span_resource_attribute), and optionally value.

A span filter on duration takes a value in milliseconds. For example, { "key": "duration", "operator": "gt", "type": "span", "value": "30000" } counts spans longer than 30 seconds.

Examples

Count error spans in a service over the last day

{
  "query": {
    "serviceNames": ["api-gateway"],
    "statusCodes": [2],
    "dateRange": { "date_from": "-1d" }
  }
}

Count how many spans match a name before fetching them

{
  "query": {
    "filterGroup": [{ "key": "name", "operator": "exact", "type": "span", "value": "redis_cluster.discovery" }],
    "dateRange": { "date_from": "-6h" }
  }
}