Closing all idle connections in docker input plugin (#9243)

This prevents error "too many open files" in most cases
This commit is contained in:
Vyacheslav Stepanov 2021-06-15 07:23:39 +03:00 committed by GitHub
parent 1d4b8d62f5
commit 905b22cac9
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 21 additions and 3 deletions

View File

@ -23,6 +23,7 @@ type Client interface {
ServiceList(ctx context.Context, options types.ServiceListOptions) ([]swarm.Service, error) ServiceList(ctx context.Context, options types.ServiceListOptions) ([]swarm.Service, error)
TaskList(ctx context.Context, options types.TaskListOptions) ([]swarm.Task, error) TaskList(ctx context.Context, options types.TaskListOptions) ([]swarm.Task, error)
NodeList(ctx context.Context, options types.NodeListOptions) ([]swarm.Node, error) NodeList(ctx context.Context, options types.NodeListOptions) ([]swarm.Node, error)
Close() error
} }
func NewEnvClient() (Client, 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) { func (c *SocketClient) NodeList(ctx context.Context, options types.NodeListOptions) ([]swarm.Node, error) {
return c.client.NodeList(ctx, options) return c.client.NodeList(ctx, options)
} }
func (c *SocketClient) Close() error {
return c.client.Close()
}

View File

@ -123,7 +123,7 @@ var sampleConfig = `
## Whether to report for each container per-device blkio (8:0, 8:1...), ## Whether to report for each container per-device blkio (8:0, 8:1...),
## network (eth0, eth1, ...) and cpu (cpu0, cpu1, ...) stats or not. ## 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'. ## 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. ## is honored.
perdevice = true perdevice = true
@ -134,12 +134,12 @@ var sampleConfig = `
## Whether to report for each container total blkio and network stats or not. ## 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'. ## 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. ## is honored.
total = false total = false
## Specifies for which classes a total metric should be issued. Total is an aggregated of the 'perdevice' values. ## 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. ## 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' ## Please note that this setting has no effect if 'total' is set to 'false'
# total_include = ["cpu", "blkio", "network"] # total_include = ["cpu", "blkio", "network"]
@ -213,6 +213,9 @@ func (d *Docker) Gather(acc telegraf.Accumulator) error {
d.client = c d.client = c
} }
// Close any idle connections in the end of gathering
defer d.client.Close()
// Create label filters if not already created // Create label filters if not already created
if !d.filtersCreated { if !d.filtersCreated {
err := d.createLabelFilters() err := d.createLabelFilters()

View File

@ -26,6 +26,7 @@ type MockClient struct {
ServiceListF func(ctx context.Context, options types.ServiceListOptions) ([]swarm.Service, error) ServiceListF func(ctx context.Context, options types.ServiceListOptions) ([]swarm.Service, error)
TaskListF func(ctx context.Context, options types.TaskListOptions) ([]swarm.Task, error) TaskListF func(ctx context.Context, options types.TaskListOptions) ([]swarm.Task, error)
NodeListF func(ctx context.Context, options types.NodeListOptions) ([]swarm.Node, 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) { func (c *MockClient) Info(ctx context.Context) (types.Info, error) {
@ -75,6 +76,10 @@ func (c *MockClient) NodeList(
return c.NodeListF(ctx, options) return c.NodeListF(ctx, options)
} }
func (c *MockClient) Close() error {
return c.CloseF()
}
var baseClient = MockClient{ var baseClient = MockClient{
InfoF: func(context.Context) (types.Info, error) { InfoF: func(context.Context) (types.Info, error) {
return info, nil return info, nil
@ -97,6 +102,9 @@ var baseClient = MockClient{
NodeListF: func(context.Context, types.NodeListOptions) ([]swarm.Node, error) { NodeListF: func(context.Context, types.NodeListOptions) ([]swarm.Node, error) {
return NodeList, nil return NodeList, nil
}, },
CloseF: func() error {
return nil
},
} }
func newClient(_ string, _ *tls.Config) (Client, error) { 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) { NodeListF: func(context.Context, types.NodeListOptions) ([]swarm.Node, error) {
return NodeList, nil return NodeList, nil
}, },
CloseF: func() error {
return nil
},
}, nil }, nil
}, },
} }