监控数据库go程序
一、编写一段go程序代码
package main
import (
"github.com/prometheus/client_golang/prometheus/promhttp"
"log"
"net/http"
"runtime"
"sync"
"time"
"github.com/prometheus/client_golang/prometheus"
)
var (
goGoroutines = prometheus.NewGauge(prometheus.GaugeOpts{
Name: "go_goroutines",
Help: "Number of current goroutines.",
})
goGCDurationSeconds = prometheus.NewHistogram(prometheus.HistogramOpts{
Name: "go_gc_duration_seconds",
Help: "A histogram of the GC duration in seconds.",
Buckets: []float64{0.001, 0.005, 0.01, 0.05, 0.1, 0.5, 1, 5, 10},
})
goMemstatsAllocBytes = prometheus.NewGauge(prometheus.GaugeOpts{
Name: "go_memstats_alloc_bytes",
Help: "Number of bytes allocated and still in use.",
})
goMemstatsHeapAllocBytes = prometheus.NewGauge(prometheus.GaugeOpts{
Name: "go_memstats_heap_alloc_bytes",
Help: "Number of heap bytes allocated and still in use.",
})
goMemstatsAllocBytesTotal = prometheus.NewCounter(prometheus.CounterOpts{
Name: "go_memstats_alloc_bytes_total",
Help: "Total number of bytes allocated, even if freed.",
})
goMemstatsSysBytes = prometheus.NewGauge(prometheus.GaugeOpts{
Name: "go_memstats_sys_bytes",
Help: "Total number of bytes obtained by system. Sum of all system allocations.",
})
goMemstatsHeapSysBytes = prometheus.NewGauge(prometheus.GaugeOpts{
Name: "go_memstats_heap_sys_bytes",
Help: "Number of heap bytes obtained from system.",
})
goMemstatsHeapIdleBytes = prometheus.NewGauge(prometheus.GaugeOpts{
Name: "go_memstats_heap_idle_bytes",
Help: "Number of heap bytes waiting to be used.",
})
goMemstatsHeapInuseBytes = prometheus.NewGauge(prometheus.GaugeOpts{
Name: "go_memstats_heap_inuse_bytes",
Help: "Number of heap bytes that are in use.",
})
goMemstatsStackInuseBytes = prometheus.NewGauge(prometheus.GaugeOpts{
Name: "go_memstats_stack_inuse_bytes",
Help: "Number of bytes in use by the stack allocator.",
})
goMemstatsStackSysBytes = prometheus.NewGauge(prometheus.GaugeOpts{
Name: "go_memstats_stack_sys_bytes",
Help: "Total number of bytes obtained from system for stack allocator.",
})
goMemstatsMspanInuseBytes = prometheus.NewGauge(prometheus.GaugeOpts{
Name: "go_memstats_mspan_inuse_bytes",
Help: "Number of bytes in use by mspan structures.",
})
goMemstatsMspanSysBytes = prometheus.NewGauge(prometheus.GaugeOpts{
Name: "go_memstats_mspan_sys_bytes",
Help: "Number of bytes obtained from system for mspan structures.",
})
goMemstatsMcacheInuseBytes = prometheus.NewGauge(prometheus.GaugeOpts{
Name: "go_memstats_mcache_inuse_bytes",
Help: "Number of bytes in use by mcache structures.",
})
goMemstatsMcacheSysBytes = prometheus.NewGauge(prometheus.GaugeOpts{
Name: "go_memstats_mcache_sys_bytes",
Help: "Number of bytes obtained from system for mcache structures.",
})
goMemstatsBuckHashSysBytes = prometheus.NewGauge(prometheus.GaugeOpts{
Name: "go_memstats_buck_hash_sys_bytes",
Help: "Number of bytes used by the profiling bucket hash table.",
})
goMemstatsGCSysBytes = prometheus.NewGauge(prometheus.GaugeOpts{
Name: "go_memstats_gc_sys_bytes",
Help: "Number of bytes used for garbage collection system metadata.",
})
)
func updateMetrics() {
var stats runtime.MemStats
runtime.ReadMemStats(&stats)
goGoroutines.Set(float64(runtime.NumGoroutine()))
goMemstatsAllocBytes.Set(float64(stats.Alloc))
goMemstatsHeapAllocBytes.Set(float64(stats.HeapAlloc))
goMemstatsAllocBytesTotal.Add(float64(stats.TotalAlloc))
goMemstatsSysBytes.Set(float64(stats.Sys))
goMemstatsHeapSysBytes.Set(float64(stats.HeapSys))
goMemstatsHeapIdleBytes.Set(float64(stats.HeapIdle))
goMemstatsHeapInuseBytes.Set(float64(stats.HeapInuse))
goMemstatsStackInuseBytes.Set(float64(stats.StackInuse))
goMemstatsStackSysBytes.Set(float64(stats.StackSys))
goMemstatsMspanInuseBytes.Set(float64(stats.MSpanInuse))
goMemstatsMspanSysBytes.Set(float64(stats.MSpanSys))
goMemstatsMcacheInuseBytes.Set(float64(stats.MCacheInuse))
goMemstatsMcacheSysBytes.Set(float64(stats.MCacheSys))
goMemstatsBuckHashSysBytes.Set(float64(stats.BuckHashSys))
goMemstatsGCSysBytes.Set(float64(stats.GCSys))
// For GC duration, we'll need to track GC events separately
// and update the histogram accordingly.
// Here's a simplified example of how to do it.
// Note: This is not the most efficient way, but serves as an illustration.
var mu sync.Mutex
var lastGCTime time.Time
mu.Lock()
if lastGCTime.IsZero() {
lastGCTime = time.Now()
} else {
duration := time.Since(lastGCTime).Seconds()
mu.Unlock()
goGCDurationSeconds.Observe(duration)
// Update lastGCTime after unlocking to avoid race conditions
mu.Lock()
lastGCTime = time.Now()
}
}
func main() {
http.Handle("/metrics", promhttp.Handler())
// 定期更新指标
go func() {
for {
updateMetrics()
// 每秒更新一次指标
time.Sleep(1 \* time.Second)
}
}()
log.Fatal(http.ListenAndServe(":8182", nil))
}
运行,访问 http://192.168.100.58:8182/metrics
![](https://img.kancloud.cn/a6/76/a6764024b22a53e5aa81df267a32b780_1477x928.png)
<hr>
二、 prometheus服务器添加go程序的地址
2.4
192.168.100.85的centos上,修改prometheus的配置文件
#进入docker-prometheus目录
cd /data/docker-prometheus
#修改prometheus.yml
vi prometheus/prometheus.yml
添加monogodb_exporter配置如下图:
![](https://img.kancloud.cn/da/96/da967ce0355d3cfe20496d8936deb0dd_525x260.png)
- job_name: "golang-exporter"
static_configs:
- targets: ["192.168.100.58:8182"]
labels:
istance: "centos2服务器go程序监听"
保存后输入命令更新:
curl -XPOST http://localhost:9090/-/reload
成功监听
![](https://img.kancloud.cn/8e/c1/8ec19bdaa9f7bd54f53f7eab10b4f433_1581x706.png)
<hr>
2.6 grafana中对go程序进行监控(因为程序参数是对着模板参数写的,所以适合,自己随便写的得自己配面板)
copy id to clipboard->grafana的dashboards中lmport dashboard
https://grafana.com/grafana/dashboards/10826-go-metrics/
![](https://img.kancloud.cn/e3/e1/e3e197b276b811cbbb5d581c89a6f3ba_1663x844.png)
![](https://img.kancloud.cn/6e/10/6e104658c167e77624421d4c6b2e365f_1881x906.png)
- Prometheus教程
- 一. dokcer 安装
- 二. 安装docker-compose
- 三. docker-compose安装Prometheus
- 四. 配置grafana的数据源
- 五. Prometheus的Exporter
- 六. Prometheus的基本术语
- 七. 监控Linux
- 八. 监控redis和mongodb
- 九. 监控mysql数据库
- 十. 监控go程序
- 十一. 监控nginx
- 十二. 监控消息队列
- 十三. 监控docker
- 十四. 监控进程
- 十五. 域名监控
- 十六. SNMP监控
- 十七. 黑盒监控
- 十八. 自定义监控
- 十九. go实现自定义监控
- 二十. 服务发现概述
- 二十一. 基于文件的服务发现
- 二十二. 基于Consul的服务发现
- 二十三. relabeling机制