| 1 | package exporter |
| 2 | |
| 3 | import ( |
| 4 | "fmt" |
| 5 | "net/url" |
| 6 | "strings" |
| 7 | "time" |
| 8 | |
| 9 | "github.com/gomodule/redigo/redis" |
| 10 | "github.com/mna/redisc" |
| 11 | log "github.com/sirupsen/logrus" |
| 12 | ) |
| 13 | |
| 14 | func (e *Exporter) configureOptions(uri string) ([]redis.DialOption, error) { |
| 15 | tlsConfig, err := e.CreateClientTLSConfig() |
| 16 | if err != nil { |
| 17 | return nil, err |
| 18 | } |
| 19 | |
| 20 | options := []redis.DialOption{ |
| 21 | redis.DialConnectTimeout(e.options.ConnectionTimeouts), |
| 22 | redis.DialReadTimeout(e.options.ConnectionTimeouts), |
| 23 | redis.DialWriteTimeout(e.options.ConnectionTimeouts), |
| 24 | redis.DialTLSConfig(tlsConfig), |
| 25 | redis.DialUseTLS(strings.HasPrefix(e.redisAddr, "rediss://")), |
| 26 | } |
| 27 | |
| 28 | if e.options.User != "" { |
| 29 | options = append(options, redis.DialUsername(e.options.User)) |
| 30 | } |
| 31 | |
| 32 | if e.options.Password != "" { |
| 33 | options = append(options, redis.DialPassword(e.options.Password)) |
| 34 | } |
| 35 | |
| 36 | if pwd, ok := e.lookupPasswordInPasswordMap(uri); ok && pwd != "" { |
| 37 | options = append(options, redis.DialPassword(pwd)) |
| 38 | } |
| 39 | |
| 40 | return options, nil |
| 41 | } |
| 42 | |
| 43 | func (e *Exporter) lookupPasswordInPasswordMap(uri string) (string, bool) { |
| 44 | u, err := url.Parse(uri) |
| 45 | if err != nil { |
| 46 | return "", false |
| 47 | } |
| 48 | |
| 49 | if e.options.User != "" { |
| 50 | u.User = url.User(e.options.User) |
| 51 | } |
| 52 | uri = u.String() |
| 53 | |
| 54 | // strip solo ":" if present in uri that has a username (and no pwd) |
| 55 | uri = strings.Replace(uri, fmt.Sprintf(":@%s", u.Host), fmt.Sprintf("@%s", u.Host), 1) |
| 56 | |
| 57 | log.Debugf("looking up in pwd map, uri: %s", uri) |
| 58 | if pwd, ok := e.options.PasswordMap[uri]; ok && pwd != "" { |
| 59 | return pwd, true |
| 60 | } |
| 61 | return "", false |
| 62 | } |
| 63 | |
| 64 | func (e *Exporter) connectToRedis() (redis.Conn, error) { |
| 65 | uri := e.redisAddr |
| 66 | if !strings.Contains(uri, "://") { |
| 67 | uri = "redis://" + uri |
| 68 | } |
| 69 | |
| 70 | options, err := e.configureOptions(uri) |
| 71 | if err != nil { |
| 72 | return nil, err |
| 73 | } |
| 74 | |
| 75 | log.Debugf("Trying DialURL(): %s", uri) |
| 76 | c, err := redis.DialURL(uri, options...) |
| 77 | if err != nil { |
| 78 | log.Debugf("DialURL() failed, err: %s", err) |
| 79 | if frags := strings.Split(e.redisAddr, "://"); len(frags) == 2 { |
| 80 | log.Debugf("Trying: Dial(): %s %s", frags[0], frags[1]) |
| 81 | c, err = redis.Dial(frags[0], frags[1], options...) |
| 82 | } else { |
| 83 | log.Debugf("Trying: Dial(): tcp %s", e.redisAddr) |
| 84 | c, err = redis.Dial("tcp", e.redisAddr, options...) |
| 85 | } |
| 86 | } |
| 87 | return c, err |
| 88 | } |
| 89 | |
| 90 | func (e *Exporter) connectToRedisCluster() (redis.Conn, error) { |
| 91 | uri := e.redisAddr |
| 92 | if !strings.Contains(uri, "://") { |
| 93 | uri = "redis://" + uri |
| 94 | } |
| 95 | |
| 96 | options, err := e.configureOptions(uri) |
| 97 | if err != nil { |
| 98 | return nil, err |
| 99 | } |
| 100 | |
| 101 | // remove url scheme for redis.Cluster.StartupNodes |
| 102 | if strings.Contains(uri, "://") { |
| 103 | u, _ := url.Parse(uri) |
| 104 | if u.Port() == "" { |
| 105 | uri = u.Host + ":6379" |
| 106 | } else { |
| 107 | uri = u.Host |
| 108 | } |
| 109 | } else { |
| 110 | if frags := strings.Split(uri, ":"); len(frags) != 2 { |
| 111 | uri = uri + ":6379" |
| 112 | } |
| 113 | } |
| 114 | |
| 115 | log.Debugf("Creating cluster object") |
| 116 | cluster := redisc.Cluster{ |
| 117 | StartupNodes: []string{uri}, |
| 118 | DialOptions: options, |
| 119 | } |
| 120 | log.Debugf("Running refresh on cluster object") |
| 121 | if err := cluster.Refresh(); err != nil { |
| 122 | log.Errorf("Cluster refresh failed: %v", err) |
| 123 | return nil, fmt.Errorf("cluster refresh failed: %w", err) |
| 124 | } |
| 125 | |
| 126 | log.Debugf("Creating redis connection object") |
| 127 | conn, err := cluster.Dial() |
| 128 | if err != nil { |
| 129 | log.Errorf("Dial failed: %v", err) |
| 130 | return nil, fmt.Errorf("dial failed: %w", err) |
| 131 | } |
| 132 | |
| 133 | c, err := redisc.RetryConn(conn, 10, 100*time.Millisecond) |
| 134 | if err != nil { |
| 135 | log.Errorf("RetryConn failed: %v", err) |
| 136 | return nil, fmt.Errorf("retryConn failed: %w", err) |
| 137 | } |
| 138 | |
| 139 | return c, err |
| 140 | } |
| 141 | |
| 142 | func doRedisCmd(c redis.Conn, cmd string, args ...any) (any, error) { |
| 143 | log.Debugf("c.Do() - running command: %s args: [%v]", cmd, args) |
| 144 | res, err := c.Do(cmd, args...) |
| 145 | if err != nil { |
| 146 | log.Debugf("c.Do() - err: %s", err) |
| 147 | } |
| 148 | log.Debugf("c.Do() - done") |
| 149 | return res, err |
| 150 | } |