Java CloudWatch警报进入单个数据点的警报
如果平均性能超过5毫秒,我将尝试创建cloudwatch警报。我已经配置了Amazon CloudWatch警报来检查平均值。但是,如果只有一个数据点超过阈值,则警报将进入警报状态Java CloudWatch警报进入单个数据点的警报,java,amazon-web-services,amazon-cloudwatch,Java,Amazon Web Services,Amazon Cloudwatch,如果平均性能超过5毫秒,我将尝试创建cloudwatch警报。我已经配置了Amazon CloudWatch警报来检查平均值。但是,如果只有一个数据点超过阈值,则警报将进入警报状态 public static void main(String[] args) { AWSExample aws = new AWSExample(); aws.testMethod(); } 这里是testMethod public void testMethod() { Instant
public static void main(String[] args) {
AWSExample aws = new AWSExample();
aws.testMethod();
}
这里是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)
所以问题是,如果只有一个数据点超过阈值,为什么报警会进入报警状态?这是因为在报警创建中有这一行:
.withEvaluationPeriods(1)
另外,您正在调用createAnAlarm()代码>每次发布数据点时。没有必要这样做,您可以创建一次警报,它将继续监视您的度量
如注释中所述,触发此警报的实际原因是阈值设置为5毫秒,但方法的预期执行时间在秒范围内。在这种情况下,要设置的正确阈值为5秒:
.withThreshold(5000.0)
我知道为什么。但问题是我希望该方法的平均性能超过5毫秒。这里的问题是什么?我如何解决IT平均在哪个时期?如果您希望在一天内获得平均值,那么您可以设置。例如,使用period(86400)
。在2分钟内获得平均值。所以我设定了120的周期。为了澄清上午5:00到5:02之间的情况,我们对其中一个方法进行了40次调用。因此,如果该方法的平均性能超过5秒,我需要创建一个警报。是否需要秒或毫秒,这可能是。withThreshold(5000.0)
?除此之外,代码看起来还可以。您可以在问题中添加CloudWatch仪表板上的图形截图(您可以在仪表板上添加警报),以便更好地说明警报何时触发以及您预计何时触发?我知道原因。但问题是我希望该方法的平均性能超过5毫秒。这里的问题是什么?我如何解决IT平均在哪个时期?如果您希望在一天内获得平均值,那么您可以设置。例如,使用period(86400)
。在2分钟内获得平均值。所以我设定了120的周期。为了澄清上午5:00到5:02之间的情况,我们对其中一个方法进行了40次调用。因此,如果该方法的平均性能超过5秒,我需要创建一个警报。是否需要秒或毫秒,这可能是。withThreshold(5000.0)
?除此之外,代码看起来还可以。您是否可以在问题中添加CloudWatch仪表板的图形截图(您可以在仪表板中添加警报),以便更好地说明警报何时触发以及您预计何时触发?