答案:在Golang中实现Prometheus告警推送需通过Alertmanager将告警以JSON格式POST到Golang编写的Web服务,该服务监听/webhook路径,解析WebhookRequest结构体中的告警信息,并可扩展钉钉通知等功能。

在Golang中实现Prometheus告警推送,核心是借助Alertmanager接收来自Prometheus的告警,并通过自定义HTTP服务接收Alertmanager的推送通知。你可以用Golang编写一个Web服务来接收、解析并处理这些告警信息,比如发送到钉钉、企业微信或写入日志。
Prometheus本身不直接发送通知,而是将触发的告警发送给Alertmanager。Alertmanager负责去重、分组和路由,然后调用你配置的Webhook接收端。
你的Golang程序需要:
下面是一个简单的Golang服务示例,用于接收Alertmanager的告警推送:
立即学习“go语言免费学习笔记(深入)”;
package main
<p>import (
"encoding/json"
"io"
"log"
"net/http"
)</p><p>// Alert 表示告警的基本结构
type Alert struct {
Status string <code>json:"status"</code>
Labels map[string]string <code>json:"labels"</code>
Annotations map[string]string <code>json:"annotations"</code>
StartsAt string <code>json:"startsAt"</code>
EndsAt string <code>json:"endsAt"</code>
}</p><p>// WebhookRequest 是Alertmanager发送的整体请求结构
type WebhookRequest struct {
Receiver string <code>json:"receiver"</code>
Status string <code>json:"status"</code>
Alerts []Alert <code>json:"alerts"</code>
}</p><p>func webhookHandler(w http.ResponseWriter, r *http.Request) {
if r.Method != http.MethodPost {
http.Error(w, "method not allowed", http.StatusMethodNotAllowed)
return
}</p><pre class="brush:php;toolbar:false;"><pre class="brush:php;toolbar:false;">body, err := io.ReadAll(r.Body)
if err != nil {
http.Error(w, "can't read body", http.StatusBadRequest)
return
}
var req WebhookRequest
if err := json.Unmarshal(body, &req); err != nil {
http.Error(w, "invalid JSON", http.StatusBadRequest)
log.Printf("Unmarshal error: %v\nBody: %s", err, body)
return
}
// 打印收到的告警
log.Printf("Received alert from receiver: %s, status: %s", req.Receiver, req.Status)
for _, alert := range req.Alerts {
log.Printf("Alert: status=%s, labels=%v, annotations=%v",
alert.Status, alert.Labels, alert.Annotations)
// 在这里可以添加发送钉钉、企业微信、邮件等逻辑
sendToDingTalk(alert)
}
w.WriteHeader(http.StatusOK)
w.Write([]byte("OK"))}
func sendToDingTalk(alert Alert) { // 实现钉钉机器人发送逻辑(使用webhook) // 示例:构造消息并用http.Post发送 // 注意:需配置钉钉机器人的access_token }
启动服务:
func main() {
http.HandleFunc("/webhook", webhookHandler)
log.Println("Server starting on :8080")
log.Fatal(http.ListenAndServe(":8080", nil))
}
修改alertmanager.yml,添加一个webhook接收器:
route: receiver: 'golang-webhook' <p>receivers:</p><ul><li>name: 'golang-webhook' webhook_configs:<ul><li>url: '<a href="https://www.php.cn/link/ec43638b66e16a5bbede9b710b12b0c7">https://www.php.cn/link/ec43638b66e16a5bbede9b710b12b0c7</a>'
重启Alertmanager后,当有告警触发时,它会POST到你的Golang服务。
你可以根据alert.Labels["alertname"]或其它字段判断告警类型,并执行不同操作:
例如发送钉钉消息:
import "bytes"
<p>func sendToDingTalk(alert Alert) {
url := "<a href="https://www.php.cn/link/fddb49982f360e8a94aa8642ed545a16">https://www.php.cn/link/fddb49982f360e8a94aa8642ed545a16</a>"
msg := map[string]interface{}{
"msgtype": "text",
"text": map[string]string{
"content": "告警:" + alert.Labels["alertname"] + ",状态:" + alert.Status,
},
}
payload, <em> := json.Marshal(msg)
resp, </em> := http.Post(url, "application/json", bytes.NewBuffer(payload))
resp.Body.Close()
}
基本上就这些。不复杂但容易忽略细节,比如Alertmanager的网络可达性、JSON解析容错、服务稳定性等。
以上就是如何在Golang中实现Prometheus告警推送的详细内容,更多请关注php中文网其它相关文章!
每个人都需要一台速度更快、更稳定的 PC。随着时间的推移,垃圾文件、旧注册表数据和不必要的后台进程会占用资源并降低性能。幸运的是,许多工具可以让 Windows 保持平稳运行。
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号