Prometheus

GORM 提供了 Prometheus 插件来收集 DBStats 或用户自定义指标

https://github.com/go-gorm/prometheus

用法

import (
"gorm.io/gorm"
"gorm.io/driver/sqlite"
"gorm.io/plugin/prometheus"
)

db, err := gorm.Open(sqlite.Open("gorm.db"), &gorm.Config{})

db.Use(prometheus.New(prometheus.Config{
DBName: "db1", // use `DBName` as metrics label
RefreshInterval: 15, // Refresh metrics interval (default 15 seconds)
PushAddr: "prometheus pusher address", // push metrics if `PushAddr` configured
StartServer: true, // start http server to expose metrics
HTTPServerPort: 8080, // configure http server port, default port 8080 (if you have configured multiple instances, only the first `HTTPServerPort` will be used to start server)
MetricsCollector: []prometheus.MetricsCollector {
&prometheus.MySQL{
VariableNames: []string{"Threads_running"},
},
}, // user defined metrics
}))

用户自定义指标

您可以定义您的指标并使用 GORM Prometheus 插件收集它们,该插件需要实现 MetricsCollector 接口

type MetricsCollector interface {
Metrics(*Prometheus) []prometheus.Collector
}

MySQL

GORM 提供了一个如何将 MySQL 状态收集为指标的示例,请查看 prometheus.MySQL

&prometheus.MySQL{
Prefix: "gorm_status_",
// Metrics name prefix, default is `gorm_status_`
// For example, Threads_running's metric name is `gorm_status_Threads_running`
Interval: 100,
// Fetch interval, default use Prometheus's RefreshInterval
VariableNames: []string{"Threads_running"},
// Select variables from SHOW STATUS, if not set, uses all status variables
}

铂金赞助商

黄金赞助商

铂金赞助商

黄金赞助商