Merge ~cjwatson/launchpad:logging-context-oops into launchpad:master

Proposed by Colin Watson
Status: Merged
Approved by: Colin Watson
Approved revision: 3729a7eca89722612aa683ee01334989f8cc5706
Merge reported by: Otto Co-Pilot
Merged at revision: not available
Proposed branch: ~cjwatson/launchpad:logging-context-oops
Merge into: launchpad:master
Diff against target: 86 lines (+10/-1)
2 files modified
lib/lp/services/webapp/errorlog.py (+3/-0)
lib/lp/services/webapp/tests/test_errorlog.py (+7/-1)
Reviewer Review Type Date Requested Status
Ioana Lasc (community) Approve
Review via email: mp+407602@code.launchpad.net

Commit message

Add any OOPS ID to the Talisker logging context

Description of the change

This is helpful when trying to track down problems in logs.

To post a comment you must log in.
Revision history for this message
Ioana Lasc (ilasc) :
review: Approve

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1diff --git a/lib/lp/services/webapp/errorlog.py b/lib/lp/services/webapp/errorlog.py
2index ed25ace..35d83d2 100644
3--- a/lib/lp/services/webapp/errorlog.py
4+++ b/lib/lp/services/webapp/errorlog.py
5@@ -21,6 +21,7 @@ import oops_timeline
6 import pytz
7 import six
8 from six.moves.urllib.parse import urlparse
9+from talisker.logs import logging_context
10 from zope.component import getUtility
11 from zope.component.interfaces import ObjectEvent
12 from zope.error.interfaces import IErrorReportingUtility
13@@ -174,6 +175,7 @@ def attach_http_request(report, context):
14 info[1], '__lazr_webservice_error__', 500)
15 if webservice_error / 100 != 5:
16 request.oopsid = None
17+ logging_context.push(oopsid=None)
18 # Tell the oops machinery to ignore this error
19 report['ignore'] = True
20
21@@ -383,6 +385,7 @@ class ErrorReportingUtility:
22 if request:
23 request.oopsid = report.get('id')
24 request.oops = report
25+ logging_context.push(oopsid=report.get('id'))
26 return report
27
28 def _filter_bad_urls_by_referer(self, report):
29diff --git a/lib/lp/services/webapp/tests/test_errorlog.py b/lib/lp/services/webapp/tests/test_errorlog.py
30index b194a96..8f42df3 100644
31--- a/lib/lp/services/webapp/tests/test_errorlog.py
32+++ b/lib/lp/services/webapp/tests/test_errorlog.py
33@@ -1,4 +1,4 @@
34-# Copyright 2009-2019 Canonical Ltd. This software is licensed under the
35+# Copyright 2009-2021 Canonical Ltd. This software is licensed under the
36 # GNU Affero General Public License version 3 (see the file LICENSE).
37
38 """Tests for error logging & OOPS reporting."""
39@@ -16,6 +16,7 @@ import oops_amqp
40 import pytz
41 import six
42 from six.moves import http_client
43+from talisker.logs import logging_context
44 import testtools
45 from timeline.timeline import Timeline
46 from zope.interface import directlyProvides
47@@ -115,6 +116,7 @@ class TestErrorReportingUtility(TestCaseWithFactory):
48 report = utility.raising(sys.exc_info(), request)
49
50 self.assertFalse('last_oops' in report)
51+ self.assertEqual(report['id'], logging_context.flat['oopsid'])
52 last_oopsid = request.oopsid
53 try:
54 raise ArbitraryException('foo')
55@@ -123,6 +125,7 @@ class TestErrorReportingUtility(TestCaseWithFactory):
56
57 self.assertTrue('last_oops' in report)
58 self.assertEqual(report['last_oops'], last_oopsid)
59+ self.assertEqual(report['id'], logging_context.flat['oopsid'])
60
61 def test_raising_with_request(self):
62 """Test ErrorReportingUtility.raising() with a request"""
63@@ -167,6 +170,7 @@ class TestErrorReportingUtility(TestCaseWithFactory):
64 # verify that the oopsid was set on the request
65 self.assertEqual(request.oopsid, report['id'])
66 self.assertEqual(request.oops, report)
67+ self.assertEqual(report['id'], logging_context.flat['oopsid'])
68
69 def test_raising_request_with_principal_person(self):
70 utility = ErrorReportingUtility()
71@@ -187,6 +191,7 @@ class TestErrorReportingUtility(TestCaseWithFactory):
72 self.assertEqual(
73 u'my-username, 42, account-name, description |\u25a0|',
74 report['username'])
75+ self.assertEqual(report['id'], logging_context.flat['oopsid'])
76
77 def test_raising_request_with_principal_person_set_to_none(self):
78 """
79@@ -211,6 +216,7 @@ class TestErrorReportingUtility(TestCaseWithFactory):
80 self.assertEqual(
81 u'account-name, 42, account-name, description |\u25a0|',
82 report['username'])
83+ self.assertEqual(report['id'], logging_context.flat['oopsid'])
84
85 def test_raising_with_xmlrpc_request(self):
86 # Test ErrorReportingUtility.raising() with an XML-RPC request.

Subscribers

People subscribed via source and target branches

to status/vote changes: