D'autres AWS SDK exemples sont disponibles dans le GitHub dépôt AWS Doc SDK Examples.
Les traductions sont fournies par des outils de traduction automatique. En cas de conflit entre le contenu d'une traduction et celui de la version originale en anglais, la version anglaise prévaudra.
À utiliser PutAnomalyDetector
avec un AWS SDK ou CLI
Les exemples de code suivants montrent comment utiliserPutAnomalyDetector
.
Les exemples d’actions sont des extraits de code de programmes de plus grande envergure et doivent être exécutés en contexte. Vous pouvez voir cette action en contexte dans l’exemple de code suivant :
- .NET
-
- AWS 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
-
- SDKpour 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
-
- SDKpour Kotlin
-
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.")
}
}