|
14 | 14 |
|
15 | 15 | """Logging handler for App Engine Flexible |
16 | 16 |
|
17 | | -Logs to the well-known file that the fluentd sidecar container on App Engine |
18 | | -Flexible is configured to read from and send to Stackdriver Logging. |
19 | | -
|
20 | | -See the fluentd configuration here: |
21 | | -
|
22 | | -https://github.com/GoogleCloudPlatform/appengine-sidecars-docker/tree/master/fluentd_logger |
| 17 | +Sends logs to the Stackdriver Logging API with the appropriate resource |
| 18 | +and labels for App Engine logs. |
23 | 19 | """ |
24 | 20 |
|
25 | | -# This file is largely copied from: |
26 | | -# https://github.com/GoogleCloudPlatform/python-compat-runtime/blob/master |
27 | | -# /appengine-vmruntime/vmruntime/cloud_logging.py |
28 | | - |
29 | | -import logging.handlers |
30 | 21 | import os |
31 | 22 |
|
32 | | -from google.cloud.logging.handlers._helpers import format_stackdriver_json |
| 23 | +from google.cloud.logging.handlers.handlers import CloudLoggingHandler |
| 24 | +from google.cloud.logging.handlers.transports import BackgroundThreadTransport |
| 25 | +from google.cloud.logging.resource import Resource |
33 | 26 |
|
34 | | -_LOG_PATH_TEMPLATE = '/var/log/app_engine/app.{pid}.json' |
35 | | -_MAX_LOG_BYTES = 128 * 1024 * 1024 |
36 | | -_LOG_FILE_COUNT = 3 |
| 27 | +_DEFAULT_GAE_LOGGER_NAME = 'app' |
37 | 28 |
|
| 29 | +_GAE_PROJECT_ENV = 'GCLOUD_PROJECT' |
| 30 | +_GAE_SERVICE_ENV = 'GAE_SERVICE' |
| 31 | +_GAE_VERSION_ENV = 'GAE_VERSION' |
38 | 32 |
|
39 | | -class AppEngineHandler(logging.handlers.RotatingFileHandler): |
40 | | - """A handler that writes to the App Engine fluentd Stackdriver log file. |
41 | 33 |
|
42 | | - Writes to the file that the fluentd agent on App Engine Flexible is |
43 | | - configured to discover logs and send them to Stackdriver Logging. |
44 | | - Log entries are wrapped in JSON and with appropriate metadata. The |
45 | | - process of converting the user's formatted logs into a JSON payload for |
46 | | - Stackdriver Logging consumption is implemented as part of the handler |
47 | | - itself, and not as a formatting step, so as not to interfere with |
48 | | - user-defined logging formats. |
49 | | - """ |
| 34 | +class AppEngineHandler(CloudLoggingHandler): |
| 35 | + """A logging handler that sends App Engine-formatted logs to Stackdriver. |
50 | 36 |
|
51 | | - def __init__(self): |
52 | | - """Construct the handler |
| 37 | + :type client: :class:`~google.cloud.logging.client.Client` |
| 38 | + :param client: The authenticated Google Cloud Logging client for this |
| 39 | + handler to use. |
53 | 40 |
|
54 | | - Large log entries will get mangled if multiple workers write to the |
55 | | - same file simultaneously, so we'll use the worker's PID to pick a log |
56 | | - filename. |
57 | | - """ |
58 | | - self.filename = _LOG_PATH_TEMPLATE.format(pid=os.getpid()) |
59 | | - super(AppEngineHandler, self).__init__(self.filename, |
60 | | - maxBytes=_MAX_LOG_BYTES, |
61 | | - backupCount=_LOG_FILE_COUNT) |
| 41 | + :type transport: type |
| 42 | + :param transport: The transport class. It should be a subclass |
| 43 | + of :class:`.Transport`. If unspecified, |
| 44 | + :class:`.BackgroundThreadTransport` will be used. |
| 45 | + """ |
62 | 46 |
|
63 | | - def format(self, record): |
64 | | - """Format the specified record into the expected JSON structure. |
| 47 | + def __init__(self, client, |
| 48 | + transport=BackgroundThreadTransport): |
| 49 | + super(AppEngineHandler, self).__init__( |
| 50 | + client, |
| 51 | + name=_DEFAULT_GAE_LOGGER_NAME, |
| 52 | + transport=transport, |
| 53 | + resource=self.get_gae_resource()) |
65 | 54 |
|
66 | | - :type record: :class:`~logging.LogRecord` |
67 | | - :param record: the log record |
| 55 | + def get_gae_resource(self): |
| 56 | + """Return the GAE resource using the environment variables. |
68 | 57 |
|
69 | | - :rtype: str |
70 | | - :returns: JSON str to be written to the log file |
| 58 | + :rtype: :class:`~google.cloud.logging.resource.Resource` |
| 59 | + :returns: Monitored resource for GAE. |
71 | 60 | """ |
72 | | - message = super(AppEngineHandler, self).format(record) |
73 | | - return format_stackdriver_json(record, message) |
| 61 | + gae_resource = Resource( |
| 62 | + type='gae_app', |
| 63 | + labels={ |
| 64 | + 'project_id': os.environ.get(_GAE_PROJECT_ENV), |
| 65 | + 'module_id': os.environ.get(_GAE_SERVICE_ENV), |
| 66 | + 'version_id': os.environ.get(_GAE_VERSION_ENV), |
| 67 | + }, |
| 68 | + ) |
| 69 | + return gae_resource |
0 commit comments