Skip to content

Commit

Permalink
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 0 deletions.
25 changes: 25 additions & 0 deletions airflow/utils/usage_data_collection.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@

from __future__ import annotations

import os
import platform
from urllib.parse import urlencode

Expand All @@ -43,6 +44,10 @@ def usage_data_collection():
if _version_is_prerelease(airflow_version):
return

# Exclude CI environments
if _is_ci_environ():
return

scarf_domain = "https://apacheairflow.gateway.scarf.sh/scheduler"

try:
Expand Down Expand Up @@ -70,6 +75,26 @@ def _version_is_prerelease(version: str) -> bool:
return parse(version).is_prerelease


def _is_ci_environ() -> bool:
"""Return True if running in any known CI environment."""
if os.getenv("CI") == "true":
# Generic CI variable set by many CI systems (GH Actions, Travis, GitLab, CircleCI, Jenkins, Heroku)
return True

# Other CI variables set by specific CI systems
ci_env_vars = {
"CIRCLECI", # CircleCI
"CODEBUILD_BUILD_ID", # AWS CodeBuild
"GITHUB_ACTIONS", # GitHub Actions
"GITLAB_CI", # GitLab CI
"JENKINS_URL", # Jenkins
"TF_BUILD", # Azure Pipelines
"TRAVIS", # Travis CI
}

return any(var in os.environ for var in ci_env_vars)


def get_platform_info() -> tuple[str, str]:
return platform.system(), platform.machine()

Expand Down
2 changes: 2 additions & 0 deletions tests/utils/test_usage_data_collection.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,12 +43,14 @@ def test_scarf_analytics_disabled(mock_get, is_enabled, is_prerelease):

@mock.patch("airflow.settings.is_usage_data_collection_enabled", return_value=True)
@mock.patch("airflow.utils.usage_data_collection._version_is_prerelease", return_value=False)
@mock.patch("airflow.utils.usage_data_collection._is_ci_environ", return_value=False)
@mock.patch("airflow.utils.usage_data_collection.get_database_version", return_value="12.3")
@mock.patch("airflow.utils.usage_data_collection.get_database_name", return_value="postgres")
@mock.patch("httpx.get")
def test_scarf_analytics(
mock_get,
mock_is_usage_data_collection_enabled,
mock_version_is_ci,
mock_version_is_prerelease,
get_database_version,
get_database_name,
Expand Down

0 comments on commit 347a83a

Please sign in to comment.