Skip to content

Commit

Permalink
Merge pull request #3144 from daspecster/fix-logging-datetime-conversion
Browse files Browse the repository at this point in the history
Fix double conversion of datetime for log entries.
  • Loading branch information
daspecster authored Mar 16, 2017
2 parents 19e1c55 + 9baa5b3 commit 97379fc
Show file tree
Hide file tree
Showing 5 changed files with 25 additions and 8 deletions.
3 changes: 0 additions & 3 deletions logging/google/cloud/logging/_gax.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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

Expand Down
2 changes: 1 addition & 1 deletion logging/google/cloud/logging/logger.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
3 changes: 2 additions & 1 deletion logging/unit_tests/test__gax.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -412,7 +413,7 @@ def test_write_entries_w_extra_properties(self):
'severity': SEVERITY,
'labels': LABELS,
'insertId': IID,
'timestamp': NOW,
'timestamp': _datetime_to_rfc3339(NOW),
'httpRequest': REQUEST,
'operation': OPERATION,
}
Expand Down
10 changes: 7 additions & 3 deletions logging/unit_tests/test_logger.py
Original file line number Diff line number Diff line change
Expand Up @@ -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'}
Expand All @@ -667,10 +668,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': _datetime_to_rfc3339(TIMESTAMP1)},
{'jsonPayload': STRUCT, 'insertId': IID2,
'timestamp': _datetime_to_rfc3339(TIMESTAMP2)},
{'protoPayload': json.loads(MessageToJson(message)),
'insertId': IID3, 'timestamp': TIMESTAMP3},
'insertId': IID3,
'timestamp': _datetime_to_rfc3339(TIMESTAMP3)},
]
client = _Client(project=self.PROJECT)
api = client.logging_api = _DummyLoggingAPI()
Expand Down
15 changes: 15 additions & 0 deletions system_tests/logging_.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,15 @@
# See the License for the specific language governing permissions and
# limitations under the License.

import datetime
import logging
import unittest

from google.gax.errors import GaxError
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
Expand Down Expand Up @@ -130,6 +132,19 @@ def test_log_text(self):
self.assertEqual(len(entries), 1)
self.assertEqual(entries[0].payload, TEXT_PAYLOAD)

def test_log_text_with_timestamp(self):
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.replace(tzinfo=UTC))

def test_log_text_w_metadata(self):
TEXT_PAYLOAD = 'System test: test_log_text'
INSERT_ID = 'INSERTID'
Expand Down

0 comments on commit 97379fc

Please sign in to comment.