Browse Source

fix(metrics): Accept message without tags (#29905)

Accept metrics Kafka message with missing tags field in the indexer
consumer.

Relay omits the tags field of the metrics Kafka message if there are
none.

Fixes SENTRY-FOR-SENTRY-F78.
Joris Bayer 3 years ago
parent
commit
51a93ddd93

+ 1 - 1
src/sentry/sentry_metrics/indexer/indexer_consumer.py

@@ -38,7 +38,7 @@ class MetricsIndexerWorker(AbstractBatchWorker):  # type: ignore
         parsed_message: MutableMapping[str, Any] = json.loads(message.value(), use_rapid_json=True)
 
         metric_name = parsed_message["name"]
-        tags = parsed_message["tags"]
+        tags = parsed_message.get("tags", {})
 
         strings = {
             metric_name,

+ 18 - 0
tests/snuba/snuba/test_indexer_consumer.py

@@ -193,3 +193,21 @@ class MetricsIndexerConsumerTest(TestCase):
         # loading the json converts the keys to strings e.g. {"tags": {1: 3}} --> {"tags": {"1": 3}}
         assert parsed["tags"] == {str(k): v for k, v in expected["tags"].items()}
         assert parsed["metric_id"] == expected["metric_id"]
+
+    def test_payload_without_tags(self):
+        """Assert that process_message does not crash when tags are omitted"""
+        payload_without_tags = {
+            "name": "session",
+            "timestamp": ts,
+            "type": "c",
+            "value": 1.0,
+            "org_id": 1,
+            "project_id": 3,
+        }
+
+        class MockMessage:
+            def value(self):
+                return json.dumps(payload_without_tags)
+
+        translated = MetricsIndexerWorker(None).process_message(MockMessage())
+        assert translated["tags"] == {}