From 7f6c2494d03fcb67cf58582c6befd1695d1d4d9d Mon Sep 17 00:00:00 2001 From: Tom Bocklisch Date: Thu, 24 Aug 2023 10:18:46 +0200 Subject: [PATCH 1/2] only submit telemetry data if there is a license --- changelog/1493.improvement.md | 1 + rasa/telemetry.py | 5 +++++ tests/test_telemetry.py | 22 ++++++++++++++++++++-- 3 files changed, 26 insertions(+), 2 deletions(-) create mode 100644 changelog/1493.improvement.md diff --git a/changelog/1493.improvement.md b/changelog/1493.improvement.md new file mode 100644 index 000000000000..f2b43d85714f --- /dev/null +++ b/changelog/1493.improvement.md @@ -0,0 +1 @@ +Telemetry data is only send for licensed users. diff --git a/rasa/telemetry.py b/rasa/telemetry.py index 700f483522fd..0a3b35c934ca 100644 --- a/rasa/telemetry.py +++ b/rasa/telemetry.py @@ -402,6 +402,11 @@ def _send_event( logger.debug("Skipping request to external service: telemetry key not set.") return + if not "license_hash" in context: + # only send telemetry data for customers + logger.debug("Skipping telemetry reporting: no license hash found.") + return + headers = segment_request_header(write_key) resp = requests.post( diff --git a/tests/test_telemetry.py b/tests/test_telemetry.py index 0c0447ae5a33..c189345bdbcf 100644 --- a/tests/test_telemetry.py +++ b/tests/test_telemetry.py @@ -251,7 +251,9 @@ def test_segment_gets_called(monkeypatch: MonkeyPatch): with responses.RequestsMock() as rsps: rsps.add(responses.POST, "https://api.segment.io/v1/track", json={}) - telemetry._track("test event", {"foo": "bar"}, {"foobar": "baz"}) + telemetry._track( + "test event", {"foo": "bar"}, {"foobar": "baz", "license_hash": "foobar"} + ) assert len(rsps.calls) == 1 r = rsps.calls[0] @@ -274,11 +276,27 @@ def test_segment_does_not_raise_exception_on_failure(monkeypatch: MonkeyPatch): rsps.add(responses.POST, "https://api.segment.io/v1/track", body="", status=505) # this call should complete without throwing an exception - telemetry._track("test event", {"foo": "bar"}, {"foobar": "baz"}) + telemetry._track( + "test event", {"foo": "bar"}, {"foobar": "baz", "license_hash": "foobar"} + ) assert rsps.assert_call_count("https://api.segment.io/v1/track", 1) +def test_segment_does_not_get_called_without_license(monkeypatch: MonkeyPatch): + monkeypatch.setenv("RASA_TELEMETRY_ENABLED", "true") + monkeypatch.setenv("RASA_TELEMETRY_WRITE_KEY", "foobar") + telemetry.initialize_telemetry() + + with responses.RequestsMock(assert_all_requests_are_fired=False) as rsps: + rsps.add(responses.POST, "https://api.segment.io/v1/track", body="", status=505) + + # this call should complete without throwing an exception + telemetry._track("test event", {"foo": "bar"}, {"foobar": "baz"}) + + assert rsps.assert_call_count("https://api.segment.io/v1/track", 0) + + def test_environment_write_key_overwrites_key_file(monkeypatch: MonkeyPatch): monkeypatch.setenv("RASA_TELEMETRY_WRITE_KEY", "foobar") assert telemetry.telemetry_write_key() == "foobar" From 7b65bd4d42515282d3ea3275223e3bc1f634a8bd Mon Sep 17 00:00:00 2001 From: m-vdb Date: Mon, 18 Sep 2023 14:33:38 +0200 Subject: [PATCH 2/2] fix lint --- rasa/telemetry.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/rasa/telemetry.py b/rasa/telemetry.py index 0a3b35c934ca..eaaf2b711fde 100644 --- a/rasa/telemetry.py +++ b/rasa/telemetry.py @@ -402,7 +402,7 @@ def _send_event( logger.debug("Skipping request to external service: telemetry key not set.") return - if not "license_hash" in context: + if "license_hash" not in context: # only send telemetry data for customers logger.debug("Skipping telemetry reporting: no license hash found.") return