Skip to content

Commit

Permalink
fix stuff
Browse files Browse the repository at this point in the history
  • Loading branch information
liyanhui1228 committed May 16, 2017
1 parent 4d1924f commit 2591ef1
Show file tree
Hide file tree
Showing 8 changed files with 49 additions and 69 deletions.
48 changes: 21 additions & 27 deletions logging/google/cloud/logging/handlers/app_engine.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,37 +14,18 @@

"""Logging handler for App Engine Flexible
Logs to the well-known file that the fluentd sidecar container on App Engine
Flexible is configured to read from and send to Stackdriver Logging.
See the fluentd configuration here:
https://github.com/GoogleCloudPlatform/appengine-sidecars-docker/tree/master/fluentd_logger
Sends logs to the Stackdriver Logging API with the appropriate resource and labels for App Engine logs.
"""

# This file is largely copied from:
# https://github.com/GoogleCloudPlatform/python-compat-runtime/blob/master
# /appengine-vmruntime/vmruntime/cloud_logging.py

import logging.handlers
import os

from google.cloud.logging.handlers.handlers import CloudLoggingHandler
from google.cloud.logging.handlers.transports import BackgroundThreadTransport
from google.cloud.logging.resource import Resource

DEFAULT_LOGGER_NAME = 'python'

EXCLUDED_LOGGER_DEFAULTS = ('google.cloud', 'oauth2client')

GAE_RESOURCE = Resource(
type='gae_app',
labels={
'project_id': os.getenv('GCLOUD_PROJECT'),
'module_id': os.getenv('GAE_SERVICE'),
'version_id': os.getenv('GAE_VERSION'),
},
)
_GAE_PROJECT_ENV = 'GCLOUD_PROJECT'
_GAE_SERVICE_ENV = 'GAE_SERVICE'
_GAE_VERSION_ENV = 'GAE_VERSION'


class AppEngineHandler(CloudLoggingHandler):
Expand Down Expand Up @@ -76,8 +57,21 @@ class AppEngineHandler(CloudLoggingHandler):
to the global resource type.
"""

DEFAULT_LOGGER_NAME = 'projects/{}/logs/app'.format(os.environ.get(_GAE_PROJECT_ENV))

def __init__(self, client,
name=DEFAULT_LOGGER_NAME,
transport=BackgroundThreadTransport,
resource=GAE_RESOURCE):
super(AppEngineHandler, self).__init__(client, name, transport, resource)
transport=BackgroundThreadTransport):
super(AppEngineHandler, self).__init__(client, name=self.DEFAULT_LOGGER_NAME,
transport=transport, resource=self.gae_resource)

@property
def gae_resource(self):
gae_resource = Resource(
type='gae_app',
labels={
'project_id': os.environ.get(_GAE_PROJECT_ENV),
'module_id': os.environ.get(_GAE_SERVICE_ENV),
'version_id': os.environ.get(_GAE_VERSION_ENV),
},
)
return gae_resource
4 changes: 2 additions & 2 deletions logging/google/cloud/logging/handlers/handlers.py
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ def __init__(self, client,
self.name = name
self.client = client
self.transport = transport(client, name)
self.resource=resource
self.resource = resource

def emit(self, record):
"""Actually log the specified logging record.
Expand All @@ -96,7 +96,7 @@ def emit(self, record):
:param record: The record to be logged.
"""
message = super(CloudLoggingHandler, self).format(record)
self.transport.send(record, message, self.resource)
self.transport.send(record, message, resource=self.resource)


def setup_logging(handler, excluded_loggers=EXCLUDED_LOGGER_DEFAULTS,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@
from six.moves import queue

from google.cloud.logging.handlers.transports.base import Transport
from google.cloud.logging.logger import _GLOBAL_RESOURCE

_DEFAULT_GRACE_PERIOD = 5.0 # Seconds
_DEFAULT_MAX_BATCH_SIZE = 10
Expand Down Expand Up @@ -258,7 +257,7 @@ def __init__(self, client, name, grace_period=_DEFAULT_GRACE_PERIOD,
self.worker = _Worker(logger)
self.worker.start()

def send(self, record, message, resource=_GLOBAL_RESOURCE):
def send(self, record, message, resource=None):
"""Overrides Transport.send().
:type record: :class:`logging.LogRecord`
Expand All @@ -272,7 +271,7 @@ def send(self, record, message, resource=_GLOBAL_RESOURCE):
:param resource: Monitored resource of the entry, defaults
to the global resource type.
"""
self.worker.enqueue(record, message, resource)
self.worker.enqueue(record, message, resource=resource)

def flush(self):
"""Submit any pending log records."""
Expand Down
2 changes: 1 addition & 1 deletion logging/google/cloud/logging/handlers/transports/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ class Transport(object):
client and name object, and must override :meth:`send`.
"""

def send(self, record, message, resource):
def send(self, record, message, resource=None):
"""Transport send to be implemented by subclasses.
:type record: :class:`logging.LogRecord`
Expand Down
5 changes: 1 addition & 4 deletions logging/google/cloud/logging/handlers/transports/sync.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,6 @@
from google.cloud.logging.resource import Resource


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


class SyncTransport(Transport):
"""Basic sychronous transport.
Expand All @@ -33,7 +30,7 @@ class SyncTransport(Transport):
def __init__(self, client, name):
self.logger = client.logger(name)

def send(self, record, message, resource=_GLOBAL_RESOURCE):
def send(self, record, message, resource=None):
"""Overrides transport.send().
:type record: :class:`logging.LogRecord`
Expand Down
28 changes: 15 additions & 13 deletions logging/tests/unit/handlers/test_app_engine.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,11 @@
# limitations under the License.

import logging
import os
import unittest
from google.cloud.logging.resource import Resource


class TestAppEngineHandlerHandler(unittest.TestCase):

PROJECT = 'PROJECT'

def _get_target_class(self):
Expand All @@ -30,29 +29,32 @@ def _make_one(self, *args, **kw):
return self._get_target_class()(*args, **kw)

def test_ctor(self):
import os
from google.cloud._testing import _Monkey
from google.cloud.logging.handlers.app_engine import _GAE_PROJECT_ENV, _GAE_SERVICE_ENV, _GAE_VERSION_ENV

client = _Client(self.PROJECT)
handler = self._make_one(client, transport=_Transport)
with _Monkey(os, environ={_GAE_PROJECT_ENV: 'test_project',
_GAE_SERVICE_ENV: 'test_service',
_GAE_VERSION_ENV: 'test_version'}):
handler = self._make_one(client, transport=_Transport)
self.assertEqual(handler.client, client)
self.assertEqual(handler.resource.type, 'gae_app')
self.assertEqual(handler.resource.labels['project_id'], 'test_project')
self.assertEqual(handler.resource.labels['module_id'], 'test_service')
self.assertEqual(handler.resource.labels['version_id'], 'test_version')

def test_emit(self):
RESOURCE = Resource(
type='gae_app',
labels={
'project_id': os.getenv('GCLOUD_PROJECT'),
'module_id': os.getenv('GAE_SERVICE'),
'version_id': os.getenv('GAE_VERSION'),
},
)

client = _Client(self.PROJECT)
handler = self._make_one(client, transport=_Transport)
gae_resource = handler.gae_resource
logname = 'loggername'
message = 'hello world'
record = logging.LogRecord(logname, logging, None, None, message,
None, None)
handler.emit(record)

self.assertEqual(handler.transport.send_called_with, (record, message, RESOURCE))
self.assertEqual(handler.transport.send_called_with, (record, message, gae_resource))


class _Client(object):
Expand Down
12 changes: 3 additions & 9 deletions logging/tests/unit/handlers/transports/test_background_thread.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@

import logging
import unittest
from google.cloud.logging.resource import Resource

import mock
from six.moves import queue
Expand Down Expand Up @@ -48,12 +47,7 @@ def test_constructor(self):
self.assertEqual(logger.name, name)

def test_send(self):
RESOURCE = Resource(
type='gae_app',
labels={
'module_id': 'default',
'version_id': 'test',
})
from google.cloud.logging.logger import _GLOBAL_RESOURCE

client = _Client(self.PROJECT)
name = 'python_logger'
Expand All @@ -67,9 +61,9 @@ def test_send(self):
python_logger_name, logging.INFO,
None, None, message, None, None)

transport.send(record, message, RESOURCE)
transport.send(record, message, _GLOBAL_RESOURCE)

transport.worker.enqueue.assert_called_once_with(record, message, RESOURCE)
transport.worker.enqueue.assert_called_once_with(record, message, _GLOBAL_RESOURCE)

def test_flush(self):
client = _Client(self.PROJECT)
Expand Down
14 changes: 4 additions & 10 deletions logging/tests/unit/handlers/transports/test_sync.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,6 @@

import logging
import unittest
from google.cloud.logging.resource import Resource
from google.cloud.logging.logger import _GLOBAL_RESOURCE


class TestSyncHandler(unittest.TestCase):
Expand All @@ -38,12 +36,7 @@ def test_ctor(self):
self.assertEqual(transport.logger.name, 'python_logger')

def test_send(self):
RESOURCE = Resource(
type='gae_app',
labels={
'module_id': 'default',
'version_id': 'test',
})
from google.cloud.logging.logger import _GLOBAL_RESOURCE

client = _Client(self.PROJECT)

Expand All @@ -54,17 +47,18 @@ def test_send(self):
record = logging.LogRecord(python_logger_name, logging.INFO,
None, None, message, None, None)

transport.send(record, message, RESOURCE)
transport.send(record, message, _GLOBAL_RESOURCE)
EXPECTED_STRUCT = {
'message': message,
'python_logger': python_logger_name,
}
EXPECTED_SENT = (EXPECTED_STRUCT, 'INFO', RESOURCE)
EXPECTED_SENT = (EXPECTED_STRUCT, 'INFO', _GLOBAL_RESOURCE)
self.assertEqual(
transport.logger.log_struct_called_with, EXPECTED_SENT)


class _Logger(object):
from google.cloud.logging.logger import _GLOBAL_RESOURCE

def __init__(self, name):
self.name = name
Expand Down

0 comments on commit 2591ef1

Please sign in to comment.