Prometheus: 疑難排解
您可能在 Prometheus 中遇到的錯誤
👋 歡迎來到 Stackhero 文件!
Stackhero 提供一個即用型的 Prometheus cloud 解決方案,帶來多項好處,包括:
- 包含
Alert Manager,可發送警報至Slack、Mattermost、PagerDuty等。- 專用電郵伺服器發送無限電郵警報。
Blackbox用於探測HTTP、ICMP、TCP等。- 使用線上配置文件編輯器進行簡單配置。
- 只需點擊即可輕鬆更新。
- 由專用私有 VM提供的最佳性能和強大安全性。
節省時間並簡化您的生活:只需5 分鐘即可嘗試 Stackhero 的 Prometheus cloud hosting 解決方案!
解決錯誤 "received unsupported Content-Type "..." and no fallback_scrape_protocol specified for target"
隨著 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
以下是解決此問題的一些方法:
1. 更新您的目標伺服器
如果您正在使用像 Node Exporter 這樣的目標伺服器,請考慮將其更新到最新版本。最近的更新通常能確保 Content-Type 標頭正確定義,這應該可以解決 Prometheus 中的錯誤。
2. 為您的目標伺服器定義 Content-Type 標頭
對於自訂的目標伺服器,例如您開發的 API 路徑以返回 Prometheus 指標,您可以直接在回應中設置 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
3. 定義一個後備協議
您也可以在 prometheus.yml 配置文件中定義一個後備協議。如果目標伺服器未指定 Content-Type 標頭,將使用此協議。
以下是一個例子:
- job_name: "my-job"
# [...]
fallback_scrape_protocol: PrometheusText0.0.4
支持的值有 PrometheusProto、PrometheusText0.0.4、PrometheusText1.0.0、OpenMetricsText0.0.1 和 OpenMetricsText1.0.0。