查询 API

写入 API 一样,集群版本和单机版的查询API主要区别是查询功能是由独立组件 vmselect 完成的,并支持多租户。 集群版的 URL 格式为http://<vmselect>:8481/select/<accountID>/prometheus/<suffix>, 其中:

  • <accountID>是一个任意int32数字,用来标识查询的空间(即租户),详见这里
  • <suffix>见下面。

导出数据

/api/v1/export

导出 JSON line 格式的原始样本,更多信息看这篇文章

导出的数据可以使用另外一个接口/api/v1/import导入到 VictoriaMetrics。

curl http://localhost:8428/api/v1/export -d 'match[]=vm_http_request_errors_total' > filename.json
curl http://<vmselect>:8481/select/0/prometheus/api/v1/export -d 'match[]=vm_http_request_errors_total' > filename.json
curl http://localhost:8428/api/v1/export -d 'match[]=<timeseries_selector_for_export>' -d 'start=1654543486' -d 'end=1654543486'
curl -H 'Accept-Encoding: gzip' http://localhost:8428/api/v1/export -d 'match[]=<timeseries_selector_for_export>' -d 'start=2022-06-06T19:25:48' -d 'end=2022-06-06T19:29:07' data.jsonl.gz

/api/v1/export/native

导出原生二进制格式的原始数据,该数据可以通过另一个接口api/v1/import/native导入到 VictoriaMetrics。

curl http://localhost:8428/api/v1/export/native -d 'match[]=vm_http_request_errors_total' > filename.bin
curl http://<vmselect>:8481/select/0/prometheus/api/v1/export/native -d 'match[]=vm_http_request_errors_total' > filename.bin

在大型数据库上,您可能会遇到导出时间序列数量限制的问题。在这种情况下,您需要调整-search.maxExportSeries启动参数:

# count unique time series in database
wget -O- -q 'http://your_victoriametrics_instance:8428/api/v1/series/count' | jq '.data[0]'

# relaunch victoriametrics with search.maxExportSeries more than value from previous command

同上面/api/v1/export接口一样,可使用startend请求参数,来限制导出数据的时间范围。

native 格式导出的数据不会应用去重操作。预期是在数据导入过程中进行去重处理。

/api/v1/export/csv

导出 CSV 格式原始数据。它可以使用另外一个接口api/v1/import/csv导入到 VictoriaMetrics。

curl http://localhost:8428/api/v1/export/csv -d 'format=__name__,__value__,__timestamp__:unix_s' -d 'match[]=vm_http_request_errors_total' > filename.csv
curl http://<vmselect>:8481/select/0/prometheus/api/v1/export/csv -d 'format=__name__,__value__,__timestamp__:unix_s' -d 'match[]=vm_http_request_errors_total' > filename.csv

CSV 格式导出的数据会被去重。使用reduce-memory-usage=1请求参数关闭去重。

Prometheus

VictoriaMetrics 支持下面这些 Prometheus 查询 API:

/api/v1/query

curl http://localhost:8428/prometheus/api/v1/query -d 'query=vm_http_request_errors_total'
curl http://<vmselect>:8481/select/0/prometheus/api/v1/query -d 'query=vm_http_request_errors_total'

/api/v1/query_range

curl http://localhost:8428/prometheus/api/v1/query_range -d 'query=sum(increase(vm_http_request_errors_total{job="foo"}[5m]))' -d 'start=-1d' -d 'step=1h'
curl http://<vmselect>:8481/select/0/prometheus/api/v1/query_range -d 'query=sum(increase(vm_http_request_errors_total{job="foo"}[5m]))' -d 'start=-1d' -d 'step=1h'

/api/v1/series

curl http://localhost:8428/prometheus/api/v1/series -d 'match[]=vm_http_request_errors_total'
curl http://<vmselect>:8481/select/0/prometheus/api/v1/series -d 'match[]=vm_http_request_errors_total'

/api/v1/labels

curl http://localhost:8428/prometheus/api/v1/labels
curl http://<vmselect>:8481/select/0/prometheus/api/v1/labels

/api/v1/label/{name}/values

curl http://localhost:8428/prometheus/api/v1/label/job/values
curl http://<vmselect>:8481/select/0/prometheus/api/v1/label/job/values

DB 状态

/api/v1/status/tsdb

curl http://localhost:8428/prometheus/api/v1/status/tsdb
curl http://<vmselect>:8481/select/0/prometheus/api/v1/status/tsdb

VictoriaMetrics 在/api/v1/status/tsdb接口上的信息统计方式类似于 Prometheus,具体参见这些 Prometheus 文档

该接口支持以下参数,所有参数均可选:

  • topN=N:表示返回统计值最大的N条数据,默认N=10,即返回top10的统计信息数据。
  • date=YYYY-MM-DD:这里YYYY-MM-DD表示用于统计哪天的数据。默认统计当天的数据,使用date=1970-01-01表示统计全局的数据。
  • focusLabel=LABEL_NAME:在返回数据中的seriesCountByFocusLabelValue字段中,计算给定LABEL_NAME中包含 timeseries 最多的 Label Value 集合。
  • match[]=SELECTOR:这里SELECTOR用来限定统计目标,只有匹配了该过滤器的 timeseries 才会被统计,默认统计所有的 timeseries。
  • extra_label=LABEL=VALUE:使用LABEL=VALUE过滤出要统计的目标 timeseries;该参数用于简单过滤场景,计算速度要比match[]参数快。

集群版中,每个vmstorage独立存储的时间序列。vmselect通过/api/v1/status/tsdbAPI 从每个vmstorage节点请求统计信息,并通过对每个时间序列的统计信息求和来合并结果。 当同一时间序列的样本数据由于多副本重路由机制分布在多个vmstorage节点上时,可能会导致统计结果变大。 比如你的集群是3副本,统计值大概率是写入数据量的3倍。

/api/v1/series/count

返回 series 的总数。

curl http://localhost:8428/prometheus/api/v1/series/count
curl http://<vmselect>:8481/select/0/prometheus/api/v1/series/count

/api/v1/status/active_queries

返回当前活跃的查询请求。

curl http://localhost:8428/prometheus/api/v1/status/active_queries

每个vmselect实例都有独立的活跃查询列表,如果你要获取整个集群的,就要遍历所有的vmselect实例。

curl http://<vmselect>:8481/select/0/prometheus/api/v1/status/active_queries

/api/v1/status/top_queries

返回执行频率最高以及查询耗时最长的查询列表。

curl http://localhost:8428/prometheus/api/v1/status/top_queries
curl http://<vmselect>:8481/select/0/prometheus/api/v1/status/top_queries

/admin/tenants

列出在给定时间范围内已写入数据的租户,仅集群版支持该 API 。

http://<vmselect>:8481/admin/tenants?start=...&end=...`

startend参数是可选的。默认返回 VictoriaMetrics 集群中至少包含一条数据的租户列表。

Graphite

VictoriaMetrics 支持 Graphite 协议的数据写入,详见这些文档。也支持以下 Graphite 查询 API,这些 API 对于平替掉 Grafana 中的 Graphite数据源 是必需的:

所有 Graphite API 都可以使用/graphite前缀。例如,/graphite/metrics/find/metrics/find都应该有效。

VictoriaMetrics 支持__graphite__伪 Label,用处是在 MetricsQL 中使用与 Graphite 兼容的过滤器。详见这些文档

Render API

VictoriaMetrics在/render接口上只做了部分支持,Grafana 中的 Graphite 数据源会使用这个接口。 在 Grafana中 配置 Graphite 数据源时,必须将Storage-StepHTTP 请求头设置成 VictoriaMetrics 中存储的 Graphite 数据样本粒度。 例如,Storage-Step: 10s表示 VictoriaMetrics 中存储的 Graphite 数据点之间相隔是 10 秒。

Metrics API

/metrics/find

搜索 Graphite metrics.

/metrics/expand

扩展 Graphite metrics

/metrics/index.json

返回所有的指标名称.

VictoriaMetrics /metrics/find/metrics/expand接口上额外支持了以下参数:

  • label- 用于选择任意 Label Name。默认情况下,label=__name__,即选择 Metric 名称。
  • delimiter- 用于在 Metric 名称层次结构中使用不同的分隔符。例如,/metrics/find?delimiter=_&query=node*将返回所有名称以node_开头的 Metric 。默认情况下,delimiter=.

Tags API

/tags/tagSeries

注册 time series。

/tags/tagMultiSeries

批量注册 time series.

/tags

返回 tag 名称列表.

/tags/{tag_name}

返回指定<tag_name>的值列表.

/tags/findSeries

返回匹配expr的 series.

/tags/autoComplete/tags

返回匹配tagPrefix和/或expr的tag名称列表。

/tags/autoComplete/values

返回匹配valuePrefix和/或exprtag值列表。

/tags/delSeries

删除成功匹配参数path的 timeseries.