Skip to content

Commit 905b22c

Browse files
authored
Closing all idle connections in docker input plugin (#9243)
This prevents error "too many open files" in most cases
1 parent 1d4b8d6 commit 905b22c

3 files changed

Lines changed: 21 additions & 3 deletions

File tree

plugins/inputs/docker/client.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ type Client interface {
2323
ServiceList(ctx context.Context, options types.ServiceListOptions) ([]swarm.Service, error)
2424
TaskList(ctx context.Context, options types.TaskListOptions) ([]swarm.Task, error)
2525
NodeList(ctx context.Context, options types.NodeListOptions) ([]swarm.Node, error)
26+
Close() error
2627
}
2728

2829
func NewEnvClient() (Client, error) {
@@ -76,3 +77,6 @@ func (c *SocketClient) TaskList(ctx context.Context, options types.TaskListOptio
7677
func (c *SocketClient) NodeList(ctx context.Context, options types.NodeListOptions) ([]swarm.Node, error) {
7778
return c.client.NodeList(ctx, options)
7879
}
80+
func (c *SocketClient) Close() error {
81+
return c.client.Close()
82+
}

plugins/inputs/docker/docker.go

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -123,7 +123,7 @@ var sampleConfig = `
123123
## Whether to report for each container per-device blkio (8:0, 8:1...),
124124
## network (eth0, eth1, ...) and cpu (cpu0, cpu1, ...) stats or not.
125125
## Usage of this setting is discouraged since it will be deprecated in favor of 'perdevice_include'.
126-
## Default value is 'true' for backwards compatibility, please set it to 'false' so that 'perdevice_include' setting
126+
## Default value is 'true' for backwards compatibility, please set it to 'false' so that 'perdevice_include' setting
127127
## is honored.
128128
perdevice = true
129129
@@ -134,12 +134,12 @@ var sampleConfig = `
134134
135135
## Whether to report for each container total blkio and network stats or not.
136136
## Usage of this setting is discouraged since it will be deprecated in favor of 'total_include'.
137-
## Default value is 'false' for backwards compatibility, please set it to 'true' so that 'total_include' setting
137+
## Default value is 'false' for backwards compatibility, please set it to 'true' so that 'total_include' setting
138138
## is honored.
139139
total = false
140140
141141
## Specifies for which classes a total metric should be issued. Total is an aggregated of the 'perdevice' values.
142-
## Possible values are 'cpu', 'blkio' and 'network'
142+
## Possible values are 'cpu', 'blkio' and 'network'
143143
## Total 'cpu' is reported directly by Docker daemon, and 'network' and 'blkio' totals are aggregated by this plugin.
144144
## Please note that this setting has no effect if 'total' is set to 'false'
145145
# total_include = ["cpu", "blkio", "network"]
@@ -213,6 +213,9 @@ func (d *Docker) Gather(acc telegraf.Accumulator) error {
213213
d.client = c
214214
}
215215

216+
// Close any idle connections in the end of gathering
217+
defer d.client.Close()
218+
216219
// Create label filters if not already created
217220
if !d.filtersCreated {
218221
err := d.createLabelFilters()

plugins/inputs/docker/docker_test.go

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ type MockClient struct {
2626
ServiceListF func(ctx context.Context, options types.ServiceListOptions) ([]swarm.Service, error)
2727
TaskListF func(ctx context.Context, options types.TaskListOptions) ([]swarm.Task, error)
2828
NodeListF func(ctx context.Context, options types.NodeListOptions) ([]swarm.Node, error)
29+
CloseF func() error
2930
}
3031

3132
func (c *MockClient) Info(ctx context.Context) (types.Info, error) {
@@ -75,6 +76,10 @@ func (c *MockClient) NodeList(
7576
return c.NodeListF(ctx, options)
7677
}
7778

79+
func (c *MockClient) Close() error {
80+
return c.CloseF()
81+
}
82+
7883
var baseClient = MockClient{
7984
InfoF: func(context.Context) (types.Info, error) {
8085
return info, nil
@@ -97,6 +102,9 @@ var baseClient = MockClient{
97102
NodeListF: func(context.Context, types.NodeListOptions) ([]swarm.Node, error) {
98103
return NodeList, nil
99104
},
105+
CloseF: func() error {
106+
return nil
107+
},
100108
}
101109

102110
func newClient(_ string, _ *tls.Config) (Client, error) {
@@ -279,6 +287,9 @@ func TestDocker_WindowsMemoryContainerStats(t *testing.T) {
279287
NodeListF: func(context.Context, types.NodeListOptions) ([]swarm.Node, error) {
280288
return NodeList, nil
281289
},
290+
CloseF: func() error {
291+
return nil
292+
},
282293
}, nil
283294
},
284295
}

0 commit comments

Comments
 (0)