AWS AmazonCloudWatch: Refactor CloudWatch GetMetricData example (params, query, time range)
Summary
The GetMetricData examples were updated: C# sample field names changed (StartTimeUtc/EndTimeUtc -> StartTime/EndTime), and the Kotlin sample now hardcodes metricName/namespace (DiskReadBytes, AWS/EC2), changes stat from Maximum to Minimum with period 60, uses fixed ISO8601 start time and maxDatapoints 100, and drops the JSON-file based configuration reading.
Security assessment
Changes are limited to sample correctness and API parameter naming/simplification of a metrics-reading example (hardcoded namespace/metric, adjusted stat/period/time window). Nothing addresses a vulnerability, credential handling, authorization, or encryption.
Evidence
+ metricName = "DiskReadBytes"
Diff
diff --git a/AmazonCloudWatch/latest/monitoring/example_cloudwatch_GetMetricData_section.md b/AmazonCloudWatch/latest/monitoring/example_cloudwatch_GetMetricData_section.md index 70eecc9b5..c06157bf1 100644 --- a//AmazonCloudWatch/latest/monitoring/example_cloudwatch_GetMetricData_section.md +++ b//AmazonCloudWatch/latest/monitoring/example_cloudwatch_GetMetricData_section.md @@ -11,3 +11 @@ The following code examples show how to use `GetMetricData`. -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 examples: - - * [Learn the basics](./example_cloudwatch_GetStartedMetricsDashboardsAlarms_section.html) +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: @@ -55,2 +53,2 @@ There's more on GitHub. Find the complete example and learn how to set up and ru - StartTimeUtc = startTimeUtc, - EndTimeUtc = endDateUtc.Value, + StartTime = startTimeUtc, + EndTime = endDateUtc.Value, @@ -311,17 +309,7 @@ There's more on GitHub. Find the complete example and learn how to set up and ru - 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, - ) - + suspend fun getMetData() { + val start = + aws.smithy.kotlin.runtime.time.Instant + .fromIso8601("2019-10-23T10:12:35Z") + val endDate = + aws.smithy.kotlin.runtime.time.Instant + .now() @@ -330,2 +318,2 @@ There's more on GitHub. Find the complete example and learn how to set up and ru - metricName = customMetricName - namespace = customMetricNamespace + metricName = "DiskReadBytes" + namespace = "AWS/EC2" @@ -336,2 +324,2 @@ There's more on GitHub. Find the complete example and learn how to set up and ru - stat = "Maximum" - period = 1 + stat = "Minimum" + period = 60 @@ -348 +336 @@ There's more on GitHub. Find the complete example and learn how to set up and ru - val dq = ArrayList<MetricDataQuery>() + val dq = mutableListOf<MetricDataQuery>() @@ -350 +338,2 @@ There's more on GitHub. Find the complete example and learn how to set up and ru - val getMetReq = + + val request = @@ -352,8 +341,3 @@ There's more on GitHub. Find the complete example and learn how to set up and ru - maxDatapoints = 10 - scanBy = ScanBy.TimestampDescending - startTime = - aws.smithy.kotlin.runtime.time - .Instant(nowDate) - endTime = - aws.smithy.kotlin.runtime.time - .Instant(date2) + maxDatapoints = 100 + startTime = start + endTime = endDate @@ -364 +348 @@ There's more on GitHub. Find the complete example and learn how to set up and ru - val response = cwClient.getMetricData(getMetReq) + val response = cwClient.getMetricData(request)