-
Notifications
You must be signed in to change notification settings - Fork 17.2k
Add Scarf based telemetry #39510
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
Merged
Merged
Add Scarf based telemetry #39510
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
4f50051
Add Scarf based telemetry
kaxil b0b9fba
Add Scarf based telemetry on Scheduler startup
kaxil 79db51a
Rename scarf_analytics to telemetry_collection
kaxil 5eb214f
Refactor to remove redundancy
kaxil 5b9072b
Fix tests
kaxil ecb3543
fixup! Fix tests
kaxil 982827f
fixup! fixup! Fix tests
kaxil File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
|
kaxil marked this conversation as resolved.
Outdated
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,89 @@ | ||
| # | ||
| # Licensed to the Apache Software Foundation (ASF) under one | ||
| # or more contributor license agreements. See the NOTICE file | ||
| # distributed with this work for additional information | ||
| # regarding copyright ownership. The ASF licenses this file | ||
| # to you under the Apache License, Version 2.0 (the | ||
| # "License"); you may not use this file except in compliance | ||
| # with the License. You may obtain a copy of the License at | ||
| # | ||
| # http://www.apache.org/licenses/LICENSE-2.0 | ||
| # | ||
| # Unless required by applicable law or agreed to in writing, | ||
| # software distributed under the License is distributed on an | ||
| # "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
| # KIND, either express or implied. See the License for the | ||
| # specific language governing permissions and limitations | ||
| # under the License. | ||
| from __future__ import annotations | ||
|
|
||
| import platform | ||
| from urllib.parse import urlencode | ||
|
|
||
| import httpx | ||
| from packaging.version import parse | ||
|
|
||
| from airflow import __version__ as airflow_version, settings | ||
| from airflow.configuration import conf | ||
|
|
||
|
|
||
| def scarf_analytics(): | ||
| if not settings.is_telemetry_collection_enabled(): | ||
| return | ||
|
|
||
| # Exclude pre-releases and dev versions | ||
| if _version_is_prerelease(airflow_version): | ||
| return | ||
|
|
||
| scarf_domain = "https://apacheairflow.gateway.scarf.sh/scheduler" | ||
|
kaxil marked this conversation as resolved.
Outdated
|
||
|
|
||
| try: | ||
| platform_sys, arch = get_platform_info() | ||
|
|
||
| params = { | ||
| "version": airflow_version, | ||
| "python_version": get_python_version(), | ||
| "platform": platform_sys, | ||
| "arch": arch, | ||
| "database": get_database_name(), | ||
| "db_version": get_database_version(), | ||
| "executor": get_executor(), | ||
| } | ||
|
|
||
| query_string = urlencode(params) | ||
| scarf_url = f"{scarf_domain}?{query_string}" | ||
|
|
||
| httpx.get(scarf_url, timeout=5.0) | ||
| except Exception: | ||
| pass | ||
|
|
||
|
|
||
| def _version_is_prerelease(version: str) -> bool: | ||
| return parse(version).is_prerelease | ||
|
|
||
|
|
||
| def get_platform_info() -> tuple[str, str]: | ||
| return platform.system(), platform.machine() | ||
|
|
||
|
|
||
| def get_database_version() -> str: | ||
| if settings.engine is None: | ||
| return "None" | ||
|
|
||
| version_info = settings.engine.dialect.server_version_info | ||
| # Example: (1, 2, 3) -> "1.2.3" | ||
| return ".".join(map(str, version_info)) if version_info else "None" | ||
|
|
||
|
|
||
| def get_database_name() -> str: | ||
|
kaxil marked this conversation as resolved.
Outdated
|
||
| if settings.engine is None: | ||
| return "None" | ||
| return settings.engine.dialect.name | ||
|
|
||
|
|
||
| def get_executor() -> str: | ||
| return conf.get("core", "EXECUTOR") | ||
|
|
||
|
|
||
| def get_python_version() -> str: | ||
| return platform.python_version() | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,84 @@ | ||
| # | ||
| # Licensed to the Apache Software Foundation (ASF) under one | ||
| # or more contributor license agreements. See the NOTICE file | ||
| # distributed with this work for additional information | ||
| # regarding copyright ownership. The ASF licenses this file | ||
| # to you under the Apache License, Version 2.0 (the | ||
| # "License"); you may not use this file except in compliance | ||
| # with the License. You may obtain a copy of the License at | ||
| # | ||
| # http://www.apache.org/licenses/LICENSE-2.0 | ||
| # | ||
| # Unless required by applicable law or agreed to in writing, | ||
| # software distributed under the License is distributed on an | ||
| # "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
| # KIND, either express or implied. See the License for the | ||
| # specific language governing permissions and limitations | ||
| # under the License. | ||
| from __future__ import annotations | ||
|
|
||
| import platform | ||
| from unittest import mock | ||
|
|
||
| import pytest | ||
|
|
||
| from airflow import __version__ as airflow_version | ||
| from airflow.configuration import conf | ||
| from airflow.utils.scarf import get_database_version, scarf_analytics | ||
|
|
||
|
|
||
| @pytest.mark.parametrize("is_enabled, is_prerelease", [(False, True), (True, True)]) | ||
| @mock.patch("httpx.get") | ||
| def test_scarf_analytics_disabled(mock_get, is_enabled, is_prerelease): | ||
| with mock.patch("airflow.settings.is_telemetry_collection_enabled", return_value=is_enabled), mock.patch( | ||
| "airflow.utils.scarf._version_is_prerelease", return_value=is_prerelease | ||
| ): | ||
| scarf_analytics() | ||
| mock_get.assert_not_called() | ||
|
|
||
|
|
||
| @mock.patch("airflow.settings.is_telemetry_collection_enabled", return_value=True) | ||
| @mock.patch("airflow.utils.scarf._version_is_prerelease", return_value=False) | ||
| @mock.patch("airflow.utils.scarf.get_database_version", return_value="12.3") | ||
| @mock.patch("airflow.utils.scarf.get_database_name", return_value="postgres") | ||
| @mock.patch("httpx.get") | ||
| def test_scarf_analytics( | ||
| mock_get, | ||
| mock_is_telemetry_collection_enabled, | ||
| mock_version_is_prerelease, | ||
| get_database_version, | ||
| get_database_name, | ||
| ): | ||
| platform_sys = platform.system() | ||
| platform_machine = platform.machine() | ||
| python_version = platform.python_version() | ||
| executor = conf.get("core", "EXECUTOR") | ||
| scarf_endpoint = "https://apacheairflow.gateway.scarf.sh/scheduler" | ||
| scarf_analytics() | ||
|
|
||
| expected_scarf_url = ( | ||
| f"{scarf_endpoint}?version={airflow_version}" | ||
| f"&python_version={python_version}" | ||
| f"&platform={platform_sys}" | ||
| f"&arch={platform_machine}" | ||
| f"&database=postgres" | ||
| f"&db_version=12.3" | ||
| f"&executor={executor}" | ||
| ) | ||
|
|
||
| mock_get.assert_called_once_with(expected_scarf_url, timeout=5.0) | ||
|
|
||
|
|
||
| @pytest.mark.db_test | ||
| @pytest.mark.parametrize( | ||
| "version_info, expected_version", | ||
| [ | ||
| ((1, 2, 3), "1.2.3"), # Normal version tuple | ||
| (None, "None"), # No version info available | ||
| ((1,), "1"), # Single element version tuple | ||
| ((1, 2, 3, "beta", 4), "1.2.3.beta.4"), # Complex version tuple with strings | ||
| ], | ||
| ) | ||
| def test_get_database_version(version_info, expected_version): | ||
| with mock.patch("airflow.settings.engine.dialect.server_version_info", new=version_info): | ||
| assert get_database_version() == expected_version |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.