Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add a timestamp parameter to the logger. #2860

Merged
merged 4 commits into from
Dec 15, 2016
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 8 additions & 4 deletions logging/google/cloud/logging/logger.py
Original file line number Diff line number Diff line change
Expand Up @@ -379,7 +379,8 @@ def log_text(self, text, labels=None, insert_id=None, severity=None,
:param timestamp: (optional) timestamp of event being logged.
"""
self.entries.append(
('text', text, labels, insert_id, severity, http_request, timestamp))
('text', text, labels, insert_id, severity, http_request,
timestamp))

def log_struct(self, info, labels=None, insert_id=None, severity=None,
http_request=None, timestamp=None):
Expand All @@ -405,7 +406,8 @@ def log_struct(self, info, labels=None, insert_id=None, severity=None,
:param timestamp: (optional) timestamp of event being logged.
"""
self.entries.append(
('struct', info, labels, insert_id, severity, http_request, timestamp))
('struct', info, labels, insert_id, severity, http_request,
timestamp))

def log_proto(self, message, labels=None, insert_id=None, severity=None,
http_request=None, timestamp=None):
Expand All @@ -431,7 +433,8 @@ def log_proto(self, message, labels=None, insert_id=None, severity=None,
:param timestamp: (optional) timestamp of event being logged.
"""
self.entries.append(
('proto', message, labels, insert_id, severity, http_request, timestamp))
('proto', message, labels, insert_id, severity, http_request,
timestamp))

def commit(self, client=None):
"""Send saved log entries as a single API call.
Expand All @@ -452,7 +455,8 @@ def commit(self, client=None):
kwargs['labels'] = self.logger.labels

entries = []
for entry_type, entry, labels, iid, severity, http_req, timestamp in self.entries:
for entry_type, entry, labels, iid, severity, http_req, timestamp in \
self.entries:

This comment was marked as spam.

This comment was marked as spam.

if entry_type == 'text':
info = {'textPayload': entry}
elif entry_type == 'struct':
Expand Down
126 changes: 105 additions & 21 deletions logging/unit_tests/test_logger.py
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,29 @@ def test_log_text_w_default_labels(self):
self.assertEqual(api._write_entries_called_with,
(ENTRIES, None, None, None))

def test_log_text_w_timestamp(self):
import datetime

TEXT = 'TEXT'
TIMESTAMP = datetime.datetime(2016, 12, 31, 0, 1, 2, 999999)
ENTRIES = [{
'logName': 'projects/%s/logs/%s' % (
self.PROJECT, self.LOGGER_NAME),
'textPayload': TEXT,
'timestamp': '2016-12-31T00:01:02.999999Z',
'resource': {
'type': 'global',
},
}]
client = _Client(self.PROJECT)
api = client.logging_api = _DummyLoggingAPI()
logger = self._make_one(self.LOGGER_NAME, client=client)

logger.log_text(TEXT, timestamp=TIMESTAMP)

self.assertEqual(api._write_entries_called_with,
(ENTRIES, None, None, None))

def test_log_text_w_unicode_explicit_client_labels_severity_httpreq(self):
TEXT = u'TEXT'
DEFAULT_LABELS = {'foo': 'spam'}
Expand Down Expand Up @@ -243,6 +266,28 @@ def test_log_struct_w_explicit_client_labels_severity_httpreq(self):
self.assertEqual(api._write_entries_called_with,
(ENTRIES, None, None, None))

def test_log_struct_w_timestamp(self):
import datetime
STRUCT = {'message': 'MESSAGE', 'weather': 'cloudy'}
TIMESTAMP = datetime.datetime(2016, 12, 31, 0, 1, 2, 999999)
ENTRIES = [{
'logName': 'projects/%s/logs/%s' % (
self.PROJECT, self.LOGGER_NAME),
'jsonPayload': STRUCT,
'timestamp': '2016-12-31T00:01:02.999999Z',
'resource': {
'type': 'global',
},
}]
client = _Client(self.PROJECT)
api = client.logging_api = _DummyLoggingAPI()
logger = self._make_one(self.LOGGER_NAME, client=client)

logger.log_struct(STRUCT, timestamp=TIMESTAMP)

self.assertEqual(api._write_entries_called_with,
(ENTRIES, None, None, None))

def test_log_proto_w_implicit_client(self):
import json
from google.protobuf.json_format import MessageToJson
Expand Down Expand Up @@ -332,6 +377,31 @@ def test_log_proto_w_explicit_client_labels_severity_httpreq(self):
self.assertEqual(api._write_entries_called_with,
(ENTRIES, None, None, None))

def test_log_proto_w_timestamp(self):
import json
import datetime
from google.protobuf.json_format import MessageToJson
from google.protobuf.struct_pb2 import Struct, Value
message = Struct(fields={'foo': Value(bool_value=True)})
TIMESTAMP = datetime.datetime(2016, 12, 31, 0, 1, 2, 999999)
ENTRIES = [{
'logName': 'projects/%s/logs/%s' % (
self.PROJECT, self.LOGGER_NAME),
'protoPayload': json.loads(MessageToJson(message)),
'timestamp': '2016-12-31T00:01:02.999999Z',
'resource': {
'type': 'global',
},
}]
client = _Client(self.PROJECT)
api = client.logging_api = _DummyLoggingAPI()
logger = self._make_one(self.LOGGER_NAME, client=client)

logger.log_proto(message, timestamp=TIMESTAMP)

self.assertEqual(api._write_entries_called_with,
(ENTRIES, None, None, None))

def test_delete_w_bound_client(self):
client = _Client(project=self.PROJECT)
api = client.logging_api = _DummyLoggingAPI()
Expand Down Expand Up @@ -457,6 +527,7 @@ def test_log_text_defaults(self):
[('text', TEXT, None, None, None, None, None)])

def test_log_text_explicit(self):
import datetime
TEXT = 'This is the entry text'
LABELS = {'foo': 'bar', 'baz': 'qux'}
IID = 'IID'
Expand All @@ -469,25 +540,28 @@ def test_log_text_explicit(self):
'requestUrl': URI,
'status': STATUS,
}
TIMESTAMP = '2016-10-12T15:01:23.045123456Z'
TIMESTAMP = datetime.datetime(2016, 12, 31, 0, 1, 2, 999999)
client = _Client(project=self.PROJECT, connection=_make_credentials())
logger = _Logger()
batch = self._make_one(logger, client=client)
batch.log_text(TEXT, labels=LABELS, insert_id=IID, severity=SEVERITY,
http_request=REQUEST, timestamp=TIMESTAMP)
self.assertEqual(batch.entries,
[('text', TEXT, LABELS, IID, SEVERITY, REQUEST, TIMESTAMP)])
self.assertEqual(
batch.entries,
[('text', TEXT, LABELS, IID, SEVERITY, REQUEST, TIMESTAMP)])

def test_log_struct_defaults(self):
STRUCT = {'message': 'Message text', 'weather': 'partly cloudy'}
client = _Client(project=self.PROJECT, connection=_make_credentials())
logger = _Logger()
batch = self._make_one(logger, client=client)
batch.log_struct(STRUCT)
self.assertEqual(batch.entries,
[('struct', STRUCT, None, None, None, None, None)])
self.assertEqual(
batch.entries,
[('struct', STRUCT, None, None, None, None, None)])

def test_log_struct_explicit(self):
import datetime
STRUCT = {'message': 'Message text', 'weather': 'partly cloudy'}
LABELS = {'foo': 'bar', 'baz': 'qux'}
IID = 'IID'
Expand All @@ -500,14 +574,16 @@ def test_log_struct_explicit(self):
'requestUrl': URI,
'status': STATUS,
}
TIMESTAMP = '2016-10-12T15:01:23.045123456Z'
TIMESTAMP = datetime.datetime(2016, 12, 31, 0, 1, 2, 999999)
client = _Client(project=self.PROJECT, connection=_make_credentials())
logger = _Logger()
batch = self._make_one(logger, client=client)
batch.log_struct(STRUCT, labels=LABELS, insert_id=IID,
severity=SEVERITY, http_request=REQUEST, timestamp=TIMESTAMP)
self.assertEqual(batch.entries,
[('struct', STRUCT, LABELS, IID, SEVERITY, REQUEST, TIMESTAMP)])
severity=SEVERITY, http_request=REQUEST,
timestamp=TIMESTAMP)
self.assertEqual(
batch.entries,
[('struct', STRUCT, LABELS, IID, SEVERITY, REQUEST, TIMESTAMP)])

def test_log_proto_defaults(self):
from google.protobuf.struct_pb2 import Struct, Value
Expand All @@ -520,6 +596,7 @@ def test_log_proto_defaults(self):
[('proto', message, None, None, None, None, None)])

def test_log_proto_explicit(self):
import datetime
from google.protobuf.struct_pb2 import Struct, Value
message = Struct(fields={'foo': Value(bool_value=True)})
LABELS = {'foo': 'bar', 'baz': 'qux'}
Expand All @@ -533,25 +610,28 @@ def test_log_proto_explicit(self):
'requestUrl': URI,
'status': STATUS,
}
TIMESTAMP = '2016-10-12T15:01:23.045123456Z'
TIMESTAMP = datetime.datetime(2016, 12, 31, 0, 1, 2, 999999)
client = _Client(project=self.PROJECT, connection=_make_credentials())
logger = _Logger()
batch = self._make_one(logger, client=client)
batch.log_proto(message, labels=LABELS, insert_id=IID,
severity=SEVERITY, http_request=REQUEST, timestamp=TIMESTAMP)
self.assertEqual(batch.entries,
[('proto', message, LABELS, IID, SEVERITY, REQUEST, TIMESTAMP)])
severity=SEVERITY, http_request=REQUEST,
timestamp=TIMESTAMP)
self.assertEqual(
batch.entries,
[('proto', message, LABELS, IID, SEVERITY, REQUEST, TIMESTAMP)])

def test_commit_w_invalid_entry_type(self):
logger = _Logger()
client = _Client(project=self.PROJECT, connection=_make_credentials())
batch = self._make_one(logger, client)
batch.entries.append(('bogus', 'BOGUS', None, None, None, None))
batch.entries.append(('bogus', 'BOGUS', None, None, None, None, None))
with self.assertRaises(ValueError):
batch.commit()

def test_commit_w_bound_client(self):
import json
import datetime
from google.protobuf.json_format import MessageToJson
from google.protobuf.struct_pb2 import Struct, Value
TEXT = 'This is the entry text'
Expand All @@ -560,23 +640,26 @@ def test_commit_w_bound_client(self):
IID1 = 'IID1'
IID2 = 'IID2'
IID3 = 'IID3'
TIMESTAMP1 = datetime.datetime(2016, 12, 31, 0, 0, 1, 999999)
TIMESTAMP2 = datetime.datetime(2016, 12, 31, 0, 0, 2, 999999)
TIMESTAMP3 = datetime.datetime(2016, 12, 31, 0, 0, 3, 999999)
RESOURCE = {
'type': 'global',
}
ENTRIES = [
{'textPayload': TEXT, 'insertId': IID1},
{'jsonPayload': STRUCT, 'insertId': IID2},
{'textPayload': TEXT, 'insertId': IID1, 'timestamp': TIMESTAMP1},
{'jsonPayload': STRUCT, 'insertId': IID2, 'timestamp': TIMESTAMP2},
{'protoPayload': json.loads(MessageToJson(message)),
'insertId': IID3},
'insertId': IID3, 'timestamp': TIMESTAMP3},
]
client = _Client(project=self.PROJECT)
api = client.logging_api = _DummyLoggingAPI()
logger = _Logger()
batch = self._make_one(logger, client=client)

batch.log_text(TEXT, insert_id=IID1)
batch.log_struct(STRUCT, insert_id=IID2)
batch.log_proto(message, insert_id=IID3)
batch.log_text(TEXT, insert_id=IID1, timestamp=TIMESTAMP1)
batch.log_struct(STRUCT, insert_id=IID2, timestamp=TIMESTAMP2)
batch.log_proto(message, insert_id=IID3, timestamp=TIMESTAMP3)
batch.commit()

self.assertEqual(list(batch.entries), [])
Expand Down Expand Up @@ -670,6 +753,7 @@ def test_context_mgr_success(self):
(ENTRIES, logger.full_name, RESOURCE, DEFAULT_LABELS))

def test_context_mgr_failure(self):
import datetime
from google.protobuf.struct_pb2 import Struct, Value
TEXT = 'This is the entry text'
STRUCT = {'message': TEXT, 'weather': 'partly cloudy'}
Expand All @@ -684,7 +768,7 @@ def test_context_mgr_failure(self):
'requestUrl': URI,
'status': STATUS,
}
TIMESTAMP = '2016-10-12T15:01:23.045123456Z'
TIMESTAMP = datetime.datetime(2016, 12, 31, 0, 1, 2, 999999)
message = Struct(fields={'foo': Value(bool_value=True)})
client = _Client(project=self.PROJECT)
api = client.logging_api = _DummyLoggingAPI()
Expand Down