Niestandardowe metryki i alarmy CloudWatch
Dowiedz się, jak emitować własne metryki biznesowe i wydajnościowe z Lambda oraz uruchamiać alarmy i powiadomienia po przekroczeniu progów.
Niestandardowe metryki i alarmy CloudWatch to bezpłatna lekcja Serverless AWS Lambda Development na CoddyKit. To lekcja 4 z 4. Możesz przeczytać całą lekcję poniżej za darmo — a potem ćwiczyć ją interaktywnie w przeglądarce z wbudowanym edytorem kodu i tutorem AI dostępnym 24/7. To część ścieżki edukacyjnej Serverless AWS Lambda Development, a Twój postęp synchronizuje się między webem a aplikacją CoddyKit. Kurs Serverless AWS Lambda Development zawiera 4 lekcji w sumie.
Części tej lekcji nie zostały jeszcze przetłumaczone i są wyświetlane po angielsku.
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.
Ucz się Serverless AWS Lambda Development dzięki korepetycjom AI — za darmo
Pisz i uruchamiaj kod w przeglądarce, otrzymuj natychmiastową pomoc od korepetytora AI dostępnego 24/7 i kontynuuj naukę w sieci lub w aplikacji.
- Kursy
- 12
- Lekcje
- 48
Często zadawane pytania
Czy lekcja „Niestandardowe metryki i alarmy CloudWatch” jest bezpłatna?
Tak — pełny tekst „Niestandardowe metryki i alarmy CloudWatch” jest dostępny za darmo tutaj w sieci. Aby ćwiczyć ją interaktywnie (wbudowany edytor kodu i tutor AI dostępny 24/7) i odblokować resztę kursu Serverless AWS Lambda Development, przejdź na CoddyKit PRO. Kurs Serverless AWS Lambda Development zawiera 4 lekcji w sumie.
Co nauczysz się w „Niestandardowe metryki i alarmy CloudWatch”?
Dowiedz się, jak emitować własne metryki biznesowe i wydajnościowe z Lambda oraz uruchamiać alarmy i powiadomienia po przekroczeniu progów. Ćwiczysz Serverless AWS Lambda Development z praktycznym kodem, który uruchamiasz bezpośrednio w przeglądarce, a tutor AI dostępny 24/7 odpowiada na Twoje pytania podczas pracy nad lekcją.
Czy potrzebuję doświadczenia, aby zacząć Serverless AWS Lambda Development?
Nie wymagamy żadnego doświadczenia. Serverless AWS Lambda Development w CoddyKit jest strukturyzowany dla początkujących i zaawansowanych użytkowników, więc możesz zacząć tutaj lub od początku i uczyć się w swoim tempie. To lekcja 4 z 4.
Ile czasu zajmuje lekcja „Niestandardowe metryki i alarmy CloudWatch”?
Większość lekcji CoddyKit trwa około 5–10 minut. Każda lekcja to mały, interaktywny krok, dzięki czemu robisz systematyczne postępy i zawsze wracasz dokładnie do tego samego miejsca — na webie i w aplikacji.
Czy mogę pisać i uruchamiać kod w tej lekcji Serverless AWS Lambda Development?
Tak. Każda lekcja Serverless AWS Lambda Development zawiera wbudowany edytor kodu, więc piszesz i uruchamiasz prawdziwy kod bezpośrednio w przeglądarce i od razu otrzymujesz sprzężenie zwrotne od AI — bez konfiguracji na komputerze.
Wszystkie lekcje w tym kursie
- Logi i metryki CloudWatch
- Obsługa błędów i ponowienia
- Debugowanie aplikacji serverless
- Niestandardowe metryki i alarmy CloudWatch