From 9df53e7204789ff84fc4ed7bd4ed4675588a21d9 Mon Sep 17 00:00:00 2001 From: sjyang18 Date: Thu, 9 Jan 2020 19:39:36 +0000 Subject: [PATCH] NIFI-6977 - Change the reporting behavior of Azure Reporting task to report report the time when metrics are generated Signed-off-by: Pierre Villard This closes #4211. --- .../AbstractAzureLogAnalyticsReportingTask.java | 6 +++++- .../TestAzureLogAnalyticsReportingTask.java | 12 ++++++++---- 2 files changed, 13 insertions(+), 5 deletions(-) diff --git a/nifi-nar-bundles/nifi-azure-bundle/nifi-azure-reporting-task/src/main/java/org/apache/nifi/reporting/azure/loganalytics/AbstractAzureLogAnalyticsReportingTask.java b/nifi-nar-bundles/nifi-azure-bundle/nifi-azure-reporting-task/src/main/java/org/apache/nifi/reporting/azure/loganalytics/AbstractAzureLogAnalyticsReportingTask.java index 245e6bb1de..1017ee54ea 100644 --- a/nifi-nar-bundles/nifi-azure-bundle/nifi-azure-reporting-task/src/main/java/org/apache/nifi/reporting/azure/loganalytics/AbstractAzureLogAnalyticsReportingTask.java +++ b/nifi-nar-bundles/nifi-azure-bundle/nifi-azure-reporting-task/src/main/java/org/apache/nifi/reporting/azure/loganalytics/AbstractAzureLogAnalyticsReportingTask.java @@ -133,11 +133,15 @@ public abstract class AbstractAzureLogAnalyticsReportingTask extends AbstractRep protected void sendToLogAnalytics(final HttpPost request, final String workspaceId, final String linuxPrimaryKey, final String rawJson) throws IllegalArgumentException, RuntimeException, IOException { + final int bodyLength = rawJson.getBytes(UTF8).length; - final String nowRfc1123 = RFC_1123_DATE_TIME.format(ZonedDateTime.now(ZoneOffset.UTC)); + final ZonedDateTime zNow = ZonedDateTime.now(ZoneOffset.UTC); + final String nowRfc1123 = zNow.format(DateTimeFormatter.RFC_1123_DATE_TIME); + final String nowISO8601 = zNow.format(DateTimeFormatter.ISO_DATE_TIME); final String createAuthorization = createAuthorization(workspaceId, linuxPrimaryKey, bodyLength, nowRfc1123); request.addHeader("Authorization", createAuthorization); request.addHeader("x-ms-date", nowRfc1123); + request.addHeader("time-generated-field", nowISO8601); request.setEntity(new StringEntity(rawJson)); try (CloseableHttpClient httpClient = HttpClients.createDefault()) { postRequest(httpClient, request); diff --git a/nifi-nar-bundles/nifi-azure-bundle/nifi-azure-reporting-task/src/test/java/org/apache/nifi/reporting/azure/loganalytics/TestAzureLogAnalyticsReportingTask.java b/nifi-nar-bundles/nifi-azure-bundle/nifi-azure-reporting-task/src/test/java/org/apache/nifi/reporting/azure/loganalytics/TestAzureLogAnalyticsReportingTask.java index b8c4c171a9..1dbefa1a7e 100644 --- a/nifi-nar-bundles/nifi-azure-bundle/nifi-azure-reporting-task/src/test/java/org/apache/nifi/reporting/azure/loganalytics/TestAzureLogAnalyticsReportingTask.java +++ b/nifi-nar-bundles/nifi-azure-bundle/nifi-azure-reporting-task/src/test/java/org/apache/nifi/reporting/azure/loganalytics/TestAzureLogAnalyticsReportingTask.java @@ -213,9 +213,13 @@ public class TestAzureLogAnalyticsReportingTask { testedReportingTask.onTrigger(reportingContextStub); HttpPost postRequest = testedReportingTask.getPostRequest(); - ArgumentCaptor captor = ArgumentCaptor.forClass(String.class); - verify(postRequest, atLeast(1)).addHeader( eq("Authorization"), captor.capture()); - assertTrue(captor.getValue().contains("SharedKey")); + ArgumentCaptor captorAuthorization = ArgumentCaptor.forClass(String.class); + ArgumentCaptor captorXMsDate = ArgumentCaptor.forClass(String.class); + ArgumentCaptor captorTimeGeneratedField = ArgumentCaptor.forClass(String.class); + verify(postRequest, atLeast(1)).addHeader( eq("Authorization"), captorAuthorization.capture()); + verify(postRequest, atLeast(1)).addHeader( eq("x-ms-date"), captorXMsDate.capture()); + verify(postRequest, atLeast(1)).addHeader( eq("time-generated-field"), captorTimeGeneratedField.capture()); + assertTrue(captorAuthorization.getValue().contains("SharedKey")); } @@ -252,4 +256,4 @@ public class TestAzureLogAnalyticsReportingTask { super.postRequest(mockClient, request); } } -} \ No newline at end of file +}