1 Star 0 Fork 0

sqos/beats

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
redis.go 1.72 KB
一键复制 编辑 原始数据 按行查看 历史
/*
Package redis contains shared Redis functionality for the metric sets
*/
package redis
import (
"strings"
"time"
"github.com/elastic/beats/libbeat/logp"
rd "github.com/garyburd/redigo/redis"
)
// ParseRedisInfo parses the string returned by the INFO command
// Every line is split up into key and value
func ParseRedisInfo(info string) map[string]string {
// Feed every line into
result := strings.Split(info, "\r\n")
// Load redis info values into array
values := map[string]string{}
for _, value := range result {
// Values are separated by :
parts := ParseRedisLine(value, ":")
if len(parts) == 2 {
values[parts[0]] = parts[1]
}
}
return values
}
// ParseRedisLine parses a single line returned by INFO
func ParseRedisLine(s string, delimeter string) []string {
return strings.Split(s, delimeter)
}
// FetchRedisInfo returns a map of requested stats.
func FetchRedisInfo(stat string, c rd.Conn) (map[string]string, error) {
defer c.Close()
out, err := rd.String(c.Do("INFO", stat))
if err != nil {
logp.Err("Error retrieving INFO stats: %v", err)
return nil, err
}
return ParseRedisInfo(out), nil
}
// CreatePool creates a redis connection pool
func CreatePool(
host, password, network string,
maxConn int,
idleTimeout, connTimeout time.Duration,
) *rd.Pool {
return &rd.Pool{
MaxIdle: maxConn,
IdleTimeout: idleTimeout,
Dial: func() (rd.Conn, error) {
c, err := rd.Dial(network, host,
rd.DialConnectTimeout(connTimeout),
rd.DialReadTimeout(connTimeout),
rd.DialWriteTimeout(connTimeout))
if err != nil {
return nil, err
}
if password != "" {
if _, err := c.Do("AUTH", password); err != nil {
c.Close()
return nil, err
}
}
return c, err
},
}
}
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/sqos/beats.git
git@gitee.com:sqos/beats.git
sqos
beats
beats
v5.3.3

搜索帮助