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 MonitoredResource to interface #3386

Merged
merged 22 commits into from
May 11, 2017
87 changes: 60 additions & 27 deletions logging/google/cloud/logging/logger.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,10 @@

from google.protobuf.json_format import MessageToDict
from google.cloud._helpers import _datetime_to_rfc3339
from google.cloud.logging.resource import Resource


_GLOBAL_RESOURCE = Resource(type='global', labels={})

This comment was marked as spam.

This comment was marked as spam.

This comment was marked as spam.



class Logger(object):
Expand Down Expand Up @@ -91,7 +95,8 @@ 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):
http_request=None, timestamp=None,
resource=None):
"""Return a log entry resource of the appropriate type.

Helper for :meth:`log_text`, :meth:`log_struct`, and :meth:`log_proto`.
Expand Down Expand Up @@ -123,19 +128,24 @@ def _make_entry_resource(self, text=None, info=None, message=None,
:type timestamp: :class:`datetime.datetime`
:param timestamp: (Optional) timestamp of event being logged.

:type resource :class:``google.cloud.logging.resource`

This comment was marked as spam.

This comment was marked as spam.

:param resource: (Optional) Monitored resource of the entry

:rtype: dict
:returns: The JSON resource created.
"""
resource = {
entry = {
'logName': self.full_name,
'resource': {'type': 'global'},
}

if resource is not None:
entry['resource'] = resource._to_dict()

if text is not None:
resource['textPayload'] = text
entry['textPayload'] = text

if info is not None:
resource['jsonPayload'] = info
entry['jsonPayload'] = info

if message is not None:
# NOTE: If ``message`` contains an ``Any`` field with an
Expand All @@ -144,30 +154,31 @@ def _make_entry_resource(self, text=None, info=None, message=None,
# the assumption is that any types needed for the
# protobuf->JSON conversion will be known from already
# imported ``pb2`` modules.
resource['protoPayload'] = MessageToDict(message)
entry['protoPayload'] = MessageToDict(message)

if labels is None:
labels = self.labels

if labels is not None:
resource['labels'] = labels
entry['labels'] = labels

if insert_id is not None:
resource['insertId'] = insert_id
entry['insertId'] = insert_id

if severity is not None:
resource['severity'] = severity
entry['severity'] = severity

if http_request is not None:
resource['httpRequest'] = http_request
entry['httpRequest'] = http_request

if timestamp is not None:
resource['timestamp'] = _datetime_to_rfc3339(timestamp)
entry['timestamp'] = _datetime_to_rfc3339(timestamp)

return resource
return entry

def log_text(self, text, client=None, labels=None, insert_id=None,
severity=None, http_request=None, timestamp=None):
severity=None, http_request=None, timestamp=None,
resource=_GLOBAL_RESOURCE):
"""API call: log a text message via a POST request

See:
Expand All @@ -194,17 +205,23 @@ 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 resource :class:``google.cloud.logging.resource`
:param resource: (Optional) Monitored resource of the entry, defaults
to the global resource type.


This comment was marked as spam.

This comment was marked as spam.

:type timestamp: :class:`datetime.datetime`
:param timestamp: (optional) timestamp of event being logged.
"""
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)
http_request=http_request, timestamp=timestamp, resource=resource)
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):
severity=None, http_request=None, timestamp=None,
resource=_GLOBAL_RESOURCE):
"""API call: log a structured message via a POST request

See:
Expand All @@ -231,17 +248,23 @@ 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 resource :class:``google.cloud.logging.resource`
:param resource: (Optional) Monitored resource of the entry, defaults
to the global resource type.


This comment was marked as spam.

This comment was marked as spam.

:type timestamp: :class:`datetime.datetime`
:param timestamp: (optional) timestamp of event being logged.
"""
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)
http_request=http_request, timestamp=timestamp, resource=resource)
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):
severity=None, http_request=None, timestamp=None,
resource=_GLOBAL_RESOURCE):
"""API call: log a protobuf message via a POST request

See:
Expand Down Expand Up @@ -274,7 +297,8 @@ def log_proto(self, message, client=None, labels=None, insert_id=None,
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)
severity=severity, http_request=http_request, timestamp=timestamp,
resource=resource)
client.logging_api.write_entries([entry_resource])

def delete(self, client=None):
Expand Down Expand Up @@ -358,7 +382,7 @@ 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):
http_request=None, timestamp=None, resource=_GLOBAL_RESOURCE):
"""Add a text entry to be logged during :meth:`commit`.

:type text: str
Expand All @@ -382,10 +406,11 @@ def log_text(self, text, labels=None, insert_id=None, severity=None,
"""
self.entries.append(
('text', text, labels, insert_id, severity, http_request,
timestamp))
timestamp, resource))

This comment was marked as spam.

This comment was marked as spam.

This comment was marked as spam.

This comment was marked as spam.

This comment was marked as spam.


def log_struct(self, info, labels=None, insert_id=None, severity=None,
http_request=None, timestamp=None):
http_request=None, timestamp=None,
resource=_GLOBAL_RESOURCE):
"""Add a struct entry to be logged during :meth:`commit`.

:type info: dict
Expand All @@ -409,10 +434,11 @@ def log_struct(self, info, labels=None, insert_id=None, severity=None,
"""
self.entries.append(
('struct', info, labels, insert_id, severity, http_request,
timestamp))
timestamp, resource))

This comment was marked as spam.


def log_proto(self, message, labels=None, insert_id=None, severity=None,
http_request=None, timestamp=None):
http_request=None, timestamp=None,
resource=_GLOBAL_RESOURCE):
"""Add a protobuf entry to be logged during :meth:`commit`.

:type message: protobuf message
Expand All @@ -436,29 +462,34 @@ def log_proto(self, message, labels=None, insert_id=None, severity=None,
"""
self.entries.append(
('proto', message, labels, insert_id, severity, http_request,
timestamp))
timestamp, resource))

This comment was marked as spam.


def commit(self, client=None):
def commit(self, client=None, resource=None):

This comment was marked as spam.

This comment was marked as spam.

This comment was marked as spam.

This comment was marked as spam.

"""Send saved log entries as a single API call.

:type client: :class:`~google.cloud.logging.client.Client` or
``NoneType``
:param client: the client to use. If not passed, falls back to the
``client`` stored on the current batch.

:type resource :class:``google.cloud.logging.resource`
:param resource: (Optional) Monitored resource of the batch
"""
if client is None:
client = self.client

kwargs = {
'logger_name': self.logger.full_name,
'resource': {'type': 'global'},
}

if resource is not None:
kwargs['resource'] = resource._to_dict()
if self.logger.labels is not None:
kwargs['labels'] = self.logger.labels

entries = []
for (entry_type, entry, labels, iid, severity, http_req,
timestamp) in self.entries:
timestamp, resource) in self.entries:
if entry_type == 'text':
info = {'textPayload': entry}
elif entry_type == 'struct':
Expand All @@ -473,6 +504,8 @@ def commit(self, client=None):
info = {'protoPayload': MessageToDict(entry)}
else:
raise ValueError('Unknown entry type: %s' % (entry_type,))
if resource is not None:
info['resource'] = resource._to_dict()

This comment was marked as spam.

This comment was marked as spam.

This comment was marked as spam.

if labels is not None:
info['labels'] = labels
if iid is not None:
Expand Down
Loading