Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit 11589b4

Browse files
committedJun 26, 2023
stat activity summary nullable
Signed-off-by: Felix Yuan <felix.yuan@reddit.com>
1 parent 1ac773a commit 11589b4

3 files changed

+49
-8
lines changed
 

‎collector/pg_long_running_transactions_summary.go

+2-1
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,7 @@ func (PGLongRunningTransactionsSummaryCollector) Update(ctx context.Context, ins
8989
if endpoint.Valid {
9090
endpointLabel = endpoint.String
9191
}
92+
labels := []string{applicationLabel, endpointLabel}
9293

9394
maxAgeInSecondsMetric := 0.0
9495
if maxAgeInSeconds.Valid {
@@ -98,7 +99,7 @@ func (PGLongRunningTransactionsSummaryCollector) Update(ctx context.Context, ins
9899
longRunningTransactionsSummaryMaxAgeInSeconds,
99100
prometheus.GaugeValue,
100101
maxAgeInSecondsMetric,
101-
applicationLabel, endpointLabel,
102+
labels...,
102103
)
103104
}
104105
if err := rows.Err(); err != nil {

‎collector/pg_stat_activity_autovacuum_active.go

+2-1
Original file line numberDiff line numberDiff line change
@@ -75,12 +75,13 @@ func (PGStatActivityAutovacuumActiveCollector) Update(ctx context.Context, insta
7575
if err := rows.Scan(&phase, &mode, &workersCount); err != nil {
7676
return err
7777
}
78+
labels := []string{phase, mode}
7879

7980
ch <- prometheus.MustNewConstMetric(
8081
statActivityAutovacuumActiveWorkersCount,
8182
prometheus.GaugeValue,
8283
workersCount,
83-
phase, mode,
84+
labels...,
8485
)
8586
}
8687
if err := rows.Err(); err != nil {

‎collector/pg_stat_activity_summary.go

+45-6
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ package collector
1515

1616
import (
1717
"context"
18+
"database/sql"
1819

1920
"github.com/go-kit/log"
2021
"github.com/prometheus/client_golang/prometheus"
@@ -86,24 +87,62 @@ func (PGStatActivitySummaryCollector) Update(ctx context.Context, instance *inst
8687
defer rows.Close()
8788

8889
for rows.Next() {
89-
var usename, application, endpoint, command, state, waitEvent, waitEventType string
90-
var count, maxTxAge float64
90+
var usename, application, endpoint, command, state, waitEvent, waitEventType sql.NullString
91+
var count, maxTxAge sql.NullFloat64
9192

9293
if err := rows.Scan(&usename, &application, &endpoint, &command, &state, &waitEvent, &waitEventType, &count, &maxTxAge); err != nil {
9394
return err
9495
}
96+
usenameLabel := "unknown"
97+
if usename.Valid {
98+
usenameLabel = usename.String
99+
}
100+
applicationLabel := "unknown"
101+
if application.Valid {
102+
applicationLabel = application.String
103+
}
104+
endpointLabel := "unknown"
105+
if endpoint.Valid {
106+
endpointLabel = endpoint.String
107+
}
108+
commandLabel := "unknown"
109+
if command.Valid {
110+
commandLabel = command.String
111+
}
112+
stateLabel := "unknown"
113+
if state.Valid {
114+
stateLabel = state.String
115+
}
116+
waitEventLabel := "unknown"
117+
if waitEvent.Valid {
118+
waitEventLabel = waitEvent.String
119+
}
120+
waitEventTypeLabel := "unknown"
121+
if waitEventType.Valid {
122+
waitEventTypeLabel = waitEventType.String
123+
}
124+
labels := []string{usenameLabel, applicationLabel, endpointLabel, commandLabel, stateLabel, waitEventLabel, waitEventTypeLabel}
95125

126+
countMetric := 0.0
127+
if count.Valid {
128+
countMetric = count.Float64
129+
}
96130
ch <- prometheus.MustNewConstMetric(
97131
statActivitySummaryActiveCount,
98132
prometheus.GaugeValue,
99-
count,
100-
usename, application, endpoint, command, state, waitEvent, waitEventType,
133+
countMetric,
134+
labels...,
101135
)
136+
137+
maxTxAgeMetric := 0.0
138+
if maxTxAge.Valid {
139+
maxTxAgeMetric = maxTxAge.Float64
140+
}
102141
ch <- prometheus.MustNewConstMetric(
103142
statActivitySummaryMaxTxAgeInSeconds,
104143
prometheus.GaugeValue,
105-
maxTxAge,
106-
usename, application, endpoint, command, state, waitEvent, waitEventType,
144+
maxTxAgeMetric,
145+
labels...,
107146
)
108147
}
109148
if err := rows.Err(); err != nil {

0 commit comments

Comments
 (0)
Please sign in to comment.