Skip to content

Commit 91493e5

Browse files
committed
feat(osf): ENG-11735 record download events for files and zips
1 parent e0c8dd0 commit 91493e5

2 files changed

Lines changed: 46 additions & 2 deletions

File tree

osf/utils/download_telemetry.py

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,10 @@
1111
# about where they actually are.
1212
UNSET_USER_TIMEZONE = 'Etc/UTC'
1313

14+
# Identifiers worth having in the log line to track a failure back to one download.
15+
# Deliberately excludes the IP.
16+
LOGGED_CONTEXT_KEYS = ('download_type', 'resource_guid', 'file_id', 'user_guid')
17+
1418

1519
def never_breaks_downloads(fn):
1620
"""Swallow and log anything this raises.
@@ -22,8 +26,20 @@ def never_breaks_downloads(fn):
2226
def wrapped(*args, **kwargs):
2327
try:
2428
return fn(*args, **kwargs)
25-
except Exception:
26-
logger.exception('Failed to record a download event')
29+
except Exception as exc:
30+
# exc_info carries the traceback; the rest names the failure and which
31+
# download it was, so a report is actionable without reproducing it.
32+
logger.exception(
33+
'Failed to record a download event in %s: %s: %s [%s]',
34+
fn.__name__,
35+
type(exc).__name__,
36+
exc,
37+
', '.join(
38+
f'{key}={kwargs[key]!r}'
39+
for key in LOGGED_CONTEXT_KEYS
40+
if kwargs.get(key)
41+
) or 'no context',
42+
)
2743
return wrapped
2844

2945

tests/test_download_telemetry.py

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import logging
12
import time
23

34
import pytest
@@ -208,3 +209,30 @@ def explode(*args, **kwargs):
208209
record_download(download_type=DownloadEvent.FILE, resource_guid='abcde')
209210

210211
assert not DownloadEvent.objects.exists()
212+
213+
def test_failure_is_logged_with_the_cause_and_the_download(self, monkeypatch, caplog):
214+
"""A report has to be actionable without reproducing it."""
215+
def explode(*args, **kwargs):
216+
raise ValueError('boom')
217+
218+
monkeypatch.setattr('osf.utils.download_telemetry.enqueue_postcommit_task', explode)
219+
220+
with caplog.at_level(logging.ERROR, logger='osf.utils.download_telemetry'):
221+
record_download(
222+
download_type=DownloadEvent.FILE,
223+
resource_guid='abcde',
224+
user_guid='zyxwv',
225+
ip='198.51.100.7',
226+
)
227+
228+
record = caplog.records[0]
229+
message = record.getMessage()
230+
assert 'ValueError' in message
231+
assert 'boom' in message
232+
assert 'record_download' in message
233+
assert "resource_guid='abcde'" in message
234+
assert "user_guid='zyxwv'" in message
235+
# the IP is not debugging information
236+
assert '198.51.100.7' not in message
237+
# the traceback is still attached
238+
assert record.exc_info is not None

0 commit comments

Comments
 (0)