Skip to content

Instantly share code, notes, and snippets.

@omerkaya1
Created September 22, 2023 21:41
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save omerkaya1/610376f74a70eb0a715e00dcbd1ac3ae to your computer and use it in GitHub Desktop.
Save omerkaya1/610376f74a70eb0a715e00dcbd1ac3ae to your computer and use it in GitHub Desktop.
pgx conn pool metrics
package db
import (
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promauto"
)
const (
clientNamespace = "db_client"
querySubsystem = "query"
connPoolSubsystem = "conn_pool"
connSubsystem = "conn"
)
var (
labels = []string{
"method",
}
queryTime = promauto.NewHistogramVec(
prometheus.HistogramOpts{
Namespace: clientNamespace,
Subsystem: querySubsystem,
Name: "execution_time",
Help: "The query execution time",
},
labels,
)
acquireConnTime = promauto.NewHistogramVec(
prometheus.HistogramOpts{
Namespace: clientNamespace,
Subsystem: connSubsystem,
Name: "acquire_time",
Help: "The query execution time",
},
labels,
)
releaseConnTime = promauto.NewHistogramVec(
prometheus.HistogramOpts{
Namespace: clientNamespace,
Subsystem: connSubsystem,
Name: "release_time",
Help: "The query execution time",
},
labels,
)
)
// DB conn pool metrics.
var (
acquireCount = promauto.NewGauge(
prometheus.GaugeOpts{
Namespace: clientNamespace,
Subsystem: connPoolSubsystem,
Name: "acquire",
Help: "The cumulative count of successful acquires from the pool",
},
)
acquiredCount = promauto.NewGauge(
prometheus.GaugeOpts{
Namespace: clientNamespace,
Subsystem: connPoolSubsystem,
Name: "acquired",
Help: "The number of currently acquired connections in the pool",
},
)
canceledAcquireCount = promauto.NewGauge(
prometheus.GaugeOpts{
Namespace: clientNamespace,
Subsystem: connPoolSubsystem,
Name: "canceled_acquire_count",
Help: "The cumulative count of acquires from the pool that were canceled by a context",
},
)
constructingConns = promauto.NewGauge(
prometheus.GaugeOpts{
Namespace: clientNamespace,
Subsystem: connPoolSubsystem,
Name: "constructing",
Help: "The number of conns with construction in progress in the pool",
},
)
emptyAcquireCount = promauto.NewGauge(
prometheus.GaugeOpts{
Namespace: clientNamespace,
Subsystem: connPoolSubsystem,
Name: "empty_acquire_count",
Help: "The cumulative count of successful acquires from the pool that waited for a resource to be " +
"released or constructed because the pool was empty",
},
)
idleConns = promauto.NewGauge(
prometheus.GaugeOpts{
Namespace: clientNamespace,
Subsystem: connPoolSubsystem,
Name: "idle_conns",
Help: "The number of currently idle conns in the pool",
},
)
maxConns = promauto.NewGauge(
prometheus.GaugeOpts{
Namespace: clientNamespace,
Subsystem: connPoolSubsystem,
Name: "max_conns",
Help: "The maximum size of the pool",
},
)
newConnsCount = promauto.NewGauge(
prometheus.GaugeOpts{
Namespace: clientNamespace,
Subsystem: connPoolSubsystem,
Name: "new_conns_count",
Help: "The cumulative count of new connections opened",
},
)
maxLifetimeDestroyCount = promauto.NewGauge(
prometheus.GaugeOpts{
Namespace: clientNamespace,
Subsystem: connPoolSubsystem,
Name: "max_lifetime_destroy_count",
Help: "The cumulative count of connections destroyed because they exceeded MaxConnLifetime",
},
)
maxIdleDestroyCount = promauto.NewGauge(
prometheus.GaugeOpts{
Namespace: clientNamespace,
Subsystem: connPoolSubsystem,
Name: "max_idle_destroy_count",
Help: "The cumulative count of connections destroyed because they exceeded MaxConnIdleTime",
},
)
totalConns = promauto.NewGauge(
prometheus.GaugeOpts{
Namespace: clientNamespace,
Subsystem: connPoolSubsystem,
Name: "total_conns",
Help: "The total number of resources currently in the pool",
},
)
)
func queryTimeObserver(method string) prometheus.Observer {
return queryTime.WithLabelValues(method)
}
func acquireConnTimeObserver(method string) prometheus.Observer {
return acquireConnTime.WithLabelValues(method)
}
func releaseConnTimeObserver(method string) prometheus.Observer {
return releaseConnTime.WithLabelValues(method)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment