Skip to content

Commit c711ee1

Browse files
committed
Work around int32/64 counter overflows on the DB side, by casting to uint32/64 on the Go side. Fixes prometheus-community#2.
1 parent ad7ed71 commit c711ee1

File tree

2 files changed

+14
-3
lines changed

2 files changed

+14
-3
lines changed

db/db.go

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -161,8 +161,8 @@ func dbStringToFloat64(s string, re *regexp.Regexp) (float64, bool) {
161161
return result, true
162162
}
163163

164-
// Convert database.sql types to float64s for Prometheus consumption. Null types are mapped to NaN. string and []byte
165-
// types are mapped as NaN and !ok
164+
// Convert database.sql types to float64s for Prometheus consumption. Null
165+
// types are mapped to NaN. string and []byte types are mapped as NaN and !ok
166166
func ToFloat64(t interface{}, r *regexp.Regexp) (float64, bool) {
167167
switch v := t.(type) {
168168
case int32:
@@ -187,6 +187,17 @@ func ToFloat64(t interface{}, r *regexp.Regexp) (float64, bool) {
187187
}
188188
}
189189

190+
func ToUnsignedFloat64(t interface{}, r *regexp.Regexp) (float64, bool) {
191+
switch v := t.(type) {
192+
case int32:
193+
return float64(uint32(v)), true
194+
case int64:
195+
return float64(uint64(v)), true
196+
default:
197+
return ToFloat64(t, r)
198+
}
199+
}
200+
190201
// Convert database.sql to string for Prometheus labels. Null types are mapped to empty strings.
191202
func ToString(t interface{}) (string, bool) {
192203
switch v := t.(type) {

dbms_exporter.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,7 @@ func makeDescMap(metricName string, resultMap recipes.ResultMap, recipe recipes.
116116
vtype: prometheus.CounterValue,
117117
desc: newDesc(columnName, columnMapping.Description),
118118
conversion: func(in interface{}) (float64, bool) {
119-
return db.ToFloat64(in, regexp)
119+
return db.ToUnsignedFloat64(in, regexp)
120120
},
121121
}
122122
case common.GAUGE:

0 commit comments

Comments
 (0)