0
I tried two ways to read a CSV file and pass the values in the JSON value parameter, but it does not return any result, if I pass the direct values, it works.
Code 1:
import boto3
from datetime import datetime
import csv
client = boto3.client('cloudwatch', region_name='us-east-1')
with open('metrics_tcc.csv', mode='r') as csv_file:
csv_reader = csv.DictReader(csv_file)
for row in csv_reader:
response = client.get_metric_data(
MetricDataQueries=[
{
'Id': 'tcc',
'MetricStat': {
'Metric': {
'Namespace': 'metrics.tcc',
'MetricName': 'responseTime',
"Dimensions": [
{
"Name": "isColdStart",
"Value": row['isColdStart']
},
{
"Name": "requestId",
"Value": row['requestId']
},
{
"Name": "interval",
"Value": row['interval']
},
{
"Name": "target",
"Value": row['target']
}
]
},
'Period': 300,
'Stat': 'Maximum',
'Unit': 'Milliseconds'
},
'ReturnData': True
},
],
StartTime=datetime(2018, 10, 8),
EndTime=datetime(2018, 10, 9),
)
print(response['MetricDataResults'])
The exit is this JSON:
[{u'Timestamps': [], u'StatusCode': 'Complete', u'Values': [], u'Id': 'tcc', u'Label': 'responseTime'}]
In Values it was supposed to have come a value, however, it’s like you don’t understand the value passed in JSON, hence returns empty.
Second attempt:
import boto3
import numpy as np
from datetime import datetime
target, interval, isColdStart, requestId =
np.loadtxt('metrics_tcc.csv', delimiter = ',', unpack = True, dtype =
'str')
client = boto3.client('cloudwatch', region_name='us-east-1')
t = target[1]
i = interval[1]
bcs = isColdStart[1]
rq = requestId[1]
response = client.get_metric_data(
MetricDataQueries=[
{
'Id': 'tcc',
'MetricStat': {
'Metric': {
'Namespace': 'metrics.tcc',
'MetricName': 'responseTime',
"Dimensions": [
{
"Name": "isColdStart",
"Value": t
},
{
"Name": "requestId",
"Value": i
},
{
"Name": "interval",
"Value": bcs
},
{
"Name": "target",
"Value": rq
}
]
},
'Period': 300,
'Stat': 'Maximum',
'Unit': 'Milliseconds'
},
'ReturnData': True
},
],
StartTime=datetime(2018, 10, 8),
EndTime=datetime(2018, 10, 9),
)
print(response['MetricDataResults'])
The exit is the same:
[{u'Timestamps': [], u'StatusCode': 'Complete', u'Values': [], u'Id': 'tcc', u'Label': 'responseTime'}]
But if I pass the direct value, it works:
import boto3
from datetime import datetime
client = boto3.client('cloudwatch', region_name='us-east-1')
response = client.get_metric_data(
MetricDataQueries=[
{
'Id': 'tcc',
'MetricStat': {
'Metric': {
'Namespace': 'metrics.tcc',
'MetricName': 'responseTime',
"Dimensions": [
{
"Name": "isColdStart",
"Value": "false"
},
{
"Name": "requestId",
"Value": "0"
},
{
"Name": "interval",
"Value": "660"
},
{
"Name": "target",
"Value": "when-will-i-coldstart-dev-system-under-test-256"
}
]
},
'Period': 300,
'Stat': 'Maximum',
'Unit': 'Milliseconds'
},
'ReturnData': True
},
],
StartTime=datetime(2018, 10, 8),
EndTime=datetime(2018, 10, 9),
)
print(response['MetricDataResults'])
The exit is this JSON:
[{u'Timestamps': [datetime.datetime(2018, 10, 8, 20, 0, tzinfo=tzutc())], u'StatusCode': 'Complete', u'Values': [141.89045000006445], u'Id': 'tcc', u'Label': 'responseTime'}]
Note that Values is now right. Could you help me?