Services or capabilities described in Amazon Web Services documentation might vary by Region. To see the differences applicable to the China Regions,
see Getting Started with Amazon Web Services in China
(PDF).
Create a CloudWatch anomaly detector
The following code examples show how to create an Amazon CloudWatch anomaly detector.
Action examples are code excerpts from larger programs and must be run in context. You can see this action in
context in the following code example:
- .NET
-
- Amazon SDK for .NET
-
/// <summary>
/// Add an anomaly detector for a single metric.
/// </summary>
/// <param name="anomalyDetector">A single metric anomaly detector.</param>
/// <returns>True if successful.</returns>
public async Task<bool> PutAnomalyDetector(SingleMetricAnomalyDetector anomalyDetector)
{
var putAlarmDetectorResult = await _amazonCloudWatch.PutAnomalyDetectorAsync(
new PutAnomalyDetectorRequest()
{
SingleMetricAnomalyDetector = anomalyDetector
});
return putAlarmDetectorResult.HttpStatusCode == HttpStatusCode.OK;
}
- Java
-
- SDK for Java 2.x
-
public static void addAnomalyDetector(CloudWatchClient cw, String fileName) {
try {
// Read values from the JSON file.
JsonParser parser = new JsonFactory().createParser(new File(fileName));
com.fasterxml.jackson.databind.JsonNode rootNode = new ObjectMapper().readTree(parser);
String customMetricNamespace = rootNode.findValue("customMetricNamespace").asText();
String customMetricName = rootNode.findValue("customMetricName").asText();
SingleMetricAnomalyDetector singleMetricAnomalyDetector = SingleMetricAnomalyDetector.builder()
.metricName(customMetricName)
.namespace(customMetricNamespace)
.stat("Maximum")
.build();
PutAnomalyDetectorRequest anomalyDetectorRequest = PutAnomalyDetectorRequest.builder()
.singleMetricAnomalyDetector(singleMetricAnomalyDetector)
.build();
cw.putAnomalyDetector(anomalyDetectorRequest);
System.out.println("Added anomaly detector for metric "+customMetricName+".");
} catch (CloudWatchException | IOException e) {
System.err.println(e.getMessage());
System.exit(1);
}
}
- Kotlin
-
- SDK for Kotlin
-
This is prerelease documentation for a feature in preview release. It is subject to change.
suspend fun addAnomalyDetector(fileName: String?) {
// Read values from the JSON file.
val parser = JsonFactory().createParser(File(fileName))
val rootNode = ObjectMapper().readTree<JsonNode>(parser)
val customMetricNamespace = rootNode.findValue("customMetricNamespace").asText()
val customMetricName = rootNode.findValue("customMetricName").asText()
val singleMetricAnomalyDetectorVal = SingleMetricAnomalyDetector {
metricName = customMetricName
namespace = customMetricNamespace
stat = "Maximum"
}
val anomalyDetectorRequest = PutAnomalyDetectorRequest {
singleMetricAnomalyDetector = singleMetricAnomalyDetectorVal
}
CloudWatchClient { region = "us-east-1" }.use { cwClient ->
cwClient.putAnomalyDetector(anomalyDetectorRequest)
println("Added anomaly detector for metric $customMetricName.")
}
}
For a complete list of Amazon SDK developer guides and code examples, see
Using CloudWatch with an Amazon SDK.
This topic also includes information about getting started and details about previous SDK versions.