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", RefreshInterval: 15, PushAddr: "prometheus pusher address", StartServer: true, HTTPServerPort: 8080, MetricsCollector: []prometheus.MetricsCollector { &prometheus.MySQL{ VariableNames: []string{"Threads_running"}, }, }, }))
|
用户自定义指标
您可以通过 GORM Prometheus 插件定义并收集自定义的指标,这需要实现 MetricCollector
接口
type MetricsCollector interface { Metrics(*Prometheus) []prometheus.Collector }
|
MySQL
GORM 提供了一个示例,说明如何收集 MySQL 状态指标,查看 prometheus.MySQL 获取详情
&prometheus.MySQL{ Prefix: "gorm_status_", Interval: 100, VariableNames: []string{"Threads_running"}, }
|