pgxprom

package module
v0.0.0-...-3153915 Latest Latest
Warning

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

Go to latest
Published: May 27, 2024 License: MIT Imports: 8 Imported by: 0

README

pgxprom

A prometheus adapter for pgx

Getting Started

package main

import (
	"context"
	"os"

	"github.com/jackc/pgx/v5/pgxpool"
	"github.com/pgx-contrib/pgxprom"
	"github.com/prometheus/client_golang/prometheus"
)

func main() {
	config, err := pgxpool.ParseConfig(os.Getenv("PGX_DATABASE_URL"))
	if err != nil {
		panic(err)
	}

	pool, err := pgxpool.NewWithConfig(context.TODO(), config)
	if err != nil {
		panic(err)
	}

	collector := pgxprom.NewPoolStatsCollector()
	// register the pool
	collector.Register(pool)
	// register the collector
	if err := prometheus.Register(collector); err != nil {
		panic(err)
	}
}

Documentation

Index

Examples

Constants

This section is empty.

Variables

View Source
var (
	// Request is the total number of database requests.
	RequestTotal *prometheus.CounterVec
	// ErrorsTotal is the total number of database request errors.
	ErrorsTotal *prometheus.CounterVec
	// Duration is the time taken to complete a database request and process the response.
	Duration *prometheus.HistogramVec
)
View Source
var TraceBatchKey = &ContextKey{
	name: reflect.TypeOf(TraceBatchData{}).PkgPath(),
}

TraceBatchKey represents the context key of the data.

View Source
var TraceQueryKey = &ContextKey{
	name: reflect.TypeOf(TraceQueryData{}).PkgPath(),
}

TraceQueryKey represents the context key of the data.

Functions

This section is empty.

Types

type ContextKey

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

ContextKey represents a context key.

func (*ContextKey) String

func (k *ContextKey) String() string

String returns the context key as a string.

type PoolStatsCollector

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

PoolStatsCollector is a Prometheus pool collector for pgx metrics.

Example
package main

import (
	"context"
	"os"

	"github.com/jackc/pgx/v5/pgxpool"
	"github.com/pgx-contrib/pgxprom"
	"github.com/prometheus/client_golang/prometheus"
)

func main() {
	config, err := pgxpool.ParseConfig(os.Getenv("PGX_DATABASE_URL"))
	if err != nil {
		panic(err)
	}

	pool, err := pgxpool.NewWithConfig(context.TODO(), config)
	if err != nil {
		panic(err)
	}

	collector := pgxprom.NewPoolStatsCollector()
	// register the pool
	collector.Register(pool)
	// register the collector
	if err := prometheus.Register(collector); err != nil {
		panic(err)
	}
}
Output:

func NewPoolStatsCollector

func NewPoolStatsCollector() *PoolStatsCollector

NewPoolStatsCollector returns a new collector.

func (*PoolStatsCollector) Collect

func (p *PoolStatsCollector) Collect(metrics chan<- prometheus.Metric)

Collect implements the prometheus.Collector interface.

func (*PoolStatsCollector) Describe

func (p *PoolStatsCollector) Describe(descs chan<- *prometheus.Desc)

Describe implements the prometheus.Collector interface.

func (*PoolStatsCollector) Register

func (p *PoolStatsCollector) Register(pool *pgxpool.Pool)

Register append the pool the collector

func (*PoolStatsCollector) Unregister

func (p *PoolStatsCollector) Unregister(pool *pgxpool.Pool)

Unregister removes the pool from the collector

type TraceBatchData

type TraceBatchData struct {
	StartedAt time.Time
}

TraceBatchData represents a batch data

type TraceQueryData

type TraceQueryData struct {
	StartedAt time.Time
	SQL       string
	Args      []any
}

TraceQueryData represents a query data

type Tracer

type Tracer struct{}

Tracer is a Prometheus query collector for pgx metrics.

Example
package main

import (
	"context"
	"os"

	"github.com/jackc/pgx/v5/pgxpool"
	"github.com/pgx-contrib/pgxprom"
)

var count int

func main() {
	config, err := pgxpool.ParseConfig(os.Getenv("PGX_DATABASE_URL"))
	if err != nil {
		panic(err)
	}

	config.ConnConfig.Tracer = &pgxprom.Tracer{}

	conn, err := pgxpool.NewWithConfig(context.TODO(), config)
	if err != nil {
		panic(err)
	}

	row := conn.QueryRow(context.TODO(), "SELECT 1")
	if err := row.Scan(&count); err != nil {
		panic(err)
	}
}
Output:

func (*Tracer) TraceBatchEnd

func (q *Tracer) TraceBatchEnd(ctx context.Context, conn *pgx.Conn, end pgx.TraceBatchEndData)

TraceBatchEnd implements pgx.BatchTracer.

func (*Tracer) TraceBatchQuery

func (q *Tracer) TraceBatchQuery(ctx context.Context, conn *pgx.Conn, data pgx.TraceBatchQueryData)

TraceBatchQuery implements pgx.BatchTracer.

func (*Tracer) TraceBatchStart

func (q *Tracer) TraceBatchStart(ctx context.Context, conn *pgx.Conn, start pgx.TraceBatchStartData) context.Context

TraceBatchStart implements pgx.BatchTracer.

func (*Tracer) TraceQueryEnd

func (q *Tracer) TraceQueryEnd(ctx context.Context, conn *pgx.Conn, end pgx.TraceQueryEndData)

TraceQueryEnd implements pgx.QueryTracer.

func (*Tracer) TraceQueryStart

func (q *Tracer) TraceQueryStart(ctx context.Context, conn *pgx.Conn, start pgx.TraceQueryStartData) context.Context

TraceQueryStart implements pgx.QueryTracer.

Jump to

Keyboard shortcuts

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