Skip to content

Commit f90f34d

Browse files
committed
Add pg statio user queries and fix a test name
Signed-off-by: Felix Yuan <felix.yuan@reddit.com>
1 parent db534b7 commit f90f34d

File tree

3 files changed

+161
-1
lines changed

3 files changed

+161
-1
lines changed

collector/pg_stat_user_indexes_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ import (
2222
"github.com/smartystreets/goconvey/convey"
2323
)
2424

25-
func TestPgUserIndexesCollector(t *testing.T) {
25+
func TestPgStatUserIndexesCollector(t *testing.T) {
2626
db, mock, err := sqlmock.New()
2727
if err != nil {
2828
t.Fatalf("Error opening a stub db connection: %s", err)
Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
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+
"database/sql"
18+
19+
"github.com/go-kit/log"
20+
"github.com/prometheus/client_golang/prometheus"
21+
)
22+
23+
func init() {
24+
registerCollector("replication", defaultEnabled, NewPGStatWalReceiverCollector)
25+
}
26+
27+
type PGStatioUserIndexesCollector struct {
28+
log log.Logger
29+
}
30+
31+
const statioUserIndexesSubsystem = "statio_user_indexes"
32+
33+
func NewPGStatioUserIndexesCollector(collectorConfig) (Collector, error) {
34+
return &PGStatioUserIndexesCollector{}, nil
35+
}
36+
37+
var (
38+
statioUserIndexesIdxBlksRead = prometheus.NewDesc(
39+
prometheus.BuildFQName(namespace, statioUserIndexesSubsystem, "idx_blks_read"),
40+
"Number of disk blocks read from this index",
41+
[]string{"schemaname", "relname", "indexrelname"},
42+
prometheus.Labels{},
43+
)
44+
statioUserIndexesIdxBlksHit = prometheus.NewDesc(
45+
prometheus.BuildFQName(namespace, statioUserIndexesSubsystem, "idx_blks_hit"),
46+
"Number of buffer hits in this index",
47+
[]string{"schemaname", "relname", "indexrelname"},
48+
prometheus.Labels{},
49+
)
50+
51+
statioUserIndexesQuery = `
52+
SELECT
53+
schemaname,
54+
relname,
55+
indexrelname,
56+
idx_blks_read,
57+
idx_blks_hit
58+
FROM pg_statio_user_indexes
59+
`
60+
)
61+
62+
func (c *PGStatioUserIndexesCollector) Update(ctx context.Context, db *sql.DB, ch chan<- prometheus.Metric) error {
63+
rows, err := db.QueryContext(ctx,
64+
statioUserIndexesQuery)
65+
66+
if err != nil {
67+
return err
68+
}
69+
defer rows.Close()
70+
for rows.Next() {
71+
var schemaname, relname, indexrelname string
72+
var idxBlksRead, idxBlksHit float64
73+
74+
if err := rows.Scan(&schemaname, &relname, &indexrelname, &idxBlksRead, &idxBlksHit); err != nil {
75+
return err
76+
}
77+
78+
ch <- prometheus.MustNewConstMetric(
79+
statioUserIndexesIdxBlksRead,
80+
prometheus.CounterValue,
81+
idxBlksRead,
82+
schemaname, relname, indexrelname,
83+
)
84+
ch <- prometheus.MustNewConstMetric(
85+
statioUserIndexesIdxBlksHit,
86+
prometheus.CounterValue,
87+
idxBlksHit,
88+
schemaname, relname, indexrelname,
89+
)
90+
}
91+
if err := rows.Err(); err != nil {
92+
return err
93+
}
94+
return nil
95+
}
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
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 TestPgStatioUserIndexesCollector(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+
columns := []string{
32+
"schemaname",
33+
"relname",
34+
"indexrelname",
35+
"idx_blks_read",
36+
"idx_blks_hit",
37+
}
38+
rows := sqlmock.NewRows(columns).
39+
AddRow("public", "pgtest_accounts", "pgtest_accounts_pkey", 8, 9)
40+
41+
mock.ExpectQuery(sanitizeQuery(statioUserIndexesQuery)).WillReturnRows(rows)
42+
43+
ch := make(chan prometheus.Metric)
44+
go func() {
45+
defer close(ch)
46+
c := PGStatioUserIndexesCollector{}
47+
48+
if err := c.Update(context.Background(), db, ch); err != nil {
49+
t.Errorf("Error calling PGStatioUserIndexesCollector.Update: %s", err)
50+
}
51+
}()
52+
expected := []MetricResult{
53+
{labels: labelMap{"schemaname": "public", "relname": "pgtest_accounts", "indexrelname": "pgtest_accounts_pkey"}, value: 8, metricType: dto.MetricType_COUNTER},
54+
{labels: labelMap{"schemaname": "public", "relname": "pgtest_accounts", "indexrelname": "pgtest_accounts_pkey"}, value: 9, metricType: dto.MetricType_COUNTER},
55+
}
56+
convey.Convey("Metrics comparison", t, func() {
57+
for _, expect := range expected {
58+
m := readMetric(<-ch)
59+
convey.So(expect, convey.ShouldResemble, m)
60+
}
61+
})
62+
if err := mock.ExpectationsWereMet(); err != nil {
63+
t.Errorf("there were unfulfilled exceptions: %s", err)
64+
}
65+
}

0 commit comments

Comments
 (0)