首页 > 解决方案 > 来自 AWS ECS 集群的自定义指标

问题描述

我有一个启用了容器洞察的 ECS 集群。但它没有给我自定义指标和日志(来自任务的额外日志和附加的 EFS 磁盘信息)。在这种情况下添加自定义指标的推荐方法是什么 - 我可以将自定义指标添加到容器见解中吗?将 CWAgent docker 容器作为守护程序服务运行并收集这些信息会更好吗?

(我尝试使用 terraform 中的以下代码使用 CWAgent docker 守护程序服务收集自定义指标,但从未成功。感谢任何帮助)

  1. 自定义指标配置文件
{
  "agent": {
    "metrics_collection_interval": 10,
    "logfile": "/opt/aws/amazon-cloudwatch-agent/logs/amazon-cloudwatch-agent.log"
  },
  "metrics": {
    "metrics_collected": {
      "cpu": {
        "resources": [
          "*"
        ],
        "measurement": [
          {
            "name": "cpu_usage_idle",
            "rename": "CPU_USAGE_IDLE",
            "unit": "Percent"
          },
          {
            "name": "cpu_usage_nice",
            "unit": "Percent"
          },
          "cpu_usage_guest"
        ],
        "totalcpu": false,
        "metrics_collection_interval": 10
      },
      "disk": {
        "resources": [
          "/",
          "/tmp",
          "/opt/app/folder/",  --> additional EBS folder
          "/opt/app/shared_folder".  --> additional EFS folder
        ],
        "measurement": [
          {
            "name": "free",
            "rename": "DISK_FREE",
            "unit": "Gigabytes"
          },
          "total",
          "used"
        ],
        "ignore_file_system_types": [
          "sysfs",
          "devtmpfs"
        ],
        "metrics_collection_interval": 60
      },
      "diskio": {
        "resources": [
          "*"
        ],
        "measurement": [
          "reads",
          "writes",
          "read_time",
          "write_time",
          "io_time"
        ],
        "metrics_collection_interval": 60
      },
      "swap": {
        "measurement": [
          "swap_used",
          "swap_free",
          "swap_used_percent"
        ]
      },
      "mem": {
        "measurement": [
          "mem_used",
          "mem_cached",
          "mem_total"
        ],
        "metrics_collection_interval": 10
      },
      "net": {
        "resources": [
          "eth0"
        ],
        "measurement": [
          "bytes_sent",
          "bytes_recv",
          "drop_in",
          "drop_out"
        ]
      },
      "netstat": {
        "measurement": [
          "tcp_established",
          "tcp_syn_sent",
          "tcp_close"
        ],
        "metrics_collection_interval": 60
      },
      "processes": {
        "measurement": [
          "running",
          "sleeping",
          "dead"
        ]
      }
    },
    "append_dimensions": {
      "ImageId": "${aws:ImageId}",
      "InstanceId": "${aws:InstanceId}",
      "InstanceType": "${aws:InstanceType}"
    },
    "aggregation_dimensions": [
      [
        "ImageId"
      ],
      [
        "InstanceId",
        "InstanceType"
      ],
      [
        "d1"
      ],
      []
    ]
  },
  "logs": {
    "logs_collected": {
      "files": {
        "collect_list": [
          {
            "file_path": "/opt/aws/amazon-cloudwatch-agent/logs/amazon-cloudwatch-agent.log",
            "log_group_name": "/ecs/ecs-cwagent-daemon-service",
            "log_stream_name": "{instance_id}_{hostname}",
            "timezone": "Local"
          },
          {
            "file_path": "/path/in/ec2/logs.log",
            "log_group_name": "/ecs/app/other_logs/",
            "log_stream_name": "{instance_id}_{hostname}",
            "timezone": "Local"
          },
          {
            "file_path": "/path/in/ec2/mail.log",
            "log_group_name": "/ecs/app/mail_logs/",
            "log_stream_name": "{instance_id}_{hostname}",
            "timezone": "Local"
          },
          {
            "file_path": "/path/in/ec2/access_log.*",
            "log_group_name": "/ecs/app/logs/",
            "log_stream_name": "{instance_id}_{hostname}",
            "timezone": "Local"
          }
        ]
      }
    },
    "log_stream_name": "/ec2/catchall"
  }
}
  1. CWAgent 容器定义
[{
 "name": "cloudwatch-agent",
 "image": "public.ecr.aws/cloudwatch-agent/cloudwatch-agent:latest",
 "mountPoints": [
   {
     "readOnly": true,
     "containerPath": "/rootfs/proc",
     "sourceVolume": "proc"
   },
   {
     "readOnly": true,
     "containerPath": "/rootfs/dev",
     "sourceVolume": "dev"
   },
   {
     "readOnly": true,
     "containerPath": "/sys/fs/cgroup",
     "sourceVolume": "al2_cgroup"
   },
   {
     "readOnly": true,
     "containerPath": "/cgroup",
     "sourceVolume": "al1_cgroup"
   },
   {
     "readOnly": true,
     "containerPath": "/rootfs/sys/fs/cgroup",
     "sourceVolume": "al2_cgroup"
   },
   {
     "readOnly": true,
     "containerPath": "/rootfs/cgroup",
     "sourceVolume": "al1_cgroup"
   }
 ],
 "secrets": [
   {
     "name": "CW_CONFIG_CONTENT",
     "valueFrom": "${cwagent_daemon_config}" --> custom metrics config file
   }
 ],
 "logConfiguration": {
   "logDriver": "awslogs",
   "options": {
     "awslogs-create-group": "True",
     "awslogs-group": "/ecs/ecs-cwagent-daemon-service",
     "awslogs-region": "${aws_region}",
     "awslogs-stream-prefix": "ecs"
   }
 }
}]

任务定义:

resource "aws_ecs_task_definition" "cwagent_task" {
  family                   = "ecs-cwagent-daemon-service"
  network_mode             = "bridge"
  requires_compatibilities = ["EC2"]
  execution_role_arn       = aws_iam_role.cwagent_task_execution_role.arn
  task_role_arn            = aws_iam_role.cwagent_task_role.arn
  cpu                      = "128"
  memory                   = "64"
  container_definitions    = data.template_file.cwagent_json.rendered --> above json file
  volume {
    name      = "proc"
    host_path = "/proc"
  }

  volume {
    name      = "dev"
    host_path = "/dev"
  }

  volume {
    name      = "al1_cgroup"
    host_path = "/cgroup"
  }

  volume {
    name      = "al2_cgroup"
    host_path = "/sys/fs/cgroup"
  }

}

标签: amazon-web-servicesdockeramazon-ecsamazon-cloudwatchaws-cloudwatch-log-insights

解决方案


推荐阅读