【问题标题】:CloudWatch Alarm Percentage of errors API GatewayCloudWatch 警报错误百分比 API 网关
【发布时间】:2020-10-23 19:13:45
【问题描述】:

我正在尝试使用 terraform 在 Cloudwatch 中进行设置和警报。 我的告警基本需要检查网关在1分钟的2个时段内是否有超过5%的5xx错误。

我尝试了以下代码,但它不起作用:

resource "aws_cloudwatch_metric_alarm" "gateway_error_rate" {
  alarm_name          = "gateway-errors"
  comparison_operator = "GreaterThanOrEqualToThreshold"
  alarm_description   = "Gateway error rate has exceeded 5%"
  treat_missing_data  = "notBreaching"
  metric_name         = "5XXError"
  namespace           = "AWS/ApiGateway"
  period              = 60
  evaluation_periods  = 2
  threshold           = 5
  statistic           = "Average"
  unit                = "Percent"

  dimensions = {
    ApiName = "my-api"
    Stage = "dev"
  }
}

即使部署了警报,也不会显示数据。 做一些测试,我注意到这个警报显然不接受单位“百分比”。

有人在terraformcloudformation 中有关于如何配置此类警报的示例吗?

【问题讨论】:

  • 有效单位是Count。我认为您需要一个自定义指标,例如基于 5XXErrorCount 的比率。
  • 谢谢@Marcin!,根据您的评论,我发现了这个docs.aws.amazon.com/apigateway/latest/developerguide/… 所以“平均统计数据表示5XXError 错误率,即5XXError 错误的总数除以总数在此期间的请求数”,因此假设阈值 1 是 100% 的呼叫失败,我是否也应该将阈值调整为 0.05?
  • 是的,似乎正是我的建议。那么就不需要自定义指标了:-)

标签: amazon-web-services terraform amazon-cloudformation amazon-cloudwatch cloudwatch-alarms


【解决方案1】:

根据 Marcin 在 cmets 中提供的信息,我在 aws 文档中找到了 this 信息:

Ave​​rage 统计量表示 5XXError 错误率,即 5XXError 错误的总数除以该期间的请求总数。分母对应于 Count 指标(如下)。

我在 terraform 中配置的警报如下所示:

resource "aws_cloudwatch_metric_alarm" "gateway_error_rate" {
  alarm_name          = "gateway-errors"
  comparison_operator = "GreaterThanOrEqualToThreshold"
  alarm_description   = "Gateway error rate has exceeded 5%"
  treat_missing_data  = "notBreaching"
  metric_name         = "5XXError"
  namespace           = "AWS/ApiGateway"
  period              = 60
  evaluation_periods  = 2
  threshold           = 0.05
  statistic           = "Average"
  unit                = "Count"

  dimensions = {
    ApiName = "my-api"
    Stage = "dev"
  }
}

【讨论】:

    【解决方案2】:

    我在 CloudFormation 上使用它,它工作正常,我使用 SUM 而不是“百分比”

      ApiGateway5XXErrorAlarm:
        Type: 'AWS::CloudWatch::Alarm'
        Properties:
          AlarmDescription: 'Api Gateway server-side errors captured'
          Namespace: 'AWS/ApiGateway'
          MetricName: 5XXError
          Dimensions:
          - Name: ApiName
            Value: !Ref ApiGateway
          - Name: Stage
            Value: dev
          Statistic: Sum
          Period: 60
          EvaluationPeriods: 1
          Threshold: 1
          ComparisonOperator: GreaterThanOrEqualToThreshold
          AlarmActions:
          - !Ref Alerts
          TreatMissingData: notBreaching
    

    【讨论】:

    • 嗨!感谢您的回答!这会计算错误的百分比吗?我认为当 API 网关中产生多个 5XX 错误时会发出警报,但是我正在寻找的是一个警报计算百分比,当超过 5% 的调用总数是 5XX 时。
    猜你喜欢
    • 2018-12-13
    • 2021-05-13
    • 1970-01-01
    • 2021-06-03
    • 2020-03-26
    • 2021-05-25
    • 2022-01-01
    • 2013-12-19
    • 2012-10-03
    相关资源
    最近更新 更多