Prometheus: 故障排除

您可能遇到的 Prometheus 错误

👋 欢迎来到 Stackhero 文档!

Stackhero 提供即用型 Prometheus 云 解决方案,具有多种优势,包括:

  • 包含 Alert Manager,可发送警报到 SlackMattermostPagerDuty 等。
  • 专用邮件服务器发送无限制邮件警报
  • Blackbox 用于探测 HTTPICMPTCP 等。
  • 使用在线配置文件编辑器进行轻松配置
  • 只需点击即可轻松更新
  • 专用私有 VM提供的最佳性能和强大安全性

节省时间简化生活:只需 5 分钟即可试用 Stackhero 的 Prometheus 云托管 解决方案!

随着 Prometheus v3 的发布,现在目标服务器必须在响应中包含 Content-Type 头,以告知 Prometheus 回复中包含的指标协议。这影响了像 Node Exporter 这样的工具,现在应该用适当的 Content-Type 来响应 Prometheus 的 HTTP 请求。有关更多详细信息,您可以查看 Prometheus 文档

如果不满足此要求,您可能会遇到如下错误消息:

received unsupported Content-Type "application/octet-stream" and no fallback_scrape_protocol specified for target

以下是解决此问题的一些方法:

如果您正在使用像 Node Exporter 这样的目标服务器,请考虑将其更新到最新版本。最近的更新通常可以确保 Content-Type 头被正确定义,这应该可以解决 Prometheus 中的错误。

对于自定义目标服务器,例如您开发的用于返回 Prometheus 指标的 API 路由,您可以直接在响应中设置 Content-Type 头。

例如,如果您使用 HapiJS,而不是像这样返回您的指标:

return metrics

您可以这样设置 Content-Type 头:

return h.response(metrics).type('text/plain;version=0.0.4');

以下是根据您返回的指标协议支持的 Content-Type 头:

  • PrometheusProto: application/vnd.google.protobuf;proto=io.prometheus.client.MetricFamily;encoding=delimited
  • PrometheusText 0.0.4: text/plain;version=0.0.4
  • PrometheusText 1.0.0: text/plain;version=1.0.0;escaping=allow-utf-8
  • OpenMetricsText 0.0.1: application/openmetrics-text;version=0.0.1
  • OpenMetricsText 1.0.0: application/openmetrics-text;version=1.0.0

您还可以在 prometheus.yml 配置文件中定义一个备用协议。如果目标服务器未指定 Content-Type 头,将使用此协议。

以下是一个示例:

  - job_name: "my-job"
    # [...]
    fallback_scrape_protocol: PrometheusText0.0.4

支持的值有 PrometheusProtoPrometheusText0.0.4PrometheusText1.0.0OpenMetricsText0.0.1OpenMetricsText1.0.0