• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

oliver006 / redis_exporter / 17172001870

23 Aug 2025 05:58AM UTC coverage: 84.921% (-0.9%) from 85.87%
17172001870

Pull #1028

github

web-flow
Merge 891f7f01e into 7632b7b20
Pull Request #1028: sirupsen/log --> log/slog

121 of 249 new or added lines in 18 files covered. (48.59%)

6 existing lines in 1 file now uncovered.

2568 of 3024 relevant lines covered (84.92%)

13254.26 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

80.36
/exporter/redis.go
1
package exporter
2

3
import (
4
        "fmt"
5
        "log/slog"
6
        "net/url"
7
        "strings"
8
        "time"
9

10
        "github.com/gomodule/redigo/redis"
11
        "github.com/mna/redisc"
12
)
13

14
func (e *Exporter) configureOptions(uri string) ([]redis.DialOption, error) {
2,132✔
15
        tlsConfig, err := e.CreateClientTLSConfig()
2,132✔
16
        if err != nil {
2,132✔
17
                return nil, err
×
18
        }
×
19

20
        options := []redis.DialOption{
2,132✔
21
                redis.DialConnectTimeout(e.options.ConnectionTimeouts),
2,132✔
22
                redis.DialReadTimeout(e.options.ConnectionTimeouts),
2,132✔
23
                redis.DialWriteTimeout(e.options.ConnectionTimeouts),
2,132✔
24
                redis.DialTLSConfig(tlsConfig),
2,132✔
25
                redis.DialUseTLS(strings.HasPrefix(e.redisAddr, "rediss://")),
2,132✔
26
        }
2,132✔
27

2,132✔
28
        if e.options.User != "" {
2,134✔
29
                options = append(options, redis.DialUsername(e.options.User))
2✔
30
        }
2✔
31

32
        if e.options.Password != "" {
2,135✔
33
                options = append(options, redis.DialPassword(e.options.Password))
3✔
34
        }
3✔
35

36
        if pwd, ok := e.lookupPasswordInPasswordMap(uri); ok && pwd != "" {
2,137✔
37
                options = append(options, redis.DialPassword(pwd))
5✔
38
        }
5✔
39

40
        return options, nil
2,132✔
41
}
42

43
func (e *Exporter) lookupPasswordInPasswordMap(uri string) (string, bool) {
2,132✔
44
        u, err := url.Parse(uri)
2,132✔
45
        if err != nil {
2,132✔
46
                return "", false
×
47
        }
×
48

49
        if e.options.User != "" {
2,134✔
50
                u.User = url.User(e.options.User)
2✔
51
        }
2✔
52
        uri = u.String()
2,132✔
53

2,132✔
54
        // strip solo ":" if present in uri that has a username (and no pwd)
2,132✔
55
        uri = strings.Replace(uri, fmt.Sprintf(":@%s", u.Host), fmt.Sprintf("@%s", u.Host), 1)
2,132✔
56

2,132✔
57
        slog.Debug("Looking up URI in pwd map", "uri", uri)
2,132✔
58
        if pwd, ok := e.options.PasswordMap[uri]; ok && pwd != "" {
2,137✔
59
                return pwd, true
5✔
60
        }
5✔
61
        return "", false
2,127✔
62
}
63

64
func (e *Exporter) connectToRedis() (redis.Conn, error) {
2,104✔
65
        uri := e.redisAddr
2,104✔
66
        if !strings.Contains(uri, "://") {
2,110✔
67
                uri = "redis://" + uri
6✔
68
        }
6✔
69

70
        options, err := e.configureOptions(uri)
2,104✔
71
        if err != nil {
2,104✔
72
                return nil, err
×
73
        }
×
74

75
        slog.Debug("Trying to dial URL", "uri", uri)
2,104✔
76
        c, err := redis.DialURL(uri, options...)
2,104✔
77
        if err != nil {
2,108✔
78
                slog.Debug("Failed to dial URL", "error", err)
4✔
79
                if frags := strings.Split(e.redisAddr, "://"); len(frags) == 2 {
8✔
80
                        slog.Debug("Trying to dial", "protocol", frags[0], "address", frags[1])
4✔
81
                        c, err = redis.Dial(frags[0], frags[1], options...)
4✔
82
                } else {
4✔
NEW
83
                        slog.Debug("Trying to dial TCP", "address", e.redisAddr)
×
84
                        c, err = redis.Dial("tcp", e.redisAddr, options...)
×
85
                }
×
86
        }
87
        return c, err
2,104✔
88
}
89

90
func (e *Exporter) connectToRedisCluster() (redis.Conn, error) {
28✔
91
        uri := e.redisAddr
28✔
92
        if !strings.Contains(uri, "://") {
29✔
93
                uri = "redis://" + uri
1✔
94
        }
1✔
95

96
        options, err := e.configureOptions(uri)
28✔
97
        if err != nil {
28✔
98
                return nil, err
×
99
        }
×
100

101
        // remove url scheme for redis.Cluster.StartupNodes
102
        if strings.Contains(uri, "://") {
56✔
103
                u, _ := url.Parse(uri)
28✔
104
                if u.Port() == "" {
28✔
105
                        uri = u.Host + ":6379"
×
106
                } else {
28✔
107
                        uri = u.Host
28✔
108
                }
28✔
109
        } else {
×
110
                if frags := strings.Split(uri, ":"); len(frags) != 2 {
×
111
                        uri = uri + ":6379"
×
112
                }
×
113
        }
114

115
        slog.Debug("Creating cluster object")
28✔
116
        cluster := redisc.Cluster{
28✔
117
                StartupNodes: []string{uri},
28✔
118
                DialOptions:  options,
28✔
119
        }
28✔
120
        slog.Debug("Running refresh on cluster object")
28✔
121
        if err := cluster.Refresh(); err != nil {
31✔
122
                slog.Error("Cluster refresh failed", "error", err)
3✔
123
                return nil, fmt.Errorf("cluster refresh failed: %w", err)
3✔
124
        }
3✔
125

126
        slog.Debug("Creating redis connection object")
25✔
127
        conn, err := cluster.Dial()
25✔
128
        if err != nil {
25✔
NEW
129
                slog.Error("Dial failed", "error", err)
×
130
                return nil, fmt.Errorf("dial failed: %w", err)
×
131
        }
×
132

133
        c, err := redisc.RetryConn(conn, 10, 100*time.Millisecond)
25✔
134
        if err != nil {
25✔
NEW
135
                slog.Error("RetryConn failed", "error", err)
×
136
                return nil, fmt.Errorf("retryConn failed: %w", err)
×
137
        }
×
138

139
        return c, err
25✔
140
}
141

142
func doRedisCmd(c redis.Conn, cmd string, args ...interface{}) (interface{}, error) {
15,993✔
143
        slog.Debug("c.Do() - running command", "cmd", cmd, "args", args)
15,993✔
144
        res, err := c.Do(cmd, args...)
15,993✔
145
        if err != nil {
17,490✔
146
                slog.Debug("Redis command failed", "error", err)
1,497✔
147
        }
1,497✔
148
        slog.Debug("c.Do() - done")
15,993✔
149
        return res, err
15,993✔
150
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc