1 Star 0 Fork 0

zhangjungang/beats

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
data.go 1.67 KB
一键复制 编辑 原始数据 按行查看 历史
Tudor Golubenco 提交于 2016-07-15 14:03 . Generalize the converter schema
package keyspace
import (
"strings"
"github.com/elastic/beats/libbeat/common"
"github.com/elastic/beats/metricbeat/module/redis"
s "github.com/elastic/beats/metricbeat/schema"
c "github.com/elastic/beats/metricbeat/schema/mapstrstr"
)
// Map data to MapStr
func eventsMapping(info map[string]string) []common.MapStr {
events := []common.MapStr{}
for key, space := range getKeyspaceStats(info) {
space["id"] = key
events = append(events, space)
}
return events
}
func getKeyspaceStats(info map[string]string) map[string]common.MapStr {
keyspaceMap := findKeyspaceStats(info)
return parseKeyspaceStats(keyspaceMap)
}
// findKeyspaceStats will grep for keyspace ("^db" keys) and return the resulting map
func findKeyspaceStats(info map[string]string) map[string]string {
keyspace := map[string]string{}
for k, v := range info {
if strings.HasPrefix(k, "db") {
keyspace[k] = v
}
}
return keyspace
}
var schema = s.Schema{
"keys": c.Int("keys"),
"expires": c.Int("expires"),
"avg_ttl": c.Int("avg_ttl"),
}
// parseKeyspaceStats resolves the overloaded value string that Redis returns for keyspace
func parseKeyspaceStats(keyspaceMap map[string]string) map[string]common.MapStr {
keyspace := map[string]common.MapStr{}
for k, v := range keyspaceMap {
// Extract out the overloaded values for db keyspace
// fmt: info[db0] = keys=795341,expires=0,avg_ttl=0
dbInfo := redis.ParseRedisLine(v, ",")
if len(dbInfo) == 3 {
db := map[string]interface{}{}
for _, dbEntry := range dbInfo {
stats := redis.ParseRedisLine(dbEntry, "=")
if len(stats) == 2 {
db[stats[0]] = stats[1]
}
}
keyspace[k] = schema.Apply(db)
}
}
return keyspace
}
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/zhangjungang/beats.git
git@gitee.com:zhangjungang/beats.git
zhangjungang
beats
beats
v5.2.2

搜索帮助

344bd9b3 5694891 D2dac590 5694891