Where metrics come from Metric đến từ đâu
Every number in CloudWatch Metrics was observed by something and pushed there. This page is about what that something is — one rule that settles the two facts everyone trips over: EC2 gives you CPU for free but never RAM, and an ECS task reports its memory while the EC2 instance running it does not.
Mọi con số trong CloudWatch Metrics đều do một cái gì đó quan sát rồi đẩy lên. Trang này nói về cái đó là ai — một quy tắc giải quyết hai chuyện ai cũng vấp: EC2 cho sẵn CPU nhưng không bao giờ có RAM, và một ECS task báo được memory trong khi EC2 instance đang chạy nó thì không.
Reach for this page when a question turns into "can AWS even see that?" — memory or disk-space alarms on EC2, monitoring at the container level, or choosing between the CloudWatch agent, Container Insights, and doing nothing. Mở trang này khi một câu hỏi biến thành "AWS có nhìn thấy cái đó không?" — alarm memory hoặc dung lượng đĩa trên EC2, giám sát ở mức container, hoặc phải chọn giữa CloudWatch agent, Container Insights và không làm gì cả.
1 · The one rule1 · Quy tắc duy nhất
A metric is free when the operation being measured passes through AWS hardware or AWS software. Counting is a side effect of doing the work. Everything that happens purely inside your operating system has to be read by something running inside and pushed out — and that something is either yours, or AWS's, depending on who owns the layer. Một metric là miễn phí khi thao tác được đo đi xuyên qua phần cứng hoặc phần mềm của AWS. Việc đếm chỉ là hệ quả phụ của việc làm. Còn mọi thứ diễn ra hoàn toàn bên trong hệ điều hành của bạn thì phải có cái gì đó chạy bên trong đọc rồi đẩy ra — và cái đó là của bạn hay của AWS, tuỳ vào ai sở hữu tầng đó.
Solid arrow = free, because the work passed through AWS. Dashed arrow = someone has to be inside, reading.
Mũi tên liền = miễn phí, vì thao tác đã đi qua AWS. Mũi tên đứt = phải có ai đó ở bên trong, ngồi đọc.
2 · EC2 · the hypervisor's blind spot2 · EC2 · điểm mù của hypervisor
The hypervisor hands your instance a block of RAM once, at boot, and then never touches it again. Every allocation and release after that is an edit to a table inside the guest kernel. Nothing physical happens when memory is freed — so there is no event for anyone outside to count. Hypervisor giao cho instance một khối RAM đúng một lần, lúc khởi động, rồi không đụng tới nữa. Mọi lần cấp phát và giải phóng sau đó chỉ là sửa một bảng ghi bên trong kernel của guest. Không có gì xảy ra về mặt vật lý khi memory được giải phóng — nên chẳng có sự kiện nào để người bên ngoài đếm.
Compare CPU: every cycle your instance runs is a cycle the hypervisor scheduled. Compare memory: the hypervisor only ever handed over the block once.
So với CPU: mỗi chu kỳ instance chạy đều là chu kỳ do hypervisor xếp lịch. So với memory: hypervisor chỉ giao khối RAM đúng một lần rồi thôi.
- "Used memory" isn't even one number. Page cache is in use but reclaimable; shared pages are counted by several processes at once. Someone has to define the answer, and only the OS owns those definitions."Memory đã dùng" thậm chí không phải một con số duy nhất. Page cache đang dùng nhưng thu hồi được; trang shared bị nhiều process cùng tính. Phải có ai đó định nghĩa câu trả lời, mà chỉ OS mới sở hữu các định nghĩa đó.
- Disk space used is the same story. EBS reports IOPS and throughput because a Nitro card handles every request; "the filesystem is 80 % full" is metadata the guest wrote inside the volume and only the guest can interpret.Dung lượng đĩa đã dùng cũng vậy. EBS báo được IOPS và throughput vì Nitro card xử lý mọi request; còn "filesystem đầy 80 %" là metadata do guest ghi bên trong volume và chỉ guest mới hiểu.
- Per-process metrics too. "Which process is eating the box" is
procstatin the CloudWatch agent — same reason, same fix.Metric theo từng process cũng thế. "Process nào đang ngốn máy" làprocstattrong CloudWatch agent — cùng lý do, cùng cách xử lý. - The fix is one agent, not one instance at a time. SSM Distributor + State Manager installs and keeps the CloudWatch agent updated fleet-wide with no SSH; the wizard's basic config already contains
mem_used_percentanddisk_used_percent. The instance role needsCloudWatchAgentServerPolicy.Cách xử lý là một agent cho cả fleet, không phải làm từng máy. SSM Distributor + State Manager cài và tự cập nhật CloudWatch agent cho toàn fleet mà không cần SSH; cấu hình basic của wizard đã có sẵnmem_used_percentvàdisk_used_percent. Role của instance cầnCloudWatchAgentServerPolicy. - These arrive as custom metrics — they cost per metric per month, and a per-instance dimension multiplies that by the size of the fleet.Chúng về dưới dạng custom metric — tính tiền theo từng metric mỗi tháng, và một dimension theo instance sẽ nhân con số đó với kích thước fleet.
3 · Containers · why memory comes free3 · Container · vì sao memory có sẵn
Not because the hypervisor suddenly gained x-ray vision. The counter exists on both sides — the difference is that on ECS, AWS already runs software inside the host. The ECS agent has to be there to pull your image and start your task at all; reporting CPU and memory is a second job for a process that had to exist anyway. On plain EC2, AWS runs nothing inside your OS on purpose — that's what "you own the instance" means. Không phải vì hypervisor bỗng dưng nhìn xuyên được. Bộ đếm tồn tại ở cả hai bên — khác biệt là ở ECS, AWS vốn đã chạy sẵn phần mềm bên trong host. ECS agent bắt buộc phải có mặt thì mới kéo image và khởi động task của bạn được; báo cáo CPU và memory chỉ là việc thứ hai của một process dù sao cũng phải tồn tại. Còn EC2 thuần thì AWS cố ý không chạy gì trong OS của bạn — "instance là của bạn" nghĩa là vậy.
Same kernel, same kind of counter. The only thing that changed is whether AWS already had a process inside with a reason to be there.
Cùng một kernel, cùng một loại bộ đếm. Thứ duy nhất thay đổi là AWS đã có sẵn một process bên trong với lý do chính đáng để ở đó hay chưa.
The second half: a container has a denominatorNửa còn lại: container có mẫu số
A percentage needs something to divide by. A task definition declares memory: 512, so "utilization" has an obvious meaning: used ÷ reserved. The kernel is already keeping that running total anyway, because it needs it to enforce the 512 MiB ceiling — the metric is a by-product of the limit.
Muốn có phần trăm thì phải có cái để chia. Task definition khai memory: 512, nên "utilization" có nghĩa rõ ràng: đã dùng ÷ đã đặt trước. Kernel dù sao cũng đang giữ tổng luỹ kế đó, vì nó cần con số này để cưỡng chế cái trần 512 MiB — metric chỉ là sản phẩm phụ của cái giới hạn.
- Free ECS metrics are cluster and service level only, in the
AWS/ECSnamespace, 1-minute periods:CPUUtilization,MemoryUtilization, plusCPUReservation/MemoryReservationon EC2 capacity. Per-task numbers need Container Insights (paid).Metric ECS miễn phí chỉ ở mức cluster và service, namespaceAWS/ECS, chu kỳ 1 phút:CPUUtilization,MemoryUtilization, thêmCPUReservation/MemoryReservationvới capacity EC2. Muốn số liệu theo từng task thì cần Container Insights (tính phí). - Reservation ≠ Utilization.
MemoryReservationis how much the task definitions asked for against what the cluster registered — a capacity-planning number.MemoryUtilizationis what is actually being used. A cluster can be 100 % reserved and 12 % utilized.Reservation ≠ Utilization.MemoryReservationlà lượng các task definition xin so với lượng cluster đăng ký — con số để hoạch định capacity.MemoryUtilizationlà lượng đang thực sự dùng. Một cluster hoàn toàn có thể reserved 100 % mà utilized chỉ 12 %. - On EC2 capacity the ECS agent must be new enough (Linux
1.4.0+) and the container-instance role needsecs:StartTelemetrySession. Metrics silently missing on an old cluster is usually one of those two.Với capacity EC2, ECS agent phải đủ mới (Linux1.4.0+) và role của container instance cầnecs:StartTelemetrySession. Cluster cũ tự dưng thiếu metric thường là do một trong hai cái này. - Fargate needs nothing at all — AWS owns the whole host, so service CPU and memory utilization are simply there.Fargate thì không cần gì hết — AWS sở hữu toàn bộ host, nên CPU và memory utilization ở mức service tự nhiên đã có.
- "Used" still needs a definition here too. The instance-level Container Insights metric is literally called
instance_memory_working_set— working set, i.e. usage minus reclaimable cache. AWS even documents thatMemoryUtilizedcan be inaccurate for applications using the Java ZGC garbage collector. Same ambiguity as EC2, just resolved for you.Ở đây "đã dùng" cũng vẫn cần một định nghĩa. Metric mức instance của Container Insights tên đúng nghĩa đen làinstance_memory_working_set— working set, tức lượng dùng trừ đi phần cache thu hồi được. AWS thậm chí ghi rõMemoryUtilizedcó thể sai với ứng dụng dùng garbage collector ZGC của Java. Vẫn là sự mơ hồ như trên EC2, chỉ là được quyết hộ bạn.
instance_memory_utilization, instance_filesystem_utilization and friends. "Tasks look healthy, hosts are swapping to death" is exactly this gap.
Với ECS chạy trên capacity EC2, bạn có memory của task nhưng không có gì về cái máy chúng đang chạy — container instance vẫn là một EC2 instance của bạn, với đúng điểm mù ở mục 2. Muốn thấy nó thì phải triển khai CloudWatch agent lên cluster, khi đó mới có instance_memory_utilization, instance_filesystem_utilization và các metric cùng nhóm. "Task nhìn khoẻ mạnh, host thì swap đến chết" chính là khoảng trống này.
4 · The ownership ladder4 · Thang sở hữu
Every step down the ladder hands AWS one more layer — and AWS monitors what it owns. The monitoring effort you have to supply is exactly the part still shaded as yours. Mỗi bậc đi xuống là giao thêm cho AWS một tầng — và AWS giám sát cái nó sở hữu. Phần công sức giám sát bạn phải bỏ ra đúng bằng phần vẫn còn tô là của bạn.
Managed services are not "better monitored" by magic. They are monitored because AWS is already inside, running the layer.
Dịch vụ managed không phải "được giám sát tốt hơn" một cách thần kỳ. Chúng được giám sát vì AWS vốn đã ở bên trong, đang vận hành tầng đó.
| WhereỞ đâu | Free, no setupMiễn phí, không cần làm gì | Turn on / installBật / cài thêm |
|---|---|---|
| EC2 | CPUUtilization, NetworkIn/Out, EBS ops, status checks · 5 min, or 1 min with detailed monitoring5 phút, hoặc 1 phút nếu bật detailed monitoring |
CloudWatch agent → mem_used_percent, disk_used_percent, swap_used_percent, procstat · custom metrics, roll out with SSMcustom metric, triển khai bằng SSM |
| ECS on EC2ECS trên EC2 | AWS/ECS cluster + servicemức cluster + service: CPUUtilization, MemoryUtilization, CPUReservation, MemoryReservation |
Container Insights → MemoryUtilized / MemoryReserved per task familytheo task family · + CloudWatch agent for+ CloudWatch agent để có instance_memory_utilization |
| Fargate | AWS/ECS service CPU + memory utilization, automaticCPU + memory utilization mức service, tự động |
Container Insights per task; enhanced observability adds per-containertheo task; enhanced observability thêm mức từng container · no host to reachkhông có host để đụng vào |
| EKS | Control-plane metrics only — nodes and pods are yoursChỉ metric của control plane — node và pod là của bạn | Container Insights via the CloudWatch agent as a DaemonSet (or the observability add-on) + Fluent Bit for logsqua CloudWatch agent chạy dạng DaemonSet (hoặc add-on observability) + Fluent Bit cho log |
| Lambda | Max Memory Used in the REPORT line of every invocationtrong dòng REPORT của mỗi lần chạy |
Lambda Insights to chart it as a metric instead of grepping logsđể vẽ thành metric thay vì phải grep log |
Exam reflexesPhản xạ khi thi
- "Alarm on EC2 memory or disk space used" → CloudWatch agent → custom metric. Never a built-in one, at any price."Đặt alarm cho memory hoặc disk đã dùng của EC2" → CloudWatch agent → custom metric. Không bao giờ là metric có sẵn, dù trả bao nhiêu tiền.
- "…across hundreds of instances, without SSH" → SSM Distributor + State Manager to install and keep the agent current."…trên hàng trăm instance, không được SSH" → SSM Distributor + State Manager để cài và giữ agent luôn mới.
- "We enabled detailed monitoring but still no memory" → expected. Detailed monitoring changes the period, not the list."Bật detailed monitoring rồi mà vẫn không có memory" → đúng như vậy. Detailed monitoring đổi chu kỳ, không đổi danh sách.
- "Which container in the task is leaking?" → Container Insights with enhanced observability — plain Container Insights stops at the task."Container nào trong task đang rò rỉ bộ nhớ?" → Container Insights kèm enhanced observability — Container Insights thường chỉ tới mức task.
- "ECS tasks look fine but container instances run out of memory" → task metrics ≠ host metrics; deploy the CloudWatch agent for instance-level metrics."Task ECS trông ổn nhưng container instance hết memory" → metric của task ≠ metric của host; triển khai CloudWatch agent để có metric mức instance.
- "Right-size a Lambda's memory" →
Max Memory Usedin the logs (or Lambda Insights / Compute Optimizer). Nothing to install."Chỉnh đúng memory cho Lambda" →Max Memory Usedtrong log (hoặc Lambda Insights / Compute Optimizer). Không phải cài gì. - Any answer that promises in-guest visibility with nothing running in the guest is wrong. That is the whole rule, restated.Bất kỳ đáp án nào hứa nhìn được bên trong guest mà không chạy gì trong guest đều sai. Đó chính là quy tắc trên, nói theo cách khác.