開發與維運

開放告警接入Prometheus

背景介紹

Prometheus是常用的開源監控告警系統,為開發運維人員提供了數據採集、查看、告警的一體化解決方案。Prometheus的告警消息會發送到Alertmanager服務進行通知管理,並定義了同Alertmanager的交互協議。使用Prometheus時,用戶需要額外維護Alertmanager服務,並且Alertmanage在界面操作、功能上較為簡單,並不能很好覆蓋用戶的大部分使用場景。SLS的告警功能很好的補足了這這方面的不足之處,並推出了開放告警功能,將Prometheus告警消息通過Alertmanager協議收集到SLS中進行管理。

Prometheus接入SLS

要將Prometheus的告警消息接入SLS,主要分為兩個步驟:在SLS中創建開放告警應用;將SLS開放告警作為alertmanager接入Prometheus。創建開放告警應用的具體步驟,可以參考文章SLS開放告警簡介。下面介紹下如何將Prometheus的告警消息接入到SLS中。

獲取回調地址

在創建開放告警應用之後,通過點擊接口按鈕,打開如下圖所示的回調地址查看窗口。

image

回調地址由兩部分構成:域名部分和子路徑部分。其中域名部分屬於SLS的接入地址,和地域相關,每個地域都有各自不同的接入地址;子路徑部分包括用於發送消息的Access Key Id和開放告警應用。如下所示為一個完整的SLS回調地址:

cn-heyuan-intranet.log.aliyuncs.com/event/webhook/RAMAK_{ACCESS_KEY_ID}/a123_asdad

其中"cn-heyuan-intranet.log.aliyuncs.com"為域名部分,屬於SLS通用的接入地址(endpoint);event/webhook/RAMAK_{ACCESS_KEY_ID}/a123_asdad 則為子路徑部分。需要注意的是,用戶需要將子路徑部分中的{ACCESS_KEY_ID}替換為具體阿里雲RAM賬戶的Access Key Id,並且將權限策略AliyunLogOpenEventWrite賦予該賬戶;a123_asdad則為該開放告警應用的id,用於唯一區別不同的開放告警應用。

配置通知服務

Prometheus定義了向外部發送告警消息的協議:Alertmanager,可以通過在Prometheus配置文件中添加一個或者多個Alertmanager,將告警消息發送給運維開發人員。Alertmanager的配置規範如下所示:

# Per-target Alertmanager timeout when pushing alerts.
[ timeout: <duration> | default = 10s ]
# The api version of Alertmanager.
[ api_version: <string> | default = v2 ]
# Prefix for the HTTP path alerts are pushed to.
[ path_prefix: <path> | default = / ]
# Configures the protocol scheme used for requests.
[ scheme: <scheme> | default = http ]
# List of labeled statically configured Alertmanagers.
static_configs:
  # The targets specified by the static config.
  - targets:
    [ - '<host>' ]
    # Labels assigned to all metrics scraped from the targets.
  - labels:
    [ <labelname>: <labelvalue> ... ]

Prometheus允許通過靜態配置和動態發現兩種方式添加Alertmanager,對於SLS開放告警,我們只需要關心靜態配置即可。如下所示是包含SLS開放告警的Prometheus配置:

# my global config
global:
  scrape_interval:     15s # Set the scrape interval to every 15 seconds. Default is every 1 minute.
  evaluation_interval: 15s # Evaluate rules every 15 seconds. The default is every 1 minute.
  # scrape_timeout is set to the global default (10s).
# Alertmanager configuration
alerting:
  alertmanagers:
  - path_prefix: event/webhook/RAMAK_{ACCESS_KEY_ID}/test_test
    api_version: v2
    static_configs:
    - targets:
      - cn-heyuan.log.aliyuncs.com

scrape_interval和evaluation_interval用戶自行配置即可。用戶需要將path_prefix的內容替換為接口中的路徑部分(注意替換{ACCESS_KEY_ID}佔位符),將接口中的域名部分添加到targets中。之後只需要重新加載Prometheus配置,使配置生效即可。

映射規則

Prometheus告警消息內容示例如下,SLS開放告警會通過內置的規則,將該消息轉為SLS內部的告警消息:

[
    {
        "annotations": {
            "description": "description info",
            "summary": "High request latency"
        },
        "endsAt": "2020-10-28T12:28:52.710Z",
        "startsAt": "2020-10-28T12:23:37.710Z",
        "generatorURL": "http://127.0.0.1:9090/graph?g0.expr=go_threads%7Binstance%3D%22localhost%3A9090%22%2Cjob%3D%22prometheus%22%7D+%3E+0\\u0026g0.tab=1",
        "labels": {
            "alertname": "HighErrorRate",
            "instance": "localhost:9090",
            "job": "prometheus",
            "severity": "page"
        }
    }
]

告警嚴重程度

如果在Prometheus告警消息的labels/annotations中包含severity字段,開放告警會將該字段映射為告警消息的嚴重程度。目前開放告警支持多種告警消息嚴重程度,用戶可以根據自己的需要進行配置。如果沒有配置,嚴重程度將默認配置為中等。映射關係如下表所示。

告警嚴重度

關鍵字

嚴重

嚴重、緊急、critical、disaster、blocker、immediate、fatal、crit、sev0、'sev 0'、p0

高、高級、E、H、high、err、error、urgent、major、'sev 1'、sev1、p1

中、中級、告警、M、medium、unknown、warn、warning、'not classified'、average、normal、'sev 2'、sev2、p2

低、低級、L、I、info、information、suggestion、minor、informational、'sev 3'、sev3、p3

報告

報告、通知、report、dbg、debug、verbose、trivial、page、ok、'sev 4'、sev4、p4

字段映射

SLS開放告警會通過內置規則,將Prometheus消息轉為SLS內置告警消息。例如將上面的Prometheus消息,轉為如下所示的告警消息:

[{
    "aliuid": "{開放告警應用所屬的阿里雲賬號ID}",
    "alert_instance_id": "{自動生成}",
    "project": "{告警中心所屬的Project}",
    "region": "{告警消息發送的網絡接口對應的地域}",
    "alert_id": "HighErrorRate",
    "alert_type": "sls_pub",
    "alert_name": "HighErrorRate",
    "next_eval_interval": 78,
    "alert_time": 1603859020,
    "fire_time": 1603859017,
    "resolve_time": 0,
    "status": "firing",
    "labels": {
        "alertname": "HighErrorRate",
        "instance": "localhost:9090",
        "job": "prometheus"
    },
    "annotations": {
        "__pub_alert_region__": "{告警消息發送的網絡接口對應的地域}",
        "__config_app__": "sls_pub_alert",
        "__pub_alert_service__": "{開放告警服務ID}",
        "__pub_alert_app__": "{開放告警應用ID}",
        "__pub_alert_protocol__": "prometheus",
        "severity": "page"
    },
    "severity": 2,
    "policy": {
        "alert_policy_id": "{開放告警應用中配置的告警策略}",
        "action_policy_id": "{開放告警應用中配置的行動策略}",
        "repeat_interval": "{開放告警應用中配置的重複等待時間}"
    },
    "drill_down_query": "http://127.0.0.1:9090/graph?g0.expr=go_threads%7Binstance%3D%22localhost%3A9090%22%2Cjob%3D%22prometheus%22%7D+%3E+0\\u0026g0.tab=1"
}]

具體的轉換規則,請參考官方文檔接入Prometheus告警

總結

通過將Prometheus告警消息接入到SLS,用戶不再需要額外維護自己的Alertmanager服務,並且輕鬆的擴展了告警通知管理能力,從而更為高效的瞭解以及處理服務出現的問題。

Leave a Reply

Your email address will not be published. Required fields are marked *