diff --git a/plugins/inputs/ecs/cgroupv2_test.go b/plugins/inputs/ecs/cgroupv2_test.go new file mode 100644 index 0000000000000..521b8bf8ba9a2 --- /dev/null +++ b/plugins/inputs/ecs/cgroupv2_test.go @@ -0,0 +1,60 @@ +package ecs + +import ( + "os" + "testing" + "time" + + "github.com/influxdata/telegraf/plugins/parsers/influx" + "github.com/influxdata/telegraf/testutil" + "github.com/stretchr/testify/require" +) + +const cgroupID = "c69461b2c836cc3f0e3e5deb07b1f16e25f6009da2a48bb0adc7dd580befaf55" + +func TestParseCgroupV2Stats(t *testing.T) { + parser := &influx.Parser{} + require.NoError(t, parser.Init()) + expected, err := testutil.ParseMetricsFromFile("testdata/cgroupv2/stats.out", parser) + require.NoError(t, err) + + stats, err := os.Open("testdata/cgroupv2/stats.json") + require.NoError(t, err) + parsedStats, err := unmarshalStats(stats) + require.NoError(t, err) + + tags := map[string]string{ + "test_tag": "test", + } + + var acc testutil.Accumulator + memstats(cgroupID, parsedStats[cgroupID], &acc, tags, time.Now()) + cpustats(cgroupID, parsedStats[cgroupID], &acc, tags, time.Now()) + netstats(cgroupID, parsedStats[cgroupID], &acc, tags, time.Now()) + blkstats(cgroupID, parsedStats[cgroupID], &acc, tags, time.Now()) + + actual := acc.GetTelegrafMetrics() + testutil.RequireMetricsEqual(t, expected, actual, testutil.IgnoreTime()) +} + +func TestParseCgroupV2Meta(t *testing.T) { + parser := &influx.Parser{} + require.NoError(t, parser.Init()) + expected, err := testutil.ParseMetricsFromFile("testdata/cgroupv2/meta.out", parser) + require.NoError(t, err) + + meta, err := os.Open("testdata/cgroupv2/meta.json") + require.NoError(t, err) + validMeta, err := unmarshalTask(meta) + require.NoError(t, err) + + tags := map[string]string{ + "test_tag": "test", + } + + var acc testutil.Accumulator + metastats(cgroupID, validMeta.Containers[0], &acc, tags, time.Now()) + + actual := acc.GetTelegrafMetrics() + testutil.RequireMetricsEqual(t, expected, actual, testutil.IgnoreTime()) +} diff --git a/plugins/inputs/ecs/stats.go b/plugins/inputs/ecs/stats.go index 13d9aa3bc5326..f4eb042f3092b 100644 --- a/plugins/inputs/ecs/stats.go +++ b/plugins/inputs/ecs/stats.go @@ -125,7 +125,7 @@ func cpustats(id string, stats types.StatsJSON, acc telegraf.Accumulator, tags m // If we have OnlineCPUs field, then use it to restrict stats gathering to only Online CPUs // (https://github.com/moby/moby/commit/115f91d7575d6de6c7781a96a082f144fd17e400) var percpuusage []uint64 - if stats.CPUStats.OnlineCPUs > 0 { + if stats.CPUStats.OnlineCPUs > 0 && len(stats.CPUStats.CPUUsage.PercpuUsage) > 0 { percpuusage = stats.CPUStats.CPUUsage.PercpuUsage[:stats.CPUStats.OnlineCPUs] } else { percpuusage = stats.CPUStats.CPUUsage.PercpuUsage diff --git a/plugins/inputs/ecs/testdata/cgroupv2/meta.json b/plugins/inputs/ecs/testdata/cgroupv2/meta.json new file mode 100644 index 0000000000000..a2a783e730ea3 --- /dev/null +++ b/plugins/inputs/ecs/testdata/cgroupv2/meta.json @@ -0,0 +1,48 @@ +{ + "Cluster": "ecs-cluster-prod", + "TaskARN": "arn:aws:ecs:eu-west-2::task/ecs-cluster-prod/a22cfc58cdb04b27ada2caa3fd526463", + "Family": "nginx-telegraf", + "Revision": "3", + "DesiredStatus": "RUNNING", + "KnownStatus": "RUNNING", + "Containers": [ + { + "DockerId": "c69461b2c836cc3f0e3e5deb07b1f16e25f6009da2a48bb0adc7dd580befaf55", + "Name": "~internal~ecs~pause", + "DockerName": "ecs-nginx-telegraf-3-internalecspause-f0cc8fe3bfc9a8efd601", + "Image": "amazon/amazon-ecs-pause:0.1.0", + "ImageID": "", + "Labels": { + "com.amazonaws.ecs.cluster": "ecs-cluster-prod", + "com.amazonaws.ecs.container-name": "~internal~ecs~pause", + "com.amazonaws.ecs.task-arn": "arn:aws:ecs:eu-west-2::task/ecs-cluster-prod/a22cfc58cdb04b27ada2caa3fd526463", + "com.amazonaws.ecs.task-definition-family": "nginx-telegraf", + "com.amazonaws.ecs.task-definition-version": "3" + }, + "DesiredStatus": "RESOURCES_PROVISIONED", + "KnownStatus": "RESOURCES_PROVISIONED", + "Limits": { + "CPU": 2, + "Memory": 0 + }, + "CreatedAt": "2023-10-26T15:57:02.520698803Z", + "StartedAt": "2023-10-26T15:57:02.806357646Z", + "Type": "CNI_PAUSE", + "Networks": [ + { + "NetworkMode": "awsvpc", + "IPv4Addresses": [ + "10.1.4.120" + ] + } + ] + } + ], + "Limits": { + "CPU": 0.5, + "Memory": 1024 + }, + "PullStartedAt": "2023-10-26T15:57:03.528549391Z", + "PullStoppedAt": "2023-10-26T15:57:18.310985676Z", + "AvailabilityZone": "eu-west-2b" + } diff --git a/plugins/inputs/ecs/testdata/cgroupv2/meta.out b/plugins/inputs/ecs/testdata/cgroupv2/meta.out new file mode 100644 index 0000000000000..70390ab0b2cc0 --- /dev/null +++ b/plugins/inputs/ecs/testdata/cgroupv2/meta.out @@ -0,0 +1 @@ +ecs_container_meta,test_tag=test container_id="c69461b2c836cc3f0e3e5deb07b1f16e25f6009da2a48bb0adc7dd580befaf55",desired_status="RESOURCES_PROVISIONED",docker_name="ecs-nginx-telegraf-3-internalecspause-f0cc8fe3bfc9a8efd601",image="amazon/amazon-ecs-pause:0.1.0",image_id="",known_status="RESOURCES_PROVISIONED",limit_cpu=2,limit_mem=0,type="CNI_PAUSE" 1698343335413572143 diff --git a/plugins/inputs/ecs/testdata/cgroupv2/stats.json b/plugins/inputs/ecs/testdata/cgroupv2/stats.json new file mode 100644 index 0000000000000..758603d323e3f --- /dev/null +++ b/plugins/inputs/ecs/testdata/cgroupv2/stats.json @@ -0,0 +1,101 @@ +{ + "c69461b2c836cc3f0e3e5deb07b1f16e25f6009da2a48bb0adc7dd580befaf55": { + "read": "2023-10-26T15:58:32.690109934Z", + "preread": "2023-10-26T15:58:31.642796089Z", + "pids_stats": { + "current": 1, + "limit": 4618 + }, + "blkio_stats": { + "io_service_bytes_recursive": null, + "io_serviced_recursive": null, + "io_queue_recursive": null, + "io_service_time_recursive": null, + "io_wait_time_recursive": null, + "io_merged_recursive": null, + "io_time_recursive": null, + "sectors_recursive": null + }, + "num_procs": 0, + "storage_stats": {}, + "cpu_stats": { + "cpu_usage": { + "total_usage": 34818000, + "usage_in_kernelmode": 5397000, + "usage_in_usermode": 29420000 + }, + "system_cpu_usage": 16970500000000, + "online_cpus": 2, + "throttling_data": { + "periods": 0, + "throttled_periods": 0, + "throttled_time": 0 + } + }, + "precpu_stats": { + "cpu_usage": { + "total_usage": 34818000, + "usage_in_kernelmode": 5397000, + "usage_in_usermode": 29420000 + }, + "system_cpu_usage": 16968420000000, + "online_cpus": 2, + "throttling_data": { + "periods": 0, + "throttled_periods": 0, + "throttled_time": 0 + } + }, + "memory_stats": { + "usage": 303104, + "stats": { + "active_anon": 4096, + "active_file": 0, + "anon": 45056, + "anon_thp": 0, + "file": 0, + "file_dirty": 0, + "file_mapped": 0, + "file_writeback": 0, + "inactive_anon": 40960, + "inactive_file": 0, + "kernel_stack": 16384, + "pgactivate": 1, + "pgdeactivate": 0, + "pgfault": 1113, + "pglazyfree": 0, + "pglazyfreed": 0, + "pgmajfault": 0, + "pgrefill": 0, + "pgscan": 0, + "pgsteal": 0, + "shmem": 0, + "slab": 190768, + "slab_reclaimable": 66320, + "slab_unreclaimable": 124448, + "sock": 0, + "thp_collapse_alloc": 0, + "thp_fault_alloc": 0, + "unevictable": 0, + "workingset_activate": 0, + "workingset_nodereclaim": 0, + "workingset_refault": 0 + }, + "limit": 4051226624 + }, + "name": "/ecs-nginx-telegraf-3-internalecspause-f0cc8fe3bfc9a8efd601", + "id": "c69461b2c836cc3f0e3e5deb07b1f16e25f6009da2a48bb0adc7dd580befaf55", + "networks": { + "eth0": { + "rx_bytes": 14, + "rx_packets": 0, + "rx_errors": 0, + "rx_dropped": 0, + "tx_bytes": 21, + "tx_packets": 0, + "tx_errors": 0, + "tx_dropped": 0 + } + } + } + } diff --git a/plugins/inputs/ecs/testdata/cgroupv2/stats.out b/plugins/inputs/ecs/testdata/cgroupv2/stats.out new file mode 100644 index 0000000000000..4a951dc9412c1 --- /dev/null +++ b/plugins/inputs/ecs/testdata/cgroupv2/stats.out @@ -0,0 +1,5 @@ +ecs_container_mem,test_tag=test active_anon=4096u,active_file=0u,container_id="c69461b2c836cc3f0e3e5deb07b1f16e25f6009da2a48bb0adc7dd580befaf55",inactive_anon=40960u,inactive_file=0u,limit=4051226624u,max_usage=0u,pgfault=1113u,pgmajfault=0u,unevictable=0u,usage=303104u,usage_percent=0.00748178337406187 1698343335413563073 +ecs_container_cpu,cpu=cpu-total,test_tag=test container_id="c69461b2c836cc3f0e3e5deb07b1f16e25f6009da2a48bb0adc7dd580befaf55",throttling_periods=0u,throttling_throttled_periods=0u,throttling_throttled_time=0u,usage_in_kernelmode=5397000u,usage_in_usermode=29420000u,usage_percent=0,usage_system=16970500000000u,usage_total=34818000u 1698343335413566953 +ecs_container_net,network=eth0,test_tag=test container_id="c69461b2c836cc3f0e3e5deb07b1f16e25f6009da2a48bb0adc7dd580befaf55",rx_bytes=14u,rx_dropped=0u,rx_errors=0u,rx_packets=0u,tx_bytes=21u,tx_dropped=0u,tx_errors=0u,tx_packets=0u 1698343335413572143 +ecs_container_net,network=total,test_tag=test container_id="c69461b2c836cc3f0e3e5deb07b1f16e25f6009da2a48bb0adc7dd580befaf55",rx_bytes=14u,rx_dropped=0u,rx_errors=0u,rx_packets=0u,tx_bytes=21u,tx_dropped=0u,tx_errors=0u,tx_packets=0u 1698343335413572143 +ecs_container_blkio,device=total,test_tag=test container_id="c69461b2c836cc3f0e3e5deb07b1f16e25f6009da2a48bb0adc7dd580befaf55" 1698343335413575563