Skip to content

Commit

Permalink
Add some addtl logging (#547)
Browse files Browse the repository at this point in the history
* more logging when loading password file

* log error when conversion fails
  • Loading branch information
oliver006 authored Sep 21, 2021
1 parent ef05f86 commit 674b495
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 15 deletions.
18 changes: 9 additions & 9 deletions exporter/keys.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,8 @@ func getKeyInfo(c redis.Conn, key string) (info keyInfo, err error) {
case "none":
return info, errKeyTypeNotFound
case "string":
// Use PFCOUNT first because STRLEN on HyperLogLog strings returns the wrong length
// while PFCOUNT only works on HLL strings and returns an error on regular strings.
// Use PFCOUNT first because STRLEN on HyperLogLog strings returns the wrong length
// while PFCOUNT only works on HLL strings and returns an error on regular strings.
if size, err := redis.Int64(doRedisCmd(c, "PFCOUNT", key)); err == nil {
// hyperloglog
info.size = float64(size)
Expand Down Expand Up @@ -109,13 +109,13 @@ func (e *Exporter) extractCheckKeyMetrics(ch chan<- prometheus.Metric, c redis.C
case nil:
e.registerConstMetricGauge(ch, "key_size", info.size, dbLabel, k.key)

// Only run on single value strings
if info.keyType == "string" {
// Only record value metric if value is float-y
if val, err := redis.Float64(doRedisCmd(c, "GET", k.key)); err == nil {
e.registerConstMetricGauge(ch, "key_value", val, dbLabel, k.key)
}
}
// Only run on single value strings
if info.keyType == "string" {
// Only record value metric if value is float-y
if val, err := redis.Float64(doRedisCmd(c, "GET", k.key)); err == nil {
e.registerConstMetricGauge(ch, "key_value", val, dbLabel, k.key)
}
}
default:
log.Error(err)
}
Expand Down
8 changes: 6 additions & 2 deletions exporter/lua.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,17 @@ func (e *Exporter) extractLuaScriptMetrics(ch chan<- prometheus.Metric, c redis.
}

if len(kv) == 0 {
log.Debugf("Lua script returned no results")
return nil
}

for key, stringVal := range kv {
if val, err := strconv.ParseFloat(stringVal, 64); err == nil {
e.registerConstMetricGauge(ch, "script_values", val, key)
val, err := strconv.ParseFloat(stringVal, 64)
if err != nil {
log.Errorf("Error parsing lua script results, err: %s", err)
return err
}
e.registerConstMetricGauge(ch, "script_values", val, key)
}
return nil
}
9 changes: 8 additions & 1 deletion exporter/lua_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,12 +35,19 @@ func TestLuaScript(t *testing.T) {
ExpectedKeys: 0,
},
{
Name: "borked",
Name: "borked1",
Script: `return {"key1" BROKEN `,
ExpectedKeys: 0,
ExpectedError: true,
Wants: []string{`test_exporter_last_scrape_error{err="ERR Error compiling script`},
},
{
Name: "borked2",
Script: `return {"key1", "abc"}`,
ExpectedKeys: 0,
ExpectedError: true,
Wants: []string{`test_exporter_last_scrape_error{err="strconv.ParseFloat: parsing \"abc\": invalid syntax"} 1`},
},
} {
t.Run(tst.Name, func(t *testing.T) {
e, _ := NewRedisExporter(
Expand Down
12 changes: 9 additions & 3 deletions exporter/pwd_file.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,18 +9,24 @@ import (

// LoadPwdFile reads the redis password file and returns the password map
func LoadPwdFile(passwordFile string) (map[string]string, error) {
passwordMap := make(map[string]string)
res := make(map[string]string)

log.Debugf("start load password file: %s", passwordFile)
bytes, err := ioutil.ReadFile(passwordFile)
if err != nil {
log.Warnf("load password file failed: %s", err)
return nil, err
}
err = json.Unmarshal(bytes, &passwordMap)
err = json.Unmarshal(bytes, &res)
if err != nil {
log.Warnf("password file format error: %s", err)
return nil, err
}
return passwordMap, nil

log.Errorf("Loaded %d entries from %s", len(res), passwordFile)
for k := range res {
log.Debugf("%s", k)
}

return res, nil
}

0 comments on commit 674b495

Please sign in to comment.