Skip to content

Commit

Permalink
logs: add path kwarg to limit logs to HTTP requests with that path
Browse files Browse the repository at this point in the history
  • Loading branch information
snarfed committed Jun 21, 2023
1 parent 68e84c3 commit 6496775
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 11 deletions.
35 changes: 26 additions & 9 deletions logs.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import time
import urllib.request, urllib.parse, urllib.error

from flask import request
from google.cloud import ndb
from google.cloud.logging import Client
from oauth_dropins.webutil.util import json_dumps, json_loads
Expand Down Expand Up @@ -51,20 +52,23 @@ def sanitize(msg):
return SANITIZE_RE.sub(r'\1...', msg)


def url(when, key, module=None):
def url(when, key, **params):
"""Returns the relative URL (no scheme or host) to a log page.
Args:
when: datetime
key: ndb.Key or str
module: string, optional App Engine module
params: included as query params, eg module, path
"""
if isinstance(key, ndb.Key):
key = key.urlsafe().decode()
return f'log?start_time={calendar.timegm(when.utctimetuple())}&key={key}&module={module if module else ""}'
assert 'start_time' not in params and 'key' not in params, params
params.update({
'start_time': calendar.timegm(when.utctimetuple()),
'key': key.urlsafe().decode() if isinstance(key, ndb.Key) else key,
})
return 'log?' + urllib.parse.urlencode(params)


def maybe_link(when, key, time_class='dt-updated', link_class='', module=None):
def maybe_link(when, key, time_class='dt-updated', link_class='', **params):
"""Returns an HTML snippet with a timestamp and maybe a log page link.
Example:
Expand All @@ -86,7 +90,8 @@ def maybe_link(when, key, time_class='dt-updated', link_class='', module=None):
key: ndb.Key or str
time_class: string, optional class value for the <time> tag
link_class: string, optional class value for the <a> tag (if generated)
module: string, optional App Engine module to search logs of
params: dict {string: string}, query params to include in the link URL,
eg module, path
Returns: string HTML
"""
Expand All @@ -99,7 +104,7 @@ def maybe_link(when, key, time_class='dt-updated', link_class='', module=None):
time = f'<time class="{time_class}" datetime="{when.isoformat()}" title="{when.ctime()} {when.tzname()}">{util.naturaltime(when, when=now)}</time>'

if now > when > now - MAX_LOG_AGE:
return f'<a class="{link_class}" href="/{url(when, key, module=module)}">{time}</a>'
return f'<a class="{link_class}" href="/{url(when, key, **params)}">{time}</a>'

return time

Expand Down Expand Up @@ -133,7 +138,7 @@ def linkify_key(match):
return DATASTORE_KEY_RE.sub(linkify_key, msg)


def log(module=None):
def log(module=None, path=None):
"""Flask view that searches for and renders app logs for an HTTP request.
URL parameters:
Expand All @@ -151,7 +156,16 @@ def log():
Args:
module: str, App Engine module to search. Defaults to all.
path: string, optional HTTP request path to limit logs to.
Returns:
(string response body, dict headers) Flask response
"""
if not module:
module = request.values.get('module')
if not path:
path = request.values.get('path')

start_time = flask_util.get_required_param('start_time')
if not util.is_float(start_time):
return error(f"Couldn't convert start_time to float: {start_time!r}")
Expand All @@ -174,6 +188,9 @@ def log():
query = f'logName="{project}/logs/python" textPayload:"{key}" {timestamp_filter}'
if module:
query += f' resource.labels.module_id="{module}"'
if path:
query += f' httpRequest.requestUrl:"{path}"'

logger.info(f'Searching logs with: {query}')
try:
# https://googleapis.dev/python/logging/latest/client.html#google.cloud.logging_v2.client.Client.list_entries
Expand Down
4 changes: 2 additions & 2 deletions tests/test_logs.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ def setUp(self):
self.client = self.app.test_client()

def test_url(self):
self.assertEqual(f'log?start_time=172800&key={KEY_STR}&module=',
self.assertEqual(f'log?start_time=172800&key={KEY_STR}',
logs.url(datetime(1970, 1, 3), KEY))

def test_maybe_link(self):
Expand All @@ -36,7 +36,7 @@ def test_maybe_link(self):
logs.MAX_LOG_AGE = timedelta(days=99999)

self.assertEqual(
f'<a class="bar" href="/log?start_time=172800&key={KEY_STR}&module=">{actual}</a>',
f'<a class="bar" href="/log?start_time=172800&key={KEY_STR}">{actual}</a>',
logs.maybe_link(when, KEY, time_class='foo', link_class='bar'))

def test_maybe_link_future(self):
Expand Down

0 comments on commit 6496775

Please sign in to comment.