gpt4 book ai didi

java - CloudWatch Alarm 进入针对单个数据点的 ALARM

转载 作者:行者123 更新时间:2023-12-02 02:20:56 27 4
gpt4 key购买 nike

如果平均性能超过 5 毫秒,我将尝试创建 cloudwatch 警报。我已配置 Amazon CloudWatch 警报来检查平均值。但是,如果只有一个数据点超过阈值,警报就会进入“警报”状态。

public static void main(String[] args) {
AWSExample aws = new AWSExample();
aws.testMethod();
}

这是测试方法。

public void testMethod() {
Instant start = Instant.now();
try {
try {
long myValue = (long) ((Math.random())*10000);
if(myValue>8000){
myValue = myValue - 3000;
}
Thread.sleep(myValue);
} catch(InterruptedException ex) {
Thread.currentThread().interrupt();
}
} catch (Throwable t) {
t.printStackTrace();
} finally {
Instant end = Instant.now();
Duration nano = Duration.between(start, end);
long endTime = nano.toMillis();
createMetricData(endTime);
createAnAlarm();
}
}

创建指标数据的方法

public void createMetricData(Long metricValue) {
final AmazonCloudWatch cw = getAmazonCloudWatch();
Dimension dimension = new Dimension().withName("UNIQUE_METHOD").withValue("testMethod");
MetricDatum datum = new MetricDatum()
.withMetricName("Method Execution Performance")
.withUnit(StandardUnit.Milliseconds).withValue(metricValue.doubleValue())
.withDimensions(dimension)
.withTimestamp(new Date());
PutMetricDataRequest metricDataRequest = new PutMetricDataRequest()
.withNamespace("METHOD/TRAFFIC").withMetricData(datum);
PutMetricDataResult response = cw.putMetricData(metricDataRequest);
System.out.println(response);
System.out.printf("Successfully put data point %f", metricValue.doubleValue());
}

这是创建警报的方法。

private void createAnAlarm(){
final AmazonCloudWatch cw = getAmazonCloudWatch();
PutMetricAlarmRequest putMetricAlarmRequest = new PutMetricAlarmRequest()
.withPeriod(120)// The period, in seconds, over which the specified statistic is applied. Valid values are 10, 30, and any multiple of 60.
.withMetricName("Method Execution Performance")// The name for // the metric // associated // with the // alarm.
.withNamespace("METHOD/TRAFFIC")// The namespace for the metric // associated with the alarm. // https://docs.aws.amazon.com/AmazonCloudWatch/latest/monitoring/aws-namespaces.html
.withAlarmName("aws-Method-Performance")// The name for the alarm. // This name must be unique // within the AWS account.
.withEvaluationPeriods(1)// The number of periods over which // data is compared to the specified // threshold. An alarm's total // current evaluation period can be // no longer than one day, so this
// number multiplied by period cannot be more than 86,400 seconds.
.withActionsEnabled(true)// Indicates whether actions should be executed during any changes to
// the alarm state.
.withStatistic(Statistic.Average)// The statistic for the metric associated with the alarm, other than percentile
.withThreshold(5.0)// The value against which the specified statistic is compared.
.withComparisonOperator(ComparisonOperator.GreaterThanThreshold)
.withAlarmDescription("Alarm when method execution time exceeds 5 milliseconds")
.withAlarmActions("arn:aws:sns:eu-west-1:***********")//The actions to
//execute when this alarm transitions to the ALARM state from
// any other state. Each action is specified as an Amazon
// Resource Name (ARN).
.withUnit(StandardUnit.Milliseconds)// The unit of measure for
// the statistic
.withDimensions(new Dimension().withName("UNIQUE_METHOD").withValue("testMethod"));
PutMetricAlarmResult result = cw.putMetricAlarm(putMetricAlarmRequest);
System.out.println(result);
}

我希望该方法的平均性能超过 5 毫秒。这里有什么问题以及如何解决它?

最佳答案

所以问题是,如果只有一个数据点突破阈值,为什么警报会进入“警报”状态?那是因为您在警报创建中有这一行:

.withEvaluationPeriods(1)

此外,您每次发布数据点时都会调用 createAnAlarm();。无需这样做,您只需创建一次警报,它就会持续监控您的指标。

正如评论中所讨论的,触发此警报的实际原因是阈值设置为 5 毫秒,但该方法的预期执行时间在秒范围内。在这种情况下设置的正确阈值是 5 秒:

.withThreshold(5000.0)

关于java - CloudWatch Alarm 进入针对单个数据点的 ALARM,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48514299/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com