Skip to content

Commit eb6ee49

Browse files
authored
Emit metrics for how the Python version was chosen (#1069)
Currently an app's Python version can be set via a few different means: - explicitly by the user (via `runtime.txt` or `Pipfile.lock`) - implicitly via the sticky versions feature (for existing apps) - implicitly via default version for new apps / those with empty cache In order to determine the priority of features like automatic Python patch version upgrades for sticky-versioned apps, it's useful to have metrics for these. There were previously no tests for either the sticky versions feature, or changing the Python version by updating the `runtime.txt` file, so I've added some now (given that I updated the conditional to add the metrics, so useful to have coverage). I've also removed the confusing overwrite of `DEFAULT_PYTHON_VERSION` with the cached version, and kept them as two separate variables. Closes @W-8099632@. Closes @W-8099645@.
1 parent 64abfb2 commit eb6ee49

File tree

4 files changed

+34
-4
lines changed

4 files changed

+34
-4
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
## Unreleased
44

5+
- Emit metrics for how the Python version was chosen for an app (#1069).
56
- Emit Python version metric events for all builds, not just clean installs (#1066).
67

78
## v178 (2020-09-07)

bin/compile

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -189,7 +189,7 @@ source "$BIN_DIR/steps/hooks/pre_compile"
189189
# continue to use that version of Python in perpituity (warnings will be raised if
190190
# they are out–of–date).
191191
if [ -f "$CACHE_DIR/.heroku/python-version" ]; then
192-
DEFAULT_PYTHON_VERSION=$(cat "$CACHE_DIR/.heroku/python-version")
192+
CACHED_PYTHON_VERSION=$(cat "$CACHE_DIR/.heroku/python-version")
193193
fi
194194

195195
# We didn't always record the stack version. This code is in place because of that.
@@ -206,9 +206,14 @@ fi
206206
# shellcheck source=bin/steps/pipenv-python-version
207207
source "$BIN_DIR/steps/pipenv-python-version"
208208

209-
# If no runtime was provided by the user, assume the default Python runtime version.
210-
if [ ! -f runtime.txt ]; then
211-
echo "$DEFAULT_PYTHON_VERSION" > runtime.txt
209+
if [[ -f runtime.txt ]]; then
210+
mcount "version.reason.python.specified"
211+
elif [[ -n "${CACHED_PYTHON_VERSION:-}" ]]; then
212+
mcount "version.reason.python.cached"
213+
echo "${CACHED_PYTHON_VERSION}" > runtime.txt
214+
else
215+
mcount "version.reason.python.default"
216+
echo "${DEFAULT_PYTHON_VERSION}" > runtime.txt
212217
fi
213218

214219
# Create the directory for .profile.d, if it doesn't exist.

test/fixtures/no-runtime-txt/requirements.txt

Whitespace-only changes.

test/run-versions

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -214,6 +214,30 @@ testPypy2_7_warn() {
214214
fi
215215
}
216216

217+
testStickyPythonVersion() {
218+
local cache_dir="$(mktmpdir)"
219+
compile "python3_6_warn" "$cache_dir"
220+
assertCaptured "Installing python-3.6.7"
221+
assertCapturedSuccess
222+
compile "no-runtime-txt" "$cache_dir"
223+
assertCaptured "Installing python-3.6.7"
224+
assertCapturedSuccess
225+
# Whilst this file seems like an implementation detail (so something that should
226+
# not be tested), we must guarantee the filename remains consistent for backwards
227+
# compatibility across buildpack versions for already-built apps.
228+
assertFile "python-3.6.7" ".heroku/python-version"
229+
}
230+
231+
testPythonVersionChange() {
232+
local cache_dir="$(mktmpdir)"
233+
compile "python3_6_warn" "$cache_dir"
234+
assertCaptured "Installing python-3.6.7"
235+
assertCapturedSuccess
236+
compile "python3_6" "$cache_dir"
237+
assertCaptured "Found python-3.6.7, removing"
238+
assertCapturedSuccess
239+
}
240+
217241
pushd $(dirname 0) >/dev/null
218242
popd >/dev/null
219243

0 commit comments

Comments
 (0)