Skip to content

Add MonitoredResource to interface - #3386

Merged
theacodes merged 22 commits into
masterfrom
logging_resource
May 11, 2017
Merged

theacodes merged 22 commits into
masterfrom
logging_resource

Conversation

@waprin

@waprin waprin commented May 8, 2017

Copy link
Copy Markdown
Contributor

This adds MonitoredResource as an option to higher level interface.

Fixes #3377 and #2673.

I also changed the variable name from resource to entry in make_entry_resource because its' confusing to call it a resource when resource is overloaded.

Tests/docs are TBD, wanted +1 on overall direction then I'll write them.

Some notes:

  1. This is intended to be a non-breaking change to existing users. If you use log_text, log_struct etc the default is still the global resource. However, within a Batch you could also explicitly set it to None and wait to set it until the final commit call.

  2. Copied the Resource class from Monitoring. They are the same proto so we could move it to a common package . We could also use the proto but unclear if that will work with the _gax. This is what @jonparrott suggested and I think is pretty.

cc @liyanhui1228 @duggelz

@googlebot googlebot added the cla: yes This human has signed the Contributor License Agreement. label May 8, 2017
@waprin
waprin requested a review from theacodes May 8, 2017 23:01
@theacodes theacodes self-assigned this May 8, 2017

@theacodes theacodes left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@waprin I anticipate you'll also be making test updates? Both we need to verify that this works with both http and gax flavors.

Comment thread logging/google/cloud/logging/logger.py Outdated
: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.

Comment thread logging/google/cloud/logging/logger.py Outdated
timestamp, resource))

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.

).format(**self.__dict__)


class Resource(collections.namedtuple('Resource', 'type labels')):

This comment was marked as spam.

This comment was marked as spam.

@waprin

waprin commented May 8, 2017

Copy link
Copy Markdown
Contributor Author

Ya like I said just wanted +1 on overall direction before clean up tests docs since I hate writing all the tests/docs then being asked to totally change everything.

@theacodes

Copy link
Copy Markdown
Contributor

@waprin direction looks good. Let me know when you're ready for another review.

@waprin waprin changed the title [DO NOT MERGE] Add MonitoredResource to interface Add MonitoredResource to interface May 9, 2017
@waprin

waprin commented May 9, 2017

Copy link
Copy Markdown
Contributor Author

@jonparrott ready for review, looks like we have an unrelated BigQuery system test failure.

@theacodes theacodes left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This mostly LGTM, with a few questions. I'd like @dhermes or @lukesneeringer to do a pass as well. :)

Thanks for doing this, @waprin!

Comment thread logging/nox.py Outdated
serious code quality issues.
"""
session.interpreter = 'python3.6'
session.interpreter = 'python3.5'

This comment was marked as spam.

This comment was marked as spam.

This comment was marked as spam.

Comment thread logging/tests/system.py
self.assertEqual(entries[0].payload, text_payload)
self.assertEqual(entries[0].timestamp, now.replace(tzinfo=UTC))

def test_log_text_with_resource(self):

This comment was marked as spam.

This comment was marked as spam.

This comment was marked as spam.

@waprin

waprin commented May 9, 2017

Copy link
Copy Markdown
Contributor Author

System test snafu, forgot they say success without running without service account locally, fixing now.

@dhermes dhermes left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Mostly cosmetic changes, but one primary question:

Why does the Batch have self.resource but it's methods also accept resource different than self.resource?

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.

Comment thread logging/google/cloud/logging/logger.py Outdated
:type timestamp: :class:`datetime.datetime`
:param timestamp: (Optional) timestamp of event being logged.

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

This comment was marked as spam.

Comment thread logging/google/cloud/logging/logger.py Outdated
: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.

Comment thread logging/google/cloud/logging/logger.py Outdated
: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.

Comment thread logging/google/cloud/logging/logger.py Outdated
:param http_request: (optional) info about HTTP request associated with
the entry.

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

This comment was marked as spam.

This comment was marked as spam.

self.assertEqual(len(batch.entries), 0)

def test_log_text_defaults(self):
from google.cloud.logging.logger import _GLOBAL_RESOURCE

This comment was marked as spam.

This comment was marked as spam.

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.

self.entries.append(
('struct', info, labels, insert_id, severity, http_request,
timestamp))
timestamp, resource))

This comment was marked as spam.

self.entries.append(
('proto', message, labels, insert_id, severity, http_request,
timestamp))
timestamp, resource))

This comment was marked as spam.

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.

@waprin

waprin commented May 10, 2017

Copy link
Copy Markdown
Contributor Author

@dhermes as far as Batch resource goes...

If you look at the API, when you write multiple entries, you can give each individual entry a resource but also the higher level write call (which we encapsulate as a Batch) a resource. The Batch resource (self.resource) is used wherever entries are missing one. So a few places you suggested we should fall back to self.resource, but really what we want there is just for the entry resource to be missing so that Batch resource overrides it on server side.

Also since all the log_* calls default to the global resource, callers would have to explicitly specify None for the individual resources then set the Batch resource. Debatable whether this is a use case people actually want, @jonparrott seemed skeptical of whether it should go in there but I wanted to keep the client library somewhat aligned with the API. I suppose I could rip it out and just ignore the whole Batch resource aspect altogether and make Resource required on all the entry calls.

@theacodes

Copy link
Copy Markdown
Contributor

@waprin I was skeptical because I didn't see that the api has that functionality. We should match the API. We shouldn't go through and fill in the resource for each entry on our end, we should just submit it as part of the request to the server (in both gax and http).

@waprin

waprin commented May 10, 2017

Copy link
Copy Markdown
Contributor Author

Right, I believe the way I did it best matches the API semantics. Only setting it at the Batch level is also not right because your Batch might contain entries of different resource type.

Fixing coverage now...

Comment thread logging/google/cloud/logging/entries.py Outdated
:type http_request: dict
:param http_request: (optional) info about HTTP request associated with
the entry
:type resource :class:`google.cloud.logging.resource.Resource`

This comment was marked as spam.

Comment thread logging/google/cloud/logging/entries.py Outdated
:param http_request: (optional) info about HTTP request associated with
the entry

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

This comment was marked as spam.

This comment was marked as spam.

This comment was marked as spam.

Comment thread logging/google/cloud/logging/logger.py Outdated
:type client: :class:`google.cloud.logging.client.Client`
:param client: The client to use.

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

This comment was marked as spam.

Comment thread logging/google/cloud/logging/logger.py Outdated
entry = {
'logName': self.full_name,
'resource': {'type': 'global'},
'resource': resource._to_dict()

This comment was marked as spam.

This comment was marked as spam.

self.entries.append(
('text', text, labels, insert_id, severity, http_request,
timestamp))
timestamp, resource))

This comment was marked as spam.

Comment thread logging/nox.py Outdated
test runs (not system test runs), and then erases coverage data.
"""
session.interpreter = 'python3.6'
session.interpreter = 'python3.5'

This comment was marked as spam.

Comment thread logging/tests/system.py Outdated
@@ -1,4 +1,4 @@
# Copyright 2016 Google Inc.
# Copyright 2017 Google Inc.

This comment was marked as spam.

Comment thread logging/tests/system.py Outdated
labels={
'module_id': 'default',
'version_id': 'test'
})

This comment was marked as spam.

Comment thread logging/tests/unit/test_entries.py Outdated
'labels': {
'module_id':'default',
'version': 'test'
}})

This comment was marked as spam.

Comment thread logging/tests/unit/test_logger.py Outdated
'resource': {
'type': 'global',
'labels': {},

This comment was marked as spam.

@waprin

waprin commented May 10, 2017

Copy link
Copy Markdown
Contributor Author

@jonparrott @dhermes docs built, just looked carefully at all the styles/docs issue and all lgtm so this is ready for another pass

:type http_request: dict
:param http_request: (optional) info about HTTP request associated with
the entry
:type resource: :class:`~google.cloud.logging.resource.Resource`

This comment was marked as spam.

Comment thread logging/google/cloud/logging/logger.py Outdated
the entry.

:type resource: :class:`~google.cloud.logging.resource.Resource`
:param resource: Monitored resource of the entry

This comment was marked as spam.

Comment thread logging/google/cloud/logging/logger.py Outdated
:param timestamp: (optional) timestamp of event being logged.

:type resource: :class:`~google.cloud.logging.resource.Resource`
:param resource: (Optional) Monitored resource of the entry

This comment was marked as spam.

@waprin

waprin commented May 11, 2017

Copy link
Copy Markdown
Contributor Author

@dhermes ready for another pass/merge

@theacodes theacodes left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks almost good to me, two outstanding questions.

labels=None, insert_id=None, severity=None,
http_request=None, timestamp=None):
http_request=None, timestamp=None,
resource=_GLOBAL_RESOURCE):

This comment was marked as spam.

This comment was marked as spam.

This comment was marked as spam.

Comment thread logging/tests/system.py
self.assertEqual(entries[0].payload, text_payload)
self.assertEqual(entries[0].timestamp, now.replace(tzinfo=UTC))

def test_log_text_with_resource(self):

This comment was marked as spam.

@waprin

waprin commented May 11, 2017

Copy link
Copy Markdown
Contributor Author

System tests default to whatever the Client defaults to which is usually gRPC unless it's not supported. It seems a reasonable idea to parametrize the system tests to run with gRPC on and off, but don't think that's particular to these tests. Will create separate issue.

@jaitaiwan

ghost commented May 24, 2017

Copy link
Copy Markdown

Hey guys, when will this be released in the google-cloud-logging package?

@dhermes

ghost commented May 24, 2017

Copy link
Copy Markdown
Contributor

@lukesneeringer I can do a release today. WDYT?

@bjwatson

ghost commented Jun 22, 2017

Copy link
Copy Markdown

@tseaver I've had an internal request to release this Logging fix ASAP. Since you're planning to do new releases this week, this should be covered, but I just wanted to make sure.

@tseaver

ghost commented Jun 23, 2017

Copy link
Copy Markdown
Contributor

@bjwatson Still on my radar: at this point I will be making releases over the weekend, to catch up with some unplanned sitting-in-waiting-room time this week.

@bjwatson

ghost commented Jun 23, 2017

Copy link
Copy Markdown

Thanks @tseaver! Sorry to hear about excessive waiting room time this week.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

cla: yes This human has signed the Contributor License Agreement.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

7 participants