Skip to content

Commit 97379fc

Browse files
authored
Merge pull request #3144 from daspecster/fix-logging-datetime-conversion
Fix double conversion of datetime for log entries.
2 parents 19e1c55 + 9baa5b3 commit 97379fc

File tree

5 files changed

+25
-8
lines changed

5 files changed

+25
-8
lines changed

logging/google/cloud/logging/_gax.py

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,6 @@
3333
from google.protobuf.json_format import ParseDict
3434
from grpc import StatusCode
3535

36-
from google.cloud._helpers import _datetime_to_rfc3339
3736
from google.cloud._helpers import make_secure_channel
3837
from google.cloud._http import DEFAULT_USER_AGENT
3938
from google.cloud.exceptions import Conflict
@@ -452,8 +451,6 @@ def _log_entry_mapping_to_pb(mapping):
452451
the keys expected in the JSON API.
453452
"""
454453
entry_pb = LogEntry()
455-
if 'timestamp' in mapping:
456-
mapping['timestamp'] = _datetime_to_rfc3339(mapping['timestamp'])
457454
ParseDict(mapping, entry_pb)
458455
return entry_pb
459456

logging/google/cloud/logging/logger.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -476,7 +476,7 @@ def commit(self, client=None):
476476
if http_req is not None:
477477
info['httpRequest'] = http_req
478478
if timestamp is not None:
479-
info['timestamp'] = timestamp
479+
info['timestamp'] = _datetime_to_rfc3339(timestamp)
480480
entries.append(info)
481481

482482
client.logging_api.write_entries(entries, **kwargs)

logging/unit_tests/test__gax.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -369,6 +369,7 @@ def test_write_entries_w_extra_properties(self):
369369
from datetime import datetime
370370
from google.logging.type.log_severity_pb2 import WARNING
371371
from google.cloud.proto.logging.v2.log_entry_pb2 import LogEntry
372+
from google.cloud._helpers import _datetime_to_rfc3339
372373
from google.cloud._helpers import UTC, _pb_timestamp_to_datetime
373374

374375
NOW = datetime.utcnow().replace(tzinfo=UTC)
@@ -412,7 +413,7 @@ def test_write_entries_w_extra_properties(self):
412413
'severity': SEVERITY,
413414
'labels': LABELS,
414415
'insertId': IID,
415-
'timestamp': NOW,
416+
'timestamp': _datetime_to_rfc3339(NOW),
416417
'httpRequest': REQUEST,
417418
'operation': OPERATION,
418419
}

logging/unit_tests/test_logger.py

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -653,6 +653,7 @@ def test_commit_w_bound_client(self):
653653
from google.protobuf.json_format import MessageToJson
654654
from google.protobuf.struct_pb2 import Struct
655655
from google.protobuf.struct_pb2 import Value
656+
from google.cloud._helpers import _datetime_to_rfc3339
656657

657658
TEXT = 'This is the entry text'
658659
STRUCT = {'message': TEXT, 'weather': 'partly cloudy'}
@@ -667,10 +668,13 @@ def test_commit_w_bound_client(self):
667668
'type': 'global',
668669
}
669670
ENTRIES = [
670-
{'textPayload': TEXT, 'insertId': IID1, 'timestamp': TIMESTAMP1},
671-
{'jsonPayload': STRUCT, 'insertId': IID2, 'timestamp': TIMESTAMP2},
671+
{'textPayload': TEXT, 'insertId': IID1,
672+
'timestamp': _datetime_to_rfc3339(TIMESTAMP1)},
673+
{'jsonPayload': STRUCT, 'insertId': IID2,
674+
'timestamp': _datetime_to_rfc3339(TIMESTAMP2)},
672675
{'protoPayload': json.loads(MessageToJson(message)),
673-
'insertId': IID3, 'timestamp': TIMESTAMP3},
676+
'insertId': IID3,
677+
'timestamp': _datetime_to_rfc3339(TIMESTAMP3)},
674678
]
675679
client = _Client(project=self.PROJECT)
676680
api = client.logging_api = _DummyLoggingAPI()

system_tests/logging_.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,13 +12,15 @@
1212
# See the License for the specific language governing permissions and
1313
# limitations under the License.
1414

15+
import datetime
1516
import logging
1617
import unittest
1718

1819
from google.gax.errors import GaxError
1920
from google.gax.grpc import exc_to_code
2021
from grpc import StatusCode
2122

23+
from google.cloud._helpers import UTC
2224
from google.cloud.exceptions import Conflict
2325
from google.cloud.exceptions import NotFound
2426
from google.cloud.exceptions import TooManyRequests
@@ -130,6 +132,19 @@ def test_log_text(self):
130132
self.assertEqual(len(entries), 1)
131133
self.assertEqual(entries[0].payload, TEXT_PAYLOAD)
132134

135+
def test_log_text_with_timestamp(self):
136+
text_payload = 'System test: test_log_text_with_timestamp'
137+
logger = Config.CLIENT.logger(self._logger_name())
138+
now = datetime.datetime.utcnow()
139+
140+
self.to_delete.append(logger)
141+
142+
logger.log_text(text_payload, timestamp=now)
143+
entries = _list_entries(logger)
144+
self.assertEqual(len(entries), 1)
145+
self.assertEqual(entries[0].payload, text_payload)
146+
self.assertEqual(entries[0].timestamp, now.replace(tzinfo=UTC))
147+
133148
def test_log_text_w_metadata(self):
134149
TEXT_PAYLOAD = 'System test: test_log_text'
135150
INSERT_ID = 'INSERTID'

0 commit comments

Comments
 (0)