Skip to content

Commit

Permalink
feat: add custom tags for profile id
Browse files Browse the repository at this point in the history
  • Loading branch information
itsmihir committed Jul 30, 2024
1 parent f1eb7bc commit 166b64f
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 1 deletion.
14 changes: 14 additions & 0 deletions alerta/models/alert.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,13 @@

sources_search_words = ['sourceID=', 'sourceId=', 'source-id=', 'source=', 'source_id=']
destinations_search_words = ['destID=', 'destinationId=', 'destinationID=', 'destination-id=', 'destId=', 'destination_id=']
profiles_search_words = ['profile_id=']
transformation_search_words = ['transformationId=', 'transformation_id=']

resourceTypeToSearchWordsMap = {
"destination": destinations_search_words,
"source": sources_search_words,
"profile": profiles_search_words,
"transformation": transformation_search_words
}

Expand All @@ -49,6 +51,15 @@ def get_rudder_resource_from_tags(tags):
break
return rudder_resource_type, rudder_resource_id

def add_custom_tags(tags):
# for profiles alerts, add profile_id as a tag
print("Before adding custom tags: ", tags)
# for tag in tags:
# if 'job_id=' in tag:
# tags.append('profile_id=' + tag.split('=')[1])
print("After adding custom tags: ", tags)
return tags;

class Alert:

def __init__(self, resource: str, event: str, **kwargs) -> None:
Expand Down Expand Up @@ -130,6 +141,7 @@ def parse(cls, json: JSON) -> 'Alert':
rudder_resource_type = json.get('rudder_resource_type')
rudder_resource_id = json.get('rudder_resource_id')
tags = json.get('tags', list())
tags = add_custom_tags(tags)
if rudder_resource_type is None or rudder_resource_id is None:
rudder_resource_type, rudder_resource_id = get_rudder_resource_from_tags(tags)
if rudder_resource_type is None or rudder_resource_id is None:
Expand Down Expand Up @@ -258,6 +270,7 @@ def from_document(cls, doc: Dict[str, Any]) -> 'Alert':
rudder_resource_type=doc.get('rudder_resource_type', None)
rudder_resource_id=doc.get('rudder_resource_id', None)
tags=doc.get('tags', list())
tags = add_custom_tags(tags)
if rudder_resource_type is None or rudder_resource_id is None:
rudder_resource_type, rudder_resource_id = get_rudder_resource_from_tags(tags)
if rudder_resource_type is None or rudder_resource_id is None:
Expand Down Expand Up @@ -317,6 +330,7 @@ def from_record(cls, rec) -> 'Alert':
rudder_resource_id = None

tags=rec.tags
tags = add_custom_tags(tags)
if rudder_resource_type is None or rudder_resource_id is None:
rudder_resource_type, rudder_resource_id = get_rudder_resource_from_tags(tags)
if rudder_resource_type is None or rudder_resource_id is None:
Expand Down
3 changes: 2 additions & 1 deletion alerta/webhooks/prometheus.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
from alerta.models.alert import Alert

from . import WebhookBase
from alerta.models.alert import get_rudder_resource_from_tags
from alerta.models.alert import get_rudder_resource_from_tags, add_custom_tags
import json
from alerta.utils.config import get_alert_mode

Expand Down Expand Up @@ -60,6 +60,7 @@ def parse_prometheus(alert: JSON, external_url: str) -> Alert:
timeout = None

tags = [f'{k}={v}' for k, v in labels.items()] # any labels left over are used for tags
tags = add_custom_tags(tags)
event = json.dumps(labels, sort_keys=True)

# annotations
Expand Down

0 comments on commit 166b64f

Please sign in to comment.