Developer guide
TSDB for Prometheus provides a built-in interface for the Prometheus remote storage adapter. The interface is compatible with Prometheus queries, but differs from standard PromQL in two areas: regular expression support for label operators and character set constraints that affect cross-API data compatibility.
PromQL
Prometheus Query Language (PromQL) is a functional query language that lets you select and aggregate time series data in real time. For full PromQL documentation, see the PromQL documentation on the Prometheus website.
Label operators
TSDB for Prometheus supports four label operators: =, !=, =~, and !~.
The =~ and !~ operators use regular expressions for value matching, but with limited regex syntax compared to standard PromQL. The following table shows what's supported.
| Expression | =~ | !~ |
|---|---|---|
mode=~"user|sys" — vertical bar alternation | Supported | Supported |
mode=~"use*" — asterisk wildcard | Supported | Not supported |
mode=~"user.+" — dot-plus and other RE2 syntax | Not supported | Not supported |
More regex features are under development and will be available in future versions.
Examples
node__cpu__seconds__total{cpu="1",mode="user"}
node__cpu__seconds__total{cpu!="1",mode="user"}
node__cpu__seconds__total{cpu=~"1",mode="user"}
node__cpu__seconds__total{cpu=~"1|2|3",mode="user"}
node__cpu__seconds__total{cpu!~"1",mode="user"}
node__cpu__seconds__total{cpu!~"1|2|3",mode="user"}Aggregation operators
TSDB for Prometheus supports the following aggregation operators: min, max, avg, stddev, stdvar, count, topk, bottomk, quantile, and count_values.
min(node__cpu__seconds__total)
max(node__cpu__seconds__total)
avg(node__cpu__seconds__total)
stddev(node__cpu__seconds__total)
stdvar(node__cpu__seconds__total)
count(node__cpu__seconds__total)
topk(1, node__cpu__seconds__total)
bottomk(1, node__cpu__seconds__total)
quantile(1, node__cpu__seconds__total)
count_values("count", node__cpu__seconds__total)Character compatibility
Prometheus and Time Series Database (TSDB) support different character sets for metrics, tag keys, and tag values. This matters when you write data through one interface and read it through the other.
| Field | Prometheus | TSDB |
|---|---|---|
| metric | [a-zA-Z_:][a-zA-Z0-9_:]* | Letters, digits, and -_./():,[]='# |
| tagKey | [a-zA-Z_][a-zA-Z0-9_]*; names starting with _ are reserved | Letters, digits, and -_./():,[]='# |
| tagValue | Unicode characters | Letters, digits, and -_./():,[]='# |
Usage notes
Writing with Prometheus, querying with the native TSDB API
The TSDB native API may return garbled text in tag values when the source data was written by Prometheus. Tag values written by Prometheus can contain Unicode characters that TSDB encodes using a number sign (#) as an internal identifier. To avoid garbled output, query Prometheus-written data through Prometheus rather than the native TSDB API.
If you must use the native TSDB API to read Prometheus-written data, verify that all tag values contain only characters supported by TSDB before querying.
Writing with the native TSDB API, querying with Prometheus
Verify that all metrics and tag keys written through the native TSDB API contain only characters supported by Prometheus before querying through Prometheus.
Avoid number signs (`#`) in Prometheus-written tag values
TSDB uses # internally as an encoding identifier. Specifying # in a tag value written via Prometheus can interfere with encoding and decoding.