monitor

package module
v1.2.0 Latest Latest
Warning

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

Go to latest
Published: Jul 13, 2023 License: Apache-2.0 Imports: 4 Imported by: 6

README

go-pg-monitor

Go Reference main Go Report Card

Provides Prometheus metrics for go-pg database client.

Example Grafana dashboards:

grafana

Status

This package is now stable.

As the go-pg is in a maintenance mode, it is unlikely that new features will be introduced in this package. Fixing bugs and security issues will continue.

Usage

  1. Your application should already be using go-pg and exporting Prometheus metrics.

  2. Add the module to your project:

    go get github.com/hypnoglow/go-pg-monitor
    

    Also add the submodule with the go-pg version you use:

    go get github.com/hypnoglow/go-pg-monitor/gopgv10
    
  3. Add monitor to your application entrypoint:

    // Create monitor based on your *pg.DB
    mon := monitor.NewMonitor(
    	gopgv10.NewObserver(db),
    	monitor.NewMetrics(),
    )
    
    // Call this on application startup. 
    mon.Open()
    
    // Call this on application shutdown.
    mon.Close()
    

See example for details.

Exported metrics

  • go_pg_pool_hits - (Gauge) Number of times free connection was found in the pool
  • go_pg_pool_misses - (Gauge) Number of times free connection was NOT found in the pool
  • go_pg_pool_timeouts - (Gauge) Number of times a wait timeout occurred
  • go_pg_pool_total_connections - (Gauge) Number of total connections in the pool
  • go_pg_pool_idle_connections - (Gauge) Number of idle connections in the pool
  • go_pg_pool_stale_connections - (Gauge) Number of stale connections removed from the pool

Reference Grafana Dashboards

You can find two example Grafana dashboards in grafana/ directory. One may be suitable when you use only one database object in your application code, and the other when you use different objects (pools) for different parts of the application.

Note that your dashboard may be different if you use metric namespace, different k8s labels, etc. So these dashboards are provided only as a starting point for making your own.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Metrics

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

Metrics represent a set of Prometheus metrics for database client stats.

func NewMetrics

func NewMetrics(opts ...MetricsOption) *Metrics

NewMetrics returns a new configured Metrics.

type MetricsOption

type MetricsOption func(metrics *Metrics)

MetricsOption is an option for NewMetrics.

func MetricsWithConstLabels added in v0.2.0

func MetricsWithConstLabels(constLabels prometheus.Labels) MetricsOption

MetricsWithConstLabels is an option that sets metric constant labels.

func MetricsWithNamespace

func MetricsWithNamespace(namespace string) MetricsOption

MetricsWithNamespace is an option that sets metric namespace.

func MetricsWithRegisterer added in v1.1.0

func MetricsWithRegisterer(registerer prometheus.Registerer) MetricsOption

MetricsWithRegisterer is an option that sets custom registerer for metrics.

func MetricsWithSubsystem

func MetricsWithSubsystem(subsystem string) MetricsOption

MetricsWithSubsystem is an option that sets metric subsystem.

type Monitor

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

Monitor is a go-pg database client monitor.

func NewMonitor

func NewMonitor(observer Observer, metrics *Metrics, opts ...MonitorOption) *Monitor

NewMonitor returns a new configured Monitor.

func (*Monitor) Close

func (m *Monitor) Close()

Close stops the monitor.

func (*Monitor) Open

func (m *Monitor) Open()

Open starts the monitor, observing metrics at each interval.

type MonitorOption

type MonitorOption func(monitor *Monitor)

MonitorOption is an option for NewMonitor.

func MonitorWithPoolName added in v1.2.0

func MonitorWithPoolName(poolName string) MonitorOption

MonitorWithPoolName is an option that sets pool name for the monitor.

type Observer

type Observer interface {
	// Observe returns current database client stats.
	Observe() Stats
}

Observer can provide database client stats.

type Stats

type Stats struct {
	Hits     uint32
	Misses   uint32
	Timeouts uint32

	TotalConns uint32
	IdleConns  uint32
	StaleConns uint32
}

Stats represents go-pg database client stats.

Directories

Path Synopsis
example module
gopgv10 module
gopgv9 module

Jump to

Keyboard shortcuts

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