-
Notifications
You must be signed in to change notification settings - Fork 1
/
example.go
35 lines (28 loc) · 834 Bytes
/
example.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
package main
import (
"context"
"log"
"net/http"
"github.com/cmackenzie1/pgxpool-prometheus"
"github.com/jackc/pgx/v5/pgxpool"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promhttp"
)
func main() {
// Create a pgxpool.Config
config, err := pgxpool.ParseConfig("postgres://postgres:password@localhost:5432/?sslmode=disable")
if err != nil {
panic(err)
}
// Create a pgxpool.Pool
pool, err := pgxpool.NewWithConfig(context.Background(), config)
if err != nil {
panic(err)
}
defer pool.Close()
prometheus.MustRegister(pgxpool_prometheus.NewPgxPoolStatsCollector(pool, "database"))
if err := pool.QueryRow(context.Background(), "SELECT 1").Scan(nil); err != nil {
panic(err)
}
log.Fatalf("Error: %v", http.ListenAndServe(":8080", promhttp.Handler()))
}