Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

docs: improve redis commands documentation #9606

Merged
merged 2 commits into from
Aug 17, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 9 additions & 3 deletions plugins/inputs/redis/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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%"
Expand Down
38 changes: 28 additions & 10 deletions plugins/inputs/redis/redis.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,8 @@ type Redis struct {

Log telegraf.Logger

clients []Client
initialized bool
clients []Client
connected bool
}

type Client interface {
Expand Down Expand Up @@ -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%"
Expand All @@ -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
}

Expand Down Expand Up @@ -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
}
Expand All @@ -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
}

Expand Down