Skip to content

Commit ce74dae

Browse files
authored
Gitlab Collector: User Index io stats collector and test (prometheus-community#845)
* User Index io stats collector and test --------- Signed-off-by: Felix Yuan <[email protected]>
1 parent 2402783 commit ce74dae

File tree

2 files changed

+227
-0
lines changed

2 files changed

+227
-0
lines changed

collector/pg_statio_user_indexes.go

+118
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,118 @@
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(statioUserIndexesSubsystem, defaultDisabled, NewPGStatioUserIndexesCollector)
25+
}
26+
27+
type PGStatioUserIndexesCollector struct {
28+
log log.Logger
29+
}
30+
31+
const statioUserIndexesSubsystem = "statio_user_indexes"
32+
33+
func NewPGStatioUserIndexesCollector(config collectorConfig) (Collector, error) {
34+
return &PGStatioUserIndexesCollector{log: config.logger}, nil
35+
}
36+
37+
var (
38+
statioUserIndexesIdxBlksRead = prometheus.NewDesc(
39+
prometheus.BuildFQName(namespace, statioUserIndexesSubsystem, "idx_blks_read_total"),
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_total"),
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, instance *instance, ch chan<- prometheus.Metric) error {
63+
db := instance.getDB()
64+
rows, err := db.QueryContext(ctx,
65+
statioUserIndexesQuery)
66+
67+
if err != nil {
68+
return err
69+
}
70+
defer rows.Close()
71+
for rows.Next() {
72+
var schemaname, relname, indexrelname sql.NullString
73+
var idxBlksRead, idxBlksHit sql.NullFloat64
74+
75+
if err := rows.Scan(&schemaname, &relname, &indexrelname, &idxBlksRead, &idxBlksHit); err != nil {
76+
return err
77+
}
78+
schemanameLabel := "unknown"
79+
if schemaname.Valid {
80+
schemanameLabel = schemaname.String
81+
}
82+
relnameLabel := "unknown"
83+
if relname.Valid {
84+
relnameLabel = relname.String
85+
}
86+
indexrelnameLabel := "unknown"
87+
if indexrelname.Valid {
88+
indexrelnameLabel = indexrelname.String
89+
}
90+
labels := []string{schemanameLabel, relnameLabel, indexrelnameLabel}
91+
92+
idxBlksReadMetric := 0.0
93+
if idxBlksRead.Valid {
94+
idxBlksReadMetric = idxBlksRead.Float64
95+
}
96+
ch <- prometheus.MustNewConstMetric(
97+
statioUserIndexesIdxBlksRead,
98+
prometheus.CounterValue,
99+
idxBlksReadMetric,
100+
labels...,
101+
)
102+
103+
idxBlksHitMetric := 0.0
104+
if idxBlksHit.Valid {
105+
idxBlksHitMetric = idxBlksHit.Float64
106+
}
107+
ch <- prometheus.MustNewConstMetric(
108+
statioUserIndexesIdxBlksHit,
109+
prometheus.CounterValue,
110+
idxBlksHitMetric,
111+
labels...,
112+
)
113+
}
114+
if err := rows.Err(); err != nil {
115+
return err
116+
}
117+
return nil
118+
}
+109
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,109 @@
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+
inst := &instance{db: db}
32+
columns := []string{
33+
"schemaname",
34+
"relname",
35+
"indexrelname",
36+
"idx_blks_read",
37+
"idx_blks_hit",
38+
}
39+
rows := sqlmock.NewRows(columns).
40+
AddRow("public", "pgtest_accounts", "pgtest_accounts_pkey", 8, 9)
41+
42+
mock.ExpectQuery(sanitizeQuery(statioUserIndexesQuery)).WillReturnRows(rows)
43+
44+
ch := make(chan prometheus.Metric)
45+
go func() {
46+
defer close(ch)
47+
c := PGStatioUserIndexesCollector{}
48+
49+
if err := c.Update(context.Background(), inst, ch); err != nil {
50+
t.Errorf("Error calling PGStatioUserIndexesCollector.Update: %s", err)
51+
}
52+
}()
53+
expected := []MetricResult{
54+
{labels: labelMap{"schemaname": "public", "relname": "pgtest_accounts", "indexrelname": "pgtest_accounts_pkey"}, value: 8, metricType: dto.MetricType_COUNTER},
55+
{labels: labelMap{"schemaname": "public", "relname": "pgtest_accounts", "indexrelname": "pgtest_accounts_pkey"}, value: 9, metricType: dto.MetricType_COUNTER},
56+
}
57+
convey.Convey("Metrics comparison", t, func() {
58+
for _, expect := range expected {
59+
m := readMetric(<-ch)
60+
convey.So(expect, convey.ShouldResemble, m)
61+
}
62+
})
63+
if err := mock.ExpectationsWereMet(); err != nil {
64+
t.Errorf("there were unfulfilled exceptions: %s", err)
65+
}
66+
}
67+
68+
func TestPgStatioUserIndexesCollectorNull(t *testing.T) {
69+
db, mock, err := sqlmock.New()
70+
if err != nil {
71+
t.Fatalf("Error opening a stub db connection: %s", err)
72+
}
73+
defer db.Close()
74+
inst := &instance{db: db}
75+
columns := []string{
76+
"schemaname",
77+
"relname",
78+
"indexrelname",
79+
"idx_blks_read",
80+
"idx_blks_hit",
81+
}
82+
rows := sqlmock.NewRows(columns).
83+
AddRow(nil, nil, nil, nil, nil)
84+
85+
mock.ExpectQuery(sanitizeQuery(statioUserIndexesQuery)).WillReturnRows(rows)
86+
87+
ch := make(chan prometheus.Metric)
88+
go func() {
89+
defer close(ch)
90+
c := PGStatioUserIndexesCollector{}
91+
92+
if err := c.Update(context.Background(), inst, ch); err != nil {
93+
t.Errorf("Error calling PGStatioUserIndexesCollector.Update: %s", err)
94+
}
95+
}()
96+
expected := []MetricResult{
97+
{labels: labelMap{"schemaname": "unknown", "relname": "unknown", "indexrelname": "unknown"}, value: 0, metricType: dto.MetricType_COUNTER},
98+
{labels: labelMap{"schemaname": "unknown", "relname": "unknown", "indexrelname": "unknown"}, value: 0, metricType: dto.MetricType_COUNTER},
99+
}
100+
convey.Convey("Metrics comparison", t, func() {
101+
for _, expect := range expected {
102+
m := readMetric(<-ch)
103+
convey.So(expect, convey.ShouldResemble, m)
104+
}
105+
})
106+
if err := mock.ExpectationsWereMet(); err != nil {
107+
t.Errorf("there were unfulfilled exceptions: %s", err)
108+
}
109+
}

0 commit comments

Comments
 (0)