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

[ATO-1152] Port of Add license hashes to telemetry #12567

Merged
merged 11 commits into from
Jun 28, 2023
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
1 change: 1 addition & 0 deletions changelog/12567.misc.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Add plugin hook for getting the license hash from Rasa Plus and add the hash to telemetry context.
6 changes: 5 additions & 1 deletion docs/docs/telemetry/telemetry.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,8 @@ Specifically, we collect the following information for all telemetry events:
- General OS level information (operating system, number of CPUs, number of
GPUs and whether the command is run inside a CI)
- Current Rasa and Python version
- Whether the command is run inside a Docker container
- Hash of the license (if you are using Rasa Pro)

Here is an example report that shows the data reported to Rasa after running
`rasa train`:
Expand Down Expand Up @@ -117,7 +119,9 @@ Here is an example report that shows the data reported to Rasa after running
"project": "a0a7178e6e5f9e6484c5cfa3ea4497ffc0c96d0ad3f3ad8e9399a1edd88e3cf4",
"python": "3.7.5",
"rasa_open_source": "2.0.0",
"cpu": 16
"cpu": 16,
"docker": false,
"license_hash": "t1a7170e6e5f9e6484c5cfa3ea4497ffc0c96a0ad3f3ad8e9399adadd88e3cf5"
}
}
```
Expand Down
5 changes: 5 additions & 0 deletions rasa/plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,3 +52,8 @@ def configure_commandline(cmdline_arguments: argparse.Namespace) -> Optional[Tex
@hookspec # type: ignore[misc]
def init_telemetry(endpoints_file: Optional[Text]) -> None:
"""Hook specification for initialising plugin telemetry."""


@hookspec(firstresult=True) # type: ignore[misc]
def get_license_hash() -> Optional[Text]:
"""Hook specification for getting the license hash."""
4 changes: 4 additions & 0 deletions rasa/telemetry.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
CONFIG_TELEMETRY_ID,
)
from rasa.engine.storage.local_model_storage import LocalModelStorage
from rasa.plugin import plugin_manager
from rasa.shared.constants import DOCS_URL_TELEMETRY
from rasa.shared.exceptions import RasaException
import rasa.shared.utils.io
Expand Down Expand Up @@ -490,6 +491,9 @@ def _default_context_fields() -> Dict[Text, Any]:
"cpu": multiprocessing.cpu_count(),
"docker": _is_docker(),
}
license_hash = plugin_manager().hook.get_license_hash()
if license_hash:
TELEMETRY_CONTEXT["license_hash"] = license_hash

# avoid returning the cached dict --> caller could modify the dictionary...
# usually we would use `lru_cache`, but that doesn't return a dict copy and
Expand Down
36 changes: 35 additions & 1 deletion tests/test_telemetry.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

from _pytest.monkeypatch import MonkeyPatch
import jsonschema
from unittest.mock import Mock
from unittest.mock import MagicMock, Mock
import pytest
import responses

Expand Down Expand Up @@ -34,6 +34,15 @@ def patch_global_config_path(tmp_path: Path) -> Generator[None, None, None]:
rasa.constants.GLOBAL_USER_CONFIG_PATH = default_location


@pytest.fixture(autouse=True)
def patch_telemetry_context() -> Generator[None, None, None]:
"""Use a telemetry context for each test to avoid tests influencing each other."""
defaut_context = telemetry.TELEMETRY_CONTEXT
telemetry.TELEMETRY_CONTEXT = None
yield
telemetry.TELEMETRY_CONTEXT = defaut_context


async def test_events_schema(
monkeypatch: MonkeyPatch, default_agent: Agent, config_path: Text
):
Expand Down Expand Up @@ -485,3 +494,28 @@ def test_context_contains_os():
context.pop("os")

assert "os" in telemetry._default_context_fields()


def test_context_contains_license_hash(monkeypatch: MonkeyPatch) -> None:
mock = MagicMock()
mock.return_value.hook.get_license_hash.return_value = "1234567890"
monkeypatch.setattr("rasa.telemetry.plugin_manager", mock)
context = telemetry._default_context_fields()

assert "license_hash" in context
assert mock.return_value.hook.get_license_hash.called
assert context["license_hash"] == "1234567890"

# make sure it is still there after removing it
context.pop("license_hash")
assert "license_hash" in telemetry._default_context_fields()


def test_context_does_not_contain_license_hash(monkeypatch: MonkeyPatch) -> None:
mock = MagicMock()
mock.return_value.hook.get_license_hash.return_value = None
monkeypatch.setattr("rasa.telemetry.plugin_manager", mock)
context = telemetry._default_context_fields()

assert "license_hash" not in context
assert mock.return_value.hook.get_license_hash.called