CloudWatch: Posted custom metric data successfully, but can't find the data

0

Using this code: https://github.com/awsdocs/aws-doc-sdk-examples/blob/main/javav2/example_code/cloudwatch/src/main/java/com/example/cloudwatch/PutMetricAlarm.java

I was able to put a custom metric data successfully:

Successfully put data point 123.000000

However, I'm unable to find the data, cloudwatch metric screenshot: https://www.screencast.com/t/RI3pq8KWmww

I've double checked the region

My scala code:

object TestClass extends App {
  PutMetricData.main(123)
}

object PutMetricData {
  def main(args: Integer): Unit = {
    val dataPoint = args.toDouble
    val region = Region.US_WEST_2
    val cw = CloudWatchClient.builder.region(region).credentialsProvider(ProfileCredentialsProvider.create).build
    putMetData(cw, dataPoint)
    cw.close()
  }

  def putMetData(cw: CloudWatchClient, dataPoint: Double): Unit = {
    try {
      val time = ZonedDateTime.now(ZoneOffset.UTC).format(DateTimeFormatter.ISO_INSTANT)
      val instant = Instant.parse(time)
      val datum = MetricDatum.builder.metricName("PAGES_VISITED1").unit(StandardUnit.NONE).value(dataPoint).timestamp(instant).build
      val request = PutMetricDataRequest.builder.namespace("SITE/TRAFFIC1").metricData(datum).build
      cw.putMetricData(request)
    } catch {
      case e: CloudWatchException =>
      System.err.println(e.awsErrorDetails.errorMessage)
      System.exit(1)
    }
    System.out.printf("Successfully put data point %f", dataPoint)
  }
}

Context: I'm new to AWS, I didn't create any metricNames or nameSpace in AWS, I'm assuming I can change the MetricName & namespace in the code and it'll create it in AWS. Do I have to specify an existing namespace or metricName?

kn
질문됨 2년 전440회 조회
1개 답변
0

I'm not a Scala expert but the code looks good. No need to specify an existing namespace or metric name.

Usually problems like this are caused by timestamp being incorrect, e.g. being in seconds instead of milliseconds - those would get silently dropped and not generate an error. Your code seems to follow the Java SDK exactly though. 🤔

AWS
답변함 2년 전

로그인하지 않았습니다. 로그인해야 답변을 게시할 수 있습니다.

좋은 답변은 질문에 명확하게 답하고 건설적인 피드백을 제공하며 질문자의 전문적인 성장을 장려합니다.

질문 답변하기에 대한 가이드라인