首页 > 后端开发 > Golang > 正文

Golang如何使用Prometheus实现指标报警_Golang Prometheus指标报警实践详解

P粉602998670
发布: 2025-10-29 15:12:02
原创
553人浏览过
首先在Golang应用中使用prometheus/client_golang库暴露HTTP请求计数等指标,接着配置Prometheus通过scrape_configs定期抓取/metrics端点数据,然后在alerts.yml中定义基于表达式的报警规则如高延迟或高错误率,最后通过Alertmanager接收Firing状态报警并经邮件等方式发送通知。

golang如何使用prometheus实现指标报警_golang prometheus指标报警实践详解

Prometheus 是云原生生态中广泛使用的监控系统,Golang 服务结合 Prometheus 可以轻松暴露运行时指标并实现报警。要实现 Golang 应用的指标采集与报警,核心流程包括:在 Go 程序中暴露指标、配置 Prometheus 抓取、编写报警规则,并通过 Alertmanager 发送通知。下面详细介绍每一步实践。

1. 在 Golang 中暴露监控指标

使用 prometheus/client_golang 库可在 Go 服务中注册和暴露指标。常见指标类型包括 Counter(计数器)、Gauge(当前值)、Histogram(分布统计)和 Summary(分位数)。

示例代码:

package main
<p>import (
"net/http"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promhttp"
)</p><p>var (
httpRequestsTotal = prometheus.NewCounterVec(
prometheus.CounterOpts{
Name: "http_requests_total",
Help: "Total number of HTTP requests.",
},
[]string{"method", "endpoint", "status"},
)
)</p><p>func init() {
prometheus.MustRegister(httpRequestsTotal)
}</p><p>func handler(w http.ResponseWriter, r *http.Request) {
httpRequestsTotal.WithLabelValues(r.Method, r.URL.Path, "200").Inc()
w.Write([]byte("Hello World"))
}</p><p>func main() {
http.Handle("/metrics", promhttp.Handler())
http.HandleFunc("/", handler)
http.ListenAndServe(":8080", nil)
}</p>
登录后复制

启动后访问 :8080/metrics 可看到暴露的指标。确保 Prometheus 能访问此端点。

立即学习go语言免费学习笔记(深入)”;

2. 配置 Prometheus 抓取指标

编辑 prometheus.yml 文件,添加目标实例:

scrape_configs:
  - job_name: 'go-service'
    static_configs:
      - targets: ['your-go-service-ip:8080']
登录后复制

Prometheus 启动后会定期从该地址拉取 /metrics 数据。可通过 Prometheus 的 Web UI 查询指标,例如:

http_requests_total{job="go-service"}

3. 编写报警规则

报警规则定义在 Prometheus 的 rule_files 中。创建一个规则文件如 alerts.yml

标小兔AI写标书
标小兔AI写标书

一款专业的标书AI代写平台,提供专业AI标书代写服务,安全、稳定、速度快,可满足各类招投标需求,标小兔,写标书,快如兔。

标小兔AI写标书40
查看详情 标小兔AI写标书
groups:
- name: go_service_alerts
  rules:
  - alert: HighRequestLatency
    expr: histogram_quantile(0.95, sum(rate(http_request_duration_seconds_bucket[5m])) by (le)) > 0.5
    for: 2m
    labels:
      severity: warning
    annotations:
      summary: "High latency on {{ $labels.instance }}"
      description: "95th percentile latency is above 500ms"
<ul><li>alert: HighErrorRate
expr: sum(rate(http_requests_total{status=~"5.."}[5m])) / sum(rate(http_requests_total[5m])) > 0.05
for: 3m
labels:
severity: critical
annotations:
summary: "High error rate on {{ $labels.instance }}"
description: "Error rate is above 5%"
登录后复制

将该文件引入 Prometheus 配置:

rule_files:
</li><li>"alerts.yml"
登录后复制

expr 定义触发条件,for 表示持续时间,满足后进入 Pending 状态,之后变为 Firing 并通知 Alertmanager。

4. 集成 Alertmanager 发送报警

Alertmanager 负责去重、分组和发送通知。配置 alertmanager.yml 示例(邮件通知):

route:
  receiver: email-notifications
  group_wait: 30s
  group_interval: 5m
  repeat_interval: 1h
<p>receivers:</p><ul><li>name: email-notifications
email_configs:<ul><li>to: admin@example.com
from: alertmanager@example.com
smarthost: smtp.example.com:587
auth_username: "alertmanager"
auth_identity: "alertmanager@example.com"
auth_password: "password"
登录后复制

启动 Alertmanager 并确保 Prometheus 配置中指定其地址:

alerting:
alertmanagers:
- static_configs:
    - targets: ["localhost:9093"]
登录后复制

当报警触发,Alertmanager 将根据配置发送邮件或其他通知(支持钉钉、企业微信、Slack 等)。

基本上就这些。从 Go 暴露指标到 Prometheus 抓取、定义规则再到 Alertmanager 通知,整个链路清晰可控。关键是指标设计合理、报警阈值贴合业务,避免误报漏报。

以上就是Golang如何使用Prometheus实现指标报警_Golang Prometheus指标报警实践详解的详细内容,更多请关注php中文网其它相关文章!

最佳 Windows 性能的顶级免费优化软件
最佳 Windows 性能的顶级免费优化软件

每个人都需要一台速度更快、更稳定的 PC。随着时间的推移,垃圾文件、旧注册表数据和不必要的后台进程会占用资源并降低性能。幸运的是,许多工具可以让 Windows 保持平稳运行。

下载
来源:php中文网
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
最新问题
开源免费商场系统广告
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 意见反馈 讲师合作 广告合作 最新更新 English
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习
PHP中文网抖音号
发现有趣的

Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号