Skip to content

Commit 6c7c21a

Browse files
Fix telemetry issues in wheel build and unit tests (#4591)
* Reorganize to make sure capture does not run * Cleanup * Mock telemetry object * Fix logic * Minor fixes for networking * temporary trigger * testing env * Remove requirement * Try again * Last attempt * Update wheel env * Cleanup * More cleanup * Final test for cleanup * Turn off wheel build * Make sure opt out is in all tests * Fix some coverage * Cover last line * style: pre-commit fixes --------- Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
1 parent e17b549 commit 6c7c21a

File tree

12 files changed

+86
-41
lines changed

12 files changed

+86
-41
lines changed

.github/workflows/benchmark_on_push.yml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,9 @@ concurrency:
88
group: ${{ github.workflow }}-${{ github.ref }}
99
cancel-in-progress: true
1010

11+
env:
12+
PYBAMM_DISABLE_TELEMETRY: "true"
13+
1114
jobs:
1215
benchmarks:
1316
runs-on: ubuntu-latest

.github/workflows/periodic_benchmarks.yml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,9 @@ on:
1515
# workflow manually
1616
workflow_dispatch:
1717

18+
env:
19+
PYBAMM_DISABLE_TELEMETRY: "true"
20+
1821
jobs:
1922
benchmarks:
2023
runs-on: ubuntu-latest

.github/workflows/publish_pypi.yml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ on:
1818

1919
# Set options available for all jobs that use cibuildwheel
2020
env:
21+
PYBAMM_DISABLE_TELEMETRY: "true"
2122
# Increase pip debugging output, equivalent to `pip -vv`
2223
CIBW_BUILD_VERBOSITY: 2
2324
# Disable build isolation to allow pre-installing build-time dependencies.
@@ -75,6 +76,7 @@ jobs:
7576
run: pipx run cibuildwheel --output-dir wheelhouse
7677
env:
7778
CIBW_ENVIRONMENT: >
79+
PYBAMM_DISABLE_TELEMETRY="true"
7880
PYBAMM_USE_VCPKG=ON
7981
VCPKG_ROOT_DIR=C:\vcpkg
8082
VCPKG_DEFAULT_TRIPLET=x64-windows-static-md
@@ -116,6 +118,8 @@ jobs:
116118
- name: Build wheels on Linux
117119
run: pipx run cibuildwheel --output-dir wheelhouse
118120
env:
121+
CIBW_ENVIRONMENT: >
122+
PYBAMM_DISABLE_TELEMETRY="true"
119123
CIBW_ARCHS_LINUX: x86_64
120124
CIBW_BEFORE_ALL_LINUX: >
121125
yum -y install openblas-devel lapack-devel &&
@@ -242,6 +246,8 @@ jobs:
242246
python scripts/install_KLU_Sundials.py
243247
python -m cibuildwheel --output-dir wheelhouse
244248
env:
249+
CIBW_ENVIRONMENT: >
250+
PYBAMM_DISABLE_TELEMETRY="true"
245251
# 10.13 for Intel (macos-13), 11.0 for Apple Silicon (macos-14 and macos-latest)
246252
MACOSX_DEPLOYMENT_TARGET: ${{ matrix.os == 'macos-14' && '11.0' || '10.13' }}
247253
CIBW_ARCHS_MACOS: auto

.github/workflows/run_benchmarks_over_history.yml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,10 @@ on:
1818
ncommits:
1919
description: "Number of commits to benchmark between commit_start and commit_end"
2020
default: "100"
21+
22+
env:
23+
PYBAMM_DISABLE_TELEMETRY: "true"
24+
2125
jobs:
2226
benchmarks:
2327
runs-on: ubuntu-latest

.github/workflows/run_periodic_tests.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ on:
1313
- cron: "0 3 * * *"
1414

1515
env:
16+
PYBAMM_DISABLE_TELEMETRY: "true"
1617
FORCE_COLOR: 3
1718
PYBAMM_IDAKLU_EXPR_CASADI: ON
1819
PYBAMM_IDAKLU_EXPR_IREE: ON

.github/workflows/test_on_push.yml

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ on:
55
pull_request:
66

77
env:
8+
PYBAMM_DISABLE_TELEMETRY: "true"
89
FORCE_COLOR: 3
910
PYBAMM_IDAKLU_EXPR_CASADI: ON
1011
PYBAMM_IDAKLU_EXPR_IREE: ON
@@ -36,7 +37,6 @@ jobs:
3637
pre-commit run -a
3738
3839
run_unit_integration_and_coverage_tests:
39-
needs: style
4040
runs-on: ${{ matrix.os }}
4141
strategy:
4242
fail-fast: false
@@ -132,7 +132,6 @@ jobs:
132132

133133
# Skips IDAKLU module compilation for speedups, which is already tested in other jobs.
134134
run_doctests:
135-
needs: style
136135
runs-on: ubuntu-latest
137136
strategy:
138137
fail-fast: false
@@ -177,7 +176,6 @@ jobs:
177176
run: python -m nox -s docs
178177

179178
run_example_tests:
180-
needs: style
181179
runs-on: ubuntu-latest
182180
strategy:
183181
fail-fast: false
@@ -233,7 +231,6 @@ jobs:
233231
run: python -m nox -s examples
234232

235233
run_scripts_tests:
236-
needs: style
237234
runs-on: ubuntu-latest
238235
strategy:
239236
fail-fast: false

.github/workflows/work_precision_sets.yml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,9 @@ on:
55
types: [published]
66
workflow_dispatch:
77

8+
env:
9+
PYBAMM_DISABLE_TELEMETRY: "true"
10+
811
jobs:
912
benchmarks_on_release:
1013
if: github.repository_owner == 'pybamm-team'

noxfile.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,6 @@ def set_iree_state():
4949
"IREE_INDEX_URL": os.getenv(
5050
"IREE_INDEX_URL", "https://iree.dev/pip-release-links.html"
5151
),
52-
"PYBAMM_DISABLE_TELEMETRY": "true",
5352
}
5453
VENV_DIR = Path("./venv").resolve()
5554

src/pybamm/config.py

Lines changed: 21 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,18 @@
88
import time
99

1010

11+
def check_env_opt_out():
12+
return os.getenv("PYBAMM_DISABLE_TELEMETRY", "false").lower() != "false"
13+
14+
15+
def check_opt_out():
16+
opt_out = check_env_opt_out()
17+
config = pybamm.config.read()
18+
if config:
19+
opt_out = opt_out or not config["enable_telemetry"]
20+
return opt_out
21+
22+
1123
def is_running_tests(): # pragma: no cover
1224
"""
1325
Detect if the code is being run as part of a test suite or building docs with Sphinx.
@@ -21,20 +33,18 @@ def is_running_tests(): # pragma: no cover
2133
):
2234
return True
2335

24-
# Check for GitHub Actions environment variable
25-
if "GITHUB_ACTIONS" in os.environ:
26-
return True
27-
2836
# Check for other common CI environment variables
29-
ci_env_vars = ["CI", "TRAVIS", "CIRCLECI", "JENKINS_URL", "GITLAB_CI"]
37+
ci_env_vars = [
38+
"GITHUB_ACTIONS",
39+
"CI",
40+
"TRAVIS",
41+
"CIRCLECI",
42+
"JENKINS_URL",
43+
"GITLAB_CI",
44+
]
3045
if any(var in os.environ for var in ci_env_vars):
3146
return True
3247

33-
# Check for common test runner names in command-line arguments
34-
test_runners = ["pytest", "unittest", "nose", "trial", "nox", "tox"]
35-
if any(runner in arg.lower() for arg in sys.argv for runner in test_runners):
36-
return True
37-
3848
# Check if building docs with Sphinx
3949
if any(mod == "sphinx" or mod.startswith("sphinx.") for mod in sys.modules):
4050
print(
@@ -110,7 +120,7 @@ def get_input(): # pragma: no cover
110120

111121

112122
def generate():
113-
if is_running_tests():
123+
if is_running_tests() or check_opt_out():
114124
return
115125

116126
# Check if the config file already exists

src/pybamm/telemetry.py

Lines changed: 27 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,38 +1,45 @@
11
from posthog import Posthog
2-
import os
32
import pybamm
43
import sys
54

6-
_posthog = Posthog(
7-
# this is the public, write only API key, so it's ok to include it here
8-
project_api_key="phc_bLZKBW03XjgiRhbWnPsnKPr0iw0z03fA6ZZYjxgW7ej",
9-
host="https://us.i.posthog.com",
10-
)
115

12-
_posthog.log.setLevel("CRITICAL")
6+
class MockTelemetry:
7+
def __init__(self):
8+
self.disabled = True
139

10+
@staticmethod
11+
def capture(**kwargs): # pragma: no cover
12+
pass
1413

15-
def disable():
16-
_posthog.disabled = True
14+
15+
if pybamm.config.check_opt_out():
16+
_posthog = MockTelemetry()
17+
else: # pragma: no cover
18+
_posthog = Posthog(
19+
# this is the public, write only API key, so it's ok to include it here
20+
project_api_key="phc_bLZKBW03XjgiRhbWnPsnKPr0iw0z03fA6ZZYjxgW7ej",
21+
host="https://us.i.posthog.com",
22+
)
23+
_posthog.log.setLevel("CRITICAL")
1724

1825

19-
_opt_out = os.getenv("PYBAMM_DISABLE_TELEMETRY", "false").lower()
20-
if _opt_out != "false": # pragma: no cover
21-
disable()
26+
def disable():
27+
_posthog.disabled = True
2228

2329

2430
def capture(event): # pragma: no cover
25-
# don't capture events in automated testing
2631
if pybamm.config.is_running_tests() or _posthog.disabled:
2732
return
2833

29-
properties = {
30-
"python_version": sys.version,
31-
"pybamm_version": pybamm.__version__,
32-
}
34+
if pybamm.config.check_opt_out():
35+
disable()
36+
return
3337

3438
config = pybamm.config.read()
3539
if config:
36-
if config["enable_telemetry"]:
37-
user_id = config["uuid"]
38-
_posthog.capture(user_id, event, properties=properties)
40+
properties = {
41+
"python_version": sys.version,
42+
"pybamm_version": pybamm.__version__,
43+
}
44+
user_id = config["uuid"]
45+
_posthog.capture(distinct_id=user_id, event=event, properties=properties)

0 commit comments

Comments
 (0)