Skip to content

Commit 55785f0

Browse files
authored
feat: configure to get some info command metrics (OpenAtomFoundation#1762)
* feat: configure to get some info command metrics Specify the index to get info xxx(info data, info stats) by configuring, instead of using info all. Fixes: OpenAtomFoundation#1761 Signed-off-by: yaoyinnan <yaoyinnan@foxmail.com> * feat: configure to get some info command metrics Specify the index to get info xxx(info data, info stats) by configuring, instead of using info all. Fixes: OpenAtomFoundation#1761 Signed-off-by: yaoyinnan <yaoyinnan@foxmail.com> --------- Signed-off-by: yaoyinnan <yaoyinnan@foxmail.com>
1 parent 948363a commit 55785f0

7 files changed

Lines changed: 173 additions & 10 deletions

File tree

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
server = true
2+
data = true
3+
clients = true
4+
stats = true
5+
cpu = true
6+
replication = true
7+
keyspace = true
8+
9+
execcount = false
10+
commandstats = false
11+
rocksdb = false

tools/pika_exporter/exporter/client.go

Lines changed: 41 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -75,11 +75,51 @@ func (c *client) Select(db string) error {
7575
return err
7676
}
7777

78-
// INFO ALL 命令查询返回
78+
func (c *client) GetInfo() (string, error) {
79+
if InfoConf.InfoAll {
80+
return c.InfoAll()
81+
} else if InfoConf.Info {
82+
return c.Info()
83+
} else {
84+
return c.InfoSubCommand()
85+
}
86+
}
87+
7988
func (c *client) Info() (string, error) {
89+
return redis.String(c.conn.Do("INFO"))
90+
}
91+
92+
func (c *client) InfoAll() (string, error) {
8093
return redis.String(c.conn.Do("INFO", "ALL"))
8194
}
8295

96+
func (c *client) InfoSubCommand() (string, error) {
97+
var rst []string
98+
99+
sectionsMap := map[string]bool{
100+
"SERVER": InfoConf.Server,
101+
"DATA": InfoConf.Data,
102+
"CLIENTS": InfoConf.Clients,
103+
"STATS": InfoConf.Stats,
104+
"CPU": InfoConf.CPU,
105+
"REPLICATION": InfoConf.Replication,
106+
"KEYSPACE": InfoConf.Keyspace,
107+
"COMMAND_EXEC_COUNT": InfoConf.Execcount,
108+
"COMMANDSTATS": InfoConf.Commandstats,
109+
"ROCKSDB": InfoConf.Rocksdb,
110+
}
111+
for section, flag := range sectionsMap {
112+
if flag {
113+
info, err := redis.String(c.conn.Do("INFO", section))
114+
if err == nil {
115+
rst = append(rst, info)
116+
}
117+
}
118+
}
119+
120+
return strings.Join(rst, "\n"), nil
121+
}
122+
83123
func (c *client) InfoKeySpaceZero() (string, error) {
84124
return redis.String(c.conn.Do("INFO", "KEYSPACE", 0))
85125
}
@@ -88,7 +128,6 @@ func (c *client) InfoKeySpaceOne() (string, error) {
88128
return redis.String(c.conn.Do("INFO", "KEYSPACE", 1))
89129
}
90130

91-
// 返回模式信息
92131
func (c *client) InstanceModeInfo() (string, error) {
93132
getslices, err := redis.Strings(c.conn.Do("CONFIG", "GET", "instance-mode"))
94133
if err != nil {
Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
package exporter
2+
3+
import (
4+
"fmt"
5+
6+
"github.com/pelletier/go-toml"
7+
log "github.com/sirupsen/logrus"
8+
)
9+
10+
var InfoConf *InfoConfig
11+
12+
var InfoConfigPath string
13+
14+
type InfoConfig struct {
15+
Server bool `toml:"server"`
16+
Data bool `toml:"data"`
17+
Clients bool `toml:"clients"`
18+
Stats bool `toml:"stats"`
19+
CPU bool `toml:"cpu"`
20+
Replication bool `toml:"replication"`
21+
Keyspace bool `toml:"keyspace"`
22+
Execcount bool `toml:"execcount"`
23+
Commandstats bool `toml:"commandstats"`
24+
Rocksdb bool `toml:"rocksdb"`
25+
26+
Info bool
27+
InfoAll bool
28+
}
29+
30+
func LoadConfig() error {
31+
log.Println("Update configuration")
32+
err := readConfig(InfoConfigPath)
33+
if err != nil {
34+
return err
35+
}
36+
37+
InfoConf.CheckInfo()
38+
InfoConf.Display()
39+
40+
return nil
41+
}
42+
43+
func readConfig(filePath string) error {
44+
conf := InfoConfig{}
45+
tree, err := toml.LoadFile(filePath)
46+
if err != nil {
47+
return fmt.Errorf("unable to load toml file %s: %s", filePath, err)
48+
}
49+
50+
// Unmarshal the TOML data into the Config struct
51+
err = tree.Unmarshal(&conf)
52+
if err != nil {
53+
return fmt.Errorf("unable to parse toml file %s: %s", filePath, err)
54+
}
55+
56+
InfoConf = &conf
57+
58+
return nil
59+
}
60+
61+
// Display config
62+
func (c *InfoConfig) Display() {
63+
log.Println("Server:", c.Server)
64+
log.Println("Data:", c.Data)
65+
log.Println("Clients:", c.Clients)
66+
log.Println("Stats:", c.Stats)
67+
log.Println("CPU:", c.CPU)
68+
log.Println("Replication:", c.Replication)
69+
log.Println("Keyspace:", c.Keyspace)
70+
log.Println("Execcount:", c.Execcount)
71+
log.Println("Commandstats:", c.Commandstats)
72+
log.Println("Rocksdb:", c.Rocksdb)
73+
log.Println("Info:", c.Info)
74+
log.Println("InfoAll:", c.InfoAll)
75+
}
76+
77+
func (c *InfoConfig) CheckInfo() {
78+
if c.Server && c.Data && c.Clients && c.Stats && c.CPU && c.Replication && c.Keyspace {
79+
c.Info = true
80+
if c.Execcount && c.Commandstats && c.Rocksdb {
81+
c.InfoAll = true
82+
} else {
83+
c.InfoAll = false
84+
}
85+
} else {
86+
c.InfoAll = false
87+
c.Info = false
88+
}
89+
}

tools/pika_exporter/exporter/pika.go

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -216,7 +216,12 @@ func (e *exporter) scrape(ch chan<- prometheus.Metric) {
216216
}
217217

218218
func (e *exporter) collectInfo(c *client, ch chan<- prometheus.Metric) error {
219-
info, err := c.Info()
219+
// update info config
220+
if err := LoadConfig(); err != nil {
221+
log.Errorln("load config failed:", err)
222+
return err
223+
}
224+
info, err := c.GetInfo()
220225
if err != nil {
221226
return err
222227
}

tools/pika_exporter/go.mod

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ require (
77
github.com/garyburd/redigo v1.6.4
88
github.com/prometheus/client_golang v1.15.0
99
github.com/sirupsen/logrus v1.9.0
10-
github.com/stretchr/testify v1.8.2
10+
github.com/stretchr/testify v1.8.4
1111
)
1212

1313
require (
@@ -17,6 +17,7 @@ require (
1717
github.com/golang/protobuf v1.5.3 // indirect
1818
github.com/kr/text v0.2.0 // indirect
1919
github.com/matttproud/golang_protobuf_extensions v1.0.4 // indirect
20+
github.com/pelletier/go-toml v1.9.5 // indirect
2021
github.com/pmezard/go-difflib v1.0.0 // indirect
2122
github.com/prometheus/client_model v0.3.0 // indirect
2223
github.com/prometheus/common v0.42.0 // indirect

tools/pika_exporter/go.sum

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,18 +10,27 @@ github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c
1010
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
1111
github.com/garyburd/redigo v1.6.4 h1:LFu2R3+ZOPgSMWMOL+saa/zXRjw0ID2G8FepO53BGlg=
1212
github.com/garyburd/redigo v1.6.4/go.mod h1:rTb6epsqigu3kYKBnaF028A7Tf/Aw5s0cqA47doKKqw=
13+
github.com/go-logfmt/logfmt v0.5.1/go.mod h1:WYhtIu8zTZfxdn5+rREduYbwxfcBr/Vr6KEVveWlfTs=
1314
github.com/golang/protobuf v1.2.0/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U=
1415
github.com/golang/protobuf v1.3.5/go.mod h1:6O5/vntMXwX2lRkT1hjjk0nAC1IDOTvTlVgjlRvqsdk=
1516
github.com/golang/protobuf v1.5.0/go.mod h1:FsONVRAS9T7sI+LIUmWTfcYkHO4aIWwzhcaSAoJOfIk=
1617
github.com/golang/protobuf v1.5.3 h1:KhyjKVUg7Usr/dYsdSqoFveMYd5ko72D+zANwlG1mmg=
1718
github.com/golang/protobuf v1.5.3/go.mod h1:XVQd3VNwM+JqD3oG2Ue2ip4fOMUkwXdXDdiuN0vRsmY=
1819
github.com/google/go-cmp v0.5.5/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
1920
github.com/google/go-cmp v0.5.9 h1:O2Tfq5qg4qc4AmwVlvv0oLiVAGB7enBSJ2x2DqQFi38=
21+
github.com/jpillora/backoff v1.0.0/go.mod h1:J/6gKK9jxlEcS3zixgDgUAsiuZ7yrSoa/FX5e0EB2j4=
22+
github.com/json-iterator/go v1.1.12/go.mod h1:e30LSqwooZae/UwlEbR2852Gd8hjQvJoHmT4TnhNGBo=
23+
github.com/julienschmidt/httprouter v1.3.0/go.mod h1:JR6WtHb+2LUe8TCKY3cZOxFyyO8IZAc4RVcycCCAKdM=
2024
github.com/kr/pretty v0.3.1 h1:flRD4NNwYAUpkphVc1HcthR4KEIFJ65n8Mw5qdRn3LE=
2125
github.com/kr/text v0.2.0 h1:5Nx0Ya0ZqY2ygV366QzturHI13Jq95ApcVaJBhpS+AY=
2226
github.com/kr/text v0.2.0/go.mod h1:eLer722TekiGuMkidMxC/pM04lWEeraHUUmBw8l2grE=
2327
github.com/matttproud/golang_protobuf_extensions v1.0.4 h1:mmDVorXM7PCGKw94cs5zkfA9PSy5pEvNWRP0ET0TIVo=
2428
github.com/matttproud/golang_protobuf_extensions v1.0.4/go.mod h1:BSXmuO+STAnVfrANrmjBb36TMTDstsz7MSK+HVaYKv4=
29+
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd/go.mod h1:6dJC0mAP4ikYIbvyc7fijjWJddQyLn8Ig3JB5CqoB9Q=
30+
github.com/modern-go/reflect2 v1.0.2/go.mod h1:yWuevngMOJpCy52FWWMvUC8ws7m/LJsjYzDa0/r8luk=
31+
github.com/mwitkow/go-conntrack v0.0.0-20190716064945-2f068394615f/go.mod h1:qRWi+5nqEBWmkhHvq77mSJWrCKwh8bxhgT7d/eI7P4U=
32+
github.com/pelletier/go-toml v1.9.5 h1:4yBQzkHv+7BHq2PQUZF3Mx0IYxG7LsP222s7Agd3ve8=
33+
github.com/pelletier/go-toml v1.9.5/go.mod h1:u1nR/EPcESfeI/szUZKdtJ0xRNbUoANCkoOuaOx1Y+c=
2534
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
2635
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
2736
github.com/prometheus/client_golang v1.15.0 h1:5fCgGYogn0hFdhyhLbw7hEsWxufKtY9klyvdNfFlFhM=
@@ -37,13 +46,9 @@ github.com/rogpeppe/go-internal v1.10.0/go.mod h1:UQnix2H7Ngw/k4C5ijL5+65zddjncj
3746
github.com/sirupsen/logrus v1.9.0 h1:trlNQbNUG3OdDrDil03MCb1H2o9nJ1x4/5LYw7byDE0=
3847
github.com/sirupsen/logrus v1.9.0/go.mod h1:naHLuLoDiP4jHNo9R0sCBMtWGeIprob74mVsIT4qYEQ=
3948
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
40-
github.com/stretchr/objx v0.4.0/go.mod h1:YvHI0jy2hoMjB+UWwv71VJQ9isScKT/TqJzVSSt89Yw=
41-
github.com/stretchr/objx v0.5.0/go.mod h1:Yh+to48EsGEfYuaHDzXPcE3xhTkx73EhmCGUpEOglKo=
4249
github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
43-
github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
44-
github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU=
45-
github.com/stretchr/testify v1.8.2 h1:+h33VjcLVPDHtOdpUCuF+7gSuG3yGIftsP1YvFihtJ8=
46-
github.com/stretchr/testify v1.8.2/go.mod h1:w2LPCIKwWwSfY2zedu0+kehJoqGctiVI29o6fzry7u4=
50+
github.com/stretchr/testify v1.8.4 h1:CcVxjf3Q8PM0mHUKJCdn+eZZtm5yQwehR5yeSVQQcUk=
51+
github.com/stretchr/testify v1.8.4/go.mod h1:sz/lmYIOXD/1dqDmKjjqLyZ2RngseejIcXlSw2iwfAo=
4752
golang.org/x/sync v0.0.0-20181221193216-37e7f081c4d4/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
4853
golang.org/x/sys v0.0.0-20220715151400-c0bba94af5f8/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
4954
golang.org/x/sys v0.10.0 h1:SqMFp9UcQJZa+pmYuAKjd9xq1f0j5rLcDIk0mj4qAsA=
@@ -55,6 +60,7 @@ google.golang.org/protobuf v1.30.0 h1:kPPoIgf3TsEvrm0PFe15JQ+570QVxYzEvvHqChK+cn
5560
google.golang.org/protobuf v1.30.0/go.mod h1:HV8QOd/L58Z+nl8r43ehVNZIU/HEI6OcFqwMG9pJV4I=
5661
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
5762
gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c h1:Hei/4ADfdWqJk1ZMxUNpqntNwaWcugrBjAiHlqqRiVk=
63+
gopkg.in/yaml.v2 v2.4.0/go.mod h1:RDklbk79AGWmwhnvt/jBztapEOGDOx6ZbXqjP6csGnQ=
5864
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
5965
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
6066
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=

tools/pika_exporter/main.go

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ var (
3131
logLevel = flag.String("log.level", getEnv("PIKA_EXPORTER_LOG_LEVEL", "info"), "Log level, valid options: panic fatal error warn warning info debug.")
3232
logFormat = flag.String("log.format", getEnv("PIKA_EXPORTER_LOG_FORMAT", "text"), "Log format, valid options: txt and json.")
3333
showVersion = flag.Bool("version", false, "Show version information and exit.")
34+
infoConfigPath = flag.String("config", getEnv("PIKA_EXPORTER_CONFIG_PATH", "config/info.toml"), "Path to config file.")
3435
)
3536

3637
func getEnv(key string, defaultVal string) string {
@@ -57,6 +58,17 @@ func main() {
5758
return
5859
}
5960

61+
// load info config
62+
if *infoConfigPath != "" {
63+
exporter.InfoConfigPath = *infoConfigPath
64+
} else {
65+
log.Fatalln("info config path is empty")
66+
}
67+
68+
if err := exporter.LoadConfig(); err != nil {
69+
log.Fatalln("load config failed. err:", err)
70+
}
71+
6072
level, err := log.ParseLevel(*logLevel)
6173
if err != nil {
6274
log.Fatalln("parse log.level failed, err:", err)

0 commit comments

Comments
 (0)