From 9ed79fcd3c8687c280d4f8e03cce8d54fbd834f1 Mon Sep 17 00:00:00 2001 From: daspecster Date: Wed, 15 Mar 2017 13:23:23 -0400 Subject: [PATCH 1/3] Fix double conversion of datetime for log entries. --- logging/google/cloud/logging/_gax.py | 3 --- logging/unit_tests/test__gax.py | 2 +- system_tests/logging_.py | 15 +++++++++++++++ 3 files changed, 16 insertions(+), 4 deletions(-) diff --git a/logging/google/cloud/logging/_gax.py b/logging/google/cloud/logging/_gax.py index 887dd80ccb33..6a4ede985e42 100644 --- a/logging/google/cloud/logging/_gax.py +++ b/logging/google/cloud/logging/_gax.py @@ -33,7 +33,6 @@ from google.protobuf.json_format import ParseDict from grpc import StatusCode -from google.cloud._helpers import _datetime_to_rfc3339 from google.cloud._helpers import make_secure_channel from google.cloud._http import DEFAULT_USER_AGENT from google.cloud.exceptions import Conflict @@ -452,8 +451,6 @@ def _log_entry_mapping_to_pb(mapping): the keys expected in the JSON API. """ entry_pb = LogEntry() - if 'timestamp' in mapping: - mapping['timestamp'] = _datetime_to_rfc3339(mapping['timestamp']) ParseDict(mapping, entry_pb) return entry_pb diff --git a/logging/unit_tests/test__gax.py b/logging/unit_tests/test__gax.py index 35d71750aa93..8054756cfc65 100644 --- a/logging/unit_tests/test__gax.py +++ b/logging/unit_tests/test__gax.py @@ -412,7 +412,7 @@ def test_write_entries_w_extra_properties(self): 'severity': SEVERITY, 'labels': LABELS, 'insertId': IID, - 'timestamp': NOW, + 'timestamp': NOW.strftime("%Y-%m-%dT%H:%M:%S.%fZ"), 'httpRequest': REQUEST, 'operation': OPERATION, } diff --git a/system_tests/logging_.py b/system_tests/logging_.py index 5236129ee39f..21e59c7a0ee4 100644 --- a/system_tests/logging_.py +++ b/system_tests/logging_.py @@ -130,6 +130,21 @@ def test_log_text(self): self.assertEqual(len(entries), 1) self.assertEqual(entries[0].payload, TEXT_PAYLOAD) + def test_log_text_with_timestamp(self): + import datetime + + text_payload = 'System test: test_log_text_with_timestamp' + logger = Config.CLIENT.logger(self._logger_name()) + now = datetime.datetime.utcnow() + + self.to_delete.append(logger) + + logger.log_text(text_payload, timestamp=now) + entries = _list_entries(logger) + self.assertEqual(len(entries), 1) + self.assertEqual(entries[0].payload, text_payload) + # self.assertEqual(entries[0].timestamp, now) + def test_log_text_w_metadata(self): TEXT_PAYLOAD = 'System test: test_log_text' INSERT_ID = 'INSERTID' From 7d9bce00e94cd31df812519d0af09d4d8dc9fe88 Mon Sep 17 00:00:00 2001 From: daspecster Date: Wed, 15 Mar 2017 14:23:13 -0400 Subject: [PATCH 2/3] Convert batch timestamp to string. --- logging/google/cloud/logging/logger.py | 2 +- logging/unit_tests/test_logger.py | 9 ++++++--- 2 files changed, 7 insertions(+), 4 deletions(-) diff --git a/logging/google/cloud/logging/logger.py b/logging/google/cloud/logging/logger.py index 4ea35881765c..b81c27389ef4 100644 --- a/logging/google/cloud/logging/logger.py +++ b/logging/google/cloud/logging/logger.py @@ -476,7 +476,7 @@ def commit(self, client=None): if http_req is not None: info['httpRequest'] = http_req if timestamp is not None: - info['timestamp'] = timestamp + info['timestamp'] = _datetime_to_rfc3339(timestamp) entries.append(info) client.logging_api.write_entries(entries, **kwargs) diff --git a/logging/unit_tests/test_logger.py b/logging/unit_tests/test_logger.py index 31bad0402d86..1264fe798f64 100644 --- a/logging/unit_tests/test_logger.py +++ b/logging/unit_tests/test_logger.py @@ -667,10 +667,13 @@ def test_commit_w_bound_client(self): 'type': 'global', } ENTRIES = [ - {'textPayload': TEXT, 'insertId': IID1, 'timestamp': TIMESTAMP1}, - {'jsonPayload': STRUCT, 'insertId': IID2, 'timestamp': TIMESTAMP2}, + {'textPayload': TEXT, 'insertId': IID1, + 'timestamp': TIMESTAMP1.strftime("%Y-%m-%dT%H:%M:%S.%fZ")}, + {'jsonPayload': STRUCT, 'insertId': IID2, + 'timestamp': TIMESTAMP2.strftime("%Y-%m-%dT%H:%M:%S.%fZ")}, {'protoPayload': json.loads(MessageToJson(message)), - 'insertId': IID3, 'timestamp': TIMESTAMP3}, + 'insertId': IID3, + 'timestamp': TIMESTAMP3.strftime("%Y-%m-%dT%H:%M:%S.%fZ")}, ] client = _Client(project=self.PROJECT) api = client.logging_api = _DummyLoggingAPI() From 9baa5b3f896e90ccaf4abfc686cf4e92a2b1c193 Mon Sep 17 00:00:00 2001 From: daspecster Date: Thu, 16 Mar 2017 00:36:59 -0400 Subject: [PATCH 3/3] Move imports in logging system tests, use _datetime_to_rfc3339. --- logging/unit_tests/test__gax.py | 3 ++- logging/unit_tests/test_logger.py | 7 ++++--- system_tests/logging_.py | 6 +++--- 3 files changed, 9 insertions(+), 7 deletions(-) diff --git a/logging/unit_tests/test__gax.py b/logging/unit_tests/test__gax.py index 8054756cfc65..d1f73c699827 100644 --- a/logging/unit_tests/test__gax.py +++ b/logging/unit_tests/test__gax.py @@ -369,6 +369,7 @@ def test_write_entries_w_extra_properties(self): from datetime import datetime from google.logging.type.log_severity_pb2 import WARNING from google.cloud.proto.logging.v2.log_entry_pb2 import LogEntry + from google.cloud._helpers import _datetime_to_rfc3339 from google.cloud._helpers import UTC, _pb_timestamp_to_datetime NOW = datetime.utcnow().replace(tzinfo=UTC) @@ -412,7 +413,7 @@ def test_write_entries_w_extra_properties(self): 'severity': SEVERITY, 'labels': LABELS, 'insertId': IID, - 'timestamp': NOW.strftime("%Y-%m-%dT%H:%M:%S.%fZ"), + 'timestamp': _datetime_to_rfc3339(NOW), 'httpRequest': REQUEST, 'operation': OPERATION, } diff --git a/logging/unit_tests/test_logger.py b/logging/unit_tests/test_logger.py index 1264fe798f64..541c9ec501c4 100644 --- a/logging/unit_tests/test_logger.py +++ b/logging/unit_tests/test_logger.py @@ -653,6 +653,7 @@ def test_commit_w_bound_client(self): from google.protobuf.json_format import MessageToJson from google.protobuf.struct_pb2 import Struct from google.protobuf.struct_pb2 import Value + from google.cloud._helpers import _datetime_to_rfc3339 TEXT = 'This is the entry text' STRUCT = {'message': TEXT, 'weather': 'partly cloudy'} @@ -668,12 +669,12 @@ def test_commit_w_bound_client(self): } ENTRIES = [ {'textPayload': TEXT, 'insertId': IID1, - 'timestamp': TIMESTAMP1.strftime("%Y-%m-%dT%H:%M:%S.%fZ")}, + 'timestamp': _datetime_to_rfc3339(TIMESTAMP1)}, {'jsonPayload': STRUCT, 'insertId': IID2, - 'timestamp': TIMESTAMP2.strftime("%Y-%m-%dT%H:%M:%S.%fZ")}, + 'timestamp': _datetime_to_rfc3339(TIMESTAMP2)}, {'protoPayload': json.loads(MessageToJson(message)), 'insertId': IID3, - 'timestamp': TIMESTAMP3.strftime("%Y-%m-%dT%H:%M:%S.%fZ")}, + 'timestamp': _datetime_to_rfc3339(TIMESTAMP3)}, ] client = _Client(project=self.PROJECT) api = client.logging_api = _DummyLoggingAPI() diff --git a/system_tests/logging_.py b/system_tests/logging_.py index 21e59c7a0ee4..52229d98ffb5 100644 --- a/system_tests/logging_.py +++ b/system_tests/logging_.py @@ -12,6 +12,7 @@ # See the License for the specific language governing permissions and # limitations under the License. +import datetime import logging import unittest @@ -19,6 +20,7 @@ from google.gax.grpc import exc_to_code from grpc import StatusCode +from google.cloud._helpers import UTC from google.cloud.exceptions import Conflict from google.cloud.exceptions import NotFound from google.cloud.exceptions import TooManyRequests @@ -131,8 +133,6 @@ def test_log_text(self): self.assertEqual(entries[0].payload, TEXT_PAYLOAD) def test_log_text_with_timestamp(self): - import datetime - text_payload = 'System test: test_log_text_with_timestamp' logger = Config.CLIENT.logger(self._logger_name()) now = datetime.datetime.utcnow() @@ -143,7 +143,7 @@ def test_log_text_with_timestamp(self): entries = _list_entries(logger) self.assertEqual(len(entries), 1) self.assertEqual(entries[0].payload, text_payload) - # self.assertEqual(entries[0].timestamp, now) + self.assertEqual(entries[0].timestamp, now.replace(tzinfo=UTC)) def test_log_text_w_metadata(self): TEXT_PAYLOAD = 'System test: test_log_text'