-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
The application now has a metrics handler compatible with prometheus. Besides default metrics, database pooling metrics are also shared at the moment.
- Loading branch information
1 parent
7d15265
commit 25cbcbf
Showing
4 changed files
with
96 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
package metric | ||
|
||
import ( | ||
"context" | ||
"github.com/jackc/pgx/v5/pgxpool" | ||
"github.com/prometheus/client_golang/prometheus" | ||
"github.com/prometheus/client_golang/prometheus/promauto" | ||
"time" | ||
) | ||
|
||
var dbConnAcquired = promauto.NewGauge(prometheus.GaugeOpts{ | ||
Name: "dbpool_acquired_connections", | ||
Help: "Number of connections currently acquired.", | ||
}) | ||
var dbConnIdle = promauto.NewGauge(prometheus.GaugeOpts{ | ||
Name: "dbpool_idle_connections", | ||
Help: "Number of idle connections currently idle.", | ||
}) | ||
var dbConnConstructing = promauto.NewGauge(prometheus.GaugeOpts{ | ||
Name: "dbpool_constructing_connections", | ||
Help: "Number of connections being constructed.", | ||
}) | ||
var dbMaxConns = promauto.NewGauge(prometheus.GaugeOpts{ | ||
Name: "dbpool_max_connections", | ||
Help: "Maximum amount of connections permitted.", | ||
}) | ||
|
||
func updateDbPool(pool *pgxpool.Pool) { | ||
stat := pool.Stat() | ||
dbConnAcquired.Set(float64(stat.AcquiredConns())) | ||
dbConnIdle.Set(float64(stat.IdleConns())) | ||
dbConnConstructing.Set(float64(stat.ConstructingConns())) | ||
dbMaxConns.Set(float64(stat.MaxConns())) | ||
} | ||
|
||
func WatchDbPool(ctx context.Context, pool *pgxpool.Pool, refresh time.Duration) { | ||
updateDbPool(pool) | ||
ticker := time.NewTicker(refresh) | ||
go func() { | ||
for { | ||
select { | ||
case <-ticker.C: | ||
updateDbPool(pool) | ||
case <-ctx.Done(): | ||
ticker.Stop() | ||
return | ||
} | ||
} | ||
}() | ||
} |