From 405e29d54b52c2b1f54e371ea265b8b9ed9fb4c8 Mon Sep 17 00:00:00 2001 From: Salmaan Rashid Date: Fri, 31 Aug 2018 17:50:15 -0700 Subject: [PATCH 1/4] add trace to logentry --- logging/google/cloud/logging/entries.py | 19 +++++++-- logging/google/cloud/logging/logger.py | 57 +++++++++++++++++++------ logging/tests/unit/test_entries.py | 17 +++++++- logging/tests/unit/test_logger.py | 34 +++++++++------ 4 files changed, 93 insertions(+), 34 deletions(-) diff --git a/logging/google/cloud/logging/entries.py b/logging/google/cloud/logging/entries.py index 995aa6e410b3..68572e3c539b 100644 --- a/logging/google/cloud/logging/entries.py +++ b/logging/google/cloud/logging/entries.py @@ -74,11 +74,16 @@ class _BaseEntry(object): :param http_request: (optional) info about HTTP request associated with the entry. + :type trace: str + :param trace: (optional) traceid to apply to the entry. + + :type resource: :class:`~google.cloud.logging.resource.Resource` :param resource: (Optional) Monitored resource of the entry """ def __init__(self, payload, logger, insert_id=None, timestamp=None, - labels=None, severity=None, http_request=None, resource=None): + labels=None, severity=None, http_request=None, resource=None, + trace=None): self.payload = payload self.logger = logger self.insert_id = insert_id @@ -87,6 +92,7 @@ def __init__(self, payload, logger, insert_id=None, timestamp=None, self.severity = severity self.http_request = http_request self.resource = resource + self.trace = trace @classmethod def from_api_repr(cls, resource, client, loggers=None): @@ -123,6 +129,7 @@ def from_api_repr(cls, resource, client, loggers=None): labels = resource.get('labels') severity = resource.get('severity') http_request = resource.get('httpRequest') + trace = resource.get('trace') monitored_resource_dict = resource.get('resource') monitored_resource = None @@ -131,7 +138,7 @@ def from_api_repr(cls, resource, client, loggers=None): return cls(payload, logger, insert_id=insert_id, timestamp=timestamp, labels=labels, severity=severity, http_request=http_request, - resource=monitored_resource) + resource=monitored_resource, trace=trace) class TextEntry(_BaseEntry): @@ -183,17 +190,21 @@ class ProtobufEntry(_BaseEntry): :param http_request: (optional) info about HTTP request associated with the entry + :type trace: str + :param trace: (optional) traceid to apply to the entry. + :type resource: :class:`~google.cloud.logging.resource.Resource` :param resource: (Optional) Monitored resource of the entry """ _PAYLOAD_KEY = 'protoPayload' def __init__(self, payload, logger, insert_id=None, timestamp=None, - labels=None, severity=None, http_request=None, resource=None): + labels=None, severity=None, http_request=None, resource=None, + trace=None): super(ProtobufEntry, self).__init__( payload, logger, insert_id=insert_id, timestamp=timestamp, labels=labels, severity=severity, http_request=http_request, - resource=resource) + resource=resource, trace=trace) if isinstance(self.payload, any_pb2.Any): self.payload_pb = self.payload self.payload = None diff --git a/logging/google/cloud/logging/logger.py b/logging/google/cloud/logging/logger.py index b6db8828bffe..9c5156f712eb 100644 --- a/logging/google/cloud/logging/logger.py +++ b/logging/google/cloud/logging/logger.py @@ -96,7 +96,7 @@ def batch(self, client=None): def _make_entry_resource(self, text=None, info=None, message=None, labels=None, insert_id=None, severity=None, http_request=None, timestamp=None, - resource=_GLOBAL_RESOURCE): + resource=_GLOBAL_RESOURCE, trace=None): """Return a log entry resource of the appropriate type. Helper for :meth:`log_text`, :meth:`log_struct`, and :meth:`log_proto`. @@ -131,6 +131,9 @@ def _make_entry_resource(self, text=None, info=None, message=None, :type resource: :class:`~google.cloud.logging.resource.Resource` :param resource: (Optional) Monitored resource of the entry + :type trace: str + :param trace: (optional) traceid to apply to the entry. + :rtype: dict :returns: The JSON resource created. """ @@ -172,11 +175,14 @@ def _make_entry_resource(self, text=None, info=None, message=None, if timestamp is not None: entry['timestamp'] = _datetime_to_rfc3339(timestamp) + if trace is not None: + entry['trace'] = trace + return entry def log_text(self, text, client=None, labels=None, insert_id=None, severity=None, http_request=None, timestamp=None, - resource=_GLOBAL_RESOURCE): + resource=_GLOBAL_RESOURCE, trace=None): """API call: log a text message via a POST request See @@ -209,16 +215,20 @@ def log_text(self, text, client=None, labels=None, insert_id=None, :type timestamp: :class:`datetime.datetime` :param timestamp: (optional) timestamp of event being logged. + + :type trace: str + :param trace: (optional) traceid to apply to the entry. """ client = self._require_client(client) entry_resource = self._make_entry_resource( text=text, labels=labels, insert_id=insert_id, severity=severity, - http_request=http_request, timestamp=timestamp, resource=resource) + http_request=http_request, timestamp=timestamp, resource=resource, + trace=trace) client.logging_api.write_entries([entry_resource]) def log_struct(self, info, client=None, labels=None, insert_id=None, severity=None, http_request=None, timestamp=None, - resource=_GLOBAL_RESOURCE): + resource=_GLOBAL_RESOURCE, trace=None): """API call: log a structured message via a POST request See @@ -251,16 +261,20 @@ def log_struct(self, info, client=None, labels=None, insert_id=None, :type timestamp: :class:`datetime.datetime` :param timestamp: (optional) timestamp of event being logged. + + :type trace: str + :param trace: (optional) traceid to apply to the entry. """ client = self._require_client(client) entry_resource = self._make_entry_resource( info=info, labels=labels, insert_id=insert_id, severity=severity, - http_request=http_request, timestamp=timestamp, resource=resource) + http_request=http_request, timestamp=timestamp, resource=resource, + trace=trace) client.logging_api.write_entries([entry_resource]) def log_proto(self, message, client=None, labels=None, insert_id=None, severity=None, http_request=None, timestamp=None, - resource=_GLOBAL_RESOURCE): + resource=_GLOBAL_RESOURCE, trace=None): """API call: log a protobuf message via a POST request See @@ -293,12 +307,15 @@ def log_proto(self, message, client=None, labels=None, insert_id=None, :type timestamp: :class:`datetime.datetime` :param timestamp: (optional) timestamp of event being logged. + + :type trace: str + :param trace: (optional) traceid to apply to the entry. """ client = self._require_client(client) entry_resource = self._make_entry_resource( message=message, labels=labels, insert_id=insert_id, severity=severity, http_request=http_request, timestamp=timestamp, - resource=resource) + resource=resource, trace=trace) client.logging_api.write_entries([entry_resource]) def delete(self, client=None): @@ -392,7 +409,8 @@ def __exit__(self, exc_type, exc_val, exc_tb): self.commit() def log_text(self, text, labels=None, insert_id=None, severity=None, - http_request=None, timestamp=None, resource=_GLOBAL_RESOURCE): + http_request=None, timestamp=None, resource=_GLOBAL_RESOURCE, + trace=None): """Add a text entry to be logged during :meth:`commit`. :type text: str @@ -420,14 +438,17 @@ def log_text(self, text, labels=None, insert_id=None, severity=None, resource of the batch is used for this entry. If both this resource and the Batch resource are None, the API will return an error. + + :type trace: str + :param trace: (optional) traceid to apply to the entry. """ self.entries.append( ('text', text, labels, insert_id, severity, http_request, - timestamp, resource)) + timestamp, resource, trace)) def log_struct(self, info, labels=None, insert_id=None, severity=None, http_request=None, timestamp=None, - resource=_GLOBAL_RESOURCE): + resource=_GLOBAL_RESOURCE, trace=None): """Add a struct entry to be logged during :meth:`commit`. :type info: dict @@ -455,14 +476,17 @@ def log_struct(self, info, labels=None, insert_id=None, severity=None, resource of the batch is used for this entry. If both this resource and the Batch resource are None, the API will return an error. + + :type trace: str + :param trace: (optional) traceid to apply to the entry. """ self.entries.append( ('struct', info, labels, insert_id, severity, http_request, - timestamp, resource)) + timestamp, resource, trace)) def log_proto(self, message, labels=None, insert_id=None, severity=None, http_request=None, timestamp=None, - resource=_GLOBAL_RESOURCE): + resource=_GLOBAL_RESOURCE, trace=None): """Add a protobuf entry to be logged during :meth:`commit`. :type message: protobuf message @@ -490,10 +514,13 @@ def log_proto(self, message, labels=None, insert_id=None, severity=None, resource of the batch is used for this entry. If both this resource and the Batch resource are None, the API will return an error. + + :type trace: str + :param trace: (optional) traceid to apply to the entry. """ self.entries.append( ('proto', message, labels, insert_id, severity, http_request, - timestamp, resource)) + timestamp, resource, trace)) def commit(self, client=None): """Send saved log entries as a single API call. @@ -517,7 +544,7 @@ def commit(self, client=None): entries = [] for (entry_type, entry, labels, iid, severity, http_req, - timestamp, resource) in self.entries: + timestamp, resource, trace) in self.entries: if entry_type == 'text': info = {'textPayload': entry} elif entry_type == 'struct': @@ -544,6 +571,8 @@ def commit(self, client=None): info['httpRequest'] = http_req if timestamp is not None: info['timestamp'] = _datetime_to_rfc3339(timestamp) + if trace is not None: + info['trace'] = trace entries.append(info) client.logging_api.write_entries(entries, **kwargs) diff --git a/logging/tests/unit/test_entries.py b/logging/tests/unit/test_entries.py index 71b3ce561299..039b7e5ac66c 100644 --- a/logging/tests/unit/test_entries.py +++ b/logging/tests/unit/test_entries.py @@ -68,6 +68,7 @@ def test_ctor_defaults(self): self.assertIsNone(entry.severity) self.assertIsNone(entry.http_request) self.assertIsNone(entry.resource) + self.assertIsNone(entry.trace) def test_ctor_explicit(self): import datetime @@ -87,6 +88,7 @@ def test_ctor_explicit(self): 'status': STATUS, } resource = Resource(type='global', labels={}) + TRACE = '12345678-1234-5678-1234-567812345678' logger = _Logger(self.LOGGER_NAME, self.PROJECT) entry = self._make_one(PAYLOAD, logger, @@ -95,7 +97,8 @@ def test_ctor_explicit(self): labels=LABELS, severity=SEVERITY, http_request=REQUEST, - resource=resource) + resource=resource, + trace=TRACE) self.assertEqual(entry.payload, PAYLOAD) self.assertIs(entry.logger, logger) self.assertEqual(entry.insert_id, IID) @@ -106,6 +109,7 @@ def test_ctor_explicit(self): self.assertEqual(entry.http_request['requestUrl'], URI) self.assertEqual(entry.http_request['status'], STATUS) self.assertEqual(entry.resource, resource) + self.assertEqual(entry.trace, TRACE) def test_from_api_repr_missing_data_no_loggers(self): client = _Client(self.PROJECT) @@ -122,6 +126,7 @@ def test_from_api_repr_missing_data_no_loggers(self): self.assertIsNone(entry.timestamp) self.assertIsNone(entry.severity) self.assertIsNone(entry.http_request) + self.assertIsNone(entry.trace) logger = entry.logger self.assertIsInstance(logger, _Logger) self.assertIs(logger.client, client) @@ -154,6 +159,7 @@ def test_from_api_repr_w_loggers_no_logger_match(self): } ) STATUS = '500' + TRACE = '12345678-1234-5678-1234-567812345678' API_REPR = { 'dummyPayload': PAYLOAD, 'logName': LOG_NAME, @@ -167,6 +173,7 @@ def test_from_api_repr_w_loggers_no_logger_match(self): 'status': STATUS, }, 'resource': RESOURCE._to_dict(), + 'trace': TRACE } loggers = {} entry = klass.from_api_repr(API_REPR, client, loggers=loggers) @@ -184,6 +191,7 @@ def test_from_api_repr_w_loggers_no_logger_match(self): self.assertEqual(logger.name, self.LOGGER_NAME) self.assertEqual(loggers, {LOG_NAME: logger}) self.assertEqual(entry.resource, RESOURCE) + self.assertEqual(entry.trace, TRACE) def test_from_api_repr_w_loggers_w_logger_match(self): from datetime import datetime @@ -196,13 +204,15 @@ def test_from_api_repr_w_loggers_w_logger_match(self): TIMESTAMP = _datetime_to_rfc3339_w_nanos(NOW) LOG_NAME = 'projects/%s/logs/%s' % (self.PROJECT, self.LOGGER_NAME) LABELS = {'foo': 'bar', 'baz': 'qux'} + TRACE = '12345678-1234-5678-1234-567812345678' API_REPR = { 'dummyPayload': PAYLOAD, 'logName': LOG_NAME, 'insertId': IID, 'timestamp': TIMESTAMP, 'labels': LABELS, - } + 'trace': TRACE + } LOGGER = object() loggers = {LOG_NAME: LOGGER} klass = self._get_target_class() @@ -211,6 +221,7 @@ def test_from_api_repr_w_loggers_w_logger_match(self): self.assertEqual(entry.insert_id, IID) self.assertEqual(entry.timestamp, NOW) self.assertEqual(entry.labels, LABELS) + self.assertEqual(entry.trace, TRACE) self.assertIs(entry.logger, LOGGER) @@ -239,6 +250,7 @@ def test_constructor_basic(self): self.assertIsNone(pb_entry.labels) self.assertIsNone(pb_entry.severity) self.assertIsNone(pb_entry.http_request) + self.assertIsNone(pb_entry.trace) def test_constructor_with_any(self): from google.protobuf.any_pb2 import Any @@ -253,6 +265,7 @@ def test_constructor_with_any(self): self.assertIsNone(pb_entry.labels) self.assertIsNone(pb_entry.severity) self.assertIsNone(pb_entry.http_request) + self.assertIsNone(pb_entry.trace) def test_parse_message(self): import json diff --git a/logging/tests/unit/test_logger.py b/logging/tests/unit/test_logger.py index 30b07466c110..bc579e453684 100644 --- a/logging/tests/unit/test_logger.py +++ b/logging/tests/unit/test_logger.py @@ -164,6 +164,7 @@ def test_log_text_w_unicode_explicit_client_labels_severity_httpreq(self): METHOD = 'POST' URI = 'https://api.example.com/endpoint' STATUS = '500' + TRACE = '12345678-1234-5678-1234-567812345678' REQUEST = { 'requestMethod': METHOD, 'requestUrl': URI, @@ -181,6 +182,7 @@ def test_log_text_w_unicode_explicit_client_labels_severity_httpreq(self): 'insertId': IID, 'severity': SEVERITY, 'httpRequest': REQUEST, + 'trace': TRACE }] client1 = _Client(self.PROJECT) client2 = _Client(self.PROJECT) @@ -189,7 +191,7 @@ def test_log_text_w_unicode_explicit_client_labels_severity_httpreq(self): labels=DEFAULT_LABELS) logger.log_text(TEXT, client=client2, labels=LABELS, - insert_id=IID, severity=SEVERITY, http_request=REQUEST) + insert_id=IID, severity=SEVERITY, http_request=REQUEST, trace=TRACE) self.assertEqual(api._write_entries_called_with, (ENTRIES, None, None, None)) @@ -550,7 +552,7 @@ def test_log_text_defaults(self): batch.log_text(TEXT) self.assertEqual(batch.entries, [('text', TEXT, None, None, None, None, None, - _GLOBAL_RESOURCE)]) + _GLOBAL_RESOURCE, None)]) def test_log_text_explicit(self): import datetime @@ -576,17 +578,18 @@ def test_log_text_explicit(self): 'version_id': 'test' } ) + TRACE = '12345678-1234-5678-1234-567812345678' 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, - resource=RESOURCE) + resource=RESOURCE, trace=TRACE) self.assertEqual( batch.entries, [('text', TEXT, LABELS, IID, SEVERITY, REQUEST, TIMESTAMP, - RESOURCE)]) + RESOURCE, TRACE)]) def test_log_struct_defaults(self): from google.cloud.logging.logger import _GLOBAL_RESOURCE @@ -598,7 +601,7 @@ def test_log_struct_defaults(self): self.assertEqual( batch.entries, [('struct', STRUCT, None, None, None, None, None, - _GLOBAL_RESOURCE)]) + _GLOBAL_RESOURCE, None)]) def test_log_struct_explicit(self): import datetime @@ -611,6 +614,7 @@ def test_log_struct_explicit(self): METHOD = 'POST' URI = 'https://api.example.com/endpoint' STATUS = '500' + TRACE = '12345678-1234-5678-1234-567812345678' REQUEST = { 'requestMethod': METHOD, 'requestUrl': URI, @@ -630,11 +634,11 @@ def test_log_struct_explicit(self): batch = self._make_one(logger, client=client) batch.log_struct(STRUCT, labels=LABELS, insert_id=IID, severity=SEVERITY, http_request=REQUEST, - timestamp=TIMESTAMP, resource=RESOURCE) + timestamp=TIMESTAMP, resource=RESOURCE, trace=TRACE) self.assertEqual( batch.entries, [('struct', STRUCT, LABELS, IID, SEVERITY, REQUEST, TIMESTAMP, - RESOURCE)]) + RESOURCE, TRACE)]) def test_log_proto_defaults(self): from google.cloud.logging.logger import _GLOBAL_RESOURCE @@ -648,7 +652,7 @@ def test_log_proto_defaults(self): batch.log_proto(message) self.assertEqual(batch.entries, [('proto', message, None, None, None, None, None, - _GLOBAL_RESOURCE)]) + _GLOBAL_RESOURCE, None)]) def test_log_proto_explicit(self): import datetime @@ -663,6 +667,7 @@ def test_log_proto_explicit(self): METHOD = 'POST' URI = 'https://api.example.com/endpoint' STATUS = '500' + TRACE = '12345678-1234-5678-1234-567812345678' REQUEST = { 'requestMethod': METHOD, 'requestUrl': URI, @@ -681,18 +686,18 @@ def test_log_proto_explicit(self): batch = self._make_one(logger, client=client) batch.log_proto(message, labels=LABELS, insert_id=IID, severity=SEVERITY, http_request=REQUEST, - timestamp=TIMESTAMP, resource=RESOURCE) + timestamp=TIMESTAMP, resource=RESOURCE, trace=TRACE) self.assertEqual( batch.entries, [('proto', message, LABELS, IID, SEVERITY, REQUEST, TIMESTAMP, - RESOURCE)]) + RESOURCE, TRACE)]) 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, None, - None)) + None, None)) with self.assertRaises(ValueError): batch.commit() @@ -876,6 +881,7 @@ def test_context_mgr_failure(self): METHOD = 'POST' URI = 'https://api.example.com/endpoint' STATUS = '500' + TRACE = '12345678-1234-5678-1234-567812345678' REQUEST = { 'requestMethod': METHOD, 'requestUrl': URI, @@ -888,11 +894,11 @@ def test_context_mgr_failure(self): logger = _Logger() UNSENT = [ ('text', TEXT, None, IID, None, None, TIMESTAMP, - _GLOBAL_RESOURCE), + _GLOBAL_RESOURCE, None), ('struct', STRUCT, None, None, SEVERITY, None, None, - _GLOBAL_RESOURCE), + _GLOBAL_RESOURCE, None), ('proto', message, LABELS, None, None, REQUEST, None, - _GLOBAL_RESOURCE), + _GLOBAL_RESOURCE, None), ] batch = self._make_one(logger, client=client) From 782c1b8562847b9a67a7f4d70874c0f4d35ac559 Mon Sep 17 00:00:00 2001 From: Salmaan Rashid Date: Fri, 31 Aug 2018 18:09:05 -0700 Subject: [PATCH 2/4] fix ci errors --- logging/google/cloud/logging/entries.py | 6 +++--- logging/google/cloud/logging/logger.py | 14 +++++++------- logging/tests/unit/test_entries.py | 4 ++-- logging/tests/unit/test_logger.py | 4 ++-- 4 files changed, 14 insertions(+), 14 deletions(-) diff --git a/logging/google/cloud/logging/entries.py b/logging/google/cloud/logging/entries.py index 68572e3c539b..00f56a4b4381 100644 --- a/logging/google/cloud/logging/entries.py +++ b/logging/google/cloud/logging/entries.py @@ -76,7 +76,7 @@ class _BaseEntry(object): :type trace: str :param trace: (optional) traceid to apply to the entry. - + :type resource: :class:`~google.cloud.logging.resource.Resource` :param resource: (Optional) Monitored resource of the entry @@ -129,7 +129,7 @@ def from_api_repr(cls, resource, client, loggers=None): labels = resource.get('labels') severity = resource.get('severity') http_request = resource.get('httpRequest') - trace = resource.get('trace') + trace = resource.get('trace') monitored_resource_dict = resource.get('resource') monitored_resource = None @@ -191,7 +191,7 @@ class ProtobufEntry(_BaseEntry): the entry :type trace: str - :param trace: (optional) traceid to apply to the entry. + :param trace: (optional) traceid to apply to the entry. :type resource: :class:`~google.cloud.logging.resource.Resource` :param resource: (Optional) Monitored resource of the entry diff --git a/logging/google/cloud/logging/logger.py b/logging/google/cloud/logging/logger.py index 9c5156f712eb..af4f213198e3 100644 --- a/logging/google/cloud/logging/logger.py +++ b/logging/google/cloud/logging/logger.py @@ -217,7 +217,7 @@ def log_text(self, text, client=None, labels=None, insert_id=None, :param timestamp: (optional) timestamp of event being logged. :type trace: str - :param trace: (optional) traceid to apply to the entry. + :param trace: (optional) traceid to apply to the entry. """ client = self._require_client(client) entry_resource = self._make_entry_resource( @@ -263,7 +263,7 @@ def log_struct(self, info, client=None, labels=None, insert_id=None, :param timestamp: (optional) timestamp of event being logged. :type trace: str - :param trace: (optional) traceid to apply to the entry. + :param trace: (optional) traceid to apply to the entry. """ client = self._require_client(client) entry_resource = self._make_entry_resource( @@ -309,7 +309,7 @@ def log_proto(self, message, client=None, labels=None, insert_id=None, :param timestamp: (optional) timestamp of event being logged. :type trace: str - :param trace: (optional) traceid to apply to the entry. + :param trace: (optional) traceid to apply to the entry. """ client = self._require_client(client) entry_resource = self._make_entry_resource( @@ -440,7 +440,7 @@ def log_text(self, text, labels=None, insert_id=None, severity=None, the API will return an error. :type trace: str - :param trace: (optional) traceid to apply to the entry. + :param trace: (optional) traceid to apply to the entry. """ self.entries.append( ('text', text, labels, insert_id, severity, http_request, @@ -478,7 +478,7 @@ def log_struct(self, info, labels=None, insert_id=None, severity=None, the API will return an error. :type trace: str - :param trace: (optional) traceid to apply to the entry. + :param trace: (optional) traceid to apply to the entry. """ self.entries.append( ('struct', info, labels, insert_id, severity, http_request, @@ -516,7 +516,7 @@ def log_proto(self, message, labels=None, insert_id=None, severity=None, the API will return an error. :type trace: str - :param trace: (optional) traceid to apply to the entry. + :param trace: (optional) traceid to apply to the entry. """ self.entries.append( ('proto', message, labels, insert_id, severity, http_request, @@ -572,7 +572,7 @@ def commit(self, client=None): if timestamp is not None: info['timestamp'] = _datetime_to_rfc3339(timestamp) if trace is not None: - info['trace'] = trace + info['trace'] = trace entries.append(info) client.logging_api.write_entries(entries, **kwargs) diff --git a/logging/tests/unit/test_entries.py b/logging/tests/unit/test_entries.py index 039b7e5ac66c..b15974e77a2a 100644 --- a/logging/tests/unit/test_entries.py +++ b/logging/tests/unit/test_entries.py @@ -159,7 +159,7 @@ def test_from_api_repr_w_loggers_no_logger_match(self): } ) STATUS = '500' - TRACE = '12345678-1234-5678-1234-567812345678' + TRACE = '12345678-1234-5678-1234-567812345678' API_REPR = { 'dummyPayload': PAYLOAD, 'logName': LOG_NAME, @@ -212,7 +212,7 @@ def test_from_api_repr_w_loggers_w_logger_match(self): 'timestamp': TIMESTAMP, 'labels': LABELS, 'trace': TRACE - } + } LOGGER = object() loggers = {LOG_NAME: LOGGER} klass = self._get_target_class() diff --git a/logging/tests/unit/test_logger.py b/logging/tests/unit/test_logger.py index bc579e453684..80c07a69e61d 100644 --- a/logging/tests/unit/test_logger.py +++ b/logging/tests/unit/test_logger.py @@ -191,7 +191,8 @@ def test_log_text_w_unicode_explicit_client_labels_severity_httpreq(self): labels=DEFAULT_LABELS) logger.log_text(TEXT, client=client2, labels=LABELS, - insert_id=IID, severity=SEVERITY, http_request=REQUEST, trace=TRACE) + insert_id=IID, severity=SEVERITY, http_request=REQUEST, + trace=TRACE) self.assertEqual(api._write_entries_called_with, (ENTRIES, None, None, None)) @@ -881,7 +882,6 @@ def test_context_mgr_failure(self): METHOD = 'POST' URI = 'https://api.example.com/endpoint' STATUS = '500' - TRACE = '12345678-1234-5678-1234-567812345678' REQUEST = { 'requestMethod': METHOD, 'requestUrl': URI, From f909283a9104d297790b2bbaa273b7adc78bbad4 Mon Sep 17 00:00:00 2001 From: Salmaan Rashid Date: Sat, 1 Sep 2018 08:29:19 -0700 Subject: [PATCH 3/4] add coverage; cases; ordering --- logging/google/cloud/logging/entries.py | 13 ++-- logging/google/cloud/logging/logger.py | 18 ++--- logging/tests/unit/test_logger.py | 87 +++++++++++++++++++++++-- 3 files changed, 97 insertions(+), 21 deletions(-) diff --git a/logging/google/cloud/logging/entries.py b/logging/google/cloud/logging/entries.py index 00f56a4b4381..f6b07dfa5727 100644 --- a/logging/google/cloud/logging/entries.py +++ b/logging/google/cloud/logging/entries.py @@ -74,12 +74,11 @@ class _BaseEntry(object): :param http_request: (optional) info about HTTP request associated with the entry. - :type trace: str - :param trace: (optional) traceid to apply to the entry. - - :type resource: :class:`~google.cloud.logging.resource.Resource` :param resource: (Optional) Monitored resource of the entry + + :type trace: str + :param trace: (optional) traceid to apply to the entry. """ def __init__(self, payload, logger, insert_id=None, timestamp=None, labels=None, severity=None, http_request=None, resource=None, @@ -190,11 +189,11 @@ class ProtobufEntry(_BaseEntry): :param http_request: (optional) info about HTTP request associated with the entry - :type trace: str - :param trace: (optional) traceid to apply to the entry. - :type resource: :class:`~google.cloud.logging.resource.Resource` :param resource: (Optional) Monitored resource of the entry + + :type trace: str + :param trace: (optional) traceid to apply to the entry. """ _PAYLOAD_KEY = 'protoPayload' diff --git a/logging/google/cloud/logging/logger.py b/logging/google/cloud/logging/logger.py index af4f213198e3..286b200559e8 100644 --- a/logging/google/cloud/logging/logger.py +++ b/logging/google/cloud/logging/logger.py @@ -209,13 +209,13 @@ def log_text(self, text, client=None, labels=None, insert_id=None, :param http_request: (optional) info about HTTP request associated with the entry + :type timestamp: :class:`datetime.datetime` + :param timestamp: (optional) timestamp of event being logged. + :type resource: :class:`~google.cloud.logging.resource.Resource` :param resource: Monitored resource of the entry, defaults to the global resource type. - :type timestamp: :class:`datetime.datetime` - :param timestamp: (optional) timestamp of event being logged. - :type trace: str :param trace: (optional) traceid to apply to the entry. """ @@ -255,13 +255,13 @@ def log_struct(self, info, client=None, labels=None, insert_id=None, :param http_request: (optional) info about HTTP request associated with the entry. + :type timestamp: :class:`datetime.datetime` + :param timestamp: (optional) timestamp of event being logged. + :type resource: :class:`~google.cloud.logging.resource.Resource` :param resource: Monitored resource of the entry, defaults to the global resource type. - :type timestamp: :class:`datetime.datetime` - :param timestamp: (optional) timestamp of event being logged. - :type trace: str :param trace: (optional) traceid to apply to the entry. """ @@ -301,13 +301,13 @@ def log_proto(self, message, client=None, labels=None, insert_id=None, :param http_request: (optional) info about HTTP request associated with the entry. + :type timestamp: :class:`datetime.datetime` + :param timestamp: (optional) timestamp of event being logged. + :type resource: :class:`~google.cloud.logging.resource.Resource` :param resource: Monitored resource of the entry, defaults to the global resource type. - :type timestamp: :class:`datetime.datetime` - :param timestamp: (optional) timestamp of event being logged. - :type trace: str :param trace: (optional) traceid to apply to the entry. """ diff --git a/logging/tests/unit/test_logger.py b/logging/tests/unit/test_logger.py index 80c07a69e61d..b5fe746843c7 100644 --- a/logging/tests/unit/test_logger.py +++ b/logging/tests/unit/test_logger.py @@ -155,7 +155,30 @@ def test_log_text_w_timestamp(self): self.assertEqual(api._write_entries_called_with, (ENTRIES, None, None, None)) - def test_log_text_w_unicode_explicit_client_labels_severity_httpreq(self): + def test_log_text_w_trace(self): + + TEXT = 'TEXT' + TRACE = '12345678-1234-5678-1234-567812345678' + ENTRIES = [{ + 'logName': 'projects/%s/logs/%s' % ( + self.PROJECT, self.LOGGER_NAME), + 'textPayload': TEXT, + 'resource': { + 'type': 'global', + 'labels': {}, + }, + 'trace': TRACE + }] + client = _Client(self.PROJECT) + api = client.logging_api = _DummyLoggingAPI() + logger = self._make_one(self.LOGGER_NAME, client=client) + + logger.log_text(TEXT, trace=TRACE) + + self.assertEqual(api._write_entries_called_with, + (ENTRIES, None, None, None)) + + def test_log_text_w_unicode_explicit_client_labels_severity_httpreq_trace(self): TEXT = u'TEXT' DEFAULT_LABELS = {'foo': 'spam'} LABELS = {'foo': 'bar', 'baz': 'qux'} @@ -240,7 +263,7 @@ def test_log_struct_w_default_labels(self): self.assertEqual(api._write_entries_called_with, (ENTRIES, None, None, None)) - def test_log_struct_w_explicit_client_labels_severity_httpreq(self): + def test_log_struct_w_explicit_client_labels_severity_httpreq_trace(self): STRUCT = {'message': 'MESSAGE', 'weather': 'cloudy'} DEFAULT_LABELS = {'foo': 'spam'} LABELS = {'foo': 'bar', 'baz': 'qux'} @@ -249,6 +272,7 @@ def test_log_struct_w_explicit_client_labels_severity_httpreq(self): METHOD = 'POST' URI = 'https://api.example.com/endpoint' STATUS = '500' + TRACE = '12345678-1234-5678-1234-567812345678' REQUEST = { 'requestMethod': METHOD, 'requestUrl': URI, @@ -266,6 +290,7 @@ def test_log_struct_w_explicit_client_labels_severity_httpreq(self): 'insertId': IID, 'severity': SEVERITY, 'httpRequest': REQUEST, + 'trace': TRACE }] client1 = _Client(self.PROJECT) client2 = _Client(self.PROJECT) @@ -275,7 +300,7 @@ def test_log_struct_w_explicit_client_labels_severity_httpreq(self): logger.log_struct(STRUCT, client=client2, labels=LABELS, insert_id=IID, severity=SEVERITY, - http_request=REQUEST) + http_request=REQUEST, trace=TRACE) self.assertEqual(api._write_entries_called_with, (ENTRIES, None, None, None)) @@ -304,6 +329,29 @@ def test_log_struct_w_timestamp(self): self.assertEqual(api._write_entries_called_with, (ENTRIES, None, None, None)) + def test_log_struct_w_trace(self): + + STRUCT = {'message': 'MESSAGE', 'weather': 'cloudy'} + TRACE = '12345678-1234-5678-1234-567812345678' + ENTRIES = [{ + 'logName': 'projects/%s/logs/%s' % ( + self.PROJECT, self.LOGGER_NAME), + 'jsonPayload': STRUCT, + 'resource': { + 'type': 'global', + 'labels': {}, + }, + 'trace': TRACE + }] + client = _Client(self.PROJECT) + api = client.logging_api = _DummyLoggingAPI() + logger = self._make_one(self.LOGGER_NAME, client=client) + + logger.log_struct(STRUCT, trace=TRACE) + + 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 @@ -355,7 +403,7 @@ def test_log_proto_w_default_labels(self): self.assertEqual(api._write_entries_called_with, (ENTRIES, None, None, None)) - def test_log_proto_w_explicit_client_labels_severity_httpreq(self): + def test_log_proto_w_explicit_client_labels_severity_httpreq_trace(self): import json from google.protobuf.json_format import MessageToJson from google.protobuf.struct_pb2 import Struct @@ -369,6 +417,7 @@ def test_log_proto_w_explicit_client_labels_severity_httpreq(self): METHOD = 'POST' URI = 'https://api.example.com/endpoint' STATUS = '500' + TRACE = '12345678-1234-5678-1234-567812345678' REQUEST = { 'requestMethod': METHOD, 'requestUrl': URI, @@ -386,6 +435,7 @@ def test_log_proto_w_explicit_client_labels_severity_httpreq(self): 'insertId': IID, 'severity': SEVERITY, 'httpRequest': REQUEST, + 'trace': TRACE }] client1 = _Client(self.PROJECT) client2 = _Client(self.PROJECT) @@ -395,7 +445,7 @@ def test_log_proto_w_explicit_client_labels_severity_httpreq(self): logger.log_proto(message, client=client2, labels=LABELS, insert_id=IID, severity=SEVERITY, - http_request=REQUEST) + http_request=REQUEST, trace=TRACE) self.assertEqual(api._write_entries_called_with, (ENTRIES, None, None, None)) @@ -428,6 +478,33 @@ def test_log_proto_w_timestamp(self): self.assertEqual(api._write_entries_called_with, (ENTRIES, None, None, None)) + def test_log_proto_w_trace(self): + import json + from google.protobuf.json_format import MessageToJson + from google.protobuf.struct_pb2 import Struct + from google.protobuf.struct_pb2 import Value + + message = Struct(fields={'foo': Value(bool_value=True)}) + TRACE = '12345678-1234-5678-1234-567812345678' + ENTRIES = [{ + 'logName': 'projects/%s/logs/%s' % ( + self.PROJECT, self.LOGGER_NAME), + 'protoPayload': json.loads(MessageToJson(message)), + 'resource': { + 'type': 'global', + 'labels': {}, + }, + 'trace': TRACE + }] + client = _Client(self.PROJECT) + api = client.logging_api = _DummyLoggingAPI() + logger = self._make_one(self.LOGGER_NAME, client=client) + + logger.log_proto(message, trace=TRACE) + + 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() From 0715a985687a8b7f5a6f72466929bfd7cb5b9955 Mon Sep 17 00:00:00 2001 From: Salmaan Rashid Date: Sat, 1 Sep 2018 09:22:50 -0700 Subject: [PATCH 4/4] fix witespace; add trace in batch --- logging/google/cloud/logging/entries.py | 4 ++-- logging/google/cloud/logging/logger.py | 2 +- logging/tests/unit/test_logger.py | 24 +++++++++++++++--------- 3 files changed, 18 insertions(+), 12 deletions(-) diff --git a/logging/google/cloud/logging/entries.py b/logging/google/cloud/logging/entries.py index f6b07dfa5727..64a7751cf5f6 100644 --- a/logging/google/cloud/logging/entries.py +++ b/logging/google/cloud/logging/entries.py @@ -78,7 +78,7 @@ class _BaseEntry(object): :param resource: (Optional) Monitored resource of the entry :type trace: str - :param trace: (optional) traceid to apply to the entry. + :param trace: (optional) traceid to apply to the entry. """ def __init__(self, payload, logger, insert_id=None, timestamp=None, labels=None, severity=None, http_request=None, resource=None, @@ -193,7 +193,7 @@ class ProtobufEntry(_BaseEntry): :param resource: (Optional) Monitored resource of the entry :type trace: str - :param trace: (optional) traceid to apply to the entry. + :param trace: (optional) traceid to apply to the entry. """ _PAYLOAD_KEY = 'protoPayload' diff --git a/logging/google/cloud/logging/logger.py b/logging/google/cloud/logging/logger.py index 286b200559e8..2b3bd577fbed 100644 --- a/logging/google/cloud/logging/logger.py +++ b/logging/google/cloud/logging/logger.py @@ -303,7 +303,7 @@ def log_proto(self, message, client=None, labels=None, insert_id=None, :type timestamp: :class:`datetime.datetime` :param timestamp: (optional) timestamp of event being logged. - + :type resource: :class:`~google.cloud.logging.resource.Resource` :param resource: Monitored resource of the entry, defaults to the global resource type. diff --git a/logging/tests/unit/test_logger.py b/logging/tests/unit/test_logger.py index b5fe746843c7..85c8e2568584 100644 --- a/logging/tests/unit/test_logger.py +++ b/logging/tests/unit/test_logger.py @@ -178,7 +178,7 @@ def test_log_text_w_trace(self): self.assertEqual(api._write_entries_called_with, (ENTRIES, None, None, None)) - def test_log_text_w_unicode_explicit_client_labels_severity_httpreq_trace(self): + def test_log_text_w_unicode_explicit_client_labels_severity_httpreq(self): TEXT = u'TEXT' DEFAULT_LABELS = {'foo': 'spam'} LABELS = {'foo': 'bar', 'baz': 'qux'} @@ -263,7 +263,7 @@ def test_log_struct_w_default_labels(self): self.assertEqual(api._write_entries_called_with, (ENTRIES, None, None, None)) - def test_log_struct_w_explicit_client_labels_severity_httpreq_trace(self): + def test_log_struct_w_explicit_client_labels_severity_httpreq(self): STRUCT = {'message': 'MESSAGE', 'weather': 'cloudy'} DEFAULT_LABELS = {'foo': 'spam'} LABELS = {'foo': 'bar', 'baz': 'qux'} @@ -403,7 +403,7 @@ def test_log_proto_w_default_labels(self): self.assertEqual(api._write_entries_called_with, (ENTRIES, None, None, None)) - def test_log_proto_w_explicit_client_labels_severity_httpreq_trace(self): + def test_log_proto_w_explicit_client_labels_severity_httpreq(self): import json from google.protobuf.json_format import MessageToJson from google.protobuf.struct_pb2 import Struct @@ -825,26 +825,32 @@ def test_commit_w_bound_client(self): 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) + TRACE1 = '12345678-1234-5678-1234-567812345678' + TRACE2 = '12345678-1234-5678-1234-567812345679' + TRACE3 = '12345678-1234-5678-1234-567812345670' ENTRIES = [ {'textPayload': TEXT, 'insertId': IID1, 'timestamp': _datetime_to_rfc3339(TIMESTAMP1), - 'resource': _GLOBAL_RESOURCE._to_dict()}, + 'resource': _GLOBAL_RESOURCE._to_dict(), 'trace': TRACE1}, {'jsonPayload': STRUCT, 'insertId': IID2, 'timestamp': _datetime_to_rfc3339(TIMESTAMP2), - 'resource': _GLOBAL_RESOURCE._to_dict()}, + 'resource': _GLOBAL_RESOURCE._to_dict(), 'trace': TRACE2}, {'protoPayload': json.loads(MessageToJson(message)), 'insertId': IID3, 'timestamp': _datetime_to_rfc3339(TIMESTAMP3), - 'resource': _GLOBAL_RESOURCE._to_dict()}, + 'resource': _GLOBAL_RESOURCE._to_dict(), 'trace': TRACE3}, ] 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, timestamp=TIMESTAMP1) - batch.log_struct(STRUCT, insert_id=IID2, timestamp=TIMESTAMP2) - batch.log_proto(message, insert_id=IID3, timestamp=TIMESTAMP3) + batch.log_text(TEXT, insert_id=IID1, timestamp=TIMESTAMP1, + trace=TRACE1) + batch.log_struct(STRUCT, insert_id=IID2, timestamp=TIMESTAMP2, + trace=TRACE2) + batch.log_proto(message, insert_id=IID3, timestamp=TIMESTAMP3, + trace=TRACE3) batch.commit() self.assertEqual(list(batch.entries), [])