Skip to content

Commit

Permalink
appengine_config: logging: manually find and inject Google Cloud trac…
Browse files Browse the repository at this point in the history
…e id

makes log lines attach to their HTTP request and show grouped in cloud console log browser. google-cloud-logging used to do this automatically for webapp2, based on the X-Cloud-Trace-Context header, but it dropped webapp2 as of google-cloud-logging 2.0.

background:
googleapis/python-logging#110 (comment)
googleapis/python-logging#149 (comment)

also note that AppEngineHandler is evidently deprecated, so I may need to port that whole class into webutil eventually. :(
googleapis/python-logging#202
  • Loading branch information
snarfed committed Mar 6, 2021
1 parent b4e03b4 commit 38eaafc
Showing 1 changed file with 25 additions and 4 deletions.
29 changes: 25 additions & 4 deletions appengine_config.py
@@ -1,7 +1,7 @@
"""App Engine config. dev_appserver vs prod, logging, Google API clients, etc."""
import os

from .appengine_info import DEBUG
from .appengine_info import APP_ID, DEBUG

# Use lxml for BeautifulSoup explicitly.
from . import util
Expand Down Expand Up @@ -70,11 +70,32 @@
try:
import google.cloud.logging
logging_client = google.cloud.logging.Client()

if not DEBUG:
# https://stackoverflow.com/a/58296028/186123
# https://googleapis.dev/python/logging/latest/usage.html#cloud-logging-handler
from google.cloud.logging.handlers import AppEngineHandler, setup_logging
setup_logging(AppEngineHandler(logging_client, name='stdout'),

class Webapp2TraceHandler(AppEngineHandler):
"""Log handler that adds trace id based on webapp2 request header.
https://github.com/googleapis/python-logging/issues/110#issuecomment-745534629
https://github.com/googleapis/python-logging/issues/149#issuecomment-782693201
Also note that AppEngineHandler is evidently deprecated, so I may need to
port that whole class into webutil eventually. :(
https://github.com/googleapis/python-logging/issues/202
"""
def emit(self, record):
try:
import webapp2
trace = webapp2.get_request().headers.get('X-Cloud-Trace-Context')
if trace:
trace_id = trace.split('/', 1)[0]
record.trace = f'projects/{APP_ID}/traces/{trace_id}'
except (ImportError, AssertionError):
pass
return super().emit(record)

setup_logging(Webapp2TraceHandler(logging_client, name='stdout'),
log_level=logging.DEBUG)
# this currently occasionally hits the 256KB stackdriver logging limit and
# crashes in the background service. i've tried batch_size=1 and
Expand Down

0 comments on commit 38eaafc

Please sign in to comment.