prometheus

package module
v0.1.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Oct 26, 2023 License: MIT Imports: 14 Imported by: 66

README

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

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Config

type Config struct {
	DBName           string             // use DBName as metrics label
	RefreshInterval  uint32             // refresh metrics interval.
	PushAddr         string             // prometheus pusher address
	PushUser         string             // prometheus pusher basic auth user
	PushPassword     string             // prometheus pusher basic auth password
	StartServer      bool               // if true, create http server to expose metrics
	HTTPServerPort   uint32             // http server port
	MetricsCollector []MetricsCollector // collector
	Labels           map[string]string  // metrics labels
}

type DBStats

type DBStats struct {
	MaxOpenConnections prometheus.Gauge // Maximum number of open connections to the database.

	// Pool status
	OpenConnections prometheus.Gauge // The number of established connections both in use and idle.
	InUse           prometheus.Gauge // The number of connections currently in use.
	Idle            prometheus.Gauge // The number of idle connections.

	// Counters
	WaitCount         prometheus.Gauge // The total number of connections waited for.
	WaitDuration      prometheus.Gauge // The total time blocked waiting for a new connection.
	MaxIdleClosed     prometheus.Gauge // The total number of connections closed due to SetMaxIdleConns.
	MaxLifetimeClosed prometheus.Gauge // The total number of connections closed due to SetConnMaxLifetime.
	MaxIdleTimeClosed prometheus.Gauge // The total number of connections closed due to SetConnMaxIdleTime.
}

func (*DBStats) Collectors

func (stats *DBStats) Collectors() (collector []prometheus.Collector)

get collector in stats

func (*DBStats) Set

func (stats *DBStats) Set(dbStats sql.DBStats)

type MetricsCollector

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

type MySQL

type MySQL struct {
	Prefix        string
	Interval      uint32
	VariableNames []string
	// contains filtered or unexported fields
}

func (*MySQL) Metrics

func (m *MySQL) Metrics(p *Prometheus) []prometheus.Collector

type Postgres

type Postgres struct {
	Prefix        string
	Interval      uint32
	VariableNames []string
	// contains filtered or unexported fields
}

Posgres metrics providers. Metrics is being contructed from Sturct labels: Type translation:

int64 - Counter, Gauge
time.Time - Gauge
string - Label on the metrics

Example:

DatName string `gorm:"column:datname" type:"label" help:"Name of current database"` SizeBytes int64 `gorm:"column:size_bytes" type:"gauge" help:"Size of database in bytes"`

will be translated to: gorm_dbstats_size_bytes{datname="test"} 123456789

func (*Postgres) Metrics

func (m *Postgres) Metrics(p *Prometheus) []prometheus.Collector

type Prometheus

type Prometheus struct {
	*gorm.DB
	*DBStats
	*Config

	Labels     map[string]string
	Collectors []prometheus.Collector
	// contains filtered or unexported fields
}

func New

func New(config Config) *Prometheus

func (*Prometheus) Initialize

func (p *Prometheus) Initialize(db *gorm.DB) error

func (*Prometheus) Name

func (p *Prometheus) Name() string

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL