go-gorm / prometheus

Collect DB Status or user-defined metrics with Prometheus

Home Page:http://gorm.io/docs/prometheus.html

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Prometheus

Collect DB Status with Prometheus

Usage

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

db, err := gorm.Open(sqlite.Open("gorm.db"), &gorm.Config{})
if err != nil {
    fmt.Errorf("error opening database: %v", err)
}

db.Use(prometheus.New(prometheus.Config{
    DBName:          "db1",                       // `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"}},
    },
    Labels: map[string]string{
        "instance": "127.0.0.1",                  // config custom labels if necessary
    },
}))

About

Collect DB Status or user-defined metrics with Prometheus

http://gorm.io/docs/prometheus.html

License:MIT License


Languages

Language:Go 100.0%