通过OTLP Export直接写入Prometheus Remote Write的实验性标志开启

解读

国内云原生面试中,Prometheus 的 Remote Write 早已是“标配”话题,但把 OTLP(OpenTelemetry Protocol)直接对接到 Remote Write 仍属“前沿”。
出题人想验证三件事:

  1. 你是否知道 Prometheus 目前只认 HTTP/protobuf 的 Remote Write 格式,而 OTLP 是另一套基于 gRPC/HTTP 的协议;
  2. 你是否清楚官方给出的“桥接”方案仍带 --enable-feature=otlp-write-receiver 实验开关;
  3. 你是否能在生产红线(实验功能不可直接上线)与快速验证(POC、可观测性统一)之间给出权衡。
    答不到“实验标志”四个字,基本会被判定为“只背过 Remote Write 概念,没动手”。

知识点

  1. Prometheus v2.47+ 才内置 OTLP 接收器,代码路径 storage/remote/otlptranslator。
  2. 实验标志位:--enable-feature=otlp-write-receiver,启动时一次注入,不支持热加载。
  3. 端口复用:同一 --web.listen-address 下新增 /api/v1/otlp/v1/metrics 路由,Content-Type 必须为 application/x-protobuf,且需携带 x-prometheus-remote-write-version: 0.1.0 头。
  4. 翻译规则:OTLP 的 Metric 类型→Prometheus 的 Counter/Gauge/Histogram/Summary;属性→标签;资源属性加 __resource_ 前缀,避免与目标标签冲突。
  5. 限制:
    • 实验阶段无向后兼容保证,小版本升级可能改路由或删字段;
    • 不支持 Exemplar 中的 TraceID 透传;
    • 单条消息默认最大 50 MB,受 --max-otlp-request-size 控制;
    • 高并发场景下无独立队列,直接占用 Remote Write 的 remote_write.queue_config,可能反压。
  6. 国内合规:若客户环境要求“两地三中心”且需通过等保 3 级测评,实验功能需走变更评审,通常被否决,需改用 OpenTelemetry Collector 中转。

答案

步骤如下,全部在 2.47+ 版本验证通过:

  1. 下载官方二进制:tar -xzf prometheus-2.47.2.linux-amd64.tar.gz
  2. 启动时追加实验标志:
    ./prometheus \
      --config.file=prometheus.yml \
      --enable-feature=otlp-write-receiver \
      --web.listen-address=0.0.0.0:9090
    
  3. 验证路由:curl -X POST http://localhost:9090/api/v1/otlp/v1/metrics -H "Content-Type: application/x-protobuf" -H "x-prometheus-remote-write-version: 0.1.0" --data-binary @metric.pb 返回 200 即成功。
  4. 使用 OpenTelemetry Collector 的 otlphttp exporter 指向上述地址,即可把 Java/Go/Python 探针数据直写 Prometheus,无需 prometheusremotewrite exporter 二次转换。
  5. 关闭方式:去掉启动参数并重启,Prometheus 会自动卸载 OTLP 路由,无残留。

生产建议:

  • 当前阶段仅用于开发测试,上线前必须在 Collector 里做协议转换,使用稳定 Remote Write 接口;
  • 在启动脚本或 systemd unit 中显式注释“实验功能”,方便后续审计;
  • 监控自身稳定性:通过 prometheus_http_requests_total{handler="/api/v1/otlp/v1/metrics"} 观察 5xx 比例,超过 1% 立即回退。

拓展思考

  1. 如果未来 OTLP 接收器毕业为稳定功能,Prometheus 官方会把标志降级为配置项还是保持零配置?
    预测:会改为 otlp_write_receiver: true 的 prometheus.yml 开关,避免重启,同时支持热加载,但默认仍关闭,防止误开。
  2. 国内大量厂商已把 Remote Write 接口放在 SLB 后做统一鉴权,OTLP 路由一旦转正,SLB 需要同时支持 gRPC 与 HTTP/2 透传,这对传统七层负载均衡是新的挑战。
  3. 多租户场景:OTLP 的 Resource Metrics 天然带 service.namespace 字段,可与 Prometheus 的 __tenant_id__ 标签做映射,实现软隔离;但实验版本尚未接入 remote_write.headers 的动态注入,需要二次开发。