You've already forked postgres_exporter
mirror of
https://github.com/prometheus-community/postgres_exporter.git
synced 2025-08-12 14:02:47 +03:00
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 #201
This commit is contained in:
committed by
Will Rouesnel
parent
486345d8fd
commit
2b896ea2df
@@ -647,6 +647,11 @@ func dbToFloat64(t interface{}) (float64, bool) {
|
|||||||
return math.NaN(), false
|
return math.NaN(), false
|
||||||
}
|
}
|
||||||
return result, true
|
return result, true
|
||||||
|
case bool:
|
||||||
|
if v {
|
||||||
|
return 1.0, true
|
||||||
|
}
|
||||||
|
return 0.0, true
|
||||||
case nil:
|
case nil:
|
||||||
return math.NaN(), true
|
return math.NaN(), true
|
||||||
default:
|
default:
|
||||||
@@ -670,6 +675,11 @@ func dbToString(t interface{}) (string, bool) {
|
|||||||
return string(v), true
|
return string(v), true
|
||||||
case string:
|
case string:
|
||||||
return v, true
|
return v, true
|
||||||
|
case bool:
|
||||||
|
if v {
|
||||||
|
return "true", true
|
||||||
|
}
|
||||||
|
return "false", true
|
||||||
default:
|
default:
|
||||||
return "", false
|
return "", false
|
||||||
}
|
}
|
||||||
|
@@ -265,3 +265,39 @@ func UnsetEnvironment(c *C, d string) {
|
|||||||
err := os.Unsetenv(d)
|
err := os.Unsetenv(d)
|
||||||
c.Assert(err, IsNil)
|
c.Assert(err, IsNil)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// test boolean metric type gets converted to float
|
||||||
|
func (s *FunctionalSuite) TestBooleanConversionToValueAndString(c *C) {
|
||||||
|
|
||||||
|
type TestCase struct {
|
||||||
|
input interface{}
|
||||||
|
expectedString string
|
||||||
|
expectedValue float64
|
||||||
|
expectedOK bool
|
||||||
|
}
|
||||||
|
|
||||||
|
cases := []TestCase{
|
||||||
|
{
|
||||||
|
input: true,
|
||||||
|
expectedString: "true",
|
||||||
|
expectedValue: 1.0,
|
||||||
|
expectedOK: true,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
input: false,
|
||||||
|
expectedString: "false",
|
||||||
|
expectedValue: 0.0,
|
||||||
|
expectedOK: true,
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, cs := range cases {
|
||||||
|
value, ok := dbToFloat64(cs.input)
|
||||||
|
c.Assert(value, Equals, cs.expectedValue)
|
||||||
|
c.Assert(ok, Equals, cs.expectedOK)
|
||||||
|
|
||||||
|
str, ok := dbToString(cs.input)
|
||||||
|
c.Assert(str, Equals, cs.expectedString)
|
||||||
|
c.Assert(ok, Equals, cs.expectedOK)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Reference in New Issue
Block a user