---
title: "Log facets"
description: "The log facets endpoints of the HTTP API."
---

> Documentation Index
> Fetch the complete documentation index at: https://slashobserve.com/llms.txt
> Use this file to discover all available pages before exploring further.

# Log facets

## POST /datasources/{uid}/log-facets

Count the top values of each label, field or column a logs query matches over a range.

Each field answers with its most frequent values and their counts, exact where the datasource can count and from a sample of the newest lines where it cannot; every field says which with `exact`. On Loki the index labels of the matched streams (`kind` `label`) are counted exactly from the index volume API, whose counts are bytes, so those fields carry `unit` `bytes`; parsed fields (`kind` `field`) and structured-metadata fields (`kind` `metadata`, a Loki field with no parser) are counted from the newest `sample` lines of the full query, and the response says so with `sampled` and `lines_sampled`. An index label whose count does not finish within the time limit is counted from the same sample instead, with `exact` false and no `unit`, so it too counts the newest `lines_sampled` lines; the response still answers in time. The Loki query must be a log query that starts with a stream selector; the selector before the first `|` picks the streams. On ClickHouse every field (`kind` `column`) is counted exactly with one `GROUP BY` per field over the query with its top-level `ORDER BY` and `LIMIT` dropped; the query must be one SELECT without UNION, INTERSECT, EXCEPT or LIMIT BY. Without `fields`, Loki counts every index label of the matched streams plus every detected field, and ClickHouse every result column except `Timestamp`, `Body`, `TraceId` and `SpanId`, plus the top 20 keys of `LogAttributes` as `LogAttributes.<key>` when it is a result column. Fields come exact first, then by cardinality ascending, then by name; values by count descending, then by value. The query runs with the agent tools' limits, nothing is cached, and the call counts against the caller's 8 in-flight query requests. Viewers and above may call it, service-account keys included. `from` and `to` take relative expressions such as `now-1h`, epoch milliseconds or RFC 3339 times.

### Parameters

| Name | In | Type | Description |
| --- | --- | --- | --- |
| `uid` (required) | path | `string` |   |

### Request body

`application/json`, required.

| Field | Type | Description |
| --- | --- | --- |
| `query` (required) | `string` | The logs query the page runs. |
| `from` (required) | `string` |   |
| `to` (required) | `string` |   |
| `timezone` | `string` | The browser's IANA time zone, used to resolve relative times such as `now/d`. |
| `fields` | array of `string` | Count only these fields; on ClickHouse a name that is neither a result column nor `LogAttributes.<key>` answers `query_invalid`. At most 50 items. |
| `limit` | `integer` | Values per field. Between 1 and 50. Default `10`. |
| `sample` | `integer` | The newest lines to sample for fields the datasource cannot count exactly. Between 100 and 5000. Default `1000`. |

### Responses

| Status | Description | Body |
| --- | --- | --- |
| `200` | The top values of each field. | [`LogFacets`](/docs/reference/api/schemas/#logfacets) |
| `400` | The request is malformed or fails validation, `fields`, `limit` or `sample` is out of range, or the datasource is not Loki or ClickHouse (`invalid_request`), or the query is longer than 64 KiB (`query_too_long`). | [`PanelDataError`](/docs/reference/api/schemas/#paneldataerror) |
| `401` | Authentication is missing or invalid. | [`Error`](/docs/reference/api/schemas/#error) |
| `404` | The datasource does not exist (`not_found`). | [`PanelDataError`](/docs/reference/api/schemas/#paneldataerror) |
| `413` | The request body exceeds the size limit. | [`Error`](/docs/reference/api/schemas/#error) |
| `422` | The query cannot run: `query_invalid` for a Loki metric query, a ClickHouse query with a top-level UNION, INTERSECT, EXCEPT or LIMIT BY, a requested field that is not a column of the query, or an upstream rejection; `query_forbidden`, `limit_exceeded` or `unknown_macro` as for the explore query; the body carries no `ref`. | [`PanelDataError`](/docs/reference/api/schemas/#paneldataerror) |
| `429` | The caller already has the maximum number of query requests in flight, or the server's shared upstream call slots stayed busy for 5 seconds. | [`Error`](/docs/reference/api/schemas/#error) |
| `500` | The server failed to handle the request, including a datasource call that failed without a classified cause. | [`PanelDataError`](/docs/reference/api/schemas/#paneldataerror) |
| `502` | The datasource is unavailable. | [`PanelDataError`](/docs/reference/api/schemas/#paneldataerror) |
| `504` | The datasource did not answer within the 15 second query timeout, or the request did not finish within 20 seconds. | [`PanelDataError`](/docs/reference/api/schemas/#paneldataerror) |

Generated from [`api/openapi.yaml`](https://github.com/slashobserve/slashobserve/blob/c770e5e0/api/openapi.yaml) at `c770e5e0`. [Report a problem](https://github.com/slashobserve/slashobserve/issues/new).
