docs: improve redis commands documentation (#9606)

This commit is contained in:
Alexander Krantz 2021-08-17 14:13:43 -07:00 committed by GitHub
parent eb41218fe0
commit cfd7348e5c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 37 additions and 13 deletions

View File

@ -10,15 +10,21 @@
## e.g.
## tcp://localhost:6379
## tcp://:password@192.168.99.100
## unix:///var/run/redis.sock
##
## If no servers are specified, then localhost is used as the host.
## If no port is specified, 6379 is used
servers = ["tcp://localhost:6379"]
## Optional. Specify redis commands to retrieve values
# [[inputs.redis.commands]]
# command = ["get", "sample-key"]
# field = "sample-key-value"
# type = "string"
# # The command to run where each argument is a separate element
# command = ["get", "sample-key"]
# # The field to store the result in
# field = "sample-key-value"
# # The type of the result
# # Can be "string", "integer", or "float"
# type = "string"
## specify server password
# password = "s#cr@t%"

View File

@ -32,8 +32,8 @@ type Redis struct {
Log telegraf.Logger
clients []Client
initialized bool
clients []Client
connected bool
}
type Client interface {
@ -201,9 +201,13 @@ var sampleConfig = `
## Optional. Specify redis commands to retrieve values
# [[inputs.redis.commands]]
# command = ["get", "sample-key"]
# field = "sample-key-value"
# type = "string"
# # The command to run where each argument is a separate element
# command = ["get", "sample-key"]
# # The field to store the result in
# field = "sample-key-value"
# # The type of the result
# # Can be "string", "integer", or "float"
# type = "string"
## specify server password
# password = "s#cr@t%"
@ -230,8 +234,18 @@ var Tracking = map[string]string{
"role": "replication_role",
}
func (r *Redis) init() error {
if r.initialized {
func (r *Redis) Init() error {
for _, command := range r.Commands {
if command.Type != "string" && command.Type != "integer" && command.Type != "float" {
return fmt.Errorf(`unknown result type: expected one of "string", "integer", "float"; got %q`, command.Type)
}
}
return nil
}
func (r *Redis) connect() error {
if r.connected {
return nil
}
@ -299,15 +313,15 @@ func (r *Redis) init() error {
}
}
r.initialized = true
r.connected = true
return nil
}
// Reads stats from all configured servers accumulates stats.
// Returns one of the errors encountered while gather stats (if any).
func (r *Redis) Gather(acc telegraf.Accumulator) error {
if !r.initialized {
err := r.init()
if !r.connected {
err := r.connect()
if err != nil {
return err
}
@ -333,6 +347,10 @@ func (r *Redis) gatherCommandValues(client Client, acc telegraf.Accumulator) err
for _, command := range r.Commands {
val, err := client.Do(command.Type, command.Command...)
if err != nil {
if strings.Contains(err.Error(), "unexpected type=") {
return fmt.Errorf("could not get command result: %s", err)
}
return err
}