Skip to content

Commit 9c85f80

Browse files
committed
Enable Sentry's "releases" feature
The releases feature is useful in itself, and this should also stop the "discarded session update because of missing release" messages that `sentry_sdk` is logging all the time.
1 parent b5798ee commit 9c85f80

3 files changed

Lines changed: 86 additions & 3 deletions

File tree

.coveragerc

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ concurrency = gevent
55
source =
66
viahtml
77
tests/unit
8+
omit =
9+
viahtml/_version.py
810

911
[report]
1012
show_missing = True

viahtml/_version.py

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
import datetime
2+
import subprocess
3+
from subprocess import DEVNULL
4+
5+
# git-archive substitution markers. When this file is written out by a `git
6+
# archive` command, these will be replaced by the short commit hash and the
7+
# commit date, respectively.
8+
VERSION_GIT_REF = "$Format:%h$"
9+
VERSION_GIT_DATE = "$Format:%ct$"
10+
11+
# Fallback version in case we cannot derive the version.
12+
VERSION_UNKNOWN = "0+unknown"
13+
14+
15+
def fetch_git_ref():
16+
return subprocess.check_output( # noqa: S603
17+
["git", "rev-parse", "--short", "HEAD"], # noqa: S607
18+
stderr=DEVNULL,
19+
).strip()
20+
21+
22+
def fetch_git_date(ref):
23+
output = subprocess.check_output(
24+
["git", "show", "-s", "--format=%ct", ref]
25+
) # noqa: S603, S607
26+
return datetime.datetime.fromtimestamp(int(output)) # noqa: DTZ006
27+
28+
29+
def fetch_git_dirty():
30+
# Ensure git index is up-to-date first. This usually isn't necessary, but
31+
# can be needed inside a docker container where the index is out of date.
32+
subprocess.call(["git", "update-index", "-q", "--refresh"]) # noqa: S603, S607
33+
dirty_tree = bool(
34+
subprocess.call(["git", "diff-files", "--quiet"])
35+
) # noqa: S603, S607
36+
dirty_index = bool(
37+
subprocess.call(
38+
["git", "diff-index", "--quiet", "--cached", "HEAD"]
39+
) # noqa: S603, S607
40+
)
41+
return dirty_tree or dirty_index
42+
43+
44+
def git_version():
45+
ref = fetch_git_ref()
46+
date = fetch_git_date(ref)
47+
dirty = fetch_git_dirty()
48+
return pep440_version(date, ref, dirty)
49+
50+
51+
def git_archive_version(): # pragma: no cover
52+
ref = VERSION_GIT_REF
53+
date = datetime.datetime.fromtimestamp(int(VERSION_GIT_DATE)) # noqa: DTZ006
54+
return pep440_version(date, ref)
55+
56+
57+
def pep440_version(date, ref, dirty=False): # noqa: FBT002
58+
"""Build a PEP440-compliant version number from the passed information."""
59+
return f"{date.strftime('%Y%m%d')}+g{ref}{'.dirty' if dirty else ''}"
60+
61+
62+
def get_version(): # pragma: no cover
63+
"""Fetch the current application version."""
64+
# First we try to retrieve the current application version from git.
65+
try:
66+
return git_version()
67+
except subprocess.CalledProcessError:
68+
pass
69+
70+
# We are not in a git checkout or extracting the version from git failed,
71+
# so we attempt to read a version written into the header of this file by
72+
# `git archive`.
73+
if not VERSION_GIT_REF.startswith("$"):
74+
return git_archive_version()
75+
76+
# If neither of these strategies work, we fall back to VERSION_UNKNOWN.
77+
return VERSION_UNKNOWN

viahtml/wsgi.py

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,7 @@
33
import os
44
import signal
55

6-
# Our job here is to leave this `application` attribute laying around as
7-
# it's what uWSGI expects to find.
6+
from viahtml._version import get_version
87
from viahtml.app import Application
98

109

@@ -26,7 +25,12 @@ def _dump_stacks(_signal, frame): # pylint:disable=unused-argument, # pragma: n
2625
from sentry_sdk.integrations.wsgi import SentryWsgiMiddleware
2726

2827
# pylint: disable=redefined-variable-type
29-
sentry_sdk.init(dsn=os.environ["SENTRY_DSN"])
28+
sentry_sdk.init(
29+
dsn=os.environ["SENTRY_DSN"],
30+
# Enable Sentry's "Releases" feature, see:
31+
# https://docs.sentry.io/platforms/python/configuration/options/#release
32+
release=get_version(),
33+
)
3034
application = SentryWsgiMiddleware(application)
3135

3236
# Add a way to dump stacks so we can see what a uWSGI worker is doing if it

0 commit comments

Comments
 (0)