InfoSphere/GORM 中文版/ 返回书籍
Advanced Topics

Prometheus

qianmoQqianmoQ· 更新于 2026-09-15· 阅读 4 分钟· 0 次阅读

登录后可跨设备保存划线和私人笔记登录

GORM provides Prometheus plugin to collect DBStats or user-defined metrics

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

Usage

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"},
    },
  },  
}))

User-Defined Metrics

You can define your metrics and collect them with GORM Prometheus plugin, which needs to implements MetricsCollector interface

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

MySQL

GORM provides an example for how to collect MySQL Status as metrics, check it out prometheus.MySQL

&prometheus.MySQL{
  Prefix: "gorm_status_",
  
  
  Interval: 100,
  
  VariableNames: []string{"Threads_running"},
  
}

GitHub tag (latest SemVer)

评论

登录后参与评论

正在加载评论…