Skip to content

Commit

Permalink
update plugin hook usage based on the latest changes in conda (#54)
Browse files Browse the repository at this point in the history
  • Loading branch information
travishathaway authored Nov 14, 2024
1 parent cfa26b5 commit 72a12ca
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 24 deletions.
18 changes: 3 additions & 15 deletions conda_anaconda_telemetry/hooks.py
Original file line number Diff line number Diff line change
Expand Up @@ -207,36 +207,28 @@ def _conda_request_headers():
HeaderWrapper(
header=CondaRequestHeader(
name=HEADER_SYS_INFO,
description="Custom headers used to submit telemetry data",
value=get_sys_info_header_value(),
hosts=REQUEST_HEADER_HOSTS,
),
size_limit=500,
),
HeaderWrapper(
header=CondaRequestHeader(
name=HEADER_CHANNELS,
description="Header which exposes the channel URLs currently in use",
value=get_channel_urls_header_value(),
hosts=REQUEST_HEADER_HOSTS,
),
size_limit=500,
),
HeaderWrapper(
header=CondaRequestHeader(
name=HEADER_VIRTUAL_PACKAGES,
description="Header which exposes the virtual packages currently in use",
value=get_virtual_packages_header_value(),
hosts=REQUEST_HEADER_HOSTS,
),
size_limit=500,
),
HeaderWrapper(
header=CondaRequestHeader(
name=HEADER_PACKAGES,
description="Header which exposes the currently installed packages",
value=get_installed_packages_header_value(),
hosts=REQUEST_HEADER_HOSTS,
),
size_limit=5_000,
),
Expand All @@ -249,9 +241,7 @@ def _conda_request_headers():
HeaderWrapper(
header=CondaRequestHeader(
name=HEADER_SEARCH,
description="Header which exposes what is being searched for",
value=get_search_term(),
hosts=REQUEST_HEADER_HOSTS,
),
size_limit=500,
)
Expand All @@ -262,10 +252,7 @@ def _conda_request_headers():
HeaderWrapper(
header=CondaRequestHeader(
name=HEADER_INSTALL,
description="Header which exposes what is currently being installed as "
"specified on the command line",
value=get_install_arguments_header_value(),
hosts=REQUEST_HEADER_HOSTS,
),
size_limit=500,
)
Expand All @@ -275,9 +262,10 @@ def _conda_request_headers():


@hookimpl
def conda_request_headers():
def conda_session_headers(host: str):
try:
yield from _conda_request_headers()
if host in REQUEST_HEADER_HOSTS:
yield from _conda_request_headers()
except Exception as exc:
logger.debug("Failed to collect telemetry data", exc_info=exc)

Expand Down
30 changes: 21 additions & 9 deletions tests/test_hooks.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import pytest

from conda_anaconda_telemetry.hooks import (
conda_request_headers,
conda_session_headers,
conda_settings,
HEADER_INSTALL,
HEADER_CHANNELS,
Expand All @@ -14,6 +14,9 @@
timer,
)

#: Host used across all tests
TEST_HOST = "repo.anaconda.com"


@pytest.fixture(autouse=True)
def packages(mocker):
Expand Down Expand Up @@ -42,7 +45,9 @@ def test_conda_request_header_default_headers(mocker):
mocker.patch(
"conda_anaconda_telemetry.hooks.context._argparse_args", mock_argparse_args
)
headers = {header.name: header for header in tuple(conda_request_headers())}
headers = {
header.name: header for header in tuple(conda_session_headers(TEST_HOST))
}

expected_header_names_values = {
HEADER_SYS_INFO: "",
Expand All @@ -67,7 +72,7 @@ def test_conda_request_header_with_search(monkeypatch, mocker):
"conda_anaconda_telemetry.hooks.context._argparse_args", mock_argparse_args
)

header_names = {header.name for header in tuple(conda_request_headers())}
header_names = {header.name for header in tuple(conda_session_headers(TEST_HOST))}
expected_header_names = {
HEADER_SYS_INFO,
HEADER_CHANNELS,
Expand All @@ -91,7 +96,7 @@ def test_conda_request_header_with_install(monkeypatch, mocker):
"conda_anaconda_telemetry.hooks.context._argparse_args", mock_argparse_args
)

header_names = {header.name for header in tuple(conda_request_headers())}
header_names = {header.name for header in tuple(conda_session_headers(TEST_HOST))}
expected_header_names = {
HEADER_SYS_INFO,
HEADER_CHANNELS,
Expand All @@ -112,8 +117,7 @@ def test_conda_request_header_when_disabled(monkeypatch, mocker):
mocker.patch(
"conda_anaconda_telemetry.hooks.context.plugins.anaconda_telemetry", False
)

assert not tuple(conda_request_headers())
assert not tuple(conda_session_headers(TEST_HOST))


def test_timer_in_info_mode(caplog):
Expand Down Expand Up @@ -146,9 +150,9 @@ def test_conda_settings():
assert settings[0].parameter.default.value is True


def test_conda_request_headers_with_exception(mocker, caplog):
def test_conda_session_headers_with_exception(mocker, caplog):
"""
When any exception is encountered, ``conda_request_headers`` should return nothing
When any exception is encountered, ``conda_session_headers`` should return nothing
and log a debug message.
"""
caplog.set_level(logging.DEBUG)
Expand All @@ -157,7 +161,15 @@ def test_conda_request_headers_with_exception(mocker, caplog):
side_effect=Exception("Boom"),
)

assert list(conda_request_headers()) == []
assert list(conda_session_headers(TEST_HOST)) == []
assert caplog.records[0].levelname == "DEBUG"
assert "Failed to collect telemetry data" in caplog.text
assert "Exception: Boom" in caplog.text


def test_conda_session_headers_with_non_matching_url(mocker, caplog):
"""
When any exception is encountered, ``conda_session_headers`` should return nothing
and log a debug message.
"""
assert list(conda_session_headers("https://example.com")) == []

0 comments on commit 72a12ca

Please sign in to comment.