Repository: getmoto/moto

Issue:
GetMetricData incompatible with JAVA SDK v2
I'm experiencing the issue that queries to a moto cloudwatch (v 1.3.16) return no points from the AWS Java SDK v2.
The strange thing is, however, that in the raw-response (seen from the logs of the Java SDK) the data is present.

I tried to debug this a bit more and found out that the Responses have some deviations:

## Moto CloudWatch response:

```xml
<GetMetricDataResponse xmlns="http://monitoring.amazonaws.com/doc/2010-08-01/"><ResponseMetadata><RequestId>C90CEILPMV40F7STZZO49Y1N80L3KS82NGX163E91N925OKWA2TT</RequestId></ResponseMetadata><GetMetricDataResult><MetricDataResults><member><Id>myQuery</Id><Label>USERS Average</Label><StatusCode>Complete</StatusCode><Timestamps><member>2021-01-29 12:38:13.408389</member></Timestamps><Values><member>1.0</member></Values></member></MetricDataResults></GetMetricDataResult></GetMetricDataResponse>
```

**Formatted:**
```xml
<GetMetricDataResponse xmlns="http://monitoring.amazonaws.com/doc/2010-08-01/">
   <ResponseMetadata>
      <RequestId>C90CEILPMV40F7STZZO49Y1N80L3KS82NGX163E91N925OKWA2TT</RequestId>
   </ResponseMetadata>
   <GetMetricDataResult>
      <MetricDataResults>
         <member>
            <Id>myQuery</Id>
            <Label>USERS Average</Label>
            <StatusCode>Complete</StatusCode>
            <Timestamps>
               <member>2021-01-29 12:38:13.408389</member>
            </Timestamps>
            <Values>
               <member>1.0</member>
            </Values>
         </member>
      </MetricDataResults>
   </GetMetricDataResult>
</GetMetricDataResponse>
```

## AWS Response:
```xml
<GetMetricDataResponse xmlns="http://monitoring.amazonaws.com/doc/2010-08-01/">
  <GetMetricDataResult>
    <MetricDataResults>
      <member>
        <Timestamps>
          <member>2021-01-29T16:44:00Z</member>
        </Timestamps>
        <Values>
          <member>1.0</member>
        </Values>
        <Label>USERS</Label>
        <Id>myQuery</Id>
        <StatusCode>Complete</StatusCode>
      </member>
    </MetricDataResults>
    <Messages/>
  </GetMetricDataResult>
  <ResponseMetadata>
    <RequestId>d5ffe654-2796-4391-97f1-6979b9d78f11</RequestId>
  </ResponseMetadata>
</GetMetricDataResponse>
```

The most significant difference seems to be the date format to me (`2021-01-29 12:38:13.408389` vs. `2021-01-29T16:44:00Z`). 

I ran both queries in the same hour. While AWS sends the query in UTC moto seems to use my local timezone `Atlantic/Bermuda.

Apart from that, the labels are different (`USERS` vs. `USERS Average`). 

I could not yet find out what makes the AWS SDK to simply ignore the results.

I would be glad about help!


Discussion:
Here is some Java code to reproduce:

```java
@Test
void test() {
    final Instant now = Instant.now();

    final PutMetricDataRequest putRequest = PutMetricDataRequest.builder().namespace("Exasol")
            .metricData(MetricDatum.builder().metricName("USERS").timestamp(now.minus(Duration.ofMinutes(2)))
                    .value(1.2).build())
            .build();
    cloudWatch.putMetricData(putRequest);

    final MetricDataQuery metricDataQuery = MetricDataQuery.builder().id("myQuery")
            .metricStat(MetricStat.builder().stat("Average").period(60)
                    .metric(Metric.builder().namespace("Exasol").metricName("USERS").build()).build())
            .build();
    final GetMetricDataResponse result = cloudWatch.getMetricData(GetMetricDataRequest.builder()
            .metricDataQueries(metricDataQuery).startTime(now.minus(Duration.ofHours(100))).endTime(now).build());
    final List<MetricDataResult> metricDataResults = result.metricDataResults();
    assertThat(metricDataResults.size(), equalTo(1));
}
```

With the system timezone set to UTC I saw in the logs: `2021-02-01 09:06:31.227896`, with `Atlantic/Bermuda` I saw: `2021-02-01 05:08:33.997027`.

In both cases, the test failed. 

So there seem to be multiple bugs:

* One that the returned timestamp is dependant on the system timezone
* Another that causes the JAVA SDK not to show the results (maybe the date format, or something else)
Thanks for providing the test case, @jakobbraun. There are other instances where the Java SDK will choke on a wrongly formatted datetime, so I wouldn't be surprised if the results will appear when we fix the format.
Marking it as a bug.

Resolving patch:
diff --git a/moto/cloudformation/responses.py b/moto/cloudformation/responses.py
--- a/moto/cloudformation/responses.py
+++ b/moto/cloudformation/responses.py
@@ -1108,7 +1108,8 @@ def update_stack_instances(self):
   <StopStackSetOperationResult/>
   <ResponseMetadata>
     <RequestId>2188554a-07c6-4396-b2c5-example</RequestId>
-  </ResponseMetadata>                                                                                                                                                                                                                                                                                                                                                                                                                                                                                     </StopStackSetOperationResponse>
+  </ResponseMetadata>
+</StopStackSetOperationResponse>
 """
 
 DESCRIBE_STACKSET_OPERATION_RESPONSE_TEMPLATE = (
diff --git a/moto/cloudwatch/responses.py b/moto/cloudwatch/responses.py
--- a/moto/cloudwatch/responses.py
+++ b/moto/cloudwatch/responses.py
@@ -455,11 +455,6 @@ def set_alarm_state(self):
 </PutMetricDataResponse>"""
 
 GET_METRIC_DATA_TEMPLATE = """<GetMetricDataResponse xmlns="http://monitoring.amazonaws.com/doc/2010-08-01/">
-   <ResponseMetadata>
-      <RequestId>
-         {{ request_id }}
-      </RequestId>
-   </ResponseMetadata>
    <GetMetricDataResult>
        <MetricDataResults>
            {% for result in results %}
@@ -481,15 +476,14 @@ def set_alarm_state(self):
             {% endfor %}
        </MetricDataResults>
    </GetMetricDataResult>
-</GetMetricDataResponse>"""
-
-GET_METRIC_STATISTICS_TEMPLATE = """<GetMetricStatisticsResponse xmlns="http://monitoring.amazonaws.com/doc/2010-08-01/">
    <ResponseMetadata>
-      <RequestId>
-         {{ request_id }}
-      </RequestId>
+       <RequestId>
+            {{ request_id }}
+       </RequestId>
    </ResponseMetadata>
+</GetMetricDataResponse>"""
 
+GET_METRIC_STATISTICS_TEMPLATE = """<GetMetricStatisticsResponse xmlns="http://monitoring.amazonaws.com/doc/2010-08-01/">
   <GetMetricStatisticsResult>
       <Label>{{ label }}</Label>
       <Datapoints>
@@ -525,6 +519,11 @@ def set_alarm_state(self):
         {% endfor %}
       </Datapoints>
     </GetMetricStatisticsResult>
+    <ResponseMetadata>
+      <RequestId>
+        {{ request_id }}
+      </RequestId>
+    </ResponseMetadata>
 </GetMetricStatisticsResponse>"""
 
 LIST_METRICS_TEMPLATE = """<ListMetricsResponse xmlns="http://monitoring.amazonaws.com/doc/2010-08-01/">
diff --git a/moto/ses/responses.py b/moto/ses/responses.py
--- a/moto/ses/responses.py
+++ b/moto/ses/responses.py
@@ -341,10 +341,10 @@ def create_receipt_rule(self):
             </item>
         {% endfor %}
       </SendDataPoints>
-      <ResponseMetadata>
-        <RequestId>e0abcdfa-c866-11e0-b6d0-273d09173z49</RequestId>
-      </ResponseMetadata>
   </GetSendStatisticsResult>
+  <ResponseMetadata>
+    <RequestId>e0abcdfa-c866-11e0-b6d0-273d09173z49</RequestId>
+  </ResponseMetadata>
 </GetSendStatisticsResponse>"""
 
 CREATE_CONFIGURATION_SET = """<CreateConfigurationSetResponse xmlns="http://ses.amazonaws.com/doc/2010-12-01/">


Test patch: