自定义指标与 CloudWatch 告警
学习从 Lambda 发出业务指标和性能指标,并在指标越过阈值时触发告警和通知。
自定义指标与 CloudWatch 告警 是 CoddyKit 上的免费 Serverless AWS Lambda Development 课时。 这是第 4 节课,共 4 节。 你可以在下方免费阅读本课时的完整内容 — 然后在浏览器中使用内置代码编辑器和全天候 AI 导师进行实践。 这是 Serverless AWS Lambda Development 学习路径的一部分,你的进度在网页和 CoddyKit 应用中同步。 Serverless AWS Lambda Development 课程共包含 4 节课。
本课时的部分内容尚未翻译,以英文显示。
Beyond Default Metrics
Lambda publishes built-in metrics like invocations and errors, but your app has its own signals, such as orders processed or items failed. These are custom metrics.
Embedded Metric Format
The easiest way to emit metrics is the Embedded Metric Format (EMF): print structured JSON to logs and CloudWatch extracts metrics automatically.
{
"_aws": {
"CloudWatchMetrics": [{
"Namespace": "OrdersApp",
"Metrics": [{"Name": "OrdersProcessed", "Unit": "Count"}]
}]
},
"OrdersProcessed": 12
}Emitting EMF in Python
Build the EMF object and print it as JSON. No extra API call or latency is needed.
import json, time
def emit(count):
print(json.dumps({
'_aws': {
'Timestamp': int(time.time() * 1000),
'CloudWatchMetrics': [{
'Namespace': 'OrdersApp',
'Dimensions': [[]],
'Metrics': [{'Name': 'OrdersProcessed', 'Unit': 'Count'}]
}]
},
'OrdersProcessed': count
}))PutMetricData API
Alternatively call put_metric_data directly. This is synchronous and adds latency, so EMF is usually preferred for hot paths.
import boto3
cw = boto3.client('cloudwatch')
cw.put_metric_data(Namespace='OrdersApp',
MetricData=[{'MetricName': 'OrdersProcessed', 'Value': 1}])Dimensions Add Context
Dimensions are key-value tags that slice a metric, such as by region or customer tier. Keep dimension cardinality low to control cost.
What an Alarm Does
A CloudWatch alarm watches a metric and changes state when it breaches a threshold for a set number of periods, then triggers an action.
Creating an Alarm
This alarm fires when the error count exceeds 5 in a single one-minute period.
aws cloudwatch put-metric-alarm \
--alarm-name orders-errors \
--metric-name Errors \
--namespace AWS/Lambda \
--threshold 5 \
--comparison-operator GreaterThanThreshold \
--evaluation-periods 1 \
--period 60Notifying with SNS
Point the alarm action at an SNS topic so it emails, texts, or pages your on-call when triggered.
Alarm States
Alarms have three states: OK, ALARM, and INSUFFICIENT_DATA. The last means the metric had no data points in the window.
Avoid Alarm Fatigue
Too many noisy alarms train people to ignore them. Alarm on symptoms users feel, like error rate and latency, not every minor metric.
Dashboards Tie It Together
Build a CloudWatch dashboard combining built-in and custom metrics so you see the whole system health at a glance during an incident.
Quick Check
Test your metrics knowledge.
Recap
You learned to emit custom metrics via EMF or PutMetricData, add dimensions, create threshold alarms wired to SNS, and avoid alarm fatigue with dashboards.
常见问题解答
「自定义指标与 CloudWatch 告警」课时是免费的吗?
是的 — 「自定义指标与 CloudWatch 告警」的完整文本可在网页上免费阅读。要进行交互式练习(内置代码编辑器和全天候 AI 导师)并解锁 Serverless AWS Lambda Development 课程的其余内容,请升级到 CoddyKit PRO。 Serverless AWS Lambda Development 课程共包含 4 节课。
「自定义指标与 CloudWatch 告警」这节课中我会学到什么?
学习从 Lambda 发出业务指标和性能指标,并在指标越过阈值时触发告警和通知。 你通过在浏览器中直接运行的动手代码来练习 Serverless AWS Lambda Development,全天候 AI 导师会在你学习这节课的过程中回答你的问题。
学习 Serverless AWS Lambda Development 需要有经验吗?
无需任何先前经验。CoddyKit 上的 Serverless AWS Lambda Development 课程适合初学者到高级学习者,你可以从这里开始或从头开始,按照自己的节奏学习。 这是第 4 节课,共 4 节。
「自定义指标与 CloudWatch 告警」课时需要多长时间?
大多数 CoddyKit 课程大约需要 5–10 分钟。每节课都很精短且互动,所以你能稳步进步,并在网页和应用中从离开的地方继续。
我能在这节 Serverless AWS Lambda Development 课中编写并运行代码吗?
能。每节 Serverless AWS Lambda Development 课都包含内置代码编辑器,你可以在浏览器中直接编写并运行真实代码,并获得即时 AI 反馈 — 无需本地设置。
此课程中的所有课时
- CloudWatch 日志与指标
- 错误处理与重试
- 调试无服务器应用
- 自定义指标与 CloudWatch 告警