From 171f860285e5dbd9b150beacae988acf3dbbe05c Mon Sep 17 00:00:00 2001 From: Bogdan Berce Date: Mon, 16 Feb 2015 22:14:35 -0800 Subject: [PATCH] Fixed issue #12 --- applicationinsights/TelemetryClient.py | 13 ++++++++++++- .../TestTelemetryClient.py | 2 +- 2 files changed, 13 insertions(+), 2 deletions(-) diff --git a/applicationinsights/TelemetryClient.py b/applicationinsights/TelemetryClient.py index 172d5ef..f506b7c 100644 --- a/applicationinsights/TelemetryClient.py +++ b/applicationinsights/TelemetryClient.py @@ -200,7 +200,18 @@ class TelemetryClient(object): data.id = str(uuid.uuid4()) data.name = name data.start_time = start_time or datetime.datetime.utcnow().isoformat() + 'Z' - data.duration = duration or 0 + + local_duration = duration or 0 + duration_parts = [] + for multiplier in [1000, 60, 60, 24]: + duration_parts.append(local_duration % multiplier) + local_duration = int(local_duration / multiplier) + + duration_parts.reverse() + data.duration = ':'.join(['%02d' % a for a in duration_parts[:-1]]) + ('.%03d' % duration_parts[-1]) + if local_duration: + data.duration = '%d.%s' % (local_duration, data.duration) + data.response_code = response_code or '200' data.success = success data.http_method = http_method or 'GET' diff --git a/tests/applicationinsights_tests/TestTelemetryClient.py b/tests/applicationinsights_tests/TestTelemetryClient.py index 033fdc0..ebfa2e9 100644 --- a/tests/applicationinsights_tests/TestTelemetryClient.py +++ b/tests/applicationinsights_tests/TestTelemetryClient.py @@ -132,7 +132,7 @@ class TestTelemetryClient(unittest.TestCase): client.context.device = None client.track_request('test', 'http://tempuri.org', True, 'START_TIME', 13, '42', 'OPTIONS', { 'foo': 'bar' }, { 'x': 42 }) client.flush() - expected = '{"ver": 1, "name": "Microsoft.ApplicationInsights.Request", "time": "TIME_PLACEHOLDER", "sampleRate": 100.0, "iKey": "99999999-9999-9999-9999-999999999999", "tags": {"ai.internal.sdkVersion": "SDK_VERSION_PLACEHOLDER"}, "data": {"baseType": "RequestData", "baseData": {"ver": 2, "id": "ID_PLACEHOLDER", "name": "test", "startTime": "START_TIME", "duration": 13, "responseCode": "42", "success": true, "httpMethod": "OPTIONS", "url": "http://tempuri.org", "properties": {"foo": "bar"}, "measurements": {"x": 42}}}}' + expected = '{"ver": 1, "name": "Microsoft.ApplicationInsights.Request", "time": "TIME_PLACEHOLDER", "sampleRate": 100.0, "iKey": "99999999-9999-9999-9999-999999999999", "tags": {"ai.internal.sdkVersion": "SDK_VERSION_PLACEHOLDER"}, "data": {"baseType": "RequestData", "baseData": {"ver": 2, "id": "ID_PLACEHOLDER", "name": "test", "startTime": "START_TIME", "duration": "00:00:00.013", "responseCode": "42", "success": true, "httpMethod": "OPTIONS", "url": "http://tempuri.org", "properties": {"foo": "bar"}, "measurements": {"x": 42}}}}' sender.data.time = 'TIME_PLACEHOLDER' sender.data.tags['ai.internal.sdkVersion'] = 'SDK_VERSION_PLACEHOLDER' sender.data.data.base_data.id = 'ID_PLACEHOLDER'