diff --git a/plugins/inputs/redis/redis.go b/plugins/inputs/redis/redis.go index d65724d3d9c7a..0b0a91997f096 100644 --- a/plugins/inputs/redis/redis.go +++ b/plugins/inputs/redis/redis.go @@ -30,7 +30,7 @@ type Redis struct { Password string tls.ClientConfig - Log telegraf.Logger + Log telegraf.Logger `toml:"-"` clients []Client connected bool @@ -40,6 +40,7 @@ type Client interface { Do(returnType string, args ...interface{}) (interface{}, error) Info() *redis.StringCmd BaseTags() map[string]string + Close() error } type RedisClient struct { @@ -185,6 +186,10 @@ func (r *RedisClient) BaseTags() map[string]string { return tags } +func (r *RedisClient) Close() error { + return r.client.Close() +} + var replicationSlaveMetricPrefix = regexp.MustCompile(`^slave\d+`) var Tracking = map[string]string{ @@ -686,3 +691,18 @@ func coerceType(value interface{}, typ reflect.Type) reflect.Value { } return reflect.ValueOf(value) } + +func (r *Redis) Start(acc telegraf.Accumulator) error { + r.acc = acc + return nil +} + +//Stop close the client through ServiceInput interface Start/Stop methods impl. +func (r *Redis) Stop() { + for _, c := range r.clients { + err := c.Close() + if err != nil { + r.Log.Errorf("error closing client: %v", err) + } + } +} diff --git a/plugins/inputs/redis/redis_test.go b/plugins/inputs/redis/redis_test.go index a7ca994c53f80..8d57a653d3507 100644 --- a/plugins/inputs/redis/redis_test.go +++ b/plugins/inputs/redis/redis_test.go @@ -28,6 +28,10 @@ func (t *testClient) Do(_ string, _ ...interface{}) (interface{}, error) { return 2, nil } +func (t *testClient) Close() error { + return nil +} + func TestRedisConnectIntegration(t *testing.T) { if testing.Short() { t.Skip("Skipping integration test in short mode")