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. ## e.g.
## tcp://localhost:6379 ## tcp://localhost:6379
## tcp://:password@192.168.99.100 ## 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 servers are specified, then localhost is used as the host.
## If no port is specified, 6379 is used ## If no port is specified, 6379 is used
servers = ["tcp://localhost:6379"] servers = ["tcp://localhost:6379"]
## Optional. Specify redis commands to retrieve values ## Optional. Specify redis commands to retrieve values
# [[inputs.redis.commands]] # [[inputs.redis.commands]]
# command = ["get", "sample-key"] # # The command to run where each argument is a separate element
# field = "sample-key-value" # command = ["get", "sample-key"]
# type = "string" # # 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 ## specify server password
# password = "s#cr@t%" # password = "s#cr@t%"

View File

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