From 905b22cac9d3f781fc12279e11f455ee2185a464 Mon Sep 17 00:00:00 2001 From: Vyacheslav Stepanov Date: Tue, 15 Jun 2021 07:23:39 +0300 Subject: [PATCH] Closing all idle connections in docker input plugin (#9243) This prevents error "too many open files" in most cases --- plugins/inputs/docker/client.go | 4 ++++ plugins/inputs/docker/docker.go | 9 ++++++--- plugins/inputs/docker/docker_test.go | 11 +++++++++++ 3 files changed, 21 insertions(+), 3 deletions(-) diff --git a/plugins/inputs/docker/client.go b/plugins/inputs/docker/client.go index 3ea24ea74..14e439698 100644 --- a/plugins/inputs/docker/client.go +++ b/plugins/inputs/docker/client.go @@ -23,6 +23,7 @@ type Client interface { ServiceList(ctx context.Context, options types.ServiceListOptions) ([]swarm.Service, error) TaskList(ctx context.Context, options types.TaskListOptions) ([]swarm.Task, error) NodeList(ctx context.Context, options types.NodeListOptions) ([]swarm.Node, error) + Close() error } func NewEnvClient() (Client, error) { @@ -76,3 +77,6 @@ func (c *SocketClient) TaskList(ctx context.Context, options types.TaskListOptio func (c *SocketClient) NodeList(ctx context.Context, options types.NodeListOptions) ([]swarm.Node, error) { return c.client.NodeList(ctx, options) } +func (c *SocketClient) Close() error { + return c.client.Close() +} diff --git a/plugins/inputs/docker/docker.go b/plugins/inputs/docker/docker.go index 1b4435117..47eab7ce2 100644 --- a/plugins/inputs/docker/docker.go +++ b/plugins/inputs/docker/docker.go @@ -123,7 +123,7 @@ var sampleConfig = ` ## Whether to report for each container per-device blkio (8:0, 8:1...), ## network (eth0, eth1, ...) and cpu (cpu0, cpu1, ...) stats or not. ## Usage of this setting is discouraged since it will be deprecated in favor of 'perdevice_include'. - ## Default value is 'true' for backwards compatibility, please set it to 'false' so that 'perdevice_include' setting + ## Default value is 'true' for backwards compatibility, please set it to 'false' so that 'perdevice_include' setting ## is honored. perdevice = true @@ -134,12 +134,12 @@ var sampleConfig = ` ## Whether to report for each container total blkio and network stats or not. ## Usage of this setting is discouraged since it will be deprecated in favor of 'total_include'. - ## Default value is 'false' for backwards compatibility, please set it to 'true' so that 'total_include' setting + ## Default value is 'false' for backwards compatibility, please set it to 'true' so that 'total_include' setting ## is honored. total = false ## Specifies for which classes a total metric should be issued. Total is an aggregated of the 'perdevice' values. - ## Possible values are 'cpu', 'blkio' and 'network' + ## Possible values are 'cpu', 'blkio' and 'network' ## Total 'cpu' is reported directly by Docker daemon, and 'network' and 'blkio' totals are aggregated by this plugin. ## Please note that this setting has no effect if 'total' is set to 'false' # total_include = ["cpu", "blkio", "network"] @@ -213,6 +213,9 @@ func (d *Docker) Gather(acc telegraf.Accumulator) error { d.client = c } + // Close any idle connections in the end of gathering + defer d.client.Close() + // Create label filters if not already created if !d.filtersCreated { err := d.createLabelFilters() diff --git a/plugins/inputs/docker/docker_test.go b/plugins/inputs/docker/docker_test.go index 88adc600e..f5a8ff7a8 100644 --- a/plugins/inputs/docker/docker_test.go +++ b/plugins/inputs/docker/docker_test.go @@ -26,6 +26,7 @@ type MockClient struct { ServiceListF func(ctx context.Context, options types.ServiceListOptions) ([]swarm.Service, error) TaskListF func(ctx context.Context, options types.TaskListOptions) ([]swarm.Task, error) NodeListF func(ctx context.Context, options types.NodeListOptions) ([]swarm.Node, error) + CloseF func() error } func (c *MockClient) Info(ctx context.Context) (types.Info, error) { @@ -75,6 +76,10 @@ func (c *MockClient) NodeList( return c.NodeListF(ctx, options) } +func (c *MockClient) Close() error { + return c.CloseF() +} + var baseClient = MockClient{ InfoF: func(context.Context) (types.Info, error) { return info, nil @@ -97,6 +102,9 @@ var baseClient = MockClient{ NodeListF: func(context.Context, types.NodeListOptions) ([]swarm.Node, error) { return NodeList, nil }, + CloseF: func() error { + return nil + }, } func newClient(_ string, _ *tls.Config) (Client, error) { @@ -279,6 +287,9 @@ func TestDocker_WindowsMemoryContainerStats(t *testing.T) { NodeListF: func(context.Context, types.NodeListOptions) ([]swarm.Node, error) { return NodeList, nil }, + CloseF: func() error { + return nil + }, }, nil }, }