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

Closing all idle connections in docker input plugin #9243

Merged
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
4 changes: 4 additions & 0 deletions plugins/inputs/docker/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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()
}
9 changes: 6 additions & 3 deletions plugins/inputs/docker/docker.go
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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"]
Expand Down Expand Up @@ -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()
Expand Down
11 changes: 11 additions & 0 deletions plugins/inputs/docker/docker_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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
Expand All @@ -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) {
Expand Down Expand Up @@ -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
},
}
Expand Down