promdb

package
v0.0.0-...-f8a288c Latest Latest
Warning

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

Go to latest
Published: Jul 10, 2023 License: Apache-2.0 Imports: 2 Imported by: 0

README

PromDB

A quick library for adding prometheus metrics to your service for your databases

Usage

Without labels
db, _ := sql.Open("postgres", "")
collector := promdb.NewDatabaseCollector(db)
prometheus.MustRegister(collector)
With labels
var labels prometheus.Labels
var collector *promdb.DatabaseCollector

authdb, _ := sql.Open("mysql", "")
labels = prometheus.Labels{"driver": "mysql", "backend": "authdb"}
collector = promdb.NewDatabaseCollectorWithLabels(authdb, labels)
prometheus.MustRegister(collector)

billingdb, _ := sql.Open("postgres", "")
labels = prometheus.Labels{"driver": "postgres"}
collector = promdb.NewDatabaseCollectorWithLabels(billingdb, labels)
prometheus.MustRegister(collector.WithLabel("backend", "billingdb"))

eventsdb, _ := sql.Open("postgres", "")
labels = prometheus.Labels{"driver": "postgres", "backend": "eventsdb"}
collector = promdb.NewDatabaseCollector(eventsdb)
prometheus.MustRegister(collector.With(labels))

statsdb, _ := sql.Open("bigquery", "")
collector = promdb.NewDatabaseCollector(statsdb).WithLabel("driver", "bigquery")
prometheus.MustRegister(collector.WithLabel("backend", "statsdb"))
Example rendered metrics
Without labels
# HELP database_connections_idle The number of idle connections.
# TYPE database_connections_idle gauge
database_connections_idle 0
# HELP database_connections_in_use The number of connections currently in use.
# TYPE database_connections_in_use gauge
database_connections_in_use 0
With labels
# HELP database_connections_idle The number of idle connections.
# TYPE database_connections_idle gauge
database_connections_idle{backend="authdb",driver="mysql"} 0
database_connections_idle{backend="billingdb",driver="postgres"} 0
database_connections_idle{backend="eventsdb",driver="postgres"} 0
database_connections_idle{backend="statsdb",driver="bigquery"} 0
# HELP database_connections_in_use The number of connections currently in use.
# TYPE database_connections_in_use gauge
database_connections_in_use{backend="authdb",driver="mysql"} 0
database_connections_in_use{backend="billingdb",driver="postgres"} 0
database_connections_in_use{backend="eventsdb",driver="postgres"} 0
database_connections_in_use{backend="statsdb",driver="bigquery"} 0

Documentation

Overview

Package promdb is a quick library for adding prometheus metrics to your service for your databases, utilizing the sql.DBStats struct results.

Exable without labels

db, _ := sql.Open("postgres", "")
collector := promdb.NewDatabaseCollector(db)
prometheus.MustRegister(collector)

Example with labels

var labels prometheus.Labels
var collector *promdb.DatabaseCollector

authdb, _ := sql.Open("mysql", "")
labels = prometheus.Labels{"driver": "mysql", "backend": "authdb"}
collector = promdb.NewDatabaseCollectorWithLabels(authdb, labels)
prometheus.MustRegister(collector)

billingdb, _ := sql.Open("postgres", "")
labels = prometheus.Labels{"driver": "postgres"}
collector = promdb.NewDatabaseCollectorWithLabels(billingdb, labels)
prometheus.MustRegister(collector.WithLabel("backend", "billingdb"))

eventsdb, _ := sql.Open("postgres", "")
labels = prometheus.Labels{"driver": "postgres", "backend": "eventsdb"}
collector = promdb.NewDatabaseCollector(eventsdb)
prometheus.MustRegister(collector.With(labels))

statsdb, _ := sql.Open("bigquery", "")
collector = promdb.NewDatabaseCollector(statsdb).WithLabel("driver", "bigquery")
prometheus.MustRegister(collector.WithLabel("backend", "statsdb"))

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type DBWithStats

type DBWithStats interface {
	Stats() sql.DBStats
}

DBWithStats is an interface used by DatabaseCollector

type DatabaseCollector

type DatabaseCollector struct {
	// contains filtered or unexported fields
}

DatabaseCollector implements prometheus.Collector interface

func NewDatabaseCollector

func NewDatabaseCollector(db DBWithStats) *DatabaseCollector

NewDatabaseCollector creates a new instance of DatabaseCollector with no labels

func NewDatabaseCollectorWithLabels

func NewDatabaseCollectorWithLabels(db DBWithStats, labels prometheus.Labels) *DatabaseCollector

NewDatabaseCollectorWithLabels creates a new instance of DatabaseCollector with the specified prometheus labels

func (*DatabaseCollector) Collect

func (c *DatabaseCollector) Collect(ch chan<- prometheus.Metric)

Collect implements prometheus.Collector interface

func (*DatabaseCollector) Describe

func (c *DatabaseCollector) Describe(ch chan<- *prometheus.Desc)

Describe implements prometheus.Collector interface

func (*DatabaseCollector) With

With returns a new instance of DatabaseCollector with the specified labels merging with any previous labels that may have been defined

func (*DatabaseCollector) WithLabel

func (c *DatabaseCollector) WithLabel(name, value string) *DatabaseCollector

WithLabel is a shortcut to With for providing a single label and value

Jump to

Keyboard shortcuts

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