Skip to content

Commit 35e93f3

Browse files
author
Adrien ZAGABE
committed
feat: add label
1 parent b23d04f commit 35e93f3

File tree

1 file changed

+29
-7
lines changed

1 file changed

+29
-7
lines changed

collector/pg_top_queries_execution.go

Lines changed: 29 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -26,27 +26,27 @@ var (
2626
statTotalExecutionTime = prometheus.NewDesc(
2727
prometheus.BuildFQName(namespace, statTopQueriesExecutionTime, "total_seconds"),
2828
"Total time spent in the statement, in milliseconds",
29-
[]string{"queryid"},
29+
[]string{"queryid", "query", "user"},
3030
prometheus.Labels{},
3131
)
3232
statMinimumExecutionTime = prometheus.NewDesc(
3333
prometheus.BuildFQName(namespace, statTopQueriesExecutionTime, "min_time_seconds"),
3434
"Minimum time spent in the statement, in milliseconds",
35-
[]string{"queryid"},
35+
[]string{"queryid", "query", "user"},
3636
prometheus.Labels{},
3737
)
3838

3939
statMaximumExecutionTime = prometheus.NewDesc(
4040
prometheus.BuildFQName(namespace, statTopQueriesExecutionTime, "max_time_seconds"),
4141
"Maximum time spent in the statement, in milliseconds",
42-
[]string{"queryid"},
42+
[]string{"queryid", "query", "user"},
4343
prometheus.Labels{},
4444
)
4545

4646
statMeanExecutionTime = prometheus.NewDesc(
4747
prometheus.BuildFQName(namespace, statTopQueriesExecutionTime, "mean_time"),
4848
"Mean time spent in the statement, in milliseconds",
49-
[]string{"queryid"},
49+
[]string{"queryid", "query", "user"},
5050
prometheus.Labels{},
5151
)
5252

@@ -59,14 +59,16 @@ var (
5959

6060
statTopQueryExecutionQuery = `SELECT
6161
queryid,
62+
query
63+
pg_get_userbyid(userid) as user,
6264
mean_time / 1000.0 as mean_time,
6365
total_time / 1000.0 as total_seconds,
6466
min_time / 1000.0 as min_time_seconds,
6567
max_time / 1000.0 as max_time_seconds,
6668
calls
6769
FROM pg_stat_statements
6870
ORDER BY total_seconds DESC
69-
LIMIT 1;`
71+
LIMIT 10;`
7072
)
7173

7274
func (PGStatTopQueriesExecutionTime) Update(ctx context.Context, instance *instance, ch chan<- prometheus.Metric) error {
@@ -79,11 +81,11 @@ func (PGStatTopQueriesExecutionTime) Update(ctx context.Context, instance *insta
7981
defer rows.Close()
8082

8183
for rows.Next() {
82-
var queryid sql.NullString
84+
var queryid, query, user sql.NullString
8385
var meanTime, totalSeconds, minTimeSeconds, maxTimeSeconds sql.NullFloat64
8486
var calls sql.NullInt64
8587

86-
if err := rows.Scan(&queryid, &meanTime, &totalSeconds, &minTimeSeconds, &maxTimeSeconds, &calls); err != nil {
88+
if err := rows.Scan(&queryid, &query, &user, &meanTime, &totalSeconds, &minTimeSeconds, &maxTimeSeconds, &calls); err != nil {
8789
return err
8890
}
8991

@@ -92,6 +94,16 @@ func (PGStatTopQueriesExecutionTime) Update(ctx context.Context, instance *insta
9294
queryIdLabel = queryid.String
9395
}
9496

97+
queryLabel := "unknown"
98+
if query.Valid {
99+
queryIdLabel = query.String
100+
}
101+
102+
userLabel := "unknown"
103+
if user.Valid {
104+
userLabel = user.String
105+
}
106+
95107
totalSecondsMetric := 0.0
96108
if totalSeconds.Valid {
97109
totalSecondsMetric = totalSeconds.Float64
@@ -101,6 +113,8 @@ func (PGStatTopQueriesExecutionTime) Update(ctx context.Context, instance *insta
101113
prometheus.CounterValue,
102114
totalSecondsMetric,
103115
queryIdLabel,
116+
queryLabel,
117+
userLabel,
104118
)
105119

106120
meanTimeSecondsMetric := 0.0
@@ -112,6 +126,8 @@ func (PGStatTopQueriesExecutionTime) Update(ctx context.Context, instance *insta
112126
prometheus.CounterValue,
113127
meanTimeSecondsMetric,
114128
queryIdLabel,
129+
queryLabel,
130+
userLabel,
115131
)
116132

117133
minTimeSecondsMetric := 0.0
@@ -123,6 +139,8 @@ func (PGStatTopQueriesExecutionTime) Update(ctx context.Context, instance *insta
123139
prometheus.CounterValue,
124140
minTimeSecondsMetric,
125141
queryIdLabel,
142+
queryLabel,
143+
userLabel,
126144
)
127145

128146
maxTimeSecondsMetric := 0.0
@@ -134,6 +152,8 @@ func (PGStatTopQueriesExecutionTime) Update(ctx context.Context, instance *insta
134152
prometheus.CounterValue,
135153
maxTimeSecondsMetric,
136154
queryIdLabel,
155+
queryLabel,
156+
userLabel,
137157
)
138158

139159
callsMetric := 0.0
@@ -146,6 +166,8 @@ func (PGStatTopQueriesExecutionTime) Update(ctx context.Context, instance *insta
146166
prometheus.CounterValue,
147167
callsMetric,
148168
queryIdLabel,
169+
queryLabel,
170+
userLabel,
149171
)
150172
}
151173
return nil

0 commit comments

Comments
 (0)