Thuta Learning
AWS Advanced
Reliability & OperationsDevOpsအဆင့်မြင့်

Observability: Metrics၊ Logs၊ Traces နဲ့ SLO

AWS Advancedသင်ခန်းစာ 2650 မိနစ်

ဒီခန်းပြီးရင် ဘာတတ်သွားမလဲ

  • Metrics၊ logs နဲ့ traces တစ်ခုချင်းစီ ဘယ်မေးခွန်းကို ဖြေလဲ ခွဲခြားနိုင်မယ်
  • Static၊ anomaly detection နဲ့ composite alarm တွေကို noise နည်းအောင် ပေါင်းသုံးနိုင်မယ်
  • CloudWatch Logs Insights query တွေ ရေးနိုင်ပြီး Embedded Metric Format နဲ့ log ကနေ metric ထုတ်နိုင်မယ်
  • X-Ray / AWS Distro for OpenTelemetry tracing ဘယ်နေရာမှာ အသုံးဝင်လဲ ရှင်းပြနိုင်မယ်
  • SLI၊ SLO နဲ့ error budget ကို သတ်မှတ်ပြီး cause မဟုတ်ဘဲ symptom ပေါ်မှာ alert လုပ်နိုင်မယ်

ရိုးရိုးလေး ရှင်းပြမယ်

ကားတစ်စီး မောင်းနေတယ်လို့ စဉ်းစားကြည့်ပါ

Metrics တွေက ကားရဲ့ dashboard ပေါ်က gauge တွေလိုပါပဲ — speed၊ fuel၊ engine temperature စတဲ့ အချိန်နဲ့အမျှ ပြောင်းနေတဲ့ ကိန်းဂဏန်းတွေပါ။ တစ်ချက်ကြည့်ရုံနဲ့ တစ်ခုခု မှားနေလား သိရလို့ alarm လုပ်ဖို့ အကောင်းဆုံးပါ။ Logs တွေက ခရီးစဉ် diary လိုပါ — ဖြစ်ခဲ့တဲ့ အရာတိုင်းကို အသေးစိတ် ရေးထားပေမယ့် အကုန်ဖတ်ဖို့ အချိန်ကြာပါတယ်။

Traces တွေကတော့ ခရီးစဉ် တစ်ခုတည်းရဲ့ GPS လမ်းကြောင်းလိုပါ — request တစ်ခုက service တွေ ဘယ်လို ဖြတ်သွားလဲ၊ ဘယ်နေရာမှာ အချိန်ကုန်လဲ ပြပေးပါတယ်။ Observability ဆိုတာ ဒီသုံးမျိုးကို ပေါင်းပြီး 'ဘာ ပျက်နေလဲ' ကနေ 'ဘာကြောင့်လဲ' အထိ မြန်မြန် ရောက်အောင် လုပ်တာပါ။

Problem → Why → Solution

Alarm ၄၀ ရှိပေမယ့် တကယ့် incident ကို မမိဘူး

Checkout team မှာ CPU၊ memory၊ disk နဲ့ queue depth အပေါ် alarm ၄၀ ရှိပါတယ်။ On-call engineer ကို တစ်ညကို ၃၀ ကြိမ်လောက် 'CPU > 80%' အတွက် နှိုးပါတယ် — ဒါပေမယ့် အဲဒါတွေ ဘယ်တစ်ခုမှ customer ကို မထိခိုက်ပါဘူး။ ဒါကြောင့် engineer တွေက page တွေကို စ ဂရုမစိုက်ကြတော့ပါဘူး။

ပြီးတော့ တကယ့် incident ရောက်လာပါတယ်: payment provider က throttle လုပ်လို့ checkout request တွေ ၈ စက္ကန့်ကြာပြီး timeout ဖြစ်နေပါတယ်။ Function တွေက စောင့်နေရုံပဲမို့ CPU နိမ့်နေတာကြောင့် alarm တစ်ခုမှ မမြည်ပါဘူး။ Customer တွေက social media မှာ တိုင်ကြားမှ team သိပါတယ်၊ ပြီးတော့ log group ခြောက်ခုထဲက unstructured text တွေကို console ထဲမှာ တစ်ခုချင်း scroll လုပ်ပြီး အကြောင်းရင်းကို တစ်နာရီကျော် ရှာရပါတယ်။

noisy-cause-alarm.sh
# A "cause" alarm: one noisy datapoint pages a human
aws cloudwatch put-metric-alarm \
  --alarm-name web-1-cpu-high \
  --namespace AWS/EC2 --metric-name CPUUtilization \
  --dimensions Name=InstanceId,Value=i-0a1b2c3d4e5f67890 \
  --statistic Average --period 60 \
  --evaluation-periods 1 --threshold 80 \
  --comparison-operator GreaterThanThreshold \
  --alarm-actions arn:aws:sns:us-east-1:111122223333:oncall-page

Cause ကို alert လုပ်ပြီး symptom ကို မကြည့်ခဲ့လို့ပါ

CPU မြင့်တာက ပြဿနာ ဖြစ်နိုင်တဲ့ 'အကြောင်းရင်း' တစ်ခုသာ ဖြစ်ပြီး customer က CPU ကို မမြင်ပါဘူး — သူတို့ မြင်တာ latency နဲ့ error ပါ။ Datapoint တစ်ခုတည်းနဲ့ page လုပ်တဲ့ cause alarm တွေက noise ဖန်တီးပြီး symptom alarm မရှိတော့ အကြောင်းရင်း အသစ်တစ်ခု (dependency throttling) ကို လုံးဝ မမြင်ရပါဘူး။

Signal သုံးမျိုး၊ alarm သုံးမျိုး၊ ရည်မှန်းချက် တစ်ခု

SLO (service level objective)
SLI (ဥပမာ 'checkout request တွေထဲက 300 ms အောက်နဲ့ 5xx မဟုတ်ဘဲ ပြီးတဲ့ ရာခိုင်နှုန်း') အတွက် အချိန်ကာလ တစ်ခုအတွင်း ရည်မှန်းချက် — ဥပမာ ရက် ၂၈ ရက်အတွင်း 99.9%။

Signal တစ်မျိုးချင်းစီကို သူ့အားသာချက်အတိုင်း သုံးပါ။ Metrics တွေက အချိန်အလိုက် စုစည်းထားတဲ့ ကိန်းဂဏန်းတွေမို့ သိမ်းရ ဈေးသက်သာပြီး alarm လုပ်ဖို့ မြန်ပါတယ်၊ ဒါပေမယ့် request တစ်ခုချင်းရဲ့ အသေးစိတ် ပျောက်သွားပါတယ်။ Logs တွေက event တစ်ခုချင်းရဲ့ context အပြည့်ကို သိမ်းထားပေမယ့် volume များရင် ingest လုပ်ရ ဈေးကြီးပြီး ဖြစ်ပြီးမှ query လုပ်ရပါတယ်။ Traces တွေက request တစ်ခုကို service တွေကြား span တွေနဲ့ လိုက်ပြီး latency ဘယ်ကလာလဲ ပြပေးပါတယ်။

CloudWatch alarm မှာ ပုံစံ သုံးမျိုး ရှိပါတယ်။ Static threshold alarm က 'p99 latency > 1500 ms' လို ပုံသေ မျဉ်းနဲ့ နှိုင်းယှဉ်ပြီး 'N ခုထဲက M datapoint' သုံးရင် spike တစ်ခုတည်းကြောင့် မမြည်တော့ပါဘူး။ Anomaly detection alarm က metric ရဲ့ သမိုင်းကနေ နေ့စဉ်၊ အပတ်စဉ် ပုံစံကို သင်ယူပြီး မျှော်လင့်ထားတဲ့ band တစ်ခု ဆွဲပေးကာ value က band အပြင်ရောက်ရင် alarm ဖြစ်ပါတယ် — traffic ပုံမှန် အတက်အကျ ရှိတဲ့ metric တွေအတွက် ကောင်းပါတယ်။

Composite alarm ကတော့ တခြား alarm တွေကို AND၊ OR၊ NOT နဲ့ ပေါင်းတဲ့ rule တစ်ခုပါ။ Child alarm တွေမှာ action မထားဘဲ composite တစ်ခုတည်းကပဲ page လုပ်တော့ incident တစ်ခုအတွက် notification တစ်ခုပဲ ရပါတယ်။

CloudWatch Logs Insights က fields၊ filter၊ stats၊ sort၊ parse စတဲ့ pipe နဲ့ ဆက်ထားတဲ့ query language တစ်ခုပါ၊ scan လုပ်တဲ့ GB အလိုက် ကျသင့်ပါတယ်။ Log တွေကို JSON နဲ့ ရေးထားရင် field တွေကို အလိုအလျောက် ရှာတွေ့ပေးပါတယ်။ Embedded Metric Format (EMF) က အဲဒီအပေါ် တစ်ဆင့်တက်ပါတယ်: _aws metadata ပါတဲ့ JSON log line တစ်ကြောင်း ရေးလိုက်ရင် CloudWatch က metric တွေကို asynchronous အနေနဲ့ ထုတ်ယူပေးလို့ PutMetricData API call မလိုတော့ပါဘူး။ orderId လို high-cardinality field တွေက log ထဲမှာ query လုပ်ဖို့ ကျန်နေပြီး dimension မဖြစ်ပါဘူး။

Tracing အတွက် AWS X-Ray က trace ID (X-Amzn-Trace-Id header ဒါမှမဟုတ် W3C traceparent) ကို service တွေကြား ဖြန့်ပေးပြီး service map နဲ့ span timeline ဆွဲပေးပါတယ်။ Instrumentation အသစ်အတွက် AWS က AWS Distro for OpenTelemetry (ADOT) လို OpenTelemetry-based နည်းကို ဦးစားပေး အကြံပြုပါတယ်။ နောက်ဆုံးမှာ ဒါတွေ အားလုံးကို SLO တစ်ခုနဲ့ ချိတ်ပါ: SLI က user ခံစားရတာကို တိုင်းတာပြီး SLO က ရည်မှန်းချက်ကို သတ်မှတ်ကာ error budget (100% − SLO) က ဘယ်လောက် 'မှား' ခွင့်ရှိသေးလဲ ပြောပေးပါတယ်။ CloudWatch Application Signals က support လုပ်တဲ့ workload တွေအတွက် ဒီ SLO တွေကို console ထဲမှာ တိုက်ရိုက် သတ်မှတ်ခွင့် ပေးပါတယ်။

Signalဘာအတွက် အကောင်းဆုံးလဲအဓိက cost driver
MetricsAlarm၊ trend၊ dashboardMetric (dimension combination) အရေအတွက်
LogsEvent တစ်ခုချင်းရဲ့ context၊ debuggingIngest လုပ်တဲ့ GB နဲ့ scan လုပ်တဲ့ GB
TracesService တွေကြား latency ဘယ်ကလာလဲRecord/scan လုပ်တဲ့ trace (sampling)
text
FROM SIGNALS TO ONE PAGE
------------------------
  [Lambda / ECS / ALB]
     |  EMF JSON log lines          |  spans (ADOT / X-Ray)
     v                              v
 [CloudWatch Logs] --extract--> [Metrics]        [X-Ray traces]
     |                              |                  |
     | Logs Insights          +-----+------+           |
     | (why?)                 v            v           |
     |               [p99 latency]   [5xx rate]        |
     |               static alarm    anomaly alarm     |
     |                     \            /              |
     |                 [composite alarm: OR]           |
     |                          |                      |
     +------------------> on-call page <---------------+
                     (runbook links logs + traces)
Symptom → signal → cause
  1. Alarm on what users feel (SLI)
  2. Composite alarm sends one page
  3. Metrics show when and how much
  4. Traces show where
  5. Logs show why

Page လုပ်တာက symptom ပေါ်မှာပဲ၊ debug လုပ်တာက cause တွေနဲ့ပါ။ CPU လို cause metric တွေကို dashboard ပေါ်မှာ ထားပါ — page လုပ်ဖို့ မဟုတ်ပါဘူး။

Logs Insights query ရဲ့ ပုံစံ

lambda-latency.query
fields @timestamp, @duration
| filter @type = "REPORT"
| stats avg(@duration) as avgMs,
        pct(@duration, 99) as p99Ms,
        count(*) as invocations
  by bin(5m)
| sort @timestamp desc

Query တစ်ခုက '|' နဲ့ ဆက်ထားတဲ့ command တွေပါ: fields က column ရွေးတယ်၊ filter က row တွေ ချန်ထားတယ်၊ stats က စုစည်းတွက်ချက်ပြီး by bin(5m) က ၅ မိနစ်စာ bucket တွေ ခွဲတယ်။ Lambda ရဲ့ REPORT line တွေမှာ @duration၊ @maxMemoryUsed နဲ့ cold start ဖြစ်ရင် @initDuration ပါပါတယ်။

EMF နဲ့ business metric ထုတ်တဲ့ Lambda

Latency နဲ့ error rate ကို Lambda နဲ့ API Gateway က built-in metric အဖြစ် ပေးပြီးသားပါ။ ကိုယ်တိုင် ထုတ်ရမှာက business signal တွေပါ — order value၊ item count၊ payment declined ဖြစ်တာ စတာတွေပါ။ ဒီ function က order တစ်ခုချင်းအတွက် EMF line တစ်ကြောင်း print ထုတ်ပါတယ်၊ CloudWatch က အဲဒီကနေ metric သုံးခု ထုတ်ယူပြီး orderId နဲ့ customerId ကတော့ Logs Insights နဲ့ ရှာဖို့ log ထဲမှာပဲ ကျန်ပါတယ်။ Payment ခေါ်ပြီးသားလို့ ယူဆပြီး ရလဒ်ကို event ထဲ ထည့်ပေးထားပါတယ်။

checkout_metrics.py
import json

NAMESPACE = "Shop/Checkout"
DIMENSION_SETS = [["Service"], ["Service", "PaymentMethod"]]


def emf_line(timestamp_ms, dimensions, metrics, properties):
    """Build one CloudWatch Embedded Metric Format (EMF) log line."""
    record = {
        "_aws": {
            "Timestamp": timestamp_ms,
            "CloudWatchMetrics": [
                {
                    "Namespace": NAMESPACE,
                    "Dimensions": DIMENSION_SETS,
                    "Metrics": [{"Name": n, "Unit": u} for n, (_, u) in metrics.items()],
                }
            ],
        }
    }
    record.update(dimensions)
    record.update({n: v for n, (v, _) in metrics.items()})
    record.update(properties)
    return json.dumps(record)


def lambda_handler(event, context):
    order = json.loads(event["body"])
    approved = order["paymentStatus"] == "approved"
    total = round(sum(i["price"] * i["qty"] for i in order["items"]), 2)
    print(
        emf_line(
            event["requestContext"]["timeEpoch"],
            {"Service": "checkout", "PaymentMethod": order["paymentMethod"]},
            {
                "OrderValue": (total if approved else 0, "None"),
                "ItemCount": (sum(i["qty"] for i in order["items"]), "Count"),
                "Declined": (0 if approved else 1, "Count"),
            },
            {"orderId": order["orderId"], "customerId": order["customerId"]},
        )
    )
    return {"statusCode": 201 if approved else 402}


if __name__ == "__main__":
    for n, status in enumerate(["approved", "declined"], start=1):
        order = {
            "orderId": f"ord-100{n}",
            "customerId": "cus-42",
            "paymentMethod": "card",
            "paymentStatus": status,
            "items": [{"price": 19.99, "qty": 2}, {"price": 5.0, "qty": 1}],
        }
        event = {
            "requestContext": {"timeEpoch": 1794096000000},
            "body": json.dumps(order),
        }
        print(lambda_handler(event, None))

Browser ထဲမှာ run လို့ မရပါ

ဒီ code ဟာ AWS account အစစ်ပေါ်မှာ run ရမယ့် code ဖြစ်လို့ browser playground ထဲမှာ run လို့ မရပါ။ ကိုယ့် terminal (သို့) AWS CloudShell ထဲမှာ စမ်းပါ။

Code ကို တစ်ပိုင်းချင်း ရှင်းပြမယ်

  1. The _aws metadata block"_aws": {"Timestamp": timestamp_ms, "CloudWatchMetrics": [...]}

    ဒီ block ရှိလို့ CloudWatch Logs က ဒီ line ကို EMF အဖြစ် သိပါတယ်။ Timestamp က millisecond epoch ဖြစ်ရပြီး ဒီမှာ request ဝင်လာတဲ့ အချိန် (requestContext.timeEpoch) ကို သုံးထားပါတယ်။ Namespace၊ Dimensions နဲ့ Metrics က ဘယ် top-level field တွေကို metric အဖြစ် ထုတ်ရမလဲ ပြောပါတယ်။

  2. Dimension setsDIMENSION_SETS = [["Service"], ["Service", "PaymentMethod"]]

    Dimension set တစ်ခုစီက metric အသစ် တစ်စုစီ ဖန်တီးပါတယ်: Service တစ်ခုတည်းနဲ့ အားလုံးပေါင်း တစ်စု၊ Service+PaymentMethod နဲ့ payment method အလိုက် တစ်စု။ Custom metric တစ်ခုချင်းစီ ကျသင့်ငွေ ရှိလို့ set တွေ နည်းနည်းပဲ ထားပါ။

  3. Properties are not dimensions{"orderId": order["orderId"], "customerId": order["customerId"]}

    orderId နဲ့ customerId ကို dimension မလုပ်ပါဘူး — လုပ်လိုက်ရင် order တစ်ခုစီက metric အသစ် ဖြစ်ပြီး ငွေတွေ ပေါက်ကွဲသွားပါလိမ့်မယ်။ Log ထဲမှာပဲ ထားလို့ 'ဒီ customer ရဲ့ declined order တွေ' ကို Logs Insights နဲ့ ရှာလို့ ရနေဆဲပါ။

  4. print() is the transportprint(emf_line(...))

    Lambda မှာ stdout က CloudWatch Logs ဆီ တိုက်ရိုက် ရောက်လို့ API call မလိုဘဲ synchronous latency လည်း မတိုးပါဘူး။ Production မှာ Powertools for AWS Lambda ရဲ့ Metrics utility က ဒီ format ကို သင့်အတွက် ထုတ်ပေးပါတယ်။

You should see
{"_aws": {"Timestamp": 1794096000000, "CloudWatchMetrics": [{"Namespace": "Shop/Checkout", "Dimensions": [["Service"], ["Service", "PaymentMethod"]], "Metrics": [{"Name": "OrderValue", "Unit": "None"}, {"Name": "ItemCount", "Unit": "Count"}, {"Name": "Declined", "Unit": "Count"}]}]}, "Service": "checkout", "PaymentMethod": "card", "OrderValue": 44.98, "ItemCount": 3, "Declined": 0, "orderId": "ord-1001", "customerId": "cus-42"}
{'statusCode': 201}
{"_aws": {"Timestamp": 1794096000000, "CloudWatchMetrics": [{"Namespace": "Shop/Checkout", "Dimensions": [["Service"], ["Service", "PaymentMethod"]], "Metrics": [{"Name": "OrderValue", "Unit": "None"}, {"Name": "ItemCount", "Unit": "Count"}, {"Name": "Declined", "Unit": "Count"}]}]}, "Service": "checkout", "PaymentMethod": "card", "OrderValue": 0, "ItemCount": 3, "Declined": 1, "orderId": "ord-1002", "customerId": "cus-42"}
{'statusCode': 402}

Symptom ပေါ်မှာ page လုပ်တဲ့ alarm တွေ

Child alarm နှစ်ခု — p99 latency အတွက် static alarm တစ်ခုနဲ့ traffic နည်းတဲ့ ညဘက် percentile တွေ မတည်ငြိမ်တာကို သိဖို့ low-traffic alarm တစ်ခု — ကို action မပါဘဲ ဖန်တီးပါ။ ပြီးရင် composite alarm တစ်ခုတည်းကပဲ page လုပ်ပါတယ်။ '5 ခုထဲက 3 ခု' datapoint rule က spike တစ်ခုတည်းကို လျစ်လျူရှုပေးပါတယ်။

symptom-alarms.sh
aws cloudwatch put-metric-alarm \
  --alarm-name checkout-p99-latency \
  --namespace AWS/ApiGateway --metric-name Latency \
  --dimensions Name=ApiName,Value=shop-api \
  --extended-statistic p99 --period 60 \
  --evaluation-periods 5 --datapoints-to-alarm 3 \
  --threshold 1500 --comparison-operator GreaterThanThreshold \
  --treat-missing-data notBreaching

aws cloudwatch put-metric-alarm \
  --alarm-name checkout-5xx-rate \
  --namespace AWS/ApiGateway --metric-name 5XXError \
  --dimensions Name=ApiName,Value=shop-api \
  --statistic Average --period 60 \
  --evaluation-periods 5 --datapoints-to-alarm 3 \
  --threshold 0.02 --comparison-operator GreaterThanThreshold \
  --treat-missing-data notBreaching

aws cloudwatch put-metric-alarm \
  --alarm-name checkout-low-traffic \
  --namespace AWS/ApiGateway --metric-name Count \
  --dimensions Name=ApiName,Value=shop-api \
  --statistic Sum --period 300 --evaluation-periods 1 \
  --threshold 50 --comparison-operator LessThanThreshold

# Only this one has an action: one incident, one page
RULE='(ALARM("checkout-p99-latency") OR ALARM("checkout-5xx-rate"))'
RULE="$RULE"' AND NOT ALARM("checkout-low-traffic")'
aws cloudwatch put-composite-alarm \
  --alarm-name checkout-customer-impact \
  --alarm-rule "$RULE" \
  --alarm-actions arn:aws:sns:us-east-1:111122223333:oncall-page \
  --alarm-description "Runbook: https://wiki.example.com/runbooks/checkout"

5XXError ရဲ့ Average က rate ပါ

API Gateway ရဲ့ 5XXError metric က request တစ်ခုစီအတွက် 0 သို့မဟုတ် 1 ဖြစ်လို့ Average statistic က error rate (0.02 = 2%) ကို တိုက်ရိုက် ပေးပါတယ်။ Sum ကို သုံးရင် traffic များတဲ့ အချိန်မှာ အလိုလို မြင့်လာပြီး traffic နည်းတဲ့အခါ ပြဿနာကို လွတ်သွားပါတယ်။

Dashboard ကိုလည်း code အဖြစ် သိမ်းပါ။ အပေါ်ဆုံးမှာ composite alarm ရဲ့ state၊ အောက်မှာ business metric တွေ ထားလိုက်ရင် on-call က 'customer ထိခိုက်နေလား' နဲ့ 'ဘယ်လောက်လဲ' ကို တစ်ချက်တည်းနဲ့ မြင်ရပါတယ်။

dashboard.json
{
  "widgets": [
    {
      "type": "alarm",
      "x": 0, "y": 0, "width": 24, "height": 2,
      "properties": {
        "title": "Customer impact",
        "alarms": [
          "arn:aws:cloudwatch:us-east-1:111122223333:alarm:checkout-customer-impact"
        ]
      }
    },
    {
      "type": "metric",
      "x": 0, "y": 2, "width": 12, "height": 6,
      "properties": {
        "title": "Items ordered and declines (5 min sum)",
        "region": "us-east-1",
        "stat": "Sum",
        "period": 300,
        "metrics": [
          ["Shop/Checkout", "ItemCount", "Service", "checkout"],
          ["Shop/Checkout", "Declined", "Service", "checkout"]
        ]
      }
    }
  ]
}
put-dashboard.sh
aws cloudwatch put-dashboard \
  --dashboard-name checkout \
  --dashboard-body file://dashboard.json

Logs Insights နဲ့ 'ဘာကြောင့်လဲ' ကို ရှာခြင်း

Page ရောက်လာရင် ပထမ query က 'ဘယ်လောက် ဆိုးလဲ'၊ ဒုတိယက 'ဘယ်သူ့ကို ထိခိုက်လဲ' ပါ။ EMF log တွေက JSON ဖြစ်လို့ Declined၊ PaymentMethod၊ customerId field တွေကို တိုက်ရိုက် filter နဲ့ group လုပ်လို့ ရပါတယ်။ Cold start တွေကို REPORT line ထဲက @initDuration နဲ့ ရေတွက်လို့ ရပါတယ်။

declines-by-method.query
filter Service = "checkout"
| stats sum(Declined) as declined, count(*) as orders by PaymentMethod
| sort declined desc
cold-starts.query
filter @type = "REPORT" and ispresent(@initDuration)
| stats count(*) as coldStarts, avg(@initDuration) as avgInitMs by bin(1h)
run-query.sh
QUERY_ID=$(aws logs start-query \
  --log-group-name /aws/lambda/checkout \
  --start-time "$(date -d '1 hour ago' +%s)" --end-time "$(date +%s)" \
  --query-string 'filter Declined = 1 | stats count(*) by customerId | limit 10' \
  --query queryId --output text)

aws logs get-query-results --query-id "$QUERY_ID"

# Turn on tracing for the function (X-Ray active tracing)
aws lambda update-function-configuration \
  --function-name checkout --tracing-config Mode=Active

SLO နဲ့ error budget

28 ရက်အတွင်း 99.9% SLO ဆိုရင် error budget က request တွေရဲ့ 0.1% ပါ — တစ်လကို request သန်း ၁၀ ရှိရင် 'မကောင်းတဲ့' request ၁၀,၀၀၀ အထိ ခွင့်ရှိပါတယ်။ Threshold ကို ချိတ်တာထက် burn rate (budget ကို ဘယ်လောက် မြန်မြန် သုံးနေလဲ) ပေါ်မှာ alert လုပ်တာ ပိုကောင်းပါတယ်: မြန်မြန် burn ဖြစ်ရင် page လုပ်၊ ဖြည်းဖြည်း burn ရင် ticket ဖွင့်ပါ။ Budget ကုန်ခါနီးရင် feature release တွေ ရပ်ပြီး reliability ကို ဦးစားပေးဖို့ team က ကြိုတင် သဘောတူထားတာ အရေးကြီးပါတယ်။

Production မှာ

Production team တွေရဲ့ အလေ့အထ

Service တစ်ခုစီမှာ golden signal လေးခု (latency၊ traffic၊ errors၊ saturation) ပါတဲ့ dashboard တစ်ခုနဲ့ ပိုင်ရှင် team တစ်ခု ရှိပါတယ်။ Page လုပ်တဲ့ alarm တိုင်းရဲ့ description မှာ runbook link ပါပြီး runbook ထဲမှာ ကြည့်ရမယ့် Logs Insights query တွေ၊ trace filter တွေ ကြိုရေးထားပါတယ်။ ဒါကြောင့် ည ၃ နာရီ on-call က query တွေကို အစကနေ ရေးစရာ မလိုပါဘူး။

Multi-account organization တွေမှာ CloudWatch cross-account observability ကို သုံးပြီး monitoring account တစ်ခုကနေ workload account အားလုံးရဲ့ metric၊ log နဲ့ trace တွေကို ကြည့်ကြပါတယ်။ Log cost ကို ထိန်းဖို့ log group တိုင်းမှာ retention ထားတယ်၊ production မှာ DEBUG log ကို ပိတ်ထားတယ်၊ trace sampling ကို request အားလုံး မဟုတ်ဘဲ ရာခိုင်နှုန်း တစ်ခုပဲ ထားတယ်။ SLO တွေကို အပတ်စဉ် reliability review မှာ ပြန်ကြည့်ပြီး error budget ကုန်နေရင် နောက် sprint ကို reliability work အတွက် ပေးပါတယ်။

Synthetic check (CloudWatch Synthetics canary) တွေကလည်း အသုံးဝင်ပါတယ် — traffic မရှိတဲ့ ညဘက်မှာတောင် login နဲ့ checkout လမ်းကြောင်းကို မိနစ်အနည်းငယ်တစ်ခါ စမ်းပေးလို့ user တွေ မတိုင်ကြားခင် ပျက်နေတာကို သိရပါတယ်။

အမှားများ

အဖြစ်များတဲ့ အမှားတွေ

Cause metric တွေပေါ်မှာ page လုပ်ခြင်း

CPU၊ memory၊ queue depth လို metric တွေပေါ်မှာ page လုပ်ရင် customer ကို မထိခိုက်တဲ့ ပြဿနာအတွက် နှိုးခံရပြီး အကြောင်းရင်း အသစ်တစ်ခုကြောင့် ဖြစ်တဲ့ incident ကိုတော့ လွတ်သွားပါတယ်။ Latency၊ error rate၊ success rate လို symptom တွေပေါ်မှာ page လုပ်ပြီး cause metric တွေကို debugging dashboard ပေါ်မှာ ထားပါ။

High-cardinality value ကို dimension လုပ်ခြင်း

userId၊ orderId၊ requestId လို value တွေကို EMF dimension အဖြစ် ထည့်လိုက်ရင် unique value တစ်ခုစီက custom metric အသစ် ဖြစ်ပြီး တစ်ခုချင်းစီ ကျသင့်ပါတယ်။ အဲဒါတွေကို ordinary property အဖြစ်ပဲ ထားပြီး Logs Insights နဲ့ query လုပ်ပါ။

Log retention မထားခြင်း

Log group အသစ်တွေရဲ့ default retention က 'Never expire' ပါ။ နှစ်အနည်းငယ်ကြာရင် ဘယ်သူမှ မဖတ်တဲ့ debug log TB ချီအတွက် storage ပိုက်ဆံ ပေးနေရပါတယ်။ Log group ဖန်တီးတဲ့ IaC ထဲမှာ retention ကို အမြဲ ထည့်ပါ။

Alarm တွေကို ဘယ်တော့မှ မစမ်းခြင်း

Alarm တစ်ခုက dimension နာမည် စာလုံးပေါင်း မှားတာကြောင့် INSUFFICIENT_DATA မှာ အမြဲ ရှိနေနိုင်ပြီး ဘယ်တော့မှ မမြည်ပါဘူး။ aws cloudwatch set-alarm-state နဲ့ notification လမ်းကြောင်းကို စမ်းပြီး INSUFFICIENT_DATA မှာ ကြာကြာ ရှိနေတဲ့ alarm တွေကို ပုံမှန် စစ်ပါ။

💰 Cost & Cleanup

CloudWatch ကုန်ကျစရိတ်ရဲ့ အဓိက driver တွေ: custom metric တစ်ခုချင်း (dimension combination တစ်ခုချင်း) လစဉ်၊ alarm တစ်ခုချင်း (anomaly detection နဲ့ high-resolution alarm တွေက ပိုဈေးကြီး)၊ log ingest လုပ်တဲ့ GB (များသောအားဖြင့် အကြီးဆုံး)၊ log storage GB-month နဲ့ Logs Insights က scan လုပ်တဲ့ GB ပါ။ X-Ray က record နဲ့ retrieve/scan လုပ်တဲ့ trace အရေအတွက်နဲ့ ကျသင့်ပါတယ်။ Free tier မှာ metric၊ alarm နဲ့ log GB အနည်းငယ် ပါပါတယ် — အတိအကျ ပမာဏကို pricing page မှာ စစ်ပါ။

Lab ပြီးရင် alarm တွေ (composite ကို အရင်)၊ dashboard နဲ့ test log group ကို ဖျက်ပါ။

စမ်းပြီးရင် ဖျက်ဖို့ command

cleanup.sh
aws cloudwatch delete-alarms --alarm-names checkout-customer-impact
aws cloudwatch delete-alarms \
  --alarm-names checkout-p99-latency checkout-5xx-rate checkout-low-traffic \
  checkout-declines-anomaly
aws cloudwatch delete-dashboards --dashboard-names checkout
aws logs delete-log-group --log-group-name /aws/lambda/checkout

Mini challenge

Payment decline တွေအတွက် anomaly detection alarm

Decline အရေအတွက်က နေ့ဘက်မှာ များပြီး ညဘက်မှာ နည်းလို့ static threshold က အလုပ်မဖြစ်ပါဘူး။ Shop/Checkout ထဲက Declined metric (Service=checkout) အတွက် anomaly detection alarm တစ်ခု ဖန်တီးပါ။

  • 5 မိနစ် period နဲ့ Sum statistic ကို သုံးရမယ်
  • Band width 2 standard deviation ဖြစ်ရမယ်
  • Band ရဲ့ အပေါ်ဘက်ကို ကျော်မှသာ alarm ဖြစ်ရမယ် (decline နည်းတာက ပြဿနာ မဟုတ်)
  • Datapoint မရှိရင် breaching မဟုတ်လို့ သတ်မှတ်ရမယ်

ရလဒ်: describe-alarms မှာ checkout-declines-anomaly ကို ThresholdMetricId ad1 နဲ့ မြင်ရပြီး console graph မှာ band အရောင်ခြယ်ထားတာကို တွေ့ရပါမယ် (model က training data လုံလောက်မှ band ကို ပြပါတယ်)။

အရိပ်အမြွက် ကြည့်ရန်

--metrics ထဲမှာ metric query (m1) နဲ့ ANOMALY_DETECTION_BAND(m1, 2) expression (ad1) နှစ်ခု ထည့်ပြီး --threshold-metric-id ad1 နဲ့ GreaterThanUpperThreshold comparison operator ကို သုံးပါ။

အဖြေ ကြည့်ရန်
declines-anomaly-alarm.sh
cat > declines-metrics.json <<'EOF'
[
  {
    "Id": "m1",
    "ReturnData": true,
    "MetricStat": {
      "Metric": {
        "Namespace": "Shop/Checkout",
        "MetricName": "Declined",
        "Dimensions": [{ "Name": "Service", "Value": "checkout" }]
      },
      "Period": 300,
      "Stat": "Sum"
    }
  },
  { "Id": "ad1", "Expression": "ANOMALY_DETECTION_BAND(m1, 2)", "ReturnData": true }
]
EOF

aws cloudwatch put-metric-alarm \
  --alarm-name checkout-declines-anomaly \
  --metrics file://declines-metrics.json \
  --threshold-metric-id ad1 \
  --comparison-operator GreaterThanUpperThreshold \
  --evaluation-periods 3 --datapoints-to-alarm 2 \
  --treat-missing-data notBreaching \
  --alarm-actions arn:aws:sns:us-east-1:111122223333:oncall-ticket

Quick Quiz 1 / 3

Composite alarm ကို သုံးတဲ့ အဓိက အကျိုးကျေးဇူးက ဘာလဲ။

Quick Quiz 2 / 3

EMF metric တစ်ခုမှာ customerId ကို ဘယ်လို ထည့်သင့်လဲ။

Quick Quiz 3 / 3

အောက်က alarm တွေထဲက ဘယ်ဟာက on-call ကို page လုပ်သင့်ဆုံးလဲ။

မှတ်ထားရမယ့် အချက်များ

  • Metrics = ဘယ်တုန်းက/ဘယ်လောက်၊ traces = ဘယ်နေရာ၊ logs = ဘာကြောင့်
  • Symptom (latency၊ errors) ပေါ်မှာ page လုပ်ပြီး M-of-N datapoint နဲ့ composite alarm နဲ့ noise လျှော့ပါ
  • Anomaly detection က ပုံမှန် အတက်အကျ ရှိတဲ့ metric တွေအတွက်၊ static threshold က ရှင်းလင်းတဲ့ limit တွေအတွက်ပါ
  • EMF က log line ကနေ metric ထုတ်ပေးတယ် — high-cardinality ID တွေကို dimension မလုပ်ပါနဲ့
  • SLO နဲ့ error budget က 'ဘယ်လောက် reliable ဖြစ်ရမလဲ' ကို team တစ်ခုလုံး သဘောတူအောင် လုပ်ပေးပါတယ်

Amazon CloudWatch User Guide — AWS Documentation

CloudWatch Logs Insights query syntax — AWS Documentation

AWS X-Ray Developer Guide — AWS Documentation

နောက်ဆုံးပြင်ဆင်ရက်

Progress ကို ဤ browser (ဤစက်) တွင်သာ သိမ်းထားပါသည်။ အခြားစက်များနှင့် sync မလုပ်ပါ။

ဤသင်ခန်းစာတွင် အမှားတွေ့ပါက အကြောင်းကြားရန်သင့် email app ကို ဤသင်ခန်းစာလိပ်စာ ဖြည့်ပြီးသားဖြင့် ဖွင့်ပေးပါမည် — သင်ကိုယ်တိုင် ပို့မှသာ ပို့မည်ဖြစ်သည်။

Observability: Metrics၊ Logs၊ Traces နဲ့ SLO | Thuta Learning