加载中

Prometheus 远程写入端点

除了通过 bulk API 摄取指标数据外,Elasticsearch 还提供了一个原生支持 Prometheus 远程写入协议 的端点。

该端点位于 /_prometheus/api/v1/write

注意

如果您正在使用 Elastic Cloud Serverless,请改用 托管输入 (managed inputs) 来发送 Prometheus 指标。托管输入是 Elastic Cloud 部署推荐的摄取路径,并提供持久化缓冲、统一身份验证和背压处理。

Prometheus 远程写入端点允许您将指标数据直接从 Prometheus 或任何兼容 Prometheus 远程写入的客户端发送到 Elasticsearch。数据会自动存储在 时间序列数据流 (TSDS) 中。

使用 Prometheus 远程写入端点摄取指标数据具有以下优势:

  • 无需中间管道或转换器,直接从 Prometheus 摄取。
  • 简化的索引映射:无需手动创建数据流、索引模板或定义维度和指标。指标是根据 Prometheus 命名约定动态映射的。
  • Prometheus 标签会自动映射为时间序列维度。
  • 指标类型根据命名约定进行推断:以 _sum_count_total_bucket 结尾的字段被映射为计数器(counter);所有其他字段被映射为计量器(gauge)。

要将数据从 Prometheus 发送到 Elasticsearch 远程写入端点,请在您的 prometheus.yml 中添加 remote_write 配置:

remote_write:
  - url: "https://<es_endpoint>/_prometheus/api/v1/write"
    authorization:
      type: ApiKey
      credentials: <api_key>
    # basic_auth:
    #   username: <user>
    #   password: <password>
		

要使用 Grafana Alloy 发送数据,请使用 prometheus.remote_write 组件

prometheus.remote_write "elasticsearch" {
  endpoint {
    url = "https://<es_endpoint>/_prometheus/api/v1/write"

    headers = {
      "Authorization" = "ApiKey <api_key>",
    }

    // basic_auth {
    //   username = "<user>"
    //   password = "<password>"
    // }
  }
}
		

该端点接受以下 POST 请求:

  • 内容类型:application/x-protobuf
  • 压缩:snappy(Prometheus 远程写入规范要求)或未压缩
  • 正文:Protocol Buffers 编码的 WriteRequest 消息,由 Prometheus 远程写入 1.0 规范 定义

默认情况下,指标被摄取到 metrics-generic.prometheus-default 数据流中。您可以使用 URL 路径参数或每个时间序列的标签来控制目标数据流。在这两种情况下,Elasticsearch 都会清理数据集(dataset)和命名空间(namespace)值,将任何非字母数字、连字符或下划线的字符替换为 _

通过 URL 路径段设置数据集和命名空间

端点 数据流
/_prometheus/api/v1/write metrics-generic.prometheus-default
/_prometheus/metrics/{dataset}/api/v1/write metrics-{dataset}.prometheus-default
/_prometheus/metrics/{dataset}/{namespace}/api/v1/write metrics-{dataset}.prometheus-{namespace}

例如,要将基础设施指标路由到专用的数据流,请将远程写入 URL 设置为

remote_write:
  - url: "https://<es_endpoint>/_prometheus/metrics/infrastructure/production/api/v1/write"
		

这会将数据发送到 metrics-infrastructure.prometheus-production 数据流。

您还可以通过为每个时间序列附加 data_stream_datasetdata_stream_namespace 标签,将各个时间序列路由到不同的数据流。当设置这些标签时,它们优先于 URL 路径,并允许单个远程写入端点将指标扇出到多个数据流。

Elasticsearch 将其视为控制字段,不会将其存储在文档的 labels 对象中。

如果时间序列上仅存在这两个标签中的一个,则另一个值会回退到 URL 路径段(如果没有提供路径段,则回退到默认值)。

使用 write_relabel_configs 在发送前添加路由标签

remote_write:
  - url: "https://<es_endpoint>/_prometheus/api/v1/write"
    write_relabel_configs:
      - target_label: data_stream_dataset
        replacement: myapp
      - target_label: data_stream_namespace
        replacement: production
		

此示例为该远程写入目标中的每个时间序列附加 data_stream_dataset=myappdata_stream_namespace=production,并将所有指标路由到 metrics-myapp.prometheus-production

传入的 Prometheus 时间序列映射如下:

Prometheus 概念 Elasticsearch 字段 描述
时间戳 @timestamp 样本时间戳(以毫秒为单位)
__name__ 标签 metrics.<metric_name> 指标值,存储为以指标名称命名的字段
data_stream_dataset 标签 (仅用于路由) 将时间序列路由到指定的数据集;不存储在 labels
data_stream_namespace 标签 (仅用于路由) 将时间序列路由到指定的命名空间;不存储在 labels
所有其他标签(包括 __name__ labels.<label_name> 映射为时间序列维度

指标类型通过动态模板从指标名称中自动推断:

  • 计数器 (Counter):以 _sum_count_total_bucket 结尾的指标名称被映射为 double,并带有 time_series_metric: counter
  • 计量器 (Gauge):所有其他指标名称都被映射为 double,并带有 time_series_metric: gauge

这意味着 Prometheus 直方图 (histograms) 和摘要 (summaries) 通过其组件指标(_sum_count_bucket)得到支持,每个组件都会自动接收正确的指标类型。

您可以通过创建带有附加动态模板的 metrics-prometheus@custom 组件模板来覆盖或扩展默认的指标类型推断。

例如,要将以 _counter 结尾的指标映射为计数器:

				PUT /_component_template/metrics-prometheus@custom
					{
  "template": {
    "mappings": {
      "dynamic_templates": [
        {
          "counter": {
            "path_match": ["metrics.*_counter"],
            "mapping": {
              "type": "double",
              "time_series_metric": "counter"
            }
          }
        }
      ]
    }
  }
}
		

自定义动态模板会与内置模板合并。内置的计数器模式(_sum_count_total_bucket)和默认的计量器回退规则将继续与您的自定义规则一起应用。

Elasticsearch 会自动安装一个匹配 metrics-*.prometheus-* 的内置索引模板,该模板配置了:

  • 仅支持 Prometheus 远程写入 1.0 协议。尚不支持远程写入 2.0。
  • 缺少 __name__ 标签的时间序列将被丢弃。
  • 具有非有限值(NaN、Infinity)的样本将被静默丢弃。
  • 陈旧性标记 (Staleness markers) 不受支持。
© . This website operates independently and is not affiliated with or endorsed by Elasticsearch B.V. All brand names, logos, and trademarks are the property of their respective owners.