@@ -26,27 +26,27 @@ var (
26
26
statTotalExecutionTime = prometheus .NewDesc (
27
27
prometheus .BuildFQName (namespace , statTopQueriesExecutionTime , "total_seconds" ),
28
28
"Total time spent in the statement, in milliseconds" ,
29
- []string {"queryid" },
29
+ []string {"queryid" , "query" , "user" },
30
30
prometheus.Labels {},
31
31
)
32
32
statMinimumExecutionTime = prometheus .NewDesc (
33
33
prometheus .BuildFQName (namespace , statTopQueriesExecutionTime , "min_time_seconds" ),
34
34
"Minimum time spent in the statement, in milliseconds" ,
35
- []string {"queryid" },
35
+ []string {"queryid" , "query" , "user" },
36
36
prometheus.Labels {},
37
37
)
38
38
39
39
statMaximumExecutionTime = prometheus .NewDesc (
40
40
prometheus .BuildFQName (namespace , statTopQueriesExecutionTime , "max_time_seconds" ),
41
41
"Maximum time spent in the statement, in milliseconds" ,
42
- []string {"queryid" },
42
+ []string {"queryid" , "query" , "user" },
43
43
prometheus.Labels {},
44
44
)
45
45
46
46
statMeanExecutionTime = prometheus .NewDesc (
47
47
prometheus .BuildFQName (namespace , statTopQueriesExecutionTime , "mean_time" ),
48
48
"Mean time spent in the statement, in milliseconds" ,
49
- []string {"queryid" },
49
+ []string {"queryid" , "query" , "user" },
50
50
prometheus.Labels {},
51
51
)
52
52
@@ -59,14 +59,16 @@ var (
59
59
60
60
statTopQueryExecutionQuery = `SELECT
61
61
queryid,
62
+ query
63
+ pg_get_userbyid(userid) as user,
62
64
mean_time / 1000.0 as mean_time,
63
65
total_time / 1000.0 as total_seconds,
64
66
min_time / 1000.0 as min_time_seconds,
65
67
max_time / 1000.0 as max_time_seconds,
66
68
calls
67
69
FROM pg_stat_statements
68
70
ORDER BY total_seconds DESC
69
- LIMIT 1 ;`
71
+ LIMIT 10 ;`
70
72
)
71
73
72
74
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
79
81
defer rows .Close ()
80
82
81
83
for rows .Next () {
82
- var queryid sql.NullString
84
+ var queryid , query , user sql.NullString
83
85
var meanTime , totalSeconds , minTimeSeconds , maxTimeSeconds sql.NullFloat64
84
86
var calls sql.NullInt64
85
87
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 {
87
89
return err
88
90
}
89
91
@@ -92,6 +94,16 @@ func (PGStatTopQueriesExecutionTime) Update(ctx context.Context, instance *insta
92
94
queryIdLabel = queryid .String
93
95
}
94
96
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
+
95
107
totalSecondsMetric := 0.0
96
108
if totalSeconds .Valid {
97
109
totalSecondsMetric = totalSeconds .Float64
@@ -101,6 +113,8 @@ func (PGStatTopQueriesExecutionTime) Update(ctx context.Context, instance *insta
101
113
prometheus .CounterValue ,
102
114
totalSecondsMetric ,
103
115
queryIdLabel ,
116
+ queryLabel ,
117
+ userLabel ,
104
118
)
105
119
106
120
meanTimeSecondsMetric := 0.0
@@ -112,6 +126,8 @@ func (PGStatTopQueriesExecutionTime) Update(ctx context.Context, instance *insta
112
126
prometheus .CounterValue ,
113
127
meanTimeSecondsMetric ,
114
128
queryIdLabel ,
129
+ queryLabel ,
130
+ userLabel ,
115
131
)
116
132
117
133
minTimeSecondsMetric := 0.0
@@ -123,6 +139,8 @@ func (PGStatTopQueriesExecutionTime) Update(ctx context.Context, instance *insta
123
139
prometheus .CounterValue ,
124
140
minTimeSecondsMetric ,
125
141
queryIdLabel ,
142
+ queryLabel ,
143
+ userLabel ,
126
144
)
127
145
128
146
maxTimeSecondsMetric := 0.0
@@ -134,6 +152,8 @@ func (PGStatTopQueriesExecutionTime) Update(ctx context.Context, instance *insta
134
152
prometheus .CounterValue ,
135
153
maxTimeSecondsMetric ,
136
154
queryIdLabel ,
155
+ queryLabel ,
156
+ userLabel ,
137
157
)
138
158
139
159
callsMetric := 0.0
@@ -146,6 +166,8 @@ func (PGStatTopQueriesExecutionTime) Update(ctx context.Context, instance *insta
146
166
prometheus .CounterValue ,
147
167
callsMetric ,
148
168
queryIdLabel ,
169
+ queryLabel ,
170
+ userLabel ,
149
171
)
150
172
}
151
173
return nil
0 commit comments