Skip to content

Commit

Permalink
Add event metadata routing key probe filter
Browse files Browse the repository at this point in the history
  • Loading branch information
np5 committed Nov 28, 2024
1 parent 8892ca8 commit ae65a92
Show file tree
Hide file tree
Showing 5 changed files with 81 additions and 7 deletions.
42 changes: 42 additions & 0 deletions tests/core_events/test_probes.py
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,48 @@ def test_event_probes(self):
self.assertEqual(list(updated_event.metadata.iter_loaded_probes()), [self.probe])
self.assertEqual(updated_event.metadata.incident_updates, expected_incident_updates)

def test_metadata_tag_filter(self):
serialized_event = {
'_zentral': {
'created_at': '2021-02-18T20:55:00',
'id': 'ff4db218-d5b4-4c2c-b40b-1b7fdee00dfc',
'index': 0,
'tags': ["daslkjdaklasdj", "a-match-haha"],
'type': 'yolo',
},
"yolo": "fomo",
}
event = event_from_event_d(serialized_event)
probe_source = ProbeSource.objects.create(
model="BaseProbe",
name=get_random_string(12),
status=ProbeSource.ACTIVE,
body={"filters": {"metadata": [{"event_tags": ["daslkjdaklasdj", "not-a-match"]}]}}
)
probe = probe_source.load()
self.assertTrue(probe.test_event(event))

def test_routing_key_filter(self):
serialized_event = {
'_zentral': {
'created_at': '2021-02-18T20:55:00',
'id': 'ff4db218-d5b4-4c2c-b40b-1b7fdee00dfc',
'index': 0,
'routing_key': "edlkjdlqkjdqe",
'type': 'yolo',
},
"yolo": "fomo",
}
event = event_from_event_d(serialized_event)
probe_source = ProbeSource.objects.create(
model="BaseProbe",
name=get_random_string(12),
status=ProbeSource.ACTIVE,
body={"filters": {"metadata": [{"event_routing_keys": ["not-a-match", "edlkjdlqkjdqe"]}]}}
)
probe = probe_source.load()
self.assertTrue(probe.test_event(event))

def test_event_probes_with_probe_incident(self):
event = event_from_event_d(serialized_event)
if self.probe_with_incident.test_event(event):
Expand Down
9 changes: 8 additions & 1 deletion tests/core_probes/test_probe_views.py
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,9 @@ def test_create_probe(self, **kwargs):
response = self.client.post(reverse("probes:create"),
{"name": name,
"event_types": ["zentral_login",
"zentral_logout"]},
"zentral_logout"],
"event_tags": ["heartbeat"],
"event_routing_keys": "un,deux"},
follow=True)
self.assertEqual(response.status_code, 200)
self.assertTemplateUsed(response, "probes/probe.html")
Expand All @@ -91,6 +93,11 @@ def test_create_probe(self, **kwargs):
self.assertEqual(probe.name, name)
self.assertEqual(probe_source.name, name)
self.assertEqual(probe_source.pk, probe.pk)
self.assertEqual(len(probe.metadata_filters), 1)
f = probe.metadata_filters[0]
self.assertEqual(f.event_types, {"zentral_login", "zentral_logout"})
self.assertEqual(f.event_tags, {"heartbeat"})
self.assertEqual(f.event_routing_keys, {"un", "deux"})

# update probe

Expand Down
13 changes: 13 additions & 0 deletions zentral/core/probes/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -91,12 +91,18 @@ def __init__(self, data):
if event_tags is None:
event_tags = []
self.event_tags = set(event_tags)
event_routing_keys = data.get("event_routing_keys")
if event_routing_keys is None:
event_routing_keys = []
self.event_routing_keys = set(event_routing_keys)

def test_event_metadata(self, metadata):
if self.event_types and metadata.event_type not in self.event_types:
return False
if self.event_tags and not metadata.all_tags & self.event_tags:
return False
if self.event_routing_keys and metadata.routing_key not in self.event_routing_keys:
return False
return True

def get_event_type_classes(self):
Expand All @@ -119,6 +125,9 @@ def get_event_tags_display(self):
return ", ".join(sorted(t.replace("_", " ")
for t in self.event_tags))

def get_event_routing_keys_display(self):
return ", ".join(sorted(self.event_routing_keys))


class MetadataFiltersSerializer(serializers.Serializer):
event_types = serializers.ListField(
Expand All @@ -129,6 +138,10 @@ class MetadataFiltersSerializer(serializers.Serializer):
child=serializers.CharField(),
required=False
)
event_routing_keys = serializers.ListField(
child=serializers.CharField(),
required=False
)

def validate(self, data):
for key, val in data.items():
Expand Down
12 changes: 8 additions & 4 deletions zentral/core/probes/forms.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
from django import forms
from django.contrib.postgres.forms import SimpleArrayField
from django.db.models import Q
from django.utils import timezone
from django.utils.text import slugify
Expand Down Expand Up @@ -96,6 +97,8 @@ class MetadataFilterForm(forms.Form):
event_tags = forms.MultipleChoiceField(label="Event tags", choices=[], required=False)
event_types = forms.MultipleChoiceField(label="Event types", choices=[], required=False,
widget=forms.SelectMultiple(attrs={"size": 10}))
event_routing_keys = SimpleArrayField(forms.CharField(), label="Event routing keys", required=False,
help_text="Comma separated list of event routing keys.")

def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
Expand All @@ -110,13 +113,14 @@ def clean(self):
cleaned_data = self.cleaned_data
event_type = cleaned_data.get("event_types")
event_tags = cleaned_data.get("event_tags")
if not event_type and not event_tags:
raise forms.ValidationError("Choose at least one event type or one tag.")
event_routing_keys = cleaned_data.get("event_routing_keys")
if not event_type and not event_tags and not event_routing_keys:
raise forms.ValidationError("Choose at least one event type or one tag or one routing key.")
return cleaned_data

def get_serialized_filter(self):
filter_d = {}
for attr in ("event_tags", "event_types"):
for attr in ("event_tags", "event_types", "event_routing_keys"):
value = self.cleaned_data.get(attr)
if value:
filter_d[attr] = value
Expand All @@ -125,7 +129,7 @@ def get_serialized_filter(self):
@staticmethod
def get_initial(metadata_filter):
initial_d = {}
for attr in ("event_tags", "event_types"):
for attr in ("event_tags", "event_types", "event_routing_keys"):
val = getattr(metadata_filter, attr, None)
if val:
if isinstance(val, set):
Expand Down
12 changes: 10 additions & 2 deletions zentral/core/probes/templates/probes/probe.html
Original file line number Diff line number Diff line change
Expand Up @@ -217,20 +217,28 @@ <h4 class="mt-4">Metadata</h4>
<table class="table-probe table-object-properties">
{% if metadata_filter.event_tags %}
<tr>
<td>tags</td>
<td>tag{{ metadata_filter.event_tags|length|pluralize }}</td>
<td>
{{ metadata_filter.get_event_tags_display }}
</td>
</tr>
{% endif %}
{% if metadata_filter.event_types %}
<tr>
<td>type</td>
<td>type{{ metadata_filter.event_types|length|pluralize }}</td>
<td>
{{ metadata_filter.get_event_types_display }}
</td>
</tr>
{% endif %}
{% if metadata_filter.event_routing_keys %}
<tr>
<td>routing key{{ metadata_filter.event_routing_keys|length|pluralize }}</td>
<td>
{{ metadata_filter.get_event_routing_keys_display }}
</td>
</tr>
{% endif %}
</table>
{% if perms.probes.change_probesource %}
<div class="card-footer">
Expand Down

0 comments on commit ae65a92

Please sign in to comment.