Skip to content
This repository was archived by the owner on Nov 28, 2022. It is now read-only.

Commit 4e7054b

Browse files
Merge pull request #25 from ContaAzul/new-metrics
New Metric: Not granted locks
2 parents 59c9347 + 48bb294 commit 4e7054b

File tree

2 files changed

+33
-1
lines changed

2 files changed

+33
-1
lines changed

gauges/locks.go

+23-1
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,12 @@ type lockCountWithMode struct {
1111
Count float64 `db:"count"`
1212
}
1313

14+
// Locks returns the number of locks by mode
1415
func (g *Gauges) Locks() *prometheus.GaugeVec {
1516
var gauge = prometheus.NewGaugeVec(
1617
prometheus.GaugeOpts{
1718
Name: "postgresql_lock_count",
18-
Help: "count of locks by mode",
19+
Help: "Number of locks by mode",
1920
ConstLabels: g.labels,
2021
},
2122
[]string{"mode"},
@@ -47,3 +48,24 @@ func (g *Gauges) Locks() *prometheus.GaugeVec {
4748
}()
4849
return gauge
4950
}
51+
52+
// NotGrantedLocks returns the number of not granted locks
53+
func (g *Gauges) NotGrantedLocks() prometheus.Gauge {
54+
return g.new(
55+
prometheus.GaugeOpts{
56+
Name: "postgresql_not_granted_locks",
57+
Help: "Number of not granted locks",
58+
ConstLabels: g.labels,
59+
},
60+
`
61+
SELECT count(*) as count
62+
FROM pg_locks
63+
WHERE NOT granted
64+
AND database = (
65+
SELECT datid
66+
FROM pg_stat_database
67+
WHERE datname = current_database()
68+
);
69+
`,
70+
)
71+
}

gauges/locks_test.go

+10
Original file line numberDiff line numberDiff line change
@@ -15,3 +15,13 @@ func TestLocks(t *testing.T) {
1515
assertGreaterThan(t, -1, metrics[0])
1616
assertNoErrs(t, gauges)
1717
}
18+
19+
func TestNotGrantedLocks(t *testing.T) {
20+
var assert = assert.New(t)
21+
_, gauges, close := prepare(t)
22+
defer close()
23+
var metrics = evaluate(t, gauges.NotGrantedLocks())
24+
assert.Len(metrics, 1)
25+
assertGreaterThan(t, -1, metrics[0])
26+
assertNoErrs(t, gauges)
27+
}

0 commit comments

Comments
 (0)