Expose metrics from pg_stats

This commit is contained in:
Christopher Baines 2022-11-29 11:09:55 +00:00
parent 9f05f5f4f9
commit 7b69611755
2 changed files with 56 additions and 1 deletions

View file

@ -20,7 +20,8 @@
#:use-module (squee)
#:export (fetch-high-level-table-size-metrics
fetch-pg-stat-user-tables-metrics
fetch-pg-stat-user-indexes-metrics))
fetch-pg-stat-user-indexes-metrics
fetch-pg-stats-metrics))
(define (fetch-high-level-table-size-metrics conn)
;; Adapted from https://wiki.postgresql.org/wiki/Disk_Usage
@ -155,3 +156,20 @@ WHERE pg_stat_user_indexes.schemaname = 'guix_data_service'")
(idx-tup-fetch . ,idx_tup_fetch)
(bytes . ,size_in_bytes))))
(exec-query conn query)))
(define (fetch-pg-stats-metrics conn)
(define query
"
SELECT tablename, attname, null_frac, n_distinct, correlation
FROM pg_stats
WHERE schemaname = 'guix_data_service'")
(map
(match-lambda
((tablename attname null_frac n_distinct correlation)
`((table-name . ,tablename)
(column-name . ,attname)
(null-frac . ,(string->number null_frac))
(n-distinct . ,(string->number n_distinct))
(correlation . ,(and=> correlation string->number)))))
(exec-query conn query)))

View file

@ -151,6 +151,22 @@
#:labels '(name))))
pg-stat-indexes-fields))
(pg-stats-fields '(null-frac n-distinct correlation))
(pg-stats-metrics (map (lambda (field)
(cons
field
(make-gauge-metric
registry
(string-append
"pg_stats_"
(string-replace-substring
(symbol->string field)
"-"
"_"))
#:labels '(table column))))
pg-stats-fields))
(gc-metrics-updater
(get-gc-metrics-updater registry)))
@ -167,6 +183,9 @@
(pg-stat-user-indexes-metrics
(with-thread-postgresql-connection
fetch-pg-stat-user-indexes-metrics))
(pg-stats-metric-values
(with-thread-postgresql-connection
fetch-pg-stats-metrics))
(load-new-guix-revision-job-metrics
(with-thread-postgresql-connection
select-load-new-guix-revision-job-metrics)))
@ -228,6 +247,24 @@
field-values)))
pg-stat-user-indexes-metrics)
(map (lambda (field-values)
(let ((table (assq-ref field-values 'table-name))
(column (assq-ref field-values 'column-name)))
(for-each
(match-lambda
(('table-name . _) #f)
(('column-name . _) #f)
((_ . #f) #f)
((field . value)
(let ((metric (or (assq-ref pg-stats-metrics field)
(error field))))
(metric-set metric
value
#:label-values `((table . ,table)
(column . ,column))))))
field-values)))
pg-stats-metric-values)
(for-each (match-lambda
((repository-label state count)
(metric-set