Skip to content

Commit 2b896ea

Browse files
miguelHxwrouesnel
authored andcommitted
add support for boolean data types as metrics
This is useful if your database uses true/false for state and want to make prometheus alerts based on that. Before, booleans were not able to be parsed. See issue prometheus-community#201
1 parent 486345d commit 2b896ea

File tree

2 files changed

+46
-0
lines changed

2 files changed

+46
-0
lines changed

cmd/postgres_exporter/postgres_exporter.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -647,6 +647,11 @@ func dbToFloat64(t interface{}) (float64, bool) {
647647
return math.NaN(), false
648648
}
649649
return result, true
650+
case bool:
651+
if v {
652+
return 1.0, true
653+
}
654+
return 0.0, true
650655
case nil:
651656
return math.NaN(), true
652657
default:
@@ -670,6 +675,11 @@ func dbToString(t interface{}) (string, bool) {
670675
return string(v), true
671676
case string:
672677
return v, true
678+
case bool:
679+
if v {
680+
return "true", true
681+
}
682+
return "false", true
673683
default:
674684
return "", false
675685
}

cmd/postgres_exporter/postgres_exporter_test.go

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -265,3 +265,39 @@ func UnsetEnvironment(c *C, d string) {
265265
err := os.Unsetenv(d)
266266
c.Assert(err, IsNil)
267267
}
268+
269+
// test boolean metric type gets converted to float
270+
func (s *FunctionalSuite) TestBooleanConversionToValueAndString(c *C) {
271+
272+
type TestCase struct {
273+
input interface{}
274+
expectedString string
275+
expectedValue float64
276+
expectedOK bool
277+
}
278+
279+
cases := []TestCase{
280+
{
281+
input: true,
282+
expectedString: "true",
283+
expectedValue: 1.0,
284+
expectedOK: true,
285+
},
286+
{
287+
input: false,
288+
expectedString: "false",
289+
expectedValue: 0.0,
290+
expectedOK: true,
291+
},
292+
}
293+
294+
for _, cs := range cases {
295+
value, ok := dbToFloat64(cs.input)
296+
c.Assert(value, Equals, cs.expectedValue)
297+
c.Assert(ok, Equals, cs.expectedOK)
298+
299+
str, ok := dbToString(cs.input)
300+
c.Assert(str, Equals, cs.expectedString)
301+
c.Assert(ok, Equals, cs.expectedOK)
302+
}
303+
}

0 commit comments

Comments
 (0)