How to retrieve AWS Cloudwatch metrics using AWSSDK.CloudWatch?

|▌冷眼眸甩不掉的悲伤 提交于 2019-12-13 05:06:00

问题


I'm trying to retrieve data about my load balancers using the AWSSDK.CloudWatch package, but having no luck in actually getting any values out of it. It seems no matter what, the Values property of the MetricData in the response is an empty array.

AmazonCloudWatchClient client = new AmazonCloudWatchClient("MyAccessKeyId", "MySecretAccessKey", Amazon.RegionEndpoint.MyRegion);
GetMetricDataRequest request = new GetMetricDataRequest()
   {
    StartTime = DateTime.UtcNow.AddHours(-12),
    EndTime = DateTime.UtcNow,
    MetricDataQueries = new List<MetricDataQuery>()
    {
        new MetricDataQuery()
        {
            Id = "MyMetric",
            MetricStat = new MetricStat()
            {
                Metric = new Metric()
                {
                    Namespace = "AWS/ELB",
                    MetricName = "HealthyHostCount",
                    Dimensions = new List<Dimension>()
                    {
                        new Dimension()
                        {
                            Name = "LoadBalancerName",
                            Value = "MyLoadBalancerName"
                        }
                    }
                },
                Period = 300,
                Stat = "Sum",
                Unit = "None"
            }                        
        }
    },
    ScanBy = ScanBy.TimestampDescending,
    MaxDatapoints = 1000
};
GetMetricDataResponse response = client.GetMetricData(request);

I'm struggling to find any relevant examples of this. I'd prefer to be able to obtain this value per-load balancer.


回答1:


There are many things that could cause your query to return no data. This is how I would approach debugging this:

  1. Was the response 200 OK? If not, something is wrong with the query itself, missing required parameter, credentials are not valid or policy does not allow GetMetricData calls.
  2. Is the metric name correct? Full metric name must be correct, that includes namespace, metric name and all of the dimensions. CloudWatch will not distinguish between no data case and no metric case, you will just get no data back. This is a potential issue in your request, if your hosts are in a target group you may need to specify the target group dimension.
  3. Is the region endpoint correct? Metrics are separated by region and you have to call the correct region endpoint.
  4. Are the credentials from the correct account?
  5. Is the unit correct? If you are not sure about the unit, don't specify it. This is the second thing that could be an issue with your request, this metric could have the unit Count. Try it without specifying the unit.
  6. Is the time range correct? Was the data being published for the time range you are requesting?


来源:https://stackoverflow.com/questions/51445704/how-to-retrieve-aws-cloudwatch-metrics-using-awssdk-cloudwatch

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!