Deprecated: imwpcache\f884414bce24ee67f\f73723ec7b1919fa5::__construct(): Implicitly marking parameter $YECBGYFECGEAFWHA as nullable is deprecated, the explicit nullable type must be used instead in /www/wwwroot/www.chuangxiangniao.com/wp-content/plugins/imwpcache-dist/build/f884414bce24ee67ff73723ec7b1919fa5.php on line 2

Deprecated: imwpcache\f884414bce24ee67f\f73723ec7b1919fa5::__construct(): Implicitly marking parameter $BBWFDDBHHYHDXXAB as nullable is deprecated, the explicit nullable type must be used instead in /www/wwwroot/www.chuangxiangniao.com/wp-content/plugins/imwpcache-dist/build/f884414bce24ee67ff73723ec7b1919fa5.php on line 2
Golang服务监控看板 Grafana可视化_创想鸟

Golang服务监控看板 Grafana可视化

答案:通过在Go服务中使用Prometheus客户端暴露指标,部署Prometheus抓取数据,并在Grafana中配置数据源和看板,可实现对Golang服务运行状态的可视化监控,关键步骤包括指标采集、时序存储与可视化展示。

golang服务监控看板 grafana可视化

想让Golang服务的运行状态一目了然,Grafana搭配合适的监控体系是最佳选择之一。核心思路是:在Go服务中采集指标,通过时序数据库存储,再用Grafana做可视化展示。下面一步步说明如何实现。

1. 在Golang服务中暴露监控指标

使用 prometheus/client_golang 是最常见的方式。先在项目中引入依赖:

go get github.com/prometheus/client_golang/prometheusgo get github.com/prometheus/client_golang/prometheus/promhttp

然后在服务中注册常用指标,比如请求计数、响应时间、Goroutine数量等:

示例代码:

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

package main

import (“net/http””time””github.com/prometheus/client_golang/prometheus””github.com/prometheus/client_golang/prometheus/promhttp”)

var (// 请求计数器httpRequestsTotal = prometheus.NewCounterVec(prometheus.CounterOpts{Name: “http_requests_total”,Help: “Total number of HTTP requests”,},[]string{“method”, “endpoint”, “status”},)

// 请求时长直方图httpRequestDuration = prometheus.NewHistogramVec(    prometheus.HistogramOpts{        Name:    "http_request_duration_seconds",        Help:    "Duration of HTTP requests in seconds",        Buckets: []float64{0.1, 0.3, 0.5, 1.0, 3.0},    },    []string{"method", "endpoint"},)

)

func init() {prometheus.MustRegister(httpRequestsTotal)prometheus.MustRegister(httpRequestDuration)}

func handler(w http.ResponseWriter, r http.Request) {start := time.Now()// 模拟业务处理time.Sleep(100 time.Millisecond)httpRequestsTotal.WithLabelValues(r.Method, r.URL.Path, “200”).Inc()httpRequestDuration.WithLabelValues(r.Method, r.URL.Path).Observe(time.Since(start).Seconds())w.Write([]byte(“Hello”))}

func main() {http.HandleFunc(“/hello”, handler)http.Handle(“/metrics”, promhttp.Handler())http.ListenAndServe(“:8080”, nil)}

启动服务后,访问 :8080/metrics 就能看到Prometheus格式的指标输出。

2. 部署Prometheus抓取Go服务指标

Prometheus负责定时从Go服务拉取指标。配置 prometheus.yml

scrape_configs: – job_name: ‘go-service’ static_configs: – targets: [‘your-go-service-ip:8080’]

确保Prometheus能访问Go服务的 /metrics 接口。启动Prometheus后,访问其Web界面(默认9090端口),可用PromQL查询指标,如:

http_requests_totalrate(http_request_duration_seconds_sum[5m])go_goroutines

3. Grafana连接Prometheus并创建看板

安装Grafana(可通过Docker、系统包或二进制方式),启动后访问3000端口。

步骤:

登录Grafana(默认账号密码 admin/admin)添加数据源:选择 Prometheus,填写地址(如 http://prometheus-server:9090)保存并测试连接创建仪表盘(Dashboard)→ 添加Panel

常用Panel配置示例:

总请求数:使用表达式

sum(http_requests_total)

,图表类型选“Time series”QPS:使用

sum(rate(http_requests_total[1m]))

平均响应时间

rate(http_request_duration_seconds_sum[5m]) / rate(http_request_duration_seconds_count[5m])

Goroutines数量:直接查

go_goroutines

可为不同接口、状态码设置变量(Variables),实现下拉筛选,提升看板灵活性。

4. 可选增强:集成应用健康与日志

除了基础指标,还可加入:

自定义业务指标,如订单创建数、缓存命中率使用 go_memstats 监控内存分配结合 Loki 收集日志,在Grafana中关联查看错误日志设置告警规则,如QPS突降或响应时间超阈值,通过邮件或Webhook通知

基本上就这些。搭建完成后,Grafana看板能实时反映Golang服务的健康状况,帮助快速定位性能瓶颈或异常。关键是指标要有业务意义,避免堆砌无用图表。

以上就是Golang服务监控看板 Grafana可视化的详细内容,更多请关注创想鸟其它相关文章!

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。
如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至 chuangxiangniao@163.com 举报,一经查实,本站将立刻删除。
发布者:程序猿,转转请注明出处:https://www.chuangxiangniao.com/p/1399085.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
Golang处理HTTPS请求怎么做 配置TLS证书与安全连接
上一篇 2025年12月15日 15:55:21
Golang多返回值怎么处理 错误处理惯用模式解析
下一篇 2025年12月15日 15:55:31

相关推荐

发表回复

登录后才能评论
关注微信