Support instrumenting the number of database connections
Since this is now quite dynamic, it's useful to have a metric for it.
This commit is contained in:
parent
926cb2a5e1
commit
05c437d26a
1 changed files with 50 additions and 5 deletions
|
|
@ -1,5 +1,5 @@
|
||||||
;;; Guix Data Service -- Information about Guix over time
|
;;; Guix Data Service -- Information about Guix over time
|
||||||
;;; Copyright © 2019 Christopher Baines <mail@cbaines.net>
|
;;; Copyright © 2019, 2020, 2021, 2022, 2023 Christopher Baines <mail@cbaines.net>
|
||||||
;;;
|
;;;
|
||||||
;;; This program is free software: you can redistribute it and/or
|
;;; This program is free software: you can redistribute it and/or
|
||||||
;;; modify it under the terms of the GNU Affero General Public License
|
;;; modify it under the terms of the GNU Affero General Public License
|
||||||
|
|
@ -20,8 +20,10 @@
|
||||||
#:use-module (ice-9 match)
|
#:use-module (ice-9 match)
|
||||||
#:use-module (ice-9 threads)
|
#:use-module (ice-9 threads)
|
||||||
#:use-module (squee)
|
#:use-module (squee)
|
||||||
|
#:use-module (prometheus)
|
||||||
#:use-module (guix-data-service config)
|
#:use-module (guix-data-service config)
|
||||||
#:export (get-database-config
|
#:export (get-database-config
|
||||||
|
%database-metrics-registry
|
||||||
|
|
||||||
with-postgresql-connection
|
with-postgresql-connection
|
||||||
|
|
||||||
|
|
@ -42,6 +44,33 @@
|
||||||
(define pg-conn-finish
|
(define pg-conn-finish
|
||||||
(@@ (squee) pg-conn-finish))
|
(@@ (squee) pg-conn-finish))
|
||||||
|
|
||||||
|
(define %database-metrics-registry
|
||||||
|
(make-parameter #f))
|
||||||
|
|
||||||
|
(define (with-connection-gauge-metric proc)
|
||||||
|
(and=>
|
||||||
|
(%database-metrics-registry)
|
||||||
|
(lambda (registry)
|
||||||
|
(proc
|
||||||
|
(or (metrics-registry-fetch-metric registry "postgresql_connections_total")
|
||||||
|
(make-gauge-metric registry
|
||||||
|
"postgresql_connections_total"
|
||||||
|
#:labels '(name)))))))
|
||||||
|
|
||||||
|
(define (increment-connection-gauge name)
|
||||||
|
(with-connection-gauge-metric
|
||||||
|
(lambda (connection-gauge-metric)
|
||||||
|
(metric-increment connection-gauge-metric
|
||||||
|
#:label-values
|
||||||
|
`((name . ,name))))))
|
||||||
|
|
||||||
|
(define (decrement-connection-gauge name)
|
||||||
|
(with-connection-gauge-metric
|
||||||
|
(lambda (connection-gauge-metric)
|
||||||
|
(metric-decrement connection-gauge-metric
|
||||||
|
#:label-values
|
||||||
|
`((name . ,name))))))
|
||||||
|
|
||||||
(define (paramstring->alist s)
|
(define (paramstring->alist s)
|
||||||
(map
|
(map
|
||||||
(lambda (param)
|
(lambda (param)
|
||||||
|
|
@ -77,6 +106,8 @@
|
||||||
(simple-format #f "SET statement_timeout = ~A"
|
(simple-format #f "SET statement_timeout = ~A"
|
||||||
statement-timeout)))
|
statement-timeout)))
|
||||||
|
|
||||||
|
(increment-connection-gauge name)
|
||||||
|
|
||||||
conn))
|
conn))
|
||||||
|
|
||||||
(define* (with-postgresql-connection name f #:key (statement-timeout #f))
|
(define* (with-postgresql-connection name f #:key (statement-timeout #f))
|
||||||
|
|
@ -89,9 +120,13 @@
|
||||||
(f conn))
|
(f conn))
|
||||||
(lambda vals
|
(lambda vals
|
||||||
(pg-conn-finish conn)
|
(pg-conn-finish conn)
|
||||||
|
|
||||||
|
(decrement-connection-gauge name)
|
||||||
|
|
||||||
(apply values vals))))
|
(apply values vals))))
|
||||||
(lambda (key . args)
|
(lambda (key . args)
|
||||||
(pg-conn-finish conn)))))
|
(pg-conn-finish conn)
|
||||||
|
(decrement-connection-gauge name)))))
|
||||||
|
|
||||||
(define %postgresql-connection-parameters
|
(define %postgresql-connection-parameters
|
||||||
(make-parameter #f))
|
(make-parameter #f))
|
||||||
|
|
@ -99,18 +134,24 @@
|
||||||
(define %postgresql-connections-hash-table
|
(define %postgresql-connections-hash-table
|
||||||
(make-parameter #f))
|
(make-parameter #f))
|
||||||
|
|
||||||
|
(define %postgresql-connections-name
|
||||||
|
(make-parameter #f))
|
||||||
|
|
||||||
(define* (with-postgresql-connection-per-thread name thunk
|
(define* (with-postgresql-connection-per-thread name thunk
|
||||||
#:key (statement-timeout #f))
|
#:key (statement-timeout #f))
|
||||||
(parameterize ((%postgresql-connection-parameters
|
(parameterize ((%postgresql-connection-parameters
|
||||||
(list name statement-timeout))
|
(list name statement-timeout))
|
||||||
(%postgresql-connections-hash-table
|
(%postgresql-connections-hash-table
|
||||||
(make-hash-table)))
|
(make-hash-table))
|
||||||
|
(%postgresql-connections-name
|
||||||
|
name))
|
||||||
(call-with-values
|
(call-with-values
|
||||||
thunk
|
thunk
|
||||||
(lambda vals
|
(lambda vals
|
||||||
(hash-for-each
|
(hash-for-each
|
||||||
(lambda (thread conn)
|
(lambda (thread conn)
|
||||||
(pg-conn-finish conn))
|
(pg-conn-finish conn)
|
||||||
|
(decrement-connection-gauge name))
|
||||||
(%postgresql-connections-hash-table))
|
(%postgresql-connections-hash-table))
|
||||||
|
|
||||||
(apply values vals)))))
|
(apply values vals)))))
|
||||||
|
|
@ -136,6 +177,8 @@
|
||||||
(with-exception-handler
|
(with-exception-handler
|
||||||
(lambda (exn)
|
(lambda (exn)
|
||||||
(pg-conn-finish conn)
|
(pg-conn-finish conn)
|
||||||
|
(decrement-connection-gauge
|
||||||
|
(%postgresql-connections-name))
|
||||||
(set-current-thread-connection #f)
|
(set-current-thread-connection #f)
|
||||||
(raise-exception exn))
|
(raise-exception exn))
|
||||||
(lambda ()
|
(lambda ()
|
||||||
|
|
@ -153,7 +196,9 @@
|
||||||
(pg-conn-finish conn)
|
(pg-conn-finish conn)
|
||||||
(hash-remove! (%postgresql-connections-hash-table)
|
(hash-remove! (%postgresql-connections-hash-table)
|
||||||
(current-thread))
|
(current-thread))
|
||||||
(fluid-set! %thread-postgresql-connection #f))))
|
(fluid-set! %thread-postgresql-connection #f)
|
||||||
|
(decrement-connection-gauge
|
||||||
|
(%postgresql-connections-name)))))
|
||||||
|
|
||||||
(define* (with-postgresql-transaction conn f
|
(define* (with-postgresql-transaction conn f
|
||||||
#:key always-rollback?)
|
#:key always-rollback?)
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue