Repository: pulp/pulpcore

Issue:
Telemetry task gives false impression that telemetry data is being sent
I see in the logs that a telemetry task is being executed regularly (every day):

```
        {
            "child_tasks": [],
            "created_resources": [],
            "error": null,
            "finished_at": "2022-07-25T21:32:09.435330Z",
            "logging_cid": "",
            "name": "pulpcore.app.tasks.telemetry.post_telemetry",
            "parent_task": null,
            "progress_reports": [],
            "pulp_created": "2022-07-25T21:32:09.383372Z",
            "pulp_href": "/pulp/api/v3/tasks/7f4c54d6-fcda-4006-9da1-3c6910a18171/",
            "reserved_resources_record": [],
            "started_at": "2022-07-25T21:32:09.419785Z",
            "state": "completed",
            "task_group": null,
            "worker": "/pulp/api/v3/workers/c609a0d5-45cc-4756-af3c-4b50cd7520b1/"
        }
```

It gave me the impression that Pulp was sending telemetry data. It was only when I dug into the code that I saw that it only does this for dev installs. Is there a way to not execute this task if telemetry data is not being sent?


Discussion:

Resolving patch:
diff --git a/pulpcore/app/apps.py b/pulpcore/app/apps.py
--- a/pulpcore/app/apps.py
+++ b/pulpcore/app/apps.py
@@ -212,14 +212,11 @@ def ready(self):
         super().ready()
         from . import checks  # noqa
 
+        _configure_telemetry(self.apps)
+
         post_migrate.connect(
             _populate_system_id, sender=self, dispatch_uid="populate_system_id_identifier"
         )
-        post_migrate.connect(
-            _populate_telemetry_periodic_task,
-            sender=self,
-            dispatch_uid="populate_telemetry_periodic_task_identifier",
-        )
 
 
 def _populate_access_policies(sender, apps, verbosity, **kwargs):
@@ -265,14 +262,24 @@ def _populate_system_id(sender, apps, verbosity, **kwargs):
         SystemID().save()
 
 
-def _populate_telemetry_periodic_task(sender, apps, **kwargs):
-    TaskSchedule = apps.get_model("core", "TaskSchedule")
-    task_name = "pulpcore.app.tasks.telemetry.post_telemetry"
-    dispatch_interval = timedelta(days=1)
-    name = "Post Anonymous Telemetry Periodically"
-    TaskSchedule.objects.update_or_create(
-        name=name, defaults={"task_name": task_name, "dispatch_interval": dispatch_interval}
-    )
+def _configure_telemetry(apps):
+    from django.db import connection
+    from pulpcore.app.util import get_telemetry_posting_url, PRODUCTION_URL
+
+    if "core_taskschedule" in connection.introspection.table_names():
+        url = get_telemetry_posting_url()
+        TaskSchedule = apps.get_model("core", "TaskSchedule")
+        task_name = "pulpcore.app.tasks.telemetry.post_telemetry"
+        dispatch_interval = timedelta(days=1)
+        name = "Post Anonymous Telemetry Periodically"
+        # Initially only dev systems receive posted data.
+        if url == PRODUCTION_URL:
+            TaskSchedule.objects.filter(task_name=task_name).delete()
+        else:
+            TaskSchedule.objects.update_or_create(
+                name=name, defaults={"task_name": task_name, "dispatch_interval": dispatch_interval}
+            )
+    connection.close()
 
 
 def _populate_roles(sender, apps, verbosity, **kwargs):
diff --git a/pulpcore/app/tasks/telemetry.py b/pulpcore/app/tasks/telemetry.py
--- a/pulpcore/app/tasks/telemetry.py
+++ b/pulpcore/app/tasks/telemetry.py
@@ -7,6 +7,7 @@
 from asgiref.sync import sync_to_async
 
 from pulpcore.app.apps import pulp_plugin_configs
+from pulpcore.app.util import get_telemetry_posting_url
 from pulpcore.app.models import SystemID
 from pulpcore.app.models.status import ContentAppStatus
 from pulpcore.app.models.task import Worker
@@ -15,10 +16,6 @@
 logger = logging.getLogger(__name__)
 
 
-PRODUCTION_URL = "https://analytics-pulpproject-org.pulpproject.workers.dev/"
-DEV_URL = "https://dev-analytics-pulpproject-org.pulpproject.workers.dev/"
-
-
 async def _num_hosts(qs):
     hosts = set()
     items = await sync_to_async(list)(qs.all())
@@ -67,20 +64,8 @@ async def _system_id():
     return {"system_id": str(system_id_entry.pk)}
 
 
-def _get_posting_url():
-    for app in pulp_plugin_configs():
-        if ".dev" in app.version:
-            return DEV_URL
-
-    return PRODUCTION_URL
-
-
 async def post_telemetry():
-    url = _get_posting_url()
-
-    if url == PRODUCTION_URL:
-        return  # Initially only dev systems receive posted data. If we got here, bail.
-
+    url = get_telemetry_posting_url()
     data = {}
 
     awaitables = (
diff --git a/pulpcore/app/util.py b/pulpcore/app/util.py
--- a/pulpcore/app/util.py
+++ b/pulpcore/app/util.py
@@ -19,6 +19,9 @@
 # a little cache so viewset_for_model doesn't have iterate over every app every time
 _model_viewset_cache = {}
 
+PRODUCTION_URL = "https://analytics-pulpproject-org.pulpproject.workers.dev/"
+DEV_URL = "https://dev-analytics-pulpproject-org.pulpproject.workers.dev/"
+
 
 def get_url(model):
     """
@@ -243,3 +246,11 @@ def gpg_verify(public_keys, signature, detached_data=None):
         if not verified.valid:
             raise InvalidSignatureError(_("The signature is not valid."), verified=verified)
     return verified
+
+
+def get_telemetry_posting_url():
+    for app in pulp_plugin_configs():
+        if ".dev" in app.version:
+            return DEV_URL
+
+    return PRODUCTION_URL


Test patch: