Skip to content

Commit 5890879

Browse files
SticksmanSuperQ
andauthored
Gitlab Collector: Long running transactions collector and test (prometheus-community#836)
* Long running transactions collector and test --------- Signed-off-by: Felix Yuan <[email protected]> Co-authored-by: Ben Kochie <[email protected]>
1 parent ce4ee05 commit 5890879

File tree

2 files changed

+156
-0
lines changed

2 files changed

+156
-0
lines changed
+93
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
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 longRunningTransactionsSubsystem = "long_running_transactions"
24+
25+
func init() {
26+
registerCollector(longRunningTransactionsSubsystem, defaultDisabled, NewPGLongRunningTransactionsCollector)
27+
}
28+
29+
type PGLongRunningTransactionsCollector struct {
30+
log log.Logger
31+
}
32+
33+
func NewPGLongRunningTransactionsCollector(config collectorConfig) (Collector, error) {
34+
return &PGLongRunningTransactionsCollector{log: config.logger}, nil
35+
}
36+
37+
var (
38+
longRunningTransactionsCount = prometheus.NewDesc(
39+
"pg_long_running_transactions",
40+
"Current number of long running transactions",
41+
[]string{},
42+
prometheus.Labels{},
43+
)
44+
45+
longRunningTransactionsAgeInSeconds = prometheus.NewDesc(
46+
prometheus.BuildFQName(namespace, longRunningTransactionsSubsystem, "oldest_timestamp_seconds"),
47+
"The current maximum transaction age in seconds",
48+
[]string{},
49+
prometheus.Labels{},
50+
)
51+
52+
longRunningTransactionsQuery = `
53+
SELECT
54+
COUNT(*) as transactions,
55+
MAX(EXTRACT(EPOCH FROM clock_timestamp())) AS oldest_timestamp_seconds
56+
FROM pg_catalog.pg_stat_activity
57+
WHERE state is distinct from 'idle' AND query not like 'autovacuum:%'
58+
`
59+
)
60+
61+
func (PGLongRunningTransactionsCollector) Update(ctx context.Context, instance *instance, ch chan<- prometheus.Metric) error {
62+
db := instance.getDB()
63+
rows, err := db.QueryContext(ctx,
64+
longRunningTransactionsQuery)
65+
66+
if err != nil {
67+
return err
68+
}
69+
defer rows.Close()
70+
71+
for rows.Next() {
72+
var transactions, ageInSeconds float64
73+
74+
if err := rows.Scan(&transactions, &ageInSeconds); err != nil {
75+
return err
76+
}
77+
78+
ch <- prometheus.MustNewConstMetric(
79+
longRunningTransactionsCount,
80+
prometheus.GaugeValue,
81+
transactions,
82+
)
83+
ch <- prometheus.MustNewConstMetric(
84+
longRunningTransactionsAgeInSeconds,
85+
prometheus.GaugeValue,
86+
ageInSeconds,
87+
)
88+
}
89+
if err := rows.Err(); err != nil {
90+
return err
91+
}
92+
return nil
93+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
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 TestPGLongRunningTransactionsCollector(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+
"transactions",
34+
"age_in_seconds",
35+
}
36+
rows := sqlmock.NewRows(columns).
37+
AddRow(20, 1200)
38+
39+
mock.ExpectQuery(sanitizeQuery(longRunningTransactionsQuery)).WillReturnRows(rows)
40+
41+
ch := make(chan prometheus.Metric)
42+
go func() {
43+
defer close(ch)
44+
c := PGLongRunningTransactionsCollector{}
45+
46+
if err := c.Update(context.Background(), inst, ch); err != nil {
47+
t.Errorf("Error calling PGLongRunningTransactionsCollector.Update: %s", err)
48+
}
49+
}()
50+
expected := []MetricResult{
51+
{labels: labelMap{}, value: 20, metricType: dto.MetricType_GAUGE},
52+
{labels: labelMap{}, value: 1200, metricType: dto.MetricType_GAUGE},
53+
}
54+
convey.Convey("Metrics comparison", t, func() {
55+
for _, expect := range expected {
56+
m := readMetric(<-ch)
57+
convey.So(expect, convey.ShouldResemble, m)
58+
}
59+
})
60+
if err := mock.ExpectationsWereMet(); err != nil {
61+
t.Errorf("there were unfulfilled exceptions: %s", err)
62+
}
63+
}

0 commit comments

Comments
 (0)