Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[v2-10-test] Exclude Scarf Usage Data Collection in CI Environments (#44155) #44184

Merged
merged 1 commit into from
Nov 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading