Skip to content

Commit dc3e813

Browse files
Sticksmansysadmind
andauthored
Gitlab Collector: Autovacuum collector and test (#840)
* Autovacuum collector and test Signed-off-by: Felix Yuan <[email protected]> * Update collector/pg_stat_activity_autovacuum.go Co-authored-by: Joe Adams <[email protected]> Signed-off-by: Felix Yuan <[email protected]> * Update collector/pg_stat_activity_autovacuum.go Co-authored-by: Joe Adams <[email protected]> Signed-off-by: Felix Yuan <[email protected]> * Use timestamp seconds Signed-off-by: Felix Yuan <[email protected]> * query formating Signed-off-by: Felix Yuan <[email protected]> * SQL format Signed-off-by: Felix Yuan <[email protected]> * Loosen autovacuum query Signed-off-by: Felix Yuan <[email protected]> --------- Signed-off-by: Felix Yuan <[email protected]> Co-authored-by: Joe Adams <[email protected]>
1 parent 24a45f2 commit dc3e813

File tree

2 files changed

+146
-0
lines changed

2 files changed

+146
-0
lines changed

Diff for: collector/pg_stat_activity_autovacuum.go

+84
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
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+
19+
"github.com/go-kit/log"
20+
"github.com/prometheus/client_golang/prometheus"
21+
)
22+
23+
const statActivityAutovacuumSubsystem = "stat_activity_autovacuum"
24+
25+
func init() {
26+
registerCollector(statActivityAutovacuumSubsystem, defaultDisabled, NewPGStatActivityAutovacuumCollector)
27+
}
28+
29+
type PGStatActivityAutovacuumCollector struct {
30+
log log.Logger
31+
}
32+
33+
func NewPGStatActivityAutovacuumCollector(config collectorConfig) (Collector, error) {
34+
return &PGStatActivityAutovacuumCollector{log: config.logger}, nil
35+
}
36+
37+
var (
38+
statActivityAutovacuumAgeInSeconds = prometheus.NewDesc(
39+
prometheus.BuildFQName(namespace, statActivityAutovacuumSubsystem, "timestamp_seconds"),
40+
"Start timestamp of the vacuum process in seconds",
41+
[]string{"relname"},
42+
prometheus.Labels{},
43+
)
44+
45+
statActivityAutovacuumQuery = `
46+
SELECT
47+
SPLIT_PART(query, '.', 2) AS relname,
48+
EXTRACT(xact_start) AS timestamp_seconds
49+
FROM
50+
pg_catalog.pg_stat_activity
51+
WHERE
52+
query LIKE 'autovacuum:%'
53+
`
54+
)
55+
56+
func (PGStatActivityAutovacuumCollector) Update(ctx context.Context, instance *instance, ch chan<- prometheus.Metric) error {
57+
db := instance.getDB()
58+
rows, err := db.QueryContext(ctx,
59+
statActivityAutovacuumQuery)
60+
61+
if err != nil {
62+
return err
63+
}
64+
defer rows.Close()
65+
66+
for rows.Next() {
67+
var relname string
68+
var ageInSeconds float64
69+
70+
if err := rows.Scan(&relname, &ageInSeconds); err != nil {
71+
return err
72+
}
73+
74+
ch <- prometheus.MustNewConstMetric(
75+
statActivityAutovacuumAgeInSeconds,
76+
prometheus.GaugeValue,
77+
ageInSeconds, relname,
78+
)
79+
}
80+
if err := rows.Err(); err != nil {
81+
return err
82+
}
83+
return nil
84+
}

Diff for: collector/pg_stat_activity_autovacuum_test.go

+62
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
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 TestPGStatActivityAutovacuumCollector(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+
"relname",
34+
"timestamp_seconds",
35+
}
36+
rows := sqlmock.NewRows(columns).
37+
AddRow("test", 3600)
38+
39+
mock.ExpectQuery(sanitizeQuery(statActivityAutovacuumQuery)).WillReturnRows(rows)
40+
41+
ch := make(chan prometheus.Metric)
42+
go func() {
43+
defer close(ch)
44+
c := PGStatActivityAutovacuumCollector{}
45+
46+
if err := c.Update(context.Background(), inst, ch); err != nil {
47+
t.Errorf("Error calling PGStatActivityAutovacuumCollector.Update: %s", err)
48+
}
49+
}()
50+
expected := []MetricResult{
51+
{labels: labelMap{"relname": "test"}, value: 3600, metricType: dto.MetricType_GAUGE},
52+
}
53+
convey.Convey("Metrics comparison", t, func() {
54+
for _, expect := range expected {
55+
m := readMetric(<-ch)
56+
convey.So(expect, convey.ShouldResemble, m)
57+
}
58+
})
59+
if err := mock.ExpectationsWereMet(); err != nil {
60+
t.Errorf("there were unfulfilled exceptions: %s", err)
61+
}
62+
}

0 commit comments

Comments
 (0)