我正在尝试执行一个简单的CloudWatch查询以获取一些指标数据。我想获取实际指标。使用AWS CLI,我得到了预期的行为。但是,使用客户端SDK(尝试使用Python的Boto和Java的SDK)时,相同(似乎)的查询不会返回我的指标。
我的查询条件在query.json
中定义:
[
{
"Id": "test",
"MetricStat": {
"Metric": {
"Namespace": "MyNamespace",
"MetricName": "MyMetric",
"Dimensions": [
{
"Name": "SOURCE",
"Value": "TEST"
}
]
},
"Period": 3600,
"Stat": "SampleCount",
"Unit":"Seconds"
},
"ReturnData": true
}
]
CLI这有效。返回我的指标值和时间戳记
aws cloudwatch get-metric-data --max-items 10 --start-time "2020-05-01T00:00:00Z" --end-time "2020-06-01T00:00:00Z" --metric-data-queries file://./query.json
{
"MetricDataResults": [
{
"Id": "test",
"Label": "MyMetric",
"Timestamps": [
"2020-05-15T13:00:00Z",
...
],
"Values": [
1.0,
...
],
"StatusCode": "Complete"
}
],
"Messages": []
}
Boto不会返回指标,即使来自同一文件也是如此
In [77]: cw.get_metric_data(MetricDataQueries=json.load(open("query.json")), StartTime=datetime.datetime(2020,5,1), EndTime=datetime.datetime(2020,6,1), MaxDatapoints=10)
Out [77]: {'MetricDataResults': [{'Id': 'test',
'Label': 'MyMetric',
'Timestamps': [], <--Why isn't this populated?
'Values': [],
'StatusCode': 'PartialData'}],
Java大致相同的输入条件
MetricDataQuery query = new MetricDataQuery().withReturnData(true);
query.setMetricStat(new MetricStat()
.withStat("SampleCount")
.withPeriod(36000)
.withMetric(
new Metric()
.withMetricName("MyMetric")
.withNamespace("MyNamespace)
.withDimensions(new Dimension()
.withName("SOURCE")
.withValue("TEST"))));
cw.getMetricData(new GetMetricDataRequest()
.withStartTime(Date.from(Instant.now().minus(100, ChronoUnit.DAYS)))
.withEndTime(Date.from(Instant.now()))
.withScanBy(ScanBy.TimestampDescending)
.withMaxDatapoints(10)
.withMetricDataQueries(query))
// Result ==> {Id: TestQuery,Label: MyMetric,Timestamps: [],Values: [],StatusCode: PartialData,Messages: []}
我不知道cli为什么会返回指标值,但是我尝试过的两个CDK似乎都没有。有任何提示吗?
所以问题出在MaxDataPoints
。
根据docs:
在分页之前,请求应返回的最大数据点数。如果忽略此设置,则使用默认值100,800。
但是,实际上,将该值设置得太低会导致返回no值。我可以推测该值用于限制查询中检查的指标点或周期的数量,而不返回。
In [77]: cw.get_metric_data(MetricDataQueries=json.load(open("query.json")), StartTime=datetime.datetime(2020,1,5), EndTime=datetime.datetime(2020,1,6), MaxDatapoints=10)
...
{'MetricDataResults': [{'Id': 'test',
'Label': 'Errors',
'Timestamps': [],
'Values': [],
'StatusCode': 'PartialData'}],...
vs
In [78]: cw.get_metric_data(MetricDataQueries=json.load(open("query.json")), StartTime=datetime.datetime(2020,1,5), EndTime=datetime.datetime(2020,1,6), MaxDatapoints=1000)
...
{'MetricDataResults': [{'Id': 'test',
'Label': 'Errors',
'Timestamps': [datetime.datetime(2020, 5, 27, 15, 0, tzinfo=tzutc()),
datetime.datetime(2020, 5, 27, 14, 0, tzinfo=tzutc()),
datetime.datetime(2020, 5, 27, 13, 0, tzinfo=tzutc()),
...
也许有人可以解释这种行为?