From e7e98a421cab5fe3e7c9d6b3095bcd13e994f29a Mon Sep 17 00:00:00 2001 From: Uros Milovanovic Date: Thu, 15 Jun 2023 13:06:50 +0200 Subject: [PATCH 01/10] init --- rasa/plugin.py | 5 +++++ rasa/telemetry.py | 2 ++ 2 files changed, 7 insertions(+) diff --git a/rasa/plugin.py b/rasa/plugin.py index e15d04d173d5..c657c69ced0c 100644 --- a/rasa/plugin.py +++ b/rasa/plugin.py @@ -143,3 +143,8 @@ def init_anonymization_pipeline(endpoints_file: Optional[Text]) -> None: @hookspec(firstresult=True) # type: ignore[misc] def get_anonymization_pipeline() -> Optional[Any]: """Hook specification for getting the anonymization pipeline.""" + + +@hookspec # type: ignore[misc] +def get_license_hash() -> Optional[Text]: + """Hook specification for getting the license hash.""" diff --git a/rasa/telemetry.py b/rasa/telemetry.py index d0f6980b6738..c0526fba111f 100644 --- a/rasa/telemetry.py +++ b/rasa/telemetry.py @@ -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 @@ -488,6 +489,7 @@ def _default_context_fields() -> Dict[Text, Any]: "python": sys.version.split(" ")[0], "rasa_open_source": rasa.__version__, "cpu": multiprocessing.cpu_count(), + "license_hash": plugin_manager().hook.get_license_hash(), "docker": _is_docker(), } From d9c759cd44ccbb0299275fbfaac10e583e4b52e1 Mon Sep 17 00:00:00 2001 From: Uros Milovanovic Date: Thu, 15 Jun 2023 13:50:29 +0200 Subject: [PATCH 02/10] edit docs --- docs/docs/telemetry/telemetry.mdx | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/docs/docs/telemetry/telemetry.mdx b/docs/docs/telemetry/telemetry.mdx index 44cbbaaf2b30..6ce9f71bca3e 100644 --- a/docs/docs/telemetry/telemetry.mdx +++ b/docs/docs/telemetry/telemetry.mdx @@ -81,6 +81,7 @@ 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 +- 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`: @@ -117,7 +118,8 @@ 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, + "license_hash": "a0a7178e6e5f9e6484c5cfa3ea4497ffc0c96a0ad3f3ad8e9399adadd88e3cf4" } } ``` From 8f11e4dd73505b1c62ba240c48b0c48216fceb4e Mon Sep 17 00:00:00 2001 From: Uros Milovanovic Date: Thu, 15 Jun 2023 13:52:08 +0200 Subject: [PATCH 03/10] edit hash --- docs/docs/telemetry/telemetry.mdx | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/docs/telemetry/telemetry.mdx b/docs/docs/telemetry/telemetry.mdx index 6ce9f71bca3e..47ae7743314e 100644 --- a/docs/docs/telemetry/telemetry.mdx +++ b/docs/docs/telemetry/telemetry.mdx @@ -119,7 +119,7 @@ Here is an example report that shows the data reported to Rasa after running "python": "3.7.5", "rasa_open_source": "2.0.0", "cpu": 16, - "license_hash": "a0a7178e6e5f9e6484c5cfa3ea4497ffc0c96a0ad3f3ad8e9399adadd88e3cf4" + "license_hash": "t1a7170e6e5f9e6484c5cfa3ea4497ffc0c96a0ad3f3ad8e9399adadd88e3cf5" } } ``` From d2cd5b6ae2735f94e671b879457bf97d7fbe0ee4 Mon Sep 17 00:00:00 2001 From: Uros Milovanovic Date: Thu, 15 Jun 2023 14:18:26 +0200 Subject: [PATCH 04/10] add changelog and test --- changelog/12512.misc.md | 1 + tests/test_telemetry.py | 10 ++++++++++ 2 files changed, 11 insertions(+) create mode 100644 changelog/12512.misc.md diff --git a/changelog/12512.misc.md b/changelog/12512.misc.md new file mode 100644 index 000000000000..f7b599c806d4 --- /dev/null +++ b/changelog/12512.misc.md @@ -0,0 +1 @@ +Add plugin hook for getting the license hash from Rasa Plus and add the hash to telemetry context. diff --git a/tests/test_telemetry.py b/tests/test_telemetry.py index 272497665de7..16d2e81b0994 100644 --- a/tests/test_telemetry.py +++ b/tests/test_telemetry.py @@ -485,3 +485,13 @@ def test_context_contains_os(): context.pop("os") assert "os" in telemetry._default_context_fields() + + +def test_context_contains_license_hash(): + context = telemetry._default_context_fields() + + assert "license_hash" in context + + context.pop("license_hash") + + assert "license_hash" in telemetry._default_context_fields() From 85ebc654e277b839e8caa992f8efdb810cd403f3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Uro=C5=A1=20Milovanovi=C4=87?= Date: Fri, 16 Jun 2023 14:36:15 +0200 Subject: [PATCH 05/10] Update rasa/plugin.py Co-authored-by: Anca Lita <27920906+ancalita@users.noreply.github.com> --- rasa/plugin.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/rasa/plugin.py b/rasa/plugin.py index c657c69ced0c..75ef613739e1 100644 --- a/rasa/plugin.py +++ b/rasa/plugin.py @@ -145,6 +145,6 @@ def get_anonymization_pipeline() -> Optional[Any]: """Hook specification for getting the anonymization pipeline.""" -@hookspec # type: ignore[misc] +@hookspec(firstresult=True) # type: ignore[misc] def get_license_hash() -> Optional[Text]: """Hook specification for getting the license hash.""" From be97d5caab3dacab1cc5ef895488f56035c1b021 Mon Sep 17 00:00:00 2001 From: Uros Milovanovic Date: Fri, 16 Jun 2023 16:03:11 +0200 Subject: [PATCH 06/10] Address review --- docs/docs/telemetry/telemetry.mdx | 2 ++ rasa/telemetry.py | 4 +++- tests/test_telemetry.py | 21 ++++++++++++++++++--- 3 files changed, 23 insertions(+), 4 deletions(-) diff --git a/docs/docs/telemetry/telemetry.mdx b/docs/docs/telemetry/telemetry.mdx index 47ae7743314e..6d5c9ac129e3 100644 --- a/docs/docs/telemetry/telemetry.mdx +++ b/docs/docs/telemetry/telemetry.mdx @@ -81,6 +81,7 @@ 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 @@ -119,6 +120,7 @@ Here is an example report that shows the data reported to Rasa after running "python": "3.7.5", "rasa_open_source": "2.0.0", "cpu": 16, + "docker": false, "license_hash": "t1a7170e6e5f9e6484c5cfa3ea4497ffc0c96a0ad3f3ad8e9399adadd88e3cf5" } } diff --git a/rasa/telemetry.py b/rasa/telemetry.py index c0526fba111f..700f483522fd 100644 --- a/rasa/telemetry.py +++ b/rasa/telemetry.py @@ -489,9 +489,11 @@ def _default_context_fields() -> Dict[Text, Any]: "python": sys.version.split(" ")[0], "rasa_open_source": rasa.__version__, "cpu": multiprocessing.cpu_count(), - "license_hash": plugin_manager().hook.get_license_hash(), "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 diff --git a/tests/test_telemetry.py b/tests/test_telemetry.py index 16d2e81b0994..4a66b160ab43 100644 --- a/tests/test_telemetry.py +++ b/tests/test_telemetry.py @@ -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 @@ -487,11 +487,26 @@ def test_context_contains_os(): assert "os" in telemetry._default_context_fields() -def test_context_contains_license_hash(): +def test_context_contains_license_hash(monkeypatch: MonkeyPatch): + 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): + 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 From 67ab9d6cc74218b83604dc2d08a9738b105c9dd6 Mon Sep 17 00:00:00 2001 From: Uros Milovanovic Date: Mon, 19 Jun 2023 09:57:40 +0200 Subject: [PATCH 07/10] fix tests --- rasa/telemetry.py | 1 + tests/test_telemetry.py | 2 ++ 2 files changed, 3 insertions(+) diff --git a/rasa/telemetry.py b/rasa/telemetry.py index 700f483522fd..6eb3367aad37 100644 --- a/rasa/telemetry.py +++ b/rasa/telemetry.py @@ -492,6 +492,7 @@ def _default_context_fields() -> Dict[Text, Any]: "docker": _is_docker(), } license_hash = plugin_manager().hook.get_license_hash() + print("LLLLLLLLLLLLLLL",license_hash) if license_hash: TELEMETRY_CONTEXT["license_hash"] = license_hash diff --git a/tests/test_telemetry.py b/tests/test_telemetry.py index 4a66b160ab43..686f692b6f9e 100644 --- a/tests/test_telemetry.py +++ b/tests/test_telemetry.py @@ -489,6 +489,7 @@ def test_context_contains_os(): def test_context_contains_license_hash(monkeypatch: MonkeyPatch): mock = MagicMock() + telemetry.TELEMETRY_CONTEXT = None # make sure we don't use the cached value mock.return_value.hook.get_license_hash.return_value = "1234567890" monkeypatch.setattr("rasa.telemetry.plugin_manager", mock) context = telemetry._default_context_fields() @@ -504,6 +505,7 @@ def test_context_contains_license_hash(monkeypatch: MonkeyPatch): def test_context_does_not_contain_license_hash(monkeypatch: MonkeyPatch): mock = MagicMock() + telemetry.TELEMETRY_CONTEXT = None # make sure we don't use the cached value mock.return_value.hook.get_license_hash.return_value = None monkeypatch.setattr("rasa.telemetry.plugin_manager", mock) context = telemetry._default_context_fields() From 393cfa9e0eaf9da1fb41492b10470ca41ad16661 Mon Sep 17 00:00:00 2001 From: Uros Milovanovic Date: Mon, 19 Jun 2023 10:23:22 +0200 Subject: [PATCH 08/10] fix tests --- rasa/telemetry.py | 1 - tests/test_telemetry.py | 4 ++-- 2 files changed, 2 insertions(+), 3 deletions(-) diff --git a/rasa/telemetry.py b/rasa/telemetry.py index 6eb3367aad37..700f483522fd 100644 --- a/rasa/telemetry.py +++ b/rasa/telemetry.py @@ -492,7 +492,6 @@ def _default_context_fields() -> Dict[Text, Any]: "docker": _is_docker(), } license_hash = plugin_manager().hook.get_license_hash() - print("LLLLLLLLLLLLLLL",license_hash) if license_hash: TELEMETRY_CONTEXT["license_hash"] = license_hash diff --git a/tests/test_telemetry.py b/tests/test_telemetry.py index 686f692b6f9e..5d9727d803eb 100644 --- a/tests/test_telemetry.py +++ b/tests/test_telemetry.py @@ -487,7 +487,7 @@ def test_context_contains_os(): assert "os" in telemetry._default_context_fields() -def test_context_contains_license_hash(monkeypatch: MonkeyPatch): +def test_context_contains_license_hash(monkeypatch: MonkeyPatch) -> None: mock = MagicMock() telemetry.TELEMETRY_CONTEXT = None # make sure we don't use the cached value mock.return_value.hook.get_license_hash.return_value = "1234567890" @@ -503,7 +503,7 @@ def test_context_contains_license_hash(monkeypatch: MonkeyPatch): assert "license_hash" in telemetry._default_context_fields() -def test_context_does_not_contain_license_hash(monkeypatch: MonkeyPatch): +def test_context_does_not_contain_license_hash(monkeypatch: MonkeyPatch) -> None: mock = MagicMock() telemetry.TELEMETRY_CONTEXT = None # make sure we don't use the cached value mock.return_value.hook.get_license_hash.return_value = None From 2704dace910bec3c3cfe2389809a4f9a1cc79a3d Mon Sep 17 00:00:00 2001 From: Uros Milovanovic Date: Mon, 19 Jun 2023 10:52:51 +0200 Subject: [PATCH 09/10] fix context reuse --- tests/test_telemetry.py | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/tests/test_telemetry.py b/tests/test_telemetry.py index 5d9727d803eb..0c0447ae5a33 100644 --- a/tests/test_telemetry.py +++ b/tests/test_telemetry.py @@ -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 new 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 ): @@ -489,7 +498,6 @@ def test_context_contains_os(): def test_context_contains_license_hash(monkeypatch: MonkeyPatch) -> None: mock = MagicMock() - telemetry.TELEMETRY_CONTEXT = None # make sure we don't use the cached value mock.return_value.hook.get_license_hash.return_value = "1234567890" monkeypatch.setattr("rasa.telemetry.plugin_manager", mock) context = telemetry._default_context_fields() @@ -505,7 +513,6 @@ def test_context_contains_license_hash(monkeypatch: MonkeyPatch) -> None: def test_context_does_not_contain_license_hash(monkeypatch: MonkeyPatch) -> None: mock = MagicMock() - telemetry.TELEMETRY_CONTEXT = None # make sure we don't use the cached value mock.return_value.hook.get_license_hash.return_value = None monkeypatch.setattr("rasa.telemetry.plugin_manager", mock) context = telemetry._default_context_fields() From 818ee36a0fcd2a19f39a8e82f30aeba4d51c3dfd Mon Sep 17 00:00:00 2001 From: Uros Milovanovic Date: Tue, 20 Jun 2023 16:16:39 +0200 Subject: [PATCH 10/10] remove changelog --- changelog/12512.misc.md | 1 - 1 file changed, 1 deletion(-) delete mode 100644 changelog/12512.misc.md diff --git a/changelog/12512.misc.md b/changelog/12512.misc.md deleted file mode 100644 index f7b599c806d4..000000000000 --- a/changelog/12512.misc.md +++ /dev/null @@ -1 +0,0 @@ -Add plugin hook for getting the license hash from Rasa Plus and add the hash to telemetry context.