Repository: Azure/WALinuxAgent

Issue:
The ProcessGoalState Duration is Too Large
The Duration telemetry value exceeds the size of an Int64, which does not make sense.  This has been seen in at least two different agent versions (2.1.3 and 2.2.21).

Discussion:

Patch:
diff --git a/azurelinuxagent/common/event.py b/azurelinuxagent/common/event.py
index 84a439f5..7fc084d9 100644
--- a/azurelinuxagent/common/event.py
+++ b/azurelinuxagent/common/event.py
@@ -254,7 +254,11 @@ __event_logger__ = EventLogger()
 
 
 def elapsed_milliseconds(utc_start):
-    d = datetime.utcnow() - utc_start
+    now = datetime.utcnow()
+    if now < utc_start:
+        return 0
+
+    d = now - utc_start
     return int(((d.days * 24 * 60 * 60 + d.seconds) * 1000) + \
                     (d.microseconds / 1000.0))
 


Test patch:
diff --git a/tests/common/test_event.py b/tests/common/test_event.py
index 01bcd7b9..4d9afeff 100644
--- a/tests/common/test_event.py
+++ b/tests/common/test_event.py
@@ -17,14 +17,10 @@
 
 from __future__ import print_function
 
-from datetime import datetime
-
-import azurelinuxagent.common.event as event
-import azurelinuxagent.common.logger as logger
+from datetime import datetime, timedelta
 
 from azurelinuxagent.common.event import add_event, \
-                                    mark_event_status, should_emit_event, \
-                                    WALAEventOperation
+    WALAEventOperation, elapsed_milliseconds
 from azurelinuxagent.common.future import ustr
 from azurelinuxagent.common.version import CURRENT_VERSION
 
@@ -217,3 +213,7 @@ class TestEvent(AgentTestCase):
         with open(last_event) as last_fh:
             last_event_text = last_fh.read()
             self.assertTrue('last event' in last_event_text)
+
+    def test_elapsed_milliseconds(self):
+        utc_start = datetime.utcnow() + timedelta(days=1)
+        self.assertEqual(0, elapsed_milliseconds(utc_start))