CloudWatch exemplos de uso SDK para Kotlin - AWS SDKExemplos de código

Há mais AWS SDK exemplos disponíveis no GitHub repositório AWS Doc SDK Examples.

As traduções são geradas por tradução automática. Em caso de conflito entre o conteúdo da tradução e da versão original em inglês, a versão em inglês prevalecerá.

CloudWatch exemplos de uso SDK para Kotlin

Os exemplos de código a seguir mostram como realizar ações e implementar cenários comuns usando o AWS SDK for Kotlin with. CloudWatch

As noções básicas são exemplos de código que mostram como realizar as operações essenciais em um serviço.

Ações são trechos de código de programas maiores e devem ser executadas em contexto. Embora as ações mostrem como chamar funções de serviço individuais, você pode ver as ações no contexto em seus cenários relacionados.

Cada exemplo inclui um link para o código-fonte completo, onde você pode encontrar instruções sobre como configurar e executar o código no contexto.

Conceitos básicos

Os exemplos de código a seguir mostram como começar a usar CloudWatch.

SDKpara Kotlin
nota

Tem mais sobre GitHub. Encontre o exemplo completo e saiba como configurar e executar no Repositório de exemplos de código da 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}") } } }
  • Para API obter detalhes, consulte a ListMetricsreferência AWS SDKdo Kotlin API.

Conceitos básicos

O exemplo de código a seguir mostra como:

  • Listar CloudWatch namespaces e métricas.

  • Obter estatísticas para uma métrica e para faturamento estimado.

  • Criar e atualizar um painel.

  • Criar e adicionar dados a uma métrica.

  • Criar e acionar um alarme e, em seguida, visualizar o histórico de alarmes.

  • Criar um detector de anomalias.

  • Obter uma imagem de métrica e, em seguida, limpar os recursos.

SDKpara Kotlin
nota

Tem mais sobre GitHub. Encontre o exemplo completo e saiba como configurar e executar no Repositório de exemplos de código da 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 }

Ações

O código de exemplo a seguir mostra como usar DeleteAlarms.

SDKpara Kotlin
nota

Tem mais sobre GitHub. Encontre o exemplo completo e saiba como configurar e executar no Repositório de exemplos de código da 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") } }
  • Para API obter detalhes, consulte a DeleteAlarmsreferência AWS SDKdo Kotlin API.

O código de exemplo a seguir mostra como usar DeleteAnomalyDetector.

SDKpara Kotlin
nota

Tem mais sobre GitHub. Encontre o exemplo completo e saiba como configurar e executar no Repositório de exemplos de código da 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.") } }

O código de exemplo a seguir mostra como usar DeleteDashboards.

SDKpara Kotlin
nota

Tem mais sobre GitHub. Encontre o exemplo completo e saiba como configurar e executar no Repositório de exemplos de código da 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.") } }
  • Para API obter detalhes, consulte a DeleteDashboardsreferência AWS SDKdo Kotlin API.

O código de exemplo a seguir mostra como usar DescribeAlarmHistory.

SDKpara Kotlin
nota

Tem mais sobre GitHub. Encontre o exemplo completo e saiba como configurar e executar no Repositório de exemplos de código da 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}") } } } } }

O código de exemplo a seguir mostra como usar DescribeAlarms.

SDKpara Kotlin
nota

Tem mais sobre GitHub. Encontre o exemplo completo e saiba como configurar e executar no Repositório de exemplos de código da 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}") } } }
  • Para API obter detalhes, consulte a DescribeAlarmsreferência AWS SDKdo Kotlin API.

O código de exemplo a seguir mostra como usar DescribeAlarmsForMetric.

SDKpara Kotlin
nota

Tem mais sobre GitHub. Encontre o exemplo completo e saiba como configurar e executar no Repositório de exemplos de código da 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.") } } }

O código de exemplo a seguir mostra como usar DescribeAnomalyDetectors.

SDKpara Kotlin
nota

Tem mais sobre GitHub. Encontre o exemplo completo e saiba como configurar e executar no Repositório de exemplos de código da 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}") } } }

O código de exemplo a seguir mostra como usar DisableAlarmActions.

SDKpara Kotlin
nota

Tem mais sobre GitHub. Encontre o exemplo completo e saiba como configurar e executar no Repositório de exemplos de código da 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") } }

O código de exemplo a seguir mostra como usar EnableAlarmActions.

SDKpara Kotlin
nota

Tem mais sobre GitHub. Encontre o exemplo completo e saiba como configurar e executar no Repositório de exemplos de código da 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") } }

O código de exemplo a seguir mostra como usar GetMetricData.

SDKpara Kotlin
nota

Tem mais sobre GitHub. Encontre o exemplo completo e saiba como configurar e executar no Repositório de exemplos de código da 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}") } } }
  • Para API obter detalhes, consulte a GetMetricDatareferência AWS SDKdo Kotlin API.

O código de exemplo a seguir mostra como usar GetMetricStatistics.

SDKpara Kotlin
nota

Tem mais sobre GitHub. Encontre o exemplo completo e saiba como configurar e executar no Repositório de exemplos de código da 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") } } } }

O código de exemplo a seguir mostra como usar GetMetricWidgetImage.

SDKpara Kotlin
nota

Tem mais sobre GitHub. Encontre o exemplo completo e saiba como configurar e executar no Repositório de exemplos de código da 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") }

O código de exemplo a seguir mostra como usar ListDashboards.

SDKpara Kotlin
nota

Tem mais sobre GitHub. Encontre o exemplo completo e saiba como configurar e executar no Repositório de exemplos de código da 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}") } } }
  • Para API obter detalhes, consulte a ListDashboardsreferência AWS SDKdo Kotlin API.

O código de exemplo a seguir mostra como usar ListMetrics.

SDKpara Kotlin
nota

Tem mais sobre GitHub. Encontre o exemplo completo e saiba como configurar e executar no Repositório de exemplos de código da 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 }
  • Para API obter detalhes, consulte a ListMetricsreferência AWS SDKdo Kotlin API.

O código de exemplo a seguir mostra como usar PutAnomalyDetector.

SDKpara Kotlin
nota

Tem mais sobre GitHub. Encontre o exemplo completo e saiba como configurar e executar no Repositório de exemplos de código da 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.") } }

O código de exemplo a seguir mostra como usar PutDashboard.

SDKpara Kotlin
nota

Tem mais sobre GitHub. Encontre o exemplo completo e saiba como configurar e executar no Repositório de exemplos de código da 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}") } } } } }
  • Para API obter detalhes, consulte a PutDashboardreferência AWS SDKdo Kotlin API.

O código de exemplo a seguir mostra como usar PutMetricAlarm.

SDKpara Kotlin
nota

Tem mais sobre GitHub. Encontre o exemplo completo e saiba como configurar e executar no Repositório de exemplos de código da 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") } }
  • Para API obter detalhes, consulte a PutMetricAlarmreferência AWS SDKdo Kotlin API.

O código de exemplo a seguir mostra como usar PutMetricData.

SDKpara Kotlin
nota

Tem mais sobre GitHub. Encontre o exemplo completo e saiba como configurar e executar no Repositório de exemplos de código da 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") } }
  • Para API obter detalhes, consulte a PutMetricDatareferência AWS SDKdo Kotlin API.