diff --git a/alerta/models/alert.py b/alerta/models/alert.py index d8beb4646..2b38c7e4f 100644 --- a/alerta/models/alert.py +++ b/alerta/models/alert.py @@ -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 } @@ -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: @@ -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: @@ -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: @@ -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: diff --git a/alerta/webhooks/prometheus.py b/alerta/webhooks/prometheus.py index 4e43ce49a..f8ce368b2 100644 --- a/alerta/webhooks/prometheus.py +++ b/alerta/webhooks/prometheus.py @@ -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 @@ -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