翻訳は機械翻訳により提供されています。提供された翻訳内容と英語版の間で齟齬、不一致または矛盾がある場合、英語版が優先します。
付録 B — カイ二乗計算の例
以下は、エラーメトリクスを収集し、データに対してカイ二乗検定を実行する例です。このコードは本番環境に対応しておらず、必要なエラー処理は行いませんが、ロジックの仕組みについての概念実証は提供されています。この例はニーズに合わせて更新する必要があります。
まず、Amazon EventBridge のスケジュールされたイベントによって 1 分おきに Lambda 関数が呼び出されます。イベントのコンテンツは、次のデータで構成されます。
{
"timestamp": "2023-03-15T15:26:37.527Z",
"namespace": "multi-az/frontend",
"metricName": "5xx",
"dimensions": [
{ "Name": "Region", "Value": "us-east-1" },
{ "Name": "Controller", "Value": "Home" },
{ "Name": "Action", "Value": "Index" }
],
"period": 60,
"stat": "Sum",
"unit": "Count",
"chiSquareMetricName": "multi-az/chi-squared",
"azs": [ "use1-az2", "use1-az4", "use1-az6" ]
}
このデータを使用して、適切な CloudWatch メトリクス (名前空間、メトリックス名、ディメンションなど) の取得に必要な共通データを指定し、各アベイラビリティーゾーンのカイ二乗結果を公開します。Python 3.9 を使用した場合、Lambda 関数のコードは次のようになります。大まかな流れとしては、指定した CloudWatch メトリクスを過去 1 分間収集し、そのデータに対してカイ二乗検定を実行し、指定されたアベイラビリティーゾーンごとに検定結果に関する CloudWatch メトリクスをパブリッシュします。
import os
import boto3
import datetime
import copy
import json
from datetime import timedelta
from scipy.stats import chisquare
from aws_embedded_metrics import metric_scope
cw_client = boto3.client("cloudwatch", os.environ.get("AWS_REGION", "us-east-1"))
@metric_scope
def handler(event, context, metrics):
metrics.set_property("Event", json.loads(json.dumps(event, default = str)))
time = datetime.datetime.strptime(event["timestamp"], "%Y-%m-%dT%H:%M:%S.%fZ")
# Round down to the previous minute
end: datetime = roundTime(time)
# Subtract a minute for the start
start: datetime = end - timedelta(minutes = 1)
# Get all the metrics that match the query
results = get_all_metrics(event, start, end, metrics)
metrics.set_property("MetricCounts", results)
# Calculate the chi squared result
chi_sq_result = chisquare(list(results.values()))
expected = sum(list(results.values())) / len(results.values())
metrics.set_property("ChiSquaredResult", chi_sq_result)
# Put the chi square metrics into CloudWatch
put_all_metrics(event, results, chi_sq_result[1], expected, start, metrics)
def get_all_metrics(detail: dict, start: datetime, end: datetime, metrics):
"""
Gets all of the error metrics for each AZ specified
"""
metric_query = {
"MetricDataQueries": [
],
"StartTime": start,
"EndTime": end
}
for az in detail["azs"]:
dim = copy.deepcopy(detail["dimensions"])
dim.append({"Name": "AZ-ID", "Value": az})
query = {
"Id": az.replace("-", "_"),
"MetricStat": {
"Metric": {
"Namespace": detail["namespace"],
"MetricName": detail["metricName"],
"Dimensions": dim
},
"Period": int(detail["period"]),
"Stat": detail["stat"],
"Unit": detail["unit"]
},
"Label": az,
"ReturnData": True
}
metric_query["MetricDataQueries"].append(query)
metrics.set_property("GetMetricRequest", json.loads(json.dumps(metric_query, default=str)))
next_token: str = None
results = {}
while True:
if next_token is not None:
metric_query["NextToken"] = next_token
data = cw_client.get_metric_data(**metric_query)
if next_token is not None:
metrics.set_property("GetMetricResult::" + next_token, json.loads(json.dumps(data, default = str)))
else:
metrics.set_property("GetMetricResult", json.loads(json.dumps(data, default = str)))
for item in data["MetricDataResults"]:
key = item["Id"].replace("_", "-")
if key not in results:
results[key] = 0
results[key] += sum(item["Values"])
if "NextToken" in data:
next_token = data["NextToken"]
if next_token is None:
break
return results
def put_all_metrics(detail: dict, results: dict, chi_sq_value: float, expected: float, timestamp: datetime, metrics):
"""
Adds the chi squared metric for all AZs to CloudWatch
"""
farthest_from_expected = None
if len(results) > 0:
keys = list(results.keys())
farthest_from_expected = keys[0]
for key in keys:
if abs(results[key] - expected) > abs(results[farthest_from_expected] - expected):
farthest_from_expected = key
metric_query = {
"Namespace": detail["namespace"],
"MetricData": []
}
for az in detail["azs"]:
dim = copy.deepcopy(detail["dimensions"])
dim.append({"Name": "AZ-ID", "Value": az})
query = {
"MetricName": detail["chiSquareMetricName"],
"Dimensions": dim,
"Timestamp": timestamp,
}
if chi_sq_value <= 0.05 and az == farthest_from_expected:
query["Value"] = 1
else:
query["Value"] = 0
metric_query["MetricData"].append(query)
metrics.set_property("PutMetricRequest", json.loads(json.dumps(metric_query, default = str)))
cw_client.put_metric_data(**metric_query)
def roundTime(dt=None, roundTo=60):
"""Round a datetime object to any time lapse in seconds
dt : datetime.datetime object, default now.
roundTo : Closest number of seconds to round to, default 1 minute.
"""
if dt == None : dt = datetime.datetime.now()
seconds = (dt.replace(tzinfo=None) - dt.min).seconds
rounding = (seconds+roundTo/2) // roundTo * roundTo
return dt + datetime.timedelta(0,rounding-seconds,-dt.microsecond)
その後、AZ ごとにアラームを作成できます。以下は use1-az2
の例で、最大値が 1 に等しい 3 つの 1 分間のデータポイントを連続して警告します (1 は、カイ二乗検定でエラー率の統計的に有意な偏りが確認された場合に公開されるメトリクスです)。
{
"Type": "AWS::CloudWatch::Alarm",
"Properties": {
"AlarmName": "use1-az2-chi-squared",
"ActionsEnabled": true,
"OKActions": [],
"AlarmActions": [],
"InsufficientDataActions": [],
"MetricName": "multi-az/chi-squared",
"Namespace": "multi-az/frontend",
"Statistic": "Maximum",
"Dimensions": [
{
"Name": "AZ-ID",
"Value": "use1-az2"
},
{
"Name": "Action",
"Value": "Index"
},
{
"Name": "Region",
"Value": "us-east-1"
},
{
"Name": "Controller",
"Value": "Home"
}
],
"Period": 60,
"EvaluationPeriods": 3,
"DatapointsToAlarm": 3,
"Threshold": 1,
"ComparisonOperator": "GreaterThanOrEqualToThreshold",
"TreatMissingData": "missing"
}
}
また、m-of-n アラームを作成して、これら 2 つのアラームを複合アラームと組み合わせることもできます。また、各アベイラビリティーゾーンにあるコントローラー/アクションの組み合わせまたはマイクロサービスごとに同じアラームを作成する必要があります。最後に、外れ値検出による障害検出 に示すように、コントローラーとアクションの組み合わせごとに、カイ二乗複合アラームをアベイラビリティーゾーン固有のアラームに追加できます。