You've already forked postgres_exporter
mirror of
https://github.com/prometheus-community/postgres_exporter.git
synced 2025-08-09 15:42:47 +03:00
WIP: break exporter apart so it can run as an idiomatic library.
This commit is contained in:
63
exporter/dbconv/dbconv.go
Normal file
63
exporter/dbconv/dbconv.go
Normal file
@@ -0,0 +1,63 @@
|
||||
package dbconv
|
||||
|
||||
import (
|
||||
"time"
|
||||
"strconv"
|
||||
"github.com/prometheus/common/log"
|
||||
"math"
|
||||
"fmt"
|
||||
)
|
||||
|
||||
// DbToFloat64 converts database.sql types to float64s for Prometheus consumption. Null types are mapped to NaN. string
|
||||
// and []byte types are mapped as NaN and !ok
|
||||
func DbToFloat64(t interface{}) (float64, bool) {
|
||||
switch v := t.(type) {
|
||||
case int64:
|
||||
return float64(v), true
|
||||
case float64:
|
||||
return v, true
|
||||
case time.Time:
|
||||
return float64(v.Unix()), true
|
||||
case []byte:
|
||||
// Try and convert to string and then parse to a float64
|
||||
strV := string(v)
|
||||
result, err := strconv.ParseFloat(strV, 64)
|
||||
if err != nil {
|
||||
log.Infoln("Could not parse []byte:", err)
|
||||
return math.NaN(), false
|
||||
}
|
||||
return result, true
|
||||
case string:
|
||||
result, err := strconv.ParseFloat(v, 64)
|
||||
if err != nil {
|
||||
log.Infoln("Could not parse string:", err)
|
||||
return math.NaN(), false
|
||||
}
|
||||
return result, true
|
||||
case nil:
|
||||
return math.NaN(), true
|
||||
default:
|
||||
return math.NaN(), false
|
||||
}
|
||||
}
|
||||
|
||||
// DbToString converts database.sql to string for Prometheus labels. Null types are mapped to empty strings.
|
||||
func DbToString(t interface{}) (string, bool) {
|
||||
switch v := t.(type) {
|
||||
case int64:
|
||||
return fmt.Sprintf("%v", v), true
|
||||
case float64:
|
||||
return fmt.Sprintf("%v", v), true
|
||||
case time.Time:
|
||||
return fmt.Sprintf("%v", v.Unix()), true
|
||||
case nil:
|
||||
return "", true
|
||||
case []byte:
|
||||
// Try and convert to string
|
||||
return string(v), true
|
||||
case string:
|
||||
return v, true
|
||||
default:
|
||||
return "", false
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user