CloudWatch exemples d'utilisation SDK pour Kotlin - Exemples de code de l'AWS SDK

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.

CloudWatch exemples d'utilisation SDK pour Kotlin

Les exemples de code suivants vous montrent comment effectuer des actions et implémenter des scénarios courants en utilisant le AWS SDK for Kotlin with CloudWatch.

Les principes de base sont des exemples de code qui vous montrent comment effectuer les opérations essentielles au sein d'un service.

Les actions sont des extraits de code de programmes plus larges et doivent être exécutées dans leur contexte. Les actions vous indiquent comment appeler des fonctions de service individuelles, mais vous pouvez les visualiser dans leur contexte dans les scénarios correspondants.

Chaque exemple inclut un lien vers le code source complet, où vous trouverez des instructions sur la façon de configurer et d'exécuter le code en contexte.

Mise en route

Les exemples de code suivants montrent comment commencer à utiliser CloudWatch.

SDKpour Kotlin
Note

Il y en a plus à ce sujet GitHub. Trouvez l’exemple complet et découvrez comment le configurer et l’exécuter dans le référentiel d’exemples de code AWS.

/** Before running this Kotlin code example, set up your development environment, including your credentials. For more information, see the following documentation topic: https://docs.aws.amazon.com/sdk-for-kotlin/latest/developer-guide/setup.html */ suspend fun main(args: Array<String>) { val usage = """ Usage: <namespace> Where: namespace - The namespace to filter against (for example, AWS/EC2). """ if (args.size != 1) { println(usage) exitProcess(0) } val namespace = args[0] listAllMets(namespace) } suspend fun listAllMets(namespaceVal: String?) { val request = ListMetricsRequest { namespace = namespaceVal } CloudWatchClient { region = "us-east-1" }.use { cwClient -> cwClient .listMetricsPaginated(request) .transform { it.metrics?.forEach { obj -> emit(obj) } } .collect { obj -> println("Name is ${obj.metricName}") println("Namespace is ${obj.namespace}") } } }
  • Pour API plus de détails, voir ListMetricsla APIréférence AWS SDK à Kotlin.

Principes de base

L’exemple de code suivant illustre comment :

  • CloudWatch Répertoriez les espaces de noms et les métriques.

  • obtenir les statistiques d'une métrique et de la facturation estimée ;

  • créer et mettre à jour un tableau de bord ;

  • créer et ajouter des données à une métrique ;

  • créer et déclencher une alerte, puis consulter l'historique des alertes ;

  • créer un détecteur d'anomalies ;

  • obtenez une image de métrique, puis nettoyer les ressources.

SDKpour Kotlin
Note

Il y en a plus à ce sujet GitHub. Trouvez l’exemple complet et découvrez comment le configurer et l’exécuter dans le référentiel d’exemples de code AWS.

/** Before running this Kotlin code example, set up your development environment, including your credentials. For more information, see the following documentation topic: https://docs.aws.amazon.com/sdk-for-kotlin/latest/developer-guide/setup.html To enable billing metrics and statistics for this example, make sure billing alerts are enabled for your account: https://docs.aws.amazon.com/AmazonCloudWatch/latest/monitoring/monitor_estimated_charges_with_cloudwatch.html#turning_on_billing_metrics This Kotlin code example performs the following tasks: 1. List available namespaces from Amazon CloudWatch. Select a namespace from the list. 2. List available metrics within the selected namespace. 3. Get statistics for the selected metric over the last day. 4. Get CloudWatch estimated billing for the last week. 5. Create a new CloudWatch dashboard with metrics. 6. List dashboards using a paginator. 7. Create a new custom metric by adding data for it. 8. Add the custom metric to the dashboard. 9. Create an alarm for the custom metric. 10. Describe current alarms. 11. Get current data for the new custom metric. 12. Push data into the custom metric to trigger the alarm. 13. Check the alarm state using the action DescribeAlarmsForMetric. 14. Get alarm history for the new alarm. 15. Add an anomaly detector for the custom metric. 16. Describe current anomaly detectors. 17. Get a metric image for the custom metric. 18. Clean up the Amazon CloudWatch resources. */ val DASHES: String? = String(CharArray(80)).replace("\u0000", "-") suspend fun main(args: Array<String>) { val usage = """ Usage: <myDate> <costDateWeek> <dashboardName> <dashboardJson> <dashboardAdd> <settings> <metricImage> Where: myDate - The start date to use to get metric statistics. (For example, 2023-01-11T18:35:24.00Z.) costDateWeek - The start date to use to get AWS Billing and Cost Management statistics. (For example, 2023-01-11T18:35:24.00Z.) dashboardName - The name of the dashboard to create. dashboardJson - The location of a JSON file to use to create a dashboard. (See Readme file.) dashboardAdd - The location of a JSON file to use to update a dashboard. (See Readme file.) settings - The location of a JSON file from which various values are read. (See Readme file.) metricImage - The location of a BMP file that is used to create a graph. """ if (args.size != 7) { println(usage) System.exit(1) } val myDate = args[0] val costDateWeek = args[1] val dashboardName = args[2] val dashboardJson = args[3] val dashboardAdd = args[4] val settings = args[5] var metricImage = args[6] val dataPoint = "10.0".toDouble() val inOb = Scanner(System.`in`) println(DASHES) println("Welcome to the Amazon CloudWatch example scenario.") println(DASHES) println(DASHES) println("1. List at least five available unique namespaces from Amazon CloudWatch. Select a CloudWatch namespace from the list.") val list: ArrayList<String> = listNameSpaces() for (z in 0..4) { println(" ${z + 1}. ${list[z]}") } var selectedNamespace: String var selectedMetrics = "" var num = inOb.nextLine().toInt() println("You selected $num") if (1 <= num && num <= 5) { selectedNamespace = list[num - 1] } else { println("You did not select a valid option.") exitProcess(1) } println("You selected $selectedNamespace") println(DASHES) println(DASHES) println("2. List available metrics within the selected namespace and select one from the list.") val metList = listMets(selectedNamespace) for (z in 0..4) { println(" ${ z + 1}. ${metList?.get(z)}") } num = inOb.nextLine().toInt() if (1 <= num && num <= 5) { selectedMetrics = metList!![num - 1] } else { println("You did not select a valid option.") System.exit(1) } println("You selected $selectedMetrics") val myDimension = getSpecificMet(selectedNamespace) if (myDimension == null) { println("Error - Dimension is null") exitProcess(1) } println(DASHES) println(DASHES) println("3. Get statistics for the selected metric over the last day.") val metricOption: String val statTypes = ArrayList<String>() statTypes.add("SampleCount") statTypes.add("Average") statTypes.add("Sum") statTypes.add("Minimum") statTypes.add("Maximum") for (t in 0..4) { println(" ${t + 1}. ${statTypes[t]}") } println("Select a metric statistic by entering a number from the preceding list:") num = inOb.nextLine().toInt() if (1 <= num && num <= 5) { metricOption = statTypes[num - 1] } else { println("You did not select a valid option.") exitProcess(1) } println("You selected $metricOption") getAndDisplayMetricStatistics(selectedNamespace, selectedMetrics, metricOption, myDate, myDimension) println(DASHES) println(DASHES) println("4. Get CloudWatch estimated billing for the last week.") getMetricStatistics(costDateWeek) println(DASHES) println(DASHES) println("5. Create a new CloudWatch dashboard with metrics.") createDashboardWithMetrics(dashboardName, dashboardJson) println(DASHES) println(DASHES) println("6. List dashboards using a paginator.") listDashboards() println(DASHES) println(DASHES) println("7. Create a new custom metric by adding data to it.") createNewCustomMetric(dataPoint) println(DASHES) println(DASHES) println("8. Add an additional metric to the dashboard.") addMetricToDashboard(dashboardAdd, dashboardName) println(DASHES) println(DASHES) println("9. Create an alarm for the custom metric.") val alarmName: String = createAlarm(settings) println(DASHES) println(DASHES) println("10. Describe 10 current alarms.") describeAlarms() println(DASHES) println(DASHES) println("11. Get current data for the new custom metric.") getCustomMetricData(settings) println(DASHES) println(DASHES) println("12. Push data into the custom metric to trigger the alarm.") addMetricDataForAlarm(settings) println(DASHES) println(DASHES) println("13. Check the alarm state using the action DescribeAlarmsForMetric.") checkForMetricAlarm(settings) println(DASHES) println(DASHES) println("14. Get alarm history for the new alarm.") getAlarmHistory(settings, myDate) println(DASHES) println(DASHES) println("15. Add an anomaly detector for the custom metric.") addAnomalyDetector(settings) println(DASHES) println(DASHES) println("16. Describe current anomaly detectors.") describeAnomalyDetectors(settings) println(DASHES) println(DASHES) println("17. Get a metric image for the custom metric.") getAndOpenMetricImage(metricImage) println(DASHES) println(DASHES) println("18. Clean up the Amazon CloudWatch resources.") deleteDashboard(dashboardName) deleteAlarm(alarmName) deleteAnomalyDetector(settings) println(DASHES) println(DASHES) println("The Amazon CloudWatch example scenario is complete.") println(DASHES) } suspend fun deleteAnomalyDetector(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 request = DeleteAnomalyDetectorRequest { singleMetricAnomalyDetector = singleMetricAnomalyDetectorVal } CloudWatchClient { region = "us-east-1" }.use { cwClient -> cwClient.deleteAnomalyDetector(request) println("Successfully deleted the Anomaly Detector.") } } suspend fun deleteAlarm(alarmNameVal: String) { val request = DeleteAlarmsRequest { alarmNames = listOf(alarmNameVal) } CloudWatchClient { region = "us-east-1" }.use { cwClient -> cwClient.deleteAlarms(request) println("Successfully deleted alarm $alarmNameVal") } } suspend fun deleteDashboard(dashboardName: String) { val dashboardsRequest = DeleteDashboardsRequest { dashboardNames = listOf(dashboardName) } CloudWatchClient { region = "us-east-1" }.use { cwClient -> cwClient.deleteDashboards(dashboardsRequest) println("$dashboardName was successfully deleted.") } } suspend fun getAndOpenMetricImage(fileName: String) { println("Getting Image data for custom metric.") val myJSON = """{ "title": "Example Metric Graph", "view": "timeSeries", "stacked ": false, "period": 10, "width": 1400, "height": 600, "metrics": [ [ "AWS/Billing", "EstimatedCharges", "Currency", "USD" ] ] }""" val imageRequest = GetMetricWidgetImageRequest { metricWidget = myJSON } CloudWatchClient { region = "us-east-1" }.use { cwClient -> val response = cwClient.getMetricWidgetImage(imageRequest) val bytes = response.metricWidgetImage if (bytes != null) { File(fileName).writeBytes(bytes) } } println("You have successfully written data to $fileName") } suspend fun describeAnomalyDetectors(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 detectorsRequest = DescribeAnomalyDetectorsRequest { maxResults = 10 metricName = customMetricName namespace = customMetricNamespace } CloudWatchClient { region = "us-east-1" }.use { cwClient -> val response = cwClient.describeAnomalyDetectors(detectorsRequest) response.anomalyDetectors?.forEach { detector -> println("Metric name: ${detector.singleMetricAnomalyDetector?.metricName}") println("State: ${detector.stateValue}") } } } 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.") } } suspend fun getAlarmHistory( fileName: String, date: String, ) { // Read values from the JSON file. val parser = JsonFactory().createParser(File(fileName)) val rootNode = ObjectMapper().readTree<JsonNode>(parser) val alarmNameVal = rootNode.findValue("exampleAlarmName").asText() val start = Instant.parse(date) val endDateVal = Instant.now() val historyRequest = DescribeAlarmHistoryRequest { startDate = aws.smithy.kotlin.runtime.time .Instant(start) endDate = aws.smithy.kotlin.runtime.time .Instant(endDateVal) alarmName = alarmNameVal historyItemType = HistoryItemType.Action } CloudWatchClient { credentialsProvider = EnvironmentCredentialsProvider() region = "us-east-1" }.use { cwClient -> val response = cwClient.describeAlarmHistory(historyRequest) val historyItems = response.alarmHistoryItems if (historyItems != null) { if (historyItems.isEmpty()) { println("No alarm history data found for $alarmNameVal.") } else { for (item in historyItems) { println("History summary ${item.historySummary}") println("Time stamp: ${item.timestamp}") } } } } } suspend fun checkForMetricAlarm(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() var hasAlarm = false var retries = 10 val metricRequest = DescribeAlarmsForMetricRequest { metricName = customMetricName namespace = customMetricNamespace } CloudWatchClient { region = "us-east-1" }.use { cwClient -> while (!hasAlarm && retries > 0) { val response = cwClient.describeAlarmsForMetric(metricRequest) if (response.metricAlarms?.count()!! > 0) { hasAlarm = true } retries-- delay(20000) println(".") } if (!hasAlarm) { println("No Alarm state found for $customMetricName after 10 retries.") } else { println("Alarm state found for $customMetricName.") } } } suspend fun addMetricDataForAlarm(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() // Set an Instant object. val time = ZonedDateTime.now(ZoneOffset.UTC).format(DateTimeFormatter.ISO_INSTANT) val instant = Instant.parse(time) val datum = MetricDatum { metricName = customMetricName unit = StandardUnit.None value = 1001.00 timestamp = aws.smithy.kotlin.runtime.time .Instant(instant) } val datum2 = MetricDatum { metricName = customMetricName unit = StandardUnit.None value = 1002.00 timestamp = aws.smithy.kotlin.runtime.time .Instant(instant) } val metricDataList = ArrayList<MetricDatum>() metricDataList.add(datum) metricDataList.add(datum2) val request = PutMetricDataRequest { namespace = customMetricNamespace metricData = metricDataList } CloudWatchClient { region = "us-east-1" }.use { cwClient -> cwClient.putMetricData(request) println("Added metric values for for metric $customMetricName") } } suspend fun getCustomMetricData(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() // Set the date. val nowDate = Instant.now() val hours: Long = 1 val minutes: Long = 30 val date2 = nowDate.plus(hours, ChronoUnit.HOURS).plus( minutes, ChronoUnit.MINUTES, ) val met = Metric { metricName = customMetricName namespace = customMetricNamespace } val metStat = MetricStat { stat = "Maximum" period = 1 metric = met } val dataQUery = MetricDataQuery { metricStat = metStat id = "foo2" returnData = true } val dq = ArrayList<MetricDataQuery>() dq.add(dataQUery) val getMetReq = GetMetricDataRequest { maxDatapoints = 10 scanBy = ScanBy.TimestampDescending startTime = aws.smithy.kotlin.runtime.time .Instant(nowDate) endTime = aws.smithy.kotlin.runtime.time .Instant(date2) metricDataQueries = dq } CloudWatchClient { region = "us-east-1" }.use { cwClient -> val response = cwClient.getMetricData(getMetReq) response.metricDataResults?.forEach { item -> println("The label is ${item.label}") println("The status code is ${item.statusCode}") } } } suspend fun describeAlarms() { val typeList = ArrayList<AlarmType>() typeList.add(AlarmType.MetricAlarm) val alarmsRequest = DescribeAlarmsRequest { alarmTypes = typeList maxRecords = 10 } CloudWatchClient { region = "us-east-1" }.use { cwClient -> val response = cwClient.describeAlarms(alarmsRequest) response.metricAlarms?.forEach { alarm -> println("Alarm name: ${alarm.alarmName}") println("Alarm description: ${alarm.alarmDescription}") } } } suspend fun createAlarm(fileName: String): String { // Read values from the JSON file. val parser = JsonFactory().createParser(File(fileName)) val rootNode: JsonNode = ObjectMapper().readTree(parser) val customMetricNamespace = rootNode.findValue("customMetricNamespace").asText() val customMetricName = rootNode.findValue("customMetricName").asText() val alarmNameVal = rootNode.findValue("exampleAlarmName").asText() val emailTopic = rootNode.findValue("emailTopic").asText() val accountId = rootNode.findValue("accountId").asText() val region2 = rootNode.findValue("region").asText() // Create a List for alarm actions. val alarmActionObs: MutableList<String> = ArrayList() alarmActionObs.add("arn:aws:sns:$region2:$accountId:$emailTopic") val alarmRequest = PutMetricAlarmRequest { alarmActions = alarmActionObs alarmDescription = "Example metric alarm" alarmName = alarmNameVal comparisonOperator = ComparisonOperator.GreaterThanOrEqualToThreshold threshold = 100.00 metricName = customMetricName namespace = customMetricNamespace evaluationPeriods = 1 period = 10 statistic = Statistic.Maximum datapointsToAlarm = 1 treatMissingData = "ignore" } CloudWatchClient { region = "us-east-1" }.use { cwClient -> cwClient.putMetricAlarm(alarmRequest) println("$alarmNameVal was successfully created!") return alarmNameVal } } suspend fun addMetricToDashboard( fileNameVal: String, dashboardNameVal: String, ) { val dashboardRequest = PutDashboardRequest { dashboardName = dashboardNameVal dashboardBody = readFileAsString(fileNameVal) } CloudWatchClient { region = "us-east-1" }.use { cwClient -> cwClient.putDashboard(dashboardRequest) println("$dashboardNameVal was successfully updated.") } } suspend fun createNewCustomMetric(dataPoint: Double) { val dimension = Dimension { name = "UNIQUE_PAGES" value = "URLS" } // Set an Instant object. val time = ZonedDateTime.now(ZoneOffset.UTC).format(DateTimeFormatter.ISO_INSTANT) val instant = Instant.parse(time) val datum = MetricDatum { metricName = "PAGES_VISITED" unit = StandardUnit.None value = dataPoint timestamp = aws.smithy.kotlin.runtime.time .Instant(instant) dimensions = listOf(dimension) } val request = PutMetricDataRequest { namespace = "SITE/TRAFFIC" metricData = listOf(datum) } CloudWatchClient { region = "us-east-1" }.use { cwClient -> cwClient.putMetricData(request) println("Added metric values for for metric PAGES_VISITED") } } suspend fun listDashboards() { CloudWatchClient { region = "us-east-1" }.use { cwClient -> cwClient .listDashboardsPaginated({}) .transform { it.dashboardEntries?.forEach { obj -> emit(obj) } } .collect { obj -> println("Name is ${obj.dashboardName}") println("Dashboard ARN is ${obj.dashboardArn}") } } } suspend fun createDashboardWithMetrics( dashboardNameVal: String, fileNameVal: String, ) { val dashboardRequest = PutDashboardRequest { dashboardName = dashboardNameVal dashboardBody = readFileAsString(fileNameVal) } CloudWatchClient { region = "us-east-1" }.use { cwClient -> val response = cwClient.putDashboard(dashboardRequest) println("$dashboardNameVal was successfully created.") val messages = response.dashboardValidationMessages if (messages != null) { if (messages.isEmpty()) { println("There are no messages in the new Dashboard") } else { for (message in messages) { println("Message is: ${message.message}") } } } } } fun readFileAsString(file: String): String = String(Files.readAllBytes(Paths.get(file))) suspend fun getMetricStatistics(costDateWeek: String?) { val start = Instant.parse(costDateWeek) val endDate = Instant.now() val dimension = Dimension { name = "Currency" value = "USD" } val dimensionList: MutableList<Dimension> = ArrayList() dimensionList.add(dimension) val statisticsRequest = GetMetricStatisticsRequest { metricName = "EstimatedCharges" namespace = "AWS/Billing" dimensions = dimensionList statistics = listOf(Statistic.Maximum) startTime = aws.smithy.kotlin.runtime.time .Instant(start) endTime = aws.smithy.kotlin.runtime.time .Instant(endDate) period = 86400 } CloudWatchClient { region = "us-east-1" }.use { cwClient -> val response = cwClient.getMetricStatistics(statisticsRequest) val data: List<Datapoint>? = response.datapoints if (data != null) { if (!data.isEmpty()) { for (datapoint in data) { println("Timestamp: ${datapoint.timestamp} Maximum value: ${datapoint.maximum}") } } else { println("The returned data list is empty") } } } } suspend fun getAndDisplayMetricStatistics( nameSpaceVal: String, metVal: String, metricOption: String, date: String, myDimension: Dimension, ) { val start = Instant.parse(date) val endDate = Instant.now() val statisticsRequest = GetMetricStatisticsRequest { endTime = aws.smithy.kotlin.runtime.time .Instant(endDate) startTime = aws.smithy.kotlin.runtime.time .Instant(start) dimensions = listOf(myDimension) metricName = metVal namespace = nameSpaceVal period = 86400 statistics = listOf(Statistic.fromValue(metricOption)) } CloudWatchClient { region = "us-east-1" }.use { cwClient -> val response = cwClient.getMetricStatistics(statisticsRequest) val data = response.datapoints if (data != null) { if (data.isNotEmpty()) { for (datapoint in data) { println("Timestamp: ${datapoint.timestamp} Maximum value: ${datapoint.maximum}") } } else { println("The returned data list is empty") } } } } suspend fun listMets(namespaceVal: String?): ArrayList<String>? { val metList = ArrayList<String>() val request = ListMetricsRequest { namespace = namespaceVal } CloudWatchClient { region = "us-east-1" }.use { cwClient -> val reponse = cwClient.listMetrics(request) reponse.metrics?.forEach { metrics -> val data = metrics.metricName if (!metList.contains(data)) { metList.add(data!!) } } } return metList } suspend fun getSpecificMet(namespaceVal: String?): Dimension? { val request = ListMetricsRequest { namespace = namespaceVal } CloudWatchClient { region = "us-east-1" }.use { cwClient -> val response = cwClient.listMetrics(request) val myList = response.metrics if (myList != null) { return myList[0].dimensions?.get(0) } } return null } suspend fun listNameSpaces(): ArrayList<String> { val nameSpaceList = ArrayList<String>() CloudWatchClient { region = "us-east-1" }.use { cwClient -> val response = cwClient.listMetrics(ListMetricsRequest {}) response.metrics?.forEach { metrics -> val data = metrics.namespace if (!nameSpaceList.contains(data)) { nameSpaceList.add(data!!) } } } return nameSpaceList }

Actions

L'exemple de code suivant montre comment utiliserDeleteAlarms.

SDKpour Kotlin
Note

Il y en a plus à ce sujet GitHub. Trouvez l’exemple complet et découvrez comment le configurer et l’exécuter dans le référentiel d’exemples de code AWS.

suspend fun deleteAlarm(alarmNameVal: String) { val request = DeleteAlarmsRequest { alarmNames = listOf(alarmNameVal) } CloudWatchClient { region = "us-east-1" }.use { cwClient -> cwClient.deleteAlarms(request) println("Successfully deleted alarm $alarmNameVal") } }
  • Pour API plus de détails, voir DeleteAlarmsla APIréférence AWS SDK à Kotlin.

L'exemple de code suivant montre comment utiliserDeleteAnomalyDetector.

SDKpour Kotlin
Note

Il y en a plus à ce sujet GitHub. Trouvez l’exemple complet et découvrez comment le configurer et l’exécuter dans le référentiel d’exemples de code AWS.

suspend fun deleteAnomalyDetector(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 request = DeleteAnomalyDetectorRequest { singleMetricAnomalyDetector = singleMetricAnomalyDetectorVal } CloudWatchClient { region = "us-east-1" }.use { cwClient -> cwClient.deleteAnomalyDetector(request) println("Successfully deleted the Anomaly Detector.") } }

L'exemple de code suivant montre comment utiliserDeleteDashboards.

SDKpour Kotlin
Note

Il y en a plus à ce sujet GitHub. Trouvez l’exemple complet et découvrez comment le configurer et l’exécuter dans le référentiel d’exemples de code AWS.

suspend fun deleteDashboard(dashboardName: String) { val dashboardsRequest = DeleteDashboardsRequest { dashboardNames = listOf(dashboardName) } CloudWatchClient { region = "us-east-1" }.use { cwClient -> cwClient.deleteDashboards(dashboardsRequest) println("$dashboardName was successfully deleted.") } }
  • Pour API plus de détails, voir DeleteDashboardsla APIréférence AWS SDK à Kotlin.

L'exemple de code suivant montre comment utiliserDescribeAlarmHistory.

SDKpour Kotlin
Note

Il y en a plus à ce sujet GitHub. Trouvez l’exemple complet et découvrez comment le configurer et l’exécuter dans le référentiel d’exemples de code AWS.

suspend fun getAlarmHistory( fileName: String, date: String, ) { // Read values from the JSON file. val parser = JsonFactory().createParser(File(fileName)) val rootNode = ObjectMapper().readTree<JsonNode>(parser) val alarmNameVal = rootNode.findValue("exampleAlarmName").asText() val start = Instant.parse(date) val endDateVal = Instant.now() val historyRequest = DescribeAlarmHistoryRequest { startDate = aws.smithy.kotlin.runtime.time .Instant(start) endDate = aws.smithy.kotlin.runtime.time .Instant(endDateVal) alarmName = alarmNameVal historyItemType = HistoryItemType.Action } CloudWatchClient { credentialsProvider = EnvironmentCredentialsProvider() region = "us-east-1" }.use { cwClient -> val response = cwClient.describeAlarmHistory(historyRequest) val historyItems = response.alarmHistoryItems if (historyItems != null) { if (historyItems.isEmpty()) { println("No alarm history data found for $alarmNameVal.") } else { for (item in historyItems) { println("History summary ${item.historySummary}") println("Time stamp: ${item.timestamp}") } } } } }

L'exemple de code suivant montre comment utiliserDescribeAlarms.

SDKpour Kotlin
Note

Il y en a plus à ce sujet GitHub. Trouvez l’exemple complet et découvrez comment le configurer et l’exécuter dans le référentiel d’exemples de code AWS.

suspend fun describeAlarms() { val typeList = ArrayList<AlarmType>() typeList.add(AlarmType.MetricAlarm) val alarmsRequest = DescribeAlarmsRequest { alarmTypes = typeList maxRecords = 10 } CloudWatchClient { region = "us-east-1" }.use { cwClient -> val response = cwClient.describeAlarms(alarmsRequest) response.metricAlarms?.forEach { alarm -> println("Alarm name: ${alarm.alarmName}") println("Alarm description: ${alarm.alarmDescription}") } } }
  • Pour API plus de détails, voir DescribeAlarmsla APIréférence AWS SDK à Kotlin.

L'exemple de code suivant montre comment utiliserDescribeAlarmsForMetric.

SDKpour Kotlin
Note

Il y en a plus à ce sujet GitHub. Trouvez l’exemple complet et découvrez comment le configurer et l’exécuter dans le référentiel d’exemples de code AWS.

suspend fun checkForMetricAlarm(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() var hasAlarm = false var retries = 10 val metricRequest = DescribeAlarmsForMetricRequest { metricName = customMetricName namespace = customMetricNamespace } CloudWatchClient { region = "us-east-1" }.use { cwClient -> while (!hasAlarm && retries > 0) { val response = cwClient.describeAlarmsForMetric(metricRequest) if (response.metricAlarms?.count()!! > 0) { hasAlarm = true } retries-- delay(20000) println(".") } if (!hasAlarm) { println("No Alarm state found for $customMetricName after 10 retries.") } else { println("Alarm state found for $customMetricName.") } } }

L'exemple de code suivant montre comment utiliserDescribeAnomalyDetectors.

SDKpour Kotlin
Note

Il y en a plus à ce sujet GitHub. Trouvez l’exemple complet et découvrez comment le configurer et l’exécuter dans le référentiel d’exemples de code AWS.

suspend fun describeAnomalyDetectors(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 detectorsRequest = DescribeAnomalyDetectorsRequest { maxResults = 10 metricName = customMetricName namespace = customMetricNamespace } CloudWatchClient { region = "us-east-1" }.use { cwClient -> val response = cwClient.describeAnomalyDetectors(detectorsRequest) response.anomalyDetectors?.forEach { detector -> println("Metric name: ${detector.singleMetricAnomalyDetector?.metricName}") println("State: ${detector.stateValue}") } } }

L'exemple de code suivant montre comment utiliserDisableAlarmActions.

SDKpour Kotlin
Note

Il y en a plus à ce sujet GitHub. Trouvez l’exemple complet et découvrez comment le configurer et l’exécuter dans le référentiel d’exemples de code AWS.

suspend fun disableActions(alarmName: String) { val request = DisableAlarmActionsRequest { alarmNames = listOf(alarmName) } CloudWatchClient { region = "us-east-1" }.use { cwClient -> cwClient.disableAlarmActions(request) println("Successfully disabled actions on alarm $alarmName") } }

L'exemple de code suivant montre comment utiliserEnableAlarmActions.

SDKpour Kotlin
Note

Il y en a plus à ce sujet GitHub. Trouvez l’exemple complet et découvrez comment le configurer et l’exécuter dans le référentiel d’exemples de code AWS.

suspend fun enableActions(alarm: String) { val request = EnableAlarmActionsRequest { alarmNames = listOf(alarm) } CloudWatchClient { region = "us-east-1" }.use { cwClient -> cwClient.enableAlarmActions(request) println("Successfully enabled actions on alarm $alarm") } }

L'exemple de code suivant montre comment utiliserGetMetricData.

SDKpour Kotlin
Note

Il y en a plus à ce sujet GitHub. Trouvez l’exemple complet et découvrez comment le configurer et l’exécuter dans le référentiel d’exemples de code AWS.

suspend fun getCustomMetricData(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() // Set the date. val nowDate = Instant.now() val hours: Long = 1 val minutes: Long = 30 val date2 = nowDate.plus(hours, ChronoUnit.HOURS).plus( minutes, ChronoUnit.MINUTES, ) val met = Metric { metricName = customMetricName namespace = customMetricNamespace } val metStat = MetricStat { stat = "Maximum" period = 1 metric = met } val dataQUery = MetricDataQuery { metricStat = metStat id = "foo2" returnData = true } val dq = ArrayList<MetricDataQuery>() dq.add(dataQUery) val getMetReq = GetMetricDataRequest { maxDatapoints = 10 scanBy = ScanBy.TimestampDescending startTime = aws.smithy.kotlin.runtime.time .Instant(nowDate) endTime = aws.smithy.kotlin.runtime.time .Instant(date2) metricDataQueries = dq } CloudWatchClient { region = "us-east-1" }.use { cwClient -> val response = cwClient.getMetricData(getMetReq) response.metricDataResults?.forEach { item -> println("The label is ${item.label}") println("The status code is ${item.statusCode}") } } }
  • Pour API plus de détails, voir GetMetricDatala APIréférence AWS SDK à Kotlin.

L'exemple de code suivant montre comment utiliserGetMetricStatistics.

SDKpour Kotlin
Note

Il y en a plus à ce sujet GitHub. Trouvez l’exemple complet et découvrez comment le configurer et l’exécuter dans le référentiel d’exemples de code AWS.

suspend fun getAndDisplayMetricStatistics( nameSpaceVal: String, metVal: String, metricOption: String, date: String, myDimension: Dimension, ) { val start = Instant.parse(date) val endDate = Instant.now() val statisticsRequest = GetMetricStatisticsRequest { endTime = aws.smithy.kotlin.runtime.time .Instant(endDate) startTime = aws.smithy.kotlin.runtime.time .Instant(start) dimensions = listOf(myDimension) metricName = metVal namespace = nameSpaceVal period = 86400 statistics = listOf(Statistic.fromValue(metricOption)) } CloudWatchClient { region = "us-east-1" }.use { cwClient -> val response = cwClient.getMetricStatistics(statisticsRequest) val data = response.datapoints if (data != null) { if (data.isNotEmpty()) { for (datapoint in data) { println("Timestamp: ${datapoint.timestamp} Maximum value: ${datapoint.maximum}") } } else { println("The returned data list is empty") } } } }

L'exemple de code suivant montre comment utiliserGetMetricWidgetImage.

SDKpour Kotlin
Note

Il y en a plus à ce sujet GitHub. Trouvez l’exemple complet et découvrez comment le configurer et l’exécuter dans le référentiel d’exemples de code AWS.

suspend fun getAndOpenMetricImage(fileName: String) { println("Getting Image data for custom metric.") val myJSON = """{ "title": "Example Metric Graph", "view": "timeSeries", "stacked ": false, "period": 10, "width": 1400, "height": 600, "metrics": [ [ "AWS/Billing", "EstimatedCharges", "Currency", "USD" ] ] }""" val imageRequest = GetMetricWidgetImageRequest { metricWidget = myJSON } CloudWatchClient { region = "us-east-1" }.use { cwClient -> val response = cwClient.getMetricWidgetImage(imageRequest) val bytes = response.metricWidgetImage if (bytes != null) { File(fileName).writeBytes(bytes) } } println("You have successfully written data to $fileName") }

L'exemple de code suivant montre comment utiliserListDashboards.

SDKpour Kotlin
Note

Il y en a plus à ce sujet GitHub. Trouvez l’exemple complet et découvrez comment le configurer et l’exécuter dans le référentiel d’exemples de code AWS.

suspend fun listDashboards() { CloudWatchClient { region = "us-east-1" }.use { cwClient -> cwClient .listDashboardsPaginated({}) .transform { it.dashboardEntries?.forEach { obj -> emit(obj) } } .collect { obj -> println("Name is ${obj.dashboardName}") println("Dashboard ARN is ${obj.dashboardArn}") } } }
  • Pour API plus de détails, voir ListDashboardsla APIréférence AWS SDK à Kotlin.

L'exemple de code suivant montre comment utiliserListMetrics.

SDKpour Kotlin
Note

Il y en a plus à ce sujet GitHub. Trouvez l’exemple complet et découvrez comment le configurer et l’exécuter dans le référentiel d’exemples de code AWS.

suspend fun listMets(namespaceVal: String?): ArrayList<String>? { val metList = ArrayList<String>() val request = ListMetricsRequest { namespace = namespaceVal } CloudWatchClient { region = "us-east-1" }.use { cwClient -> val reponse = cwClient.listMetrics(request) reponse.metrics?.forEach { metrics -> val data = metrics.metricName if (!metList.contains(data)) { metList.add(data!!) } } } return metList }
  • Pour API plus de détails, voir ListMetricsla APIréférence AWS SDK à Kotlin.

L'exemple de code suivant montre comment utiliserPutAnomalyDetector.

SDKpour Kotlin
Note

Il y en a plus à ce sujet GitHub. Trouvez l’exemple complet et découvrez comment le configurer et l’exécuter dans le référentiel d’exemples de code AWS.

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.") } }

L'exemple de code suivant montre comment utiliserPutDashboard.

SDKpour Kotlin
Note

Il y en a plus à ce sujet GitHub. Trouvez l’exemple complet et découvrez comment le configurer et l’exécuter dans le référentiel d’exemples de code AWS.

suspend fun createDashboardWithMetrics( dashboardNameVal: String, fileNameVal: String, ) { val dashboardRequest = PutDashboardRequest { dashboardName = dashboardNameVal dashboardBody = readFileAsString(fileNameVal) } CloudWatchClient { region = "us-east-1" }.use { cwClient -> val response = cwClient.putDashboard(dashboardRequest) println("$dashboardNameVal was successfully created.") val messages = response.dashboardValidationMessages if (messages != null) { if (messages.isEmpty()) { println("There are no messages in the new Dashboard") } else { for (message in messages) { println("Message is: ${message.message}") } } } } }
  • Pour API plus de détails, voir PutDashboardla APIréférence AWS SDK à Kotlin.

L'exemple de code suivant montre comment utiliserPutMetricAlarm.

SDKpour Kotlin
Note

Il y en a plus à ce sujet GitHub. Trouvez l’exemple complet et découvrez comment le configurer et l’exécuter dans le référentiel d’exemples de code AWS.

suspend fun putMetricAlarm( alarmNameVal: String, instanceIdVal: String, ) { val dimensionOb = Dimension { name = "InstanceId" value = instanceIdVal } val request = PutMetricAlarmRequest { alarmName = alarmNameVal comparisonOperator = ComparisonOperator.GreaterThanThreshold evaluationPeriods = 1 metricName = "CPUUtilization" namespace = "AWS/EC2" period = 60 statistic = Statistic.fromValue("Average") threshold = 70.0 actionsEnabled = false alarmDescription = "An Alarm created by the Kotlin SDK when server CPU utilization exceeds 70%" unit = StandardUnit.fromValue("Seconds") dimensions = listOf(dimensionOb) } CloudWatchClient { region = "us-east-1" }.use { cwClient -> cwClient.putMetricAlarm(request) println("Successfully created an alarm with name $alarmNameVal") } }
  • Pour API plus de détails, voir PutMetricAlarmla APIréférence AWS SDK à Kotlin.

L'exemple de code suivant montre comment utiliserPutMetricData.

SDKpour Kotlin
Note

Il y en a plus à ce sujet GitHub. Trouvez l’exemple complet et découvrez comment le configurer et l’exécuter dans le référentiel d’exemples de code AWS.

suspend fun addMetricDataForAlarm(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() // Set an Instant object. val time = ZonedDateTime.now(ZoneOffset.UTC).format(DateTimeFormatter.ISO_INSTANT) val instant = Instant.parse(time) val datum = MetricDatum { metricName = customMetricName unit = StandardUnit.None value = 1001.00 timestamp = aws.smithy.kotlin.runtime.time .Instant(instant) } val datum2 = MetricDatum { metricName = customMetricName unit = StandardUnit.None value = 1002.00 timestamp = aws.smithy.kotlin.runtime.time .Instant(instant) } val metricDataList = ArrayList<MetricDatum>() metricDataList.add(datum) metricDataList.add(datum2) val request = PutMetricDataRequest { namespace = customMetricNamespace metricData = metricDataList } CloudWatchClient { region = "us-east-1" }.use { cwClient -> cwClient.putMetricData(request) println("Added metric values for for metric $customMetricName") } }
  • Pour API plus de détails, voir PutMetricDatala APIréférence AWS SDK à Kotlin.