From 81df6a34f15cab9d388ab7773f8f89703321c710 Mon Sep 17 00:00:00 2001 From: Aditya C S Date: Mon, 21 Aug 2017 01:12:34 +0530 Subject: [PATCH 01/12] Collect Docker Swarm metrics in docker input plugin --- plugins/inputs/docker/client.go | 13 ++++++ plugins/inputs/docker/docker.go | 81 +++++++++++++++++++++++++++++++++ 2 files changed, 94 insertions(+) diff --git a/plugins/inputs/docker/client.go b/plugins/inputs/docker/client.go index e918231a030f6..a021b59c46845 100644 --- a/plugins/inputs/docker/client.go +++ b/plugins/inputs/docker/client.go @@ -6,6 +6,7 @@ import ( "net/http" "github.com/docker/docker/api/types" + "github.com/docker/docker/api/types/swarm" docker "github.com/docker/docker/client" "github.com/docker/go-connections/sockets" ) @@ -20,6 +21,9 @@ type Client interface { ContainerList(ctx context.Context, options types.ContainerListOptions) ([]types.Container, error) ContainerStats(ctx context.Context, containerID string, stream bool) (types.ContainerStats, error) ContainerInspect(ctx context.Context, containerID string) (types.ContainerJSON, error) + 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) } func NewEnvClient() (Client, error) { @@ -65,3 +69,12 @@ func (c *SocketClient) ContainerStats(ctx context.Context, containerID string, s func (c *SocketClient) ContainerInspect(ctx context.Context, containerID string) (types.ContainerJSON, error) { return c.client.ContainerInspect(ctx, containerID) } +func (c *SocketClient) ServiceList(ctx context.Context, options types.ServiceListOptions) ([]swarm.Service, error) { + return c.client.ServiceList(ctx, options) +} +func (c *SocketClient) TaskList(ctx context.Context, options types.TaskListOptions) ([]swarm.Task, error) { + return c.client.TaskList(ctx, options) +} +func (c *SocketClient) NodeList(ctx context.Context, options types.NodeListOptions) ([]swarm.Node, error) { + return c.client.NodeList(ctx, options) +} diff --git a/plugins/inputs/docker/docker.go b/plugins/inputs/docker/docker.go index 3634e596ff353..e64d60057e27c 100644 --- a/plugins/inputs/docker/docker.go +++ b/plugins/inputs/docker/docker.go @@ -14,6 +14,7 @@ import ( "time" "github.com/docker/docker/api/types" + "github.com/docker/docker/api/types/swarm" "github.com/influxdata/telegraf" "github.com/influxdata/telegraf/filter" "github.com/influxdata/telegraf/internal" @@ -35,6 +36,8 @@ type Docker struct { Endpoint string ContainerNames []string + SwarmEnabled bool `toml:swarm_enabled` + Timeout internal.Duration PerDevice bool `toml:"perdevice"` Total bool `toml:"total"` @@ -82,6 +85,9 @@ var sampleConfig = ` ## To use environment variables (ie, docker-machine), set endpoint = "ENV" endpoint = "unix:///var/run/docker.sock" + ## Set to true to collect Swarm metrics(desired_replicas, running_replicas) + swarm_enabled = false + ## Only collect metrics for these containers, collect all if empty container_names = [] @@ -160,6 +166,13 @@ func (d *Docker) Gather(acc telegraf.Accumulator) error { acc.AddError(err) } + if d.SwarmEnabled { + err := d.gatherSwarmInfo(acc) + if err != nil { + acc.AddError(err) + } + } + // List containers opts := types.ContainerListOptions{} ctx, cancel := context.WithTimeout(context.Background(), d.Timeout.Duration) @@ -187,6 +200,74 @@ func (d *Docker) Gather(acc telegraf.Accumulator) error { return nil } +func (d *Docker) gatherSwarmInfo(acc telegraf.Accumulator) error { + + ctx, cancel := context.WithTimeout(context.Background(), d.Timeout.Duration) + defer cancel() + services, err := d.client.ServiceList(ctx, types.ServiceListOptions{}) + if err != nil { + return err + } + + if len(services) > 0 { + + tasks, err := d.client.TaskList(ctx, types.TaskListOptions{}) + if err != nil { + return err + } + + nodes, err := d.client.NodeList(ctx, types.NodeListOptions{}) + if err != nil { + return err + } + + running := map[string]int{} + tasksNoShutdown := map[string]int{} + + activeNodes := make(map[string]struct{}) + for _, n := range nodes { + if n.Status.State != swarm.NodeStateDown { + activeNodes[n.ID] = struct{}{} + } + } + + for _, task := range tasks { + if task.DesiredState != swarm.TaskStateShutdown { + tasksNoShutdown[task.ServiceID]++ + } + + if _, nodeActive := activeNodes[task.NodeID]; nodeActive && task.Status.State == swarm.TaskStateRunning { + running[task.ServiceID]++ + } + } + + + for _, service := range services { + tags := map[string]string{} + fields := make(map[string]interface{}) + now := time.Now() + tags["swarm_service_id"] = service.ID + tags["swarm_service_name"] = service.Spec.Name + if service.Spec.Mode.Replicated != nil && service.Spec.Mode.Replicated.Replicas != nil { + fields["swarm_service_mode"] = "replicated" + fields["swarm_tasks_running"] = running[service.ID] + fields["swarm_tasks_desired"] = *service.Spec.Mode.Replicated.Replicas + } else if service.Spec.Mode.Global != nil { + fields["swarm_service_mode"] = "global" + fields["swarm_tasks_running"] = running[service.ID] + fields["swarm_tasks_desired"] = tasksNoShutdown[service.ID] + } + // Add metrics + acc.AddFields("docker_swarm", + fields, + tags, + now) + } + } + + return nil +} + func (d *Docker) gatherInfo(acc telegraf.Accumulator) error { // Init vars dataFields := make(map[string]interface{}) From 6fccb64c39dc765e6e4560a7119a33f6ecc666f9 Mon Sep 17 00:00:00 2001 From: Aditya C S Date: Mon, 21 Aug 2017 01:17:06 +0530 Subject: [PATCH 02/12] Collect Docker Swarm metrics in docker input plugin --- plugins/inputs/docker/docker.go | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/plugins/inputs/docker/docker.go b/plugins/inputs/docker/docker.go index e64d60057e27c..82b852fd538be 100644 --- a/plugins/inputs/docker/docker.go +++ b/plugins/inputs/docker/docker.go @@ -36,7 +36,7 @@ type Docker struct { Endpoint string ContainerNames []string - SwarmEnabled bool `toml:swarm_enabled` + SwarmEnabled bool `toml:swarm_enabled` Timeout internal.Duration PerDevice bool `toml:"perdevice"` @@ -201,7 +201,7 @@ func (d *Docker) Gather(acc telegraf.Accumulator) error { } func (d *Docker) gatherSwarmInfo(acc telegraf.Accumulator) error { - + ctx, cancel := context.WithTimeout(context.Background(), d.Timeout.Duration) defer cancel() services, err := d.client.ServiceList(ctx, types.ServiceListOptions{}) @@ -241,7 +241,6 @@ func (d *Docker) gatherSwarmInfo(acc telegraf.Accumulator) error { } } - for _, service := range services { tags := map[string]string{} fields := make(map[string]interface{}) @@ -264,7 +263,7 @@ func (d *Docker) gatherSwarmInfo(acc telegraf.Accumulator) error { now) } } - + return nil } From 55b094bc11c758f7132a6b3b8cc9da22dec5f96d Mon Sep 17 00:00:00 2001 From: Aditya C S Date: Mon, 21 Aug 2017 01:17:06 +0530 Subject: [PATCH 03/12] Collect Docker Swarm metrics in docker input plugin --- plugins/inputs/docker/docker.go | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/plugins/inputs/docker/docker.go b/plugins/inputs/docker/docker.go index e64d60057e27c..82b852fd538be 100644 --- a/plugins/inputs/docker/docker.go +++ b/plugins/inputs/docker/docker.go @@ -36,7 +36,7 @@ type Docker struct { Endpoint string ContainerNames []string - SwarmEnabled bool `toml:swarm_enabled` + SwarmEnabled bool `toml:swarm_enabled` Timeout internal.Duration PerDevice bool `toml:"perdevice"` @@ -201,7 +201,7 @@ func (d *Docker) Gather(acc telegraf.Accumulator) error { } func (d *Docker) gatherSwarmInfo(acc telegraf.Accumulator) error { - + ctx, cancel := context.WithTimeout(context.Background(), d.Timeout.Duration) defer cancel() services, err := d.client.ServiceList(ctx, types.ServiceListOptions{}) @@ -241,7 +241,6 @@ func (d *Docker) gatherSwarmInfo(acc telegraf.Accumulator) error { } } - for _, service := range services { tags := map[string]string{} fields := make(map[string]interface{}) @@ -264,7 +263,7 @@ func (d *Docker) gatherSwarmInfo(acc telegraf.Accumulator) error { now) } } - + return nil } From 9d23a6ad2afaaa63d342f56be61cd39a03eeddd4 Mon Sep 17 00:00:00 2001 From: Aditya C S Date: Wed, 23 Aug 2017 01:45:49 +0530 Subject: [PATCH 04/12] added unit testcases --- plugins/inputs/docker/docker_test.go | 71 ++++++++++++++++++++++++ plugins/inputs/docker/docker_testdata.go | 63 +++++++++++++++++++++ 2 files changed, 134 insertions(+) diff --git a/plugins/inputs/docker/docker_test.go b/plugins/inputs/docker/docker_test.go index 95adfcf8e6757..a25b049ae53cd 100644 --- a/plugins/inputs/docker/docker_test.go +++ b/plugins/inputs/docker/docker_test.go @@ -8,6 +8,7 @@ import ( "github.com/influxdata/telegraf/testutil" "github.com/docker/docker/api/types" + "github.com/docker/docker/api/types/swarm" "github.com/stretchr/testify/require" ) @@ -16,6 +17,9 @@ type MockClient struct { ContainerListF func(ctx context.Context, options types.ContainerListOptions) ([]types.Container, error) ContainerStatsF func(ctx context.Context, containerID string, stream bool) (types.ContainerStats, error) ContainerInspectF func(ctx context.Context, containerID string) (types.ContainerJSON, error) + 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) } func (c *MockClient) Info(ctx context.Context) (types.Info, error) { @@ -44,6 +48,27 @@ func (c *MockClient) ContainerInspect( return c.ContainerInspectF(ctx, containerID) } +func (c *MockClient) ServiceList( + ctx context.Context, + options types.ServiceListOptions, +) ([]swarm.Service, error) { + return c.ServiceListF(ctx, options) +} + +func (c *MockClient) TaskList( + ctx context.Context, + options types.TaskListOptions, +) ([]swarm.Task, error) { + return c.TaskListF(ctx, options) +} + +func (c *MockClient) NodeList( + ctx context.Context, + options types.NodeListOptions, +) ([]swarm.Node, error) { + return c.NodeListF(ctx, options) +} + func newClient(host string, tlsConfig *tls.Config) (Client, error) { return &MockClient{ InfoF: func(context.Context) (types.Info, error) { @@ -58,6 +83,15 @@ func newClient(host string, tlsConfig *tls.Config) (Client, error) { ContainerInspectF: func(context.Context, string) (types.ContainerJSON, error) { return containerInspect, nil }, + ServiceListF: func(context.Context, types.ServiceListOptions) ([]swarm.Service, error) { + return ServiceList, nil + }, + TaskListF: func(context.Context, types.TaskListOptions) ([]swarm.Task, error) { + return TaskList, nil + }, + NodeListF: func(context.Context, types.NodeListOptions) ([]swarm.Node, error) { + return NodeList, nil + }, }, nil } @@ -227,6 +261,15 @@ func TestDocker_WindowsMemoryContainerStats(t *testing.T) { ContainerInspectF: func(ctx context.Context, containerID string) (types.ContainerJSON, error) { return containerInspect, nil }, + ServiceListF: func(context.Context, types.ServiceListOptions) ([]swarm.Service, error) { + return ServiceList, nil + }, + TaskListF: func(context.Context, types.TaskListOptions) ([]swarm.Task, error) { + return TaskList, nil + }, + NodeListF: func(context.Context, types.NodeListOptions) ([]swarm.Node, error) { + return NodeList, nil + }, }, nil }, } @@ -436,3 +479,31 @@ func TestDockerGatherInfo(t *testing.T) { }, ) } + +func TestDockerGatherSwarmInfo(t *testing.T) { + var acc testutil.Accumulator + d := Docker{ + newClient: newClient, + } + + err := acc.GatherError(d.Gather) + require.NoError(t, err) + + //gatherSwarmInfo(&acc) + + // test docker_container_net measurement + + acc.AssertContainsTaggedFields(t, + "docker_swarm", + map[string]interface{}{ + "swarm_service_mode": "replicated", + "swarm_tasks_running": int(3), + "swarm_tasks_desired": int(3), + }, + map[string]string{ + "swarm_service_id": "qolkls9g5iasdiuihcyz9rnx2", + "swarm_service_name": "jenkins", + }, + ) +} + diff --git a/plugins/inputs/docker/docker_testdata.go b/plugins/inputs/docker/docker_testdata.go index d16a3a728c6e1..eb2c48c8e7661 100644 --- a/plugins/inputs/docker/docker_testdata.go +++ b/plugins/inputs/docker/docker_testdata.go @@ -6,6 +6,7 @@ import ( "time" "github.com/docker/docker/api/types" + "github.com/docker/docker/api/types/swarm" "github.com/docker/docker/api/types/container" "github.com/docker/docker/api/types/registry" ) @@ -133,6 +134,68 @@ var containerList = []types.Container{ }, } +var four = uint64(4) +var ServiceList = []swarm.Service{ + swarm.Service{ + ID: "qolkls9g5iasdiuihcyz9rnx2", + Spec: swarm.ServiceSpec{ + Annotations: swarm.Annotations{ + Name: "test1", + }, + Mode: swarm.ServiceMode{ + Replicated: &swarm.ReplicatedService{ + Replicas: &four, + }, + }, + }, + }, + swarm.Service{ + ID: "qolkls9g5iasdiuihcyz9rn3", + Spec: swarm.ServiceSpec{ + Annotations: swarm.Annotations{ + Name: "test2", + }, + Mode: swarm.ServiceMode{ + Global: &swarm.GlobalService{}, + }, + }, + }, +} + +var TaskList = []swarm.Task{ + swarm.Task{ + ServiceID: "qolkls9g5iasdiuihcyz9rnx2", + NodeID: "0cl4jturcyd1ks3fwpd010kor", + Status: swarm.TaskStatus{ + State: "running", + }, + DesiredState: "running", + }, + swarm.Task{ + ID: "pswshkmzoaht6e0i9pqqdmdg8", + NodeID: "0cl4jturcyd1ks3fwpd010kor", + Status: swarm.TaskStatus{ + State: "running", + }, + DesiredState: "running", + }, +} + +var NodeList = []swarm.Node{ + swarm.Node{ + ID: "0cl4jturcyd1ks3fwpd010kor", + Status: swarm.NodeStatus{ + State: "ready", + }, + }, + swarm.Node{ + ID: "0cl4jturcyd1ks3fwpd010kor", + Status: swarm.NodeStatus{ + State: "ready", + }, + }, +} + func containerStats() types.ContainerStats { var stat types.ContainerStats jsonStat := ` From 5695c79420f679ab3fadd2a2f95adf05f28e127a Mon Sep 17 00:00:00 2001 From: Aditya C S Date: Wed, 23 Aug 2017 01:48:46 +0530 Subject: [PATCH 05/12] added unit testcases --- plugins/inputs/docker/docker_test.go | 17 +++--- plugins/inputs/docker/docker_testdata.go | 70 ++++++++++++------------ 2 files changed, 43 insertions(+), 44 deletions(-) diff --git a/plugins/inputs/docker/docker_test.go b/plugins/inputs/docker/docker_test.go index a25b049ae53cd..255b1d368bb48 100644 --- a/plugins/inputs/docker/docker_test.go +++ b/plugins/inputs/docker/docker_test.go @@ -49,21 +49,21 @@ func (c *MockClient) ContainerInspect( } func (c *MockClient) ServiceList( - ctx context.Context, + ctx context.Context, options types.ServiceListOptions, ) ([]swarm.Service, error) { return c.ServiceListF(ctx, options) } func (c *MockClient) TaskList( - ctx context.Context, + ctx context.Context, options types.TaskListOptions, ) ([]swarm.Task, error) { return c.TaskListF(ctx, options) } func (c *MockClient) NodeList( - ctx context.Context, + ctx context.Context, options types.NodeListOptions, ) ([]swarm.Node, error) { return c.NodeListF(ctx, options) @@ -492,18 +492,17 @@ func TestDockerGatherSwarmInfo(t *testing.T) { //gatherSwarmInfo(&acc) // test docker_container_net measurement - + acc.AssertContainsTaggedFields(t, "docker_swarm", map[string]interface{}{ - "swarm_service_mode": "replicated", - "swarm_tasks_running": int(3), - "swarm_tasks_desired": int(3), + "swarm_service_mode": "replicated", + "swarm_tasks_running": int(3), + "swarm_tasks_desired": int(3), }, map[string]string{ - "swarm_service_id": "qolkls9g5iasdiuihcyz9rnx2", + "swarm_service_id": "qolkls9g5iasdiuihcyz9rnx2", "swarm_service_name": "jenkins", }, ) } - diff --git a/plugins/inputs/docker/docker_testdata.go b/plugins/inputs/docker/docker_testdata.go index eb2c48c8e7661..63b15615c50cb 100644 --- a/plugins/inputs/docker/docker_testdata.go +++ b/plugins/inputs/docker/docker_testdata.go @@ -6,9 +6,9 @@ import ( "time" "github.com/docker/docker/api/types" - "github.com/docker/docker/api/types/swarm" "github.com/docker/docker/api/types/container" "github.com/docker/docker/api/types/registry" + "github.com/docker/docker/api/types/swarm" ) var info = types.Info{ @@ -138,61 +138,61 @@ var four = uint64(4) var ServiceList = []swarm.Service{ swarm.Service{ ID: "qolkls9g5iasdiuihcyz9rnx2", - Spec: swarm.ServiceSpec{ - Annotations: swarm.Annotations{ - Name: "test1", + Spec: swarm.ServiceSpec{ + Annotations: swarm.Annotations{ + Name: "test1", + }, + Mode: swarm.ServiceMode{ + Replicated: &swarm.ReplicatedService{ + Replicas: &four, + }, }, - Mode: swarm.ServiceMode{ - Replicated: &swarm.ReplicatedService{ - Replicas: &four, - }, - }, - }, + }, }, swarm.Service{ ID: "qolkls9g5iasdiuihcyz9rn3", - Spec: swarm.ServiceSpec{ - Annotations: swarm.Annotations{ - Name: "test2", + Spec: swarm.ServiceSpec{ + Annotations: swarm.Annotations{ + Name: "test2", + }, + Mode: swarm.ServiceMode{ + Global: &swarm.GlobalService{}, }, - Mode: swarm.ServiceMode{ - Global: &swarm.GlobalService{}, - }, - }, + }, }, } var TaskList = []swarm.Task{ swarm.Task{ - ServiceID: "qolkls9g5iasdiuihcyz9rnx2", - NodeID: "0cl4jturcyd1ks3fwpd010kor", - Status: swarm.TaskStatus{ - State: "running", - }, - DesiredState: "running", + ServiceID: "qolkls9g5iasdiuihcyz9rnx2", + NodeID: "0cl4jturcyd1ks3fwpd010kor", + Status: swarm.TaskStatus{ + State: "running", + }, + DesiredState: "running", }, swarm.Task{ - ID: "pswshkmzoaht6e0i9pqqdmdg8", - NodeID: "0cl4jturcyd1ks3fwpd010kor", - Status: swarm.TaskStatus{ - State: "running", - }, - DesiredState: "running", + ID: "pswshkmzoaht6e0i9pqqdmdg8", + NodeID: "0cl4jturcyd1ks3fwpd010kor", + Status: swarm.TaskStatus{ + State: "running", + }, + DesiredState: "running", }, } var NodeList = []swarm.Node{ swarm.Node{ ID: "0cl4jturcyd1ks3fwpd010kor", - Status: swarm.NodeStatus{ - State: "ready", - }, + Status: swarm.NodeStatus{ + State: "ready", + }, }, swarm.Node{ ID: "0cl4jturcyd1ks3fwpd010kor", - Status: swarm.NodeStatus{ - State: "ready", - }, + Status: swarm.NodeStatus{ + State: "ready", + }, }, } From becfc10ff02cf5beb70cce7aec7d0d18080e115e Mon Sep 17 00:00:00 2001 From: Aditya C S Date: Wed, 23 Aug 2017 13:18:41 +0530 Subject: [PATCH 06/12] Added unit test cases --- plugins/inputs/docker/docker.go | 2 +- plugins/inputs/docker/docker_test.go | 22 +++++++++++++++++----- plugins/inputs/docker/docker_testdata.go | 19 +++++++++++++++---- 3 files changed, 33 insertions(+), 10 deletions(-) diff --git a/plugins/inputs/docker/docker.go b/plugins/inputs/docker/docker.go index a0c9b1e2fe9df..2e5fd6d40c40e 100644 --- a/plugins/inputs/docker/docker.go +++ b/plugins/inputs/docker/docker.go @@ -87,7 +87,7 @@ var sampleConfig = ` ## Set to true to collect Swarm metrics(desired_replicas, running_replicas) swarm_enabled = false - + ## Only collect metrics for these containers, collect all if empty container_names = [] diff --git a/plugins/inputs/docker/docker_test.go b/plugins/inputs/docker/docker_test.go index 255b1d368bb48..5a55cd3e1f496 100644 --- a/plugins/inputs/docker/docker_test.go +++ b/plugins/inputs/docker/docker_test.go @@ -489,20 +489,32 @@ func TestDockerGatherSwarmInfo(t *testing.T) { err := acc.GatherError(d.Gather) require.NoError(t, err) - //gatherSwarmInfo(&acc) + d.gatherSwarmInfo(&acc) // test docker_container_net measurement - acc.AssertContainsTaggedFields(t, "docker_swarm", map[string]interface{}{ "swarm_service_mode": "replicated", - "swarm_tasks_running": int(3), - "swarm_tasks_desired": int(3), + "swarm_tasks_running": int(2), + "swarm_tasks_desired": uint64(2), }, map[string]string{ "swarm_service_id": "qolkls9g5iasdiuihcyz9rnx2", - "swarm_service_name": "jenkins", + "swarm_service_name": "test1", + }, + ) + + acc.AssertContainsTaggedFields(t, + "docker_swarm", + map[string]interface{}{ + "swarm_service_mode": "global", + "swarm_tasks_running": int(1), + "swarm_tasks_desired": int(1), + }, + map[string]string{ + "swarm_service_id": "qolkls9g5iasdiuihcyz9rn3", + "swarm_service_name": "test2", }, ) } diff --git a/plugins/inputs/docker/docker_testdata.go b/plugins/inputs/docker/docker_testdata.go index 63b15615c50cb..929119fcb760d 100644 --- a/plugins/inputs/docker/docker_testdata.go +++ b/plugins/inputs/docker/docker_testdata.go @@ -134,7 +134,7 @@ var containerList = []types.Container{ }, } -var four = uint64(4) +var two = uint64(2) var ServiceList = []swarm.Service{ swarm.Service{ ID: "qolkls9g5iasdiuihcyz9rnx2", @@ -144,7 +144,7 @@ var ServiceList = []swarm.Service{ }, Mode: swarm.ServiceMode{ Replicated: &swarm.ReplicatedService{ - Replicas: &four, + Replicas: &two, }, }, }, @@ -164,6 +164,7 @@ var ServiceList = []swarm.Service{ var TaskList = []swarm.Task{ swarm.Task{ + ID: "kwh0lv7hwwbh", ServiceID: "qolkls9g5iasdiuihcyz9rnx2", NodeID: "0cl4jturcyd1ks3fwpd010kor", Status: swarm.TaskStatus{ @@ -172,8 +173,18 @@ var TaskList = []swarm.Task{ DesiredState: "running", }, swarm.Task{ - ID: "pswshkmzoaht6e0i9pqqdmdg8", - NodeID: "0cl4jturcyd1ks3fwpd010kor", + ID: "u78m5ojbivc3", + ServiceID: "qolkls9g5iasdiuihcyz9rnx2", + NodeID: "0cl4jturcyd1ks3fwpd010kor", + Status: swarm.TaskStatus{ + State: "running", + }, + DesiredState: "running", + }, + swarm.Task{ + ID: "1n1uilkhr98l", + ServiceID: "qolkls9g5iasdiuihcyz9rn3", + NodeID: "0cl4jturcyd1ks3fwpd010kor", Status: swarm.TaskStatus{ State: "running", }, From a77e8938b643b26419abe489aa5dc9f11fef570c Mon Sep 17 00:00:00 2001 From: Aditya C S Date: Wed, 4 Oct 2017 00:47:57 +0530 Subject: [PATCH 07/12] review changes --- plugins/inputs/docker/docker.go | 22 +++++++++++----------- 1 file changed, 11 insertions(+), 11 deletions(-) diff --git a/plugins/inputs/docker/docker.go b/plugins/inputs/docker/docker.go index 2e5fd6d40c40e..0ea8e86329198 100644 --- a/plugins/inputs/docker/docker.go +++ b/plugins/inputs/docker/docker.go @@ -36,7 +36,7 @@ type Docker struct { Endpoint string ContainerNames []string - SwarmEnabled bool `toml:"swarm_enabled"` + GatherServices bool `toml:"gather_services"` Timeout internal.Duration PerDevice bool `toml:"perdevice"` @@ -86,7 +86,7 @@ var sampleConfig = ` endpoint = "unix:///var/run/docker.sock" ## Set to true to collect Swarm metrics(desired_replicas, running_replicas) - swarm_enabled = false + gather_services = false ## Only collect metrics for these containers, collect all if empty container_names = [] @@ -166,7 +166,7 @@ func (d *Docker) Gather(acc telegraf.Accumulator) error { acc.AddError(err) } - if d.SwarmEnabled { + if d.GatherServices { err := d.gatherSwarmInfo(acc) if err != nil { acc.AddError(err) @@ -245,16 +245,16 @@ func (d *Docker) gatherSwarmInfo(acc telegraf.Accumulator) error { tags := map[string]string{} fields := make(map[string]interface{}) now := time.Now() - tags["swarm_service_id"] = service.ID - tags["swarm_service_name"] = service.Spec.Name + tags["service_id"] = service.ID + tags["service_name"] = service.Spec.Name if service.Spec.Mode.Replicated != nil && service.Spec.Mode.Replicated.Replicas != nil { - fields["swarm_service_mode"] = "replicated" - fields["swarm_tasks_running"] = running[service.ID] - fields["swarm_tasks_desired"] = *service.Spec.Mode.Replicated.Replicas + tags["service_mode"] = "replicated" + fields["tasks_running"] = running[service.ID] + fields["tasks_desired"] = *service.Spec.Mode.Replicated.Replicas } else if service.Spec.Mode.Global != nil { - fields["swarm_service_mode"] = "global" - fields["swarm_tasks_running"] = running[service.ID] - fields["swarm_tasks_desired"] = tasksNoShutdown[service.ID] + tags["service_mode"] = "global" + fields["tasks_running"] = running[service.ID] + fields["tasks_desired"] = tasksNoShutdown[service.ID] } // Add metrics acc.AddFields("docker_swarm", From 26f999753716b61e89425ac4663ab336cfdfa3fa Mon Sep 17 00:00:00 2001 From: Aditya C S Date: Wed, 4 Oct 2017 01:10:53 +0530 Subject: [PATCH 08/12] review changes --- plugins/inputs/docker/docker.go | 3 +++ 1 file changed, 3 insertions(+) diff --git a/plugins/inputs/docker/docker.go b/plugins/inputs/docker/docker.go index 0ea8e86329198..b379b6b25d508 100644 --- a/plugins/inputs/docker/docker.go +++ b/plugins/inputs/docker/docker.go @@ -6,6 +6,7 @@ import ( "encoding/json" "fmt" "io" + "log" "net/http" "regexp" "strconv" @@ -255,6 +256,8 @@ func (d *Docker) gatherSwarmInfo(acc telegraf.Accumulator) error { tags["service_mode"] = "global" fields["tasks_running"] = running[service.ID] fields["tasks_desired"] = tasksNoShutdown[service.ID] + } else { + log.Printf("E! Unknow Replicas Mode") } // Add metrics acc.AddFields("docker_swarm", From d0e1a998541d117710b760277c7745a465230be4 Mon Sep 17 00:00:00 2001 From: Aditya C S Date: Wed, 4 Oct 2017 01:13:05 +0530 Subject: [PATCH 09/12] review changes --- plugins/inputs/docker/docker_test.go | 20 ++++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) diff --git a/plugins/inputs/docker/docker_test.go b/plugins/inputs/docker/docker_test.go index 5a55cd3e1f496..0b69a83a924f4 100644 --- a/plugins/inputs/docker/docker_test.go +++ b/plugins/inputs/docker/docker_test.go @@ -495,26 +495,26 @@ func TestDockerGatherSwarmInfo(t *testing.T) { acc.AssertContainsTaggedFields(t, "docker_swarm", map[string]interface{}{ - "swarm_service_mode": "replicated", - "swarm_tasks_running": int(2), - "swarm_tasks_desired": uint64(2), + "service_mode": "replicated", + "tasks_running": int(2), + "tasks_desired": uint64(2), }, map[string]string{ - "swarm_service_id": "qolkls9g5iasdiuihcyz9rnx2", - "swarm_service_name": "test1", + "service_id": "qolkls9g5iasdiuihcyz9rnx2", + "service_name": "test1", }, ) acc.AssertContainsTaggedFields(t, "docker_swarm", map[string]interface{}{ - "swarm_service_mode": "global", - "swarm_tasks_running": int(1), - "swarm_tasks_desired": int(1), + "service_mode": "global", + "tasks_running": int(1), + "tasks_desired": int(1), }, map[string]string{ - "swarm_service_id": "qolkls9g5iasdiuihcyz9rn3", - "swarm_service_name": "test2", + "service_id": "qolkls9g5iasdiuihcyz9rn3", + "service_name": "test2", }, ) } From d1d45ba3bbf9f25ef361706f44ed87af5220026d Mon Sep 17 00:00:00 2001 From: Aditya C S Date: Wed, 4 Oct 2017 01:14:57 +0530 Subject: [PATCH 10/12] review changes --- plugins/inputs/docker/docker.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/plugins/inputs/docker/docker.go b/plugins/inputs/docker/docker.go index b379b6b25d508..171097621cd86 100644 --- a/plugins/inputs/docker/docker.go +++ b/plugins/inputs/docker/docker.go @@ -237,7 +237,7 @@ func (d *Docker) gatherSwarmInfo(acc telegraf.Accumulator) error { tasksNoShutdown[task.ServiceID]++ } - if _, nodeActive := activeNodes[task.NodeID]; nodeActive && task.Status.State == swarm.TaskStateRunning { + if task.Status.State == swarm.TaskStateRunning { running[task.ServiceID]++ } } From ccd65713c3812f6a5e1c9c058d76f57ba075a278 Mon Sep 17 00:00:00 2001 From: Aditya C S Date: Wed, 4 Oct 2017 01:24:29 +0530 Subject: [PATCH 11/12] review changes --- plugins/inputs/docker/docker_test.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/plugins/inputs/docker/docker_test.go b/plugins/inputs/docker/docker_test.go index 0b69a83a924f4..d67916f68c439 100644 --- a/plugins/inputs/docker/docker_test.go +++ b/plugins/inputs/docker/docker_test.go @@ -495,26 +495,26 @@ func TestDockerGatherSwarmInfo(t *testing.T) { acc.AssertContainsTaggedFields(t, "docker_swarm", map[string]interface{}{ - "service_mode": "replicated", "tasks_running": int(2), "tasks_desired": uint64(2), }, map[string]string{ "service_id": "qolkls9g5iasdiuihcyz9rnx2", "service_name": "test1", + "service_mode": "replicated", }, ) acc.AssertContainsTaggedFields(t, "docker_swarm", map[string]interface{}{ - "service_mode": "global", "tasks_running": int(1), "tasks_desired": int(1), }, map[string]string{ "service_id": "qolkls9g5iasdiuihcyz9rn3", "service_name": "test2", + "service_mode": "global", }, ) } From ca09644ca684385fe8f37fe433245d31b4d29f9a Mon Sep 17 00:00:00 2001 From: Aditya C S Date: Wed, 4 Oct 2017 01:28:54 +0530 Subject: [PATCH 12/12] review changes --- plugins/inputs/docker/docker_test.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/plugins/inputs/docker/docker_test.go b/plugins/inputs/docker/docker_test.go index d67916f68c439..b18274136e6aa 100644 --- a/plugins/inputs/docker/docker_test.go +++ b/plugins/inputs/docker/docker_test.go @@ -501,7 +501,7 @@ func TestDockerGatherSwarmInfo(t *testing.T) { map[string]string{ "service_id": "qolkls9g5iasdiuihcyz9rnx2", "service_name": "test1", - "service_mode": "replicated", + "service_mode": "replicated", }, ) @@ -514,7 +514,7 @@ func TestDockerGatherSwarmInfo(t *testing.T) { map[string]string{ "service_id": "qolkls9g5iasdiuihcyz9rn3", "service_name": "test2", - "service_mode": "global", + "service_mode": "global", }, ) }