Skip to content

Commit 4aa8cd4

Browse files
Sticksmansysadmind
andauthored
Gitlab collector: Database wraparound collector and test (#834)
* Database wraparound collector and test --------- Signed-off-by: Felix Yuan <felix.yuan@reddit.com> Co-authored-by: Joe Adams <github@joeadams.io>
1 parent 4ac5481 commit 4aa8cd4

File tree

2 files changed

+179
-0
lines changed

2 files changed

+179
-0
lines changed

collector/pg_database_wraparound.go

+115
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,115 @@
1+
// Copyright 2023 The Prometheus Authors
2+
// Licensed under the Apache License, Version 2.0 (the "License");
3+
// you may not use this file except in compliance with the License.
4+
// You may obtain a copy of the License at
5+
//
6+
// http://www.apache.org/licenses/LICENSE-2.0
7+
//
8+
// Unless required by applicable law or agreed to in writing, software
9+
// distributed under the License is distributed on an "AS IS" BASIS,
10+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11+
// See the License for the specific language governing permissions and
12+
// limitations under the License.
13+
14+
package collector
15+
16+
import (
17+
"context"
18+
"database/sql"
19+
20+
"github.com/go-kit/log"
21+
"github.com/go-kit/log/level"
22+
"github.com/prometheus/client_golang/prometheus"
23+
)
24+
25+
const databaseWraparoundSubsystem = "database_wraparound"
26+
27+
func init() {
28+
registerCollector(databaseWraparoundSubsystem, defaultDisabled, NewPGDatabaseWraparoundCollector)
29+
}
30+
31+
type PGDatabaseWraparoundCollector struct {
32+
log log.Logger
33+
}
34+
35+
func NewPGDatabaseWraparoundCollector(config collectorConfig) (Collector, error) {
36+
return &PGDatabaseWraparoundCollector{log: config.logger}, nil
37+
}
38+
39+
var (
40+
databaseWraparoundAgeDatfrozenxid = prometheus.NewDesc(
41+
prometheus.BuildFQName(namespace, databaseWraparoundSubsystem, "age_datfrozenxid_seconds"),
42+
"Age of the oldest transaction ID that has not been frozen.",
43+
[]string{"datname"},
44+
prometheus.Labels{},
45+
)
46+
databaseWraparoundAgeDatminmxid = prometheus.NewDesc(
47+
prometheus.BuildFQName(namespace, databaseWraparoundSubsystem, "age_datminmxid_seconds"),
48+
"Age of the oldest multi-transaction ID that has been replaced with a transaction ID.",
49+
[]string{"datname"},
50+
prometheus.Labels{},
51+
)
52+
53+
databaseWraparoundQuery = `
54+
SELECT
55+
datname,
56+
age(d.datfrozenxid) as age_datfrozenxid,
57+
mxid_age(d.datminmxid) as age_datminmxid
58+
FROM
59+
pg_catalog.pg_database d
60+
WHERE
61+
d.datallowconn
62+
`
63+
)
64+
65+
func (c *PGDatabaseWraparoundCollector) Update(ctx context.Context, instance *instance, ch chan<- prometheus.Metric) error {
66+
db := instance.getDB()
67+
rows, err := db.QueryContext(ctx,
68+
databaseWraparoundQuery)
69+
70+
if err != nil {
71+
return err
72+
}
73+
defer rows.Close()
74+
75+
for rows.Next() {
76+
var datname sql.NullString
77+
var ageDatfrozenxid, ageDatminmxid sql.NullFloat64
78+
79+
if err := rows.Scan(&datname, &ageDatfrozenxid, &ageDatminmxid); err != nil {
80+
return err
81+
}
82+
83+
if !datname.Valid {
84+
level.Debug(c.log).Log("msg", "Skipping database with NULL name")
85+
continue
86+
}
87+
if !ageDatfrozenxid.Valid {
88+
level.Debug(c.log).Log("msg", "Skipping stat emission with NULL age_datfrozenxid")
89+
continue
90+
}
91+
if !ageDatminmxid.Valid {
92+
level.Debug(c.log).Log("msg", "Skipping stat emission with NULL age_datminmxid")
93+
continue
94+
}
95+
96+
ageDatfrozenxidMetric := ageDatfrozenxid.Float64
97+
98+
ch <- prometheus.MustNewConstMetric(
99+
databaseWraparoundAgeDatfrozenxid,
100+
prometheus.GaugeValue,
101+
ageDatfrozenxidMetric, datname.String,
102+
)
103+
104+
ageDatminmxidMetric := ageDatminmxid.Float64
105+
ch <- prometheus.MustNewConstMetric(
106+
databaseWraparoundAgeDatminmxid,
107+
prometheus.GaugeValue,
108+
ageDatminmxidMetric, datname.String,
109+
)
110+
}
111+
if err := rows.Err(); err != nil {
112+
return err
113+
}
114+
return nil
115+
}
+64
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
// Copyright 2023 The Prometheus Authors
2+
// Licensed under the Apache License, Version 2.0 (the "License");
3+
// you may not use this file except in compliance with the License.
4+
// You may obtain a copy of the License at
5+
//
6+
// http://www.apache.org/licenses/LICENSE-2.0
7+
//
8+
// Unless required by applicable law or agreed to in writing, software
9+
// distributed under the License is distributed on an "AS IS" BASIS,
10+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11+
// See the License for the specific language governing permissions and
12+
// limitations under the License.
13+
package collector
14+
15+
import (
16+
"context"
17+
"testing"
18+
19+
"github.com/DATA-DOG/go-sqlmock"
20+
"github.com/prometheus/client_golang/prometheus"
21+
dto "github.com/prometheus/client_model/go"
22+
"github.com/smartystreets/goconvey/convey"
23+
)
24+
25+
func TestPGDatabaseWraparoundCollector(t *testing.T) {
26+
db, mock, err := sqlmock.New()
27+
if err != nil {
28+
t.Fatalf("Error opening a stub db connection: %s", err)
29+
}
30+
defer db.Close()
31+
inst := &instance{db: db}
32+
columns := []string{
33+
"datname",
34+
"age_datfrozenxid",
35+
"age_datminmxid",
36+
}
37+
rows := sqlmock.NewRows(columns).
38+
AddRow("newreddit", 87126426, 0)
39+
40+
mock.ExpectQuery(sanitizeQuery(databaseWraparoundQuery)).WillReturnRows(rows)
41+
42+
ch := make(chan prometheus.Metric)
43+
go func() {
44+
defer close(ch)
45+
c := PGDatabaseWraparoundCollector{}
46+
47+
if err := c.Update(context.Background(), inst, ch); err != nil {
48+
t.Errorf("Error calling PGDatabaseWraparoundCollector.Update: %s", err)
49+
}
50+
}()
51+
expected := []MetricResult{
52+
{labels: labelMap{"datname": "newreddit"}, value: 87126426, metricType: dto.MetricType_GAUGE},
53+
{labels: labelMap{"datname": "newreddit"}, value: 0, metricType: dto.MetricType_GAUGE},
54+
}
55+
convey.Convey("Metrics comparison", t, func() {
56+
for _, expect := range expected {
57+
m := readMetric(<-ch)
58+
convey.So(expect, convey.ShouldResemble, m)
59+
}
60+
})
61+
if err := mock.ExpectationsWereMet(); err != nil {
62+
t.Errorf("there were unfulfilled exceptions: %s", err)
63+
}
64+
}

0 commit comments

Comments
 (0)