From c5c6562f2db272cba516940e27964a9371f87122 Mon Sep 17 00:00:00 2001 From: Akash Chetty Date: Tue, 7 Feb 2023 11:14:08 +0530 Subject: [PATCH 1/6] release version v2.0.4 (#77) (#78) --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index 0015f3251..1c386883f 100644 --- a/package.json +++ b/package.json @@ -1,3 +1,3 @@ { - "version":"2.0.5" + "version":"2.0.6" } From 18a6c825b60fa5d1993ff0db40b87dedcaee9c8e Mon Sep 17 00:00:00 2001 From: Satish Kumar <97025614+satishrudderstack@users.noreply.github.com> Date: Thu, 9 Feb 2023 17:49:16 +0530 Subject: [PATCH 2/6] develop to master via temporary branch (#89) --- Dockerfile | 1 - RUDDER_ENRICH_PLUGIN_BRANCH_NAME | 2 +- alerta/utils/api.py | 4 +--- alerta/utils/config.py | 2 ++ alerta/views/alerts.py | 10 ++++++++++ alerta/webhooks/custom.py | 11 +++++++++++ alerta/webhooks/prometheus.py | 16 ++++++++++++++-- package.json | 2 +- 8 files changed, 40 insertions(+), 8 deletions(-) diff --git a/Dockerfile b/Dockerfile index 3f1fd2eda..663d4ee91 100644 --- a/Dockerfile +++ b/Dockerfile @@ -66,7 +66,6 @@ RUN /app/install-plugins.sh ADD rudder-alerta-enrichment-plugin/ /opt/rudder-alerta-enrichment-plugin/ # RUN cd /opt/rudder-alerta-enrichment-plugin && /venv/bin/pip install -r requirements.txt && mv rudder_enrichment /venv/lib/python3.7/site-packages/ && rm -rf /opt/rudder-alerta-enrichment-plugin/ RUN cd /opt/rudder-alerta-enrichment-plugin && /venv/bin/python setup.py install -ENV DITTO_JSON_PATH=/opt/rudder-alerta-enrichment-plugin/ditto/text.json ENV ALERTA_SVR_CONF_FILE /app/alertad.conf diff --git a/RUDDER_ENRICH_PLUGIN_BRANCH_NAME b/RUDDER_ENRICH_PLUGIN_BRANCH_NAME index 88d050b19..ba2906d06 100644 --- a/RUDDER_ENRICH_PLUGIN_BRANCH_NAME +++ b/RUDDER_ENRICH_PLUGIN_BRANCH_NAME @@ -1 +1 @@ -main \ No newline at end of file +main diff --git a/alerta/utils/api.py b/alerta/utils/api.py index 9bd7b247f..72b2f820d 100644 --- a/alerta/utils/api.py +++ b/alerta/utils/api.py @@ -15,7 +15,6 @@ from alerta.exceptions import (AlertaException, ApiError, BlackoutPeriod, ForwardingLoop, HeartbeatReceived, InvalidAction, RateLimit, RejectException) -from alerta.models.alert_metadata import AlertMetadata def assign_customer(wanted: str = None, permission: Scope = Scope.admin_alerts) -> Optional[str]: @@ -64,8 +63,7 @@ def enrich_alert(alert: Alert) -> Alert: logging.debug('RudderEnrichment: enrich_alert %s', alert) # TODO: do not populate enriched_data field, instead add all the metadata that we retrieved to the alert # the end user message will be created later by the processor - alert_metadata = AlertMetadata.find_by_alert(alert=alert.resource).serialize - alert = plugin.enrich_alert(alert, alert_metadata) + alert = plugin.enrich_alert(alert) StatsD.increment("enrichment_process_count", 1, {"name": plugin.name}) except (RejectException, HeartbeatReceived, BlackoutPeriod, RateLimit, ForwardingLoop, AlertaException): StatsD.increment("enrichment_process_error", 1, {"name": plugin.name}) diff --git a/alerta/utils/config.py b/alerta/utils/config.py index 72838fffd..61556c061 100644 --- a/alerta/utils/config.py +++ b/alerta/utils/config.py @@ -115,6 +115,8 @@ def get_user_config(): if config['DEFAULT_ENVIRONMENT'] not in config['ALLOWED_ENVIRONMENTS']: raise RuntimeError(f"Default environment \"{config['DEFAULT_ENVIRONMENT']}\" not in list of allowed environments") + config['ALERT_METADATA_CONFIG_FILE'] = get_config('ALERT_METADATA_CONFIG_FILE', default=None, type=str, config=config) + return config diff --git a/alerta/views/alerts.py b/alerta/views/alerts.py index 467cb11f2..1b3ac6567 100644 --- a/alerta/views/alerts.py +++ b/alerta/views/alerts.py @@ -26,6 +26,10 @@ from ..models.channel_rule import CustomerChannelRuleMap from ..models.event_log import EventLog from ..stats import StatsD +import logging +import json + +log_object = logging.getLogger('alerta.views.alerts') receive_timer = Timer('alerts', 'received', 'Received alerts', 'Total time and number of received alerts') gets_timer = Timer('alerts', 'queries', 'Alert queries', 'Total time and number of alert queries') @@ -68,6 +72,12 @@ def audit_trail_alert(event: str): request=request) try: + log_payload = { + "alert_name": alert.resource, + "rudder_resource_type": alert.rudder_resource_type, + "rudder_resource_id": alert.rudder_resource_id + } + log_object.warn('Received alert %s' % json.dumps(log_payload)) alert = process_alert(alert) except RejectException as e: audit_trail_alert(event='alert-rejected') diff --git a/alerta/webhooks/custom.py b/alerta/webhooks/custom.py index 86cfb311a..4742cfbb3 100644 --- a/alerta/webhooks/custom.py +++ b/alerta/webhooks/custom.py @@ -12,6 +12,10 @@ from alerta.utils.audit import write_audit_trail from . import webhooks +import logging +import json + +LOG = logging.getLogger('alerta.webhooks') @webhooks.route('/webhooks/', defaults={'path': ''}, methods=['OPTIONS', 'GET', 'POST']) @@ -52,6 +56,13 @@ def audit_trail_alert(event: str): request=request) try: + log_payload = { + "alert_name": alert.resource, + "rudder_resource_type": alert.rudder_resource_type, + "rudder_resource_id": alert.rudder_resource_id, + "webhook": webhook + } + LOG.warn('Received webhook alert %s' % json.dumps(log_payload)) alert = process_alert(alert) except RejectException as e: audit_trail_alert(event='alert-rejected') diff --git a/alerta/webhooks/prometheus.py b/alerta/webhooks/prometheus.py index c3df1d68e..08e745d4e 100644 --- a/alerta/webhooks/prometheus.py +++ b/alerta/webhooks/prometheus.py @@ -8,6 +8,7 @@ from alerta.models.alert import Alert from . import WebhookBase +from alerta.models.alert import get_rudder_resource_from_tags JSON = Dict[str, Any] dt = datetime.datetime @@ -43,7 +44,7 @@ def parse_prometheus(alert: JSON, external_url: str) -> Alert: severity = 'unknown' # labels - resource = labels.pop('exported_instance', None) or labels.pop('instance', 'n/a') + resource = labels.pop('resource', None) or labels.pop('alertname') event = labels.pop('event', None) or labels.pop('alertname') environment = labels.pop('environment', current_app.config['DEFAULT_ENVIRONMENT']) customer = labels.pop('customer', None) @@ -77,6 +78,13 @@ def parse_prometheus(alert: JSON, external_url: str) -> Alert: } attributes.update(annotations) # any annotations left over are used for attributes + rudder_resource_type = labels.get('rudder_resource_type') + rudder_resource_id = labels.get('rudder_resource_id') + 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: + raise ValueError('rudder_resource_type or rudder_resource_id missing - couldnt parse them from tags too') + return Alert( resource=resource, event=event, @@ -93,7 +101,11 @@ def parse_prometheus(alert: JSON, external_url: str) -> Alert: event_type='prometheusAlert', timeout=timeout, raw_data=alert, - tags=tags + tags=tags, + enriched_data=labels.get('enriched_data', None), + properties=labels.get('properties', None), + rudder_resource_type=rudder_resource_type, + rudder_resource_id=rudder_resource_id, ) diff --git a/package.json b/package.json index 1c386883f..083ac1792 100644 --- a/package.json +++ b/package.json @@ -1,3 +1,3 @@ { - "version":"2.0.6" + "version":"2.0.7" } From bdcf93aef8a0272014b577461cedf89f3fd963f8 Mon Sep 17 00:00:00 2001 From: Satish Kumar <97025614+satishrudderstack@users.noreply.github.com> Date: Wed, 15 Feb 2023 12:45:46 +0530 Subject: [PATCH 3/6] merge develop to master via temp branch (#92) --- alerta/database/backends/postgres/base.py | 1 - package.json | 2 +- 2 files changed, 1 insertion(+), 2 deletions(-) diff --git a/alerta/database/backends/postgres/base.py b/alerta/database/backends/postgres/base.py index 91ddf1130..a6b1e8272 100644 --- a/alerta/database/backends/postgres/base.py +++ b/alerta/database/backends/postgres/base.py @@ -170,7 +170,6 @@ def is_duplicate(self, alert): WHERE environment=%(environment)s AND resource=%(resource)s AND event=%(event)s - AND severity=%(severity)s AND rudder_resource_type=%(rudder_resource_type)s AND rudder_resource_id=%(rudder_resource_id)s AND {customer} diff --git a/package.json b/package.json index 083ac1792..66cf7f8e6 100644 --- a/package.json +++ b/package.json @@ -1,3 +1,3 @@ { - "version":"2.0.7" + "version":"2.0.8" } From e7029ed34dd3a1ddfe2fc466649bb552aed1e320 Mon Sep 17 00:00:00 2001 From: Satish Kumar <97025614+satishrudderstack@users.noreply.github.com> Date: Thu, 16 Feb 2023 09:18:46 +0530 Subject: [PATCH 4/6] chore: update release 2.0.9 (#93) --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index 66cf7f8e6..e3f63380e 100644 --- a/package.json +++ b/package.json @@ -1,3 +1,3 @@ { - "version":"2.0.8" + "version":"2.0.9" } From 466ec248924e2a14f166331557d570da67c1e63c Mon Sep 17 00:00:00 2001 From: Satish Kumar <97025614+satishrudderstack@users.noreply.github.com> Date: Thu, 16 Feb 2023 12:28:41 +0530 Subject: [PATCH 5/6] fix: construct `event` field from tags set. Alerts are getting wrongly correlated (#94) --- alerta/webhooks/prometheus.py | 3 ++- package.json | 2 +- 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/alerta/webhooks/prometheus.py b/alerta/webhooks/prometheus.py index 08e745d4e..096e94980 100644 --- a/alerta/webhooks/prometheus.py +++ b/alerta/webhooks/prometheus.py @@ -9,6 +9,7 @@ from . import WebhookBase from alerta.models.alert import get_rudder_resource_from_tags +import json JSON = Dict[str, Any] dt = datetime.datetime @@ -45,7 +46,6 @@ def parse_prometheus(alert: JSON, external_url: str) -> Alert: # labels resource = labels.pop('resource', None) or labels.pop('alertname') - event = labels.pop('event', None) or labels.pop('alertname') environment = labels.pop('environment', current_app.config['DEFAULT_ENVIRONMENT']) customer = labels.pop('customer', None) correlate = labels.pop('correlate').split(',') if 'correlate' in labels else None @@ -59,6 +59,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 + event = json.dumps(labels, sort_keys=True) # annotations value = annotations.pop('value', None) diff --git a/package.json b/package.json index e3f63380e..44ef67363 100644 --- a/package.json +++ b/package.json @@ -1,3 +1,3 @@ { - "version":"2.0.9" + "version":"2.0.10" } From 194c5d591455015799c7b34c57906408facaa66e Mon Sep 17 00:00:00 2001 From: Satish Kumar <97025614+satishrudderstack@users.noreply.github.com> Date: Thu, 16 Feb 2023 13:43:24 +0530 Subject: [PATCH 6/6] feat: read `alertMode` from alert metadata config (#95) --- alerta/models/alert.py | 3 ++- alerta/utils/config.py | 12 ++++++++++++ alerta/webhooks/prometheus.py | 3 ++- package.json | 2 +- 4 files changed, 17 insertions(+), 3 deletions(-) diff --git a/alerta/models/alert.py b/alerta/models/alert.py index 4f35fb2cf..0c2c6061d 100644 --- a/alerta/models/alert.py +++ b/alerta/models/alert.py @@ -16,6 +16,7 @@ from alerta.utils.format import DateTime from alerta.utils.hooks import status_change_hook from alerta.utils.response import absolute_url +from alerta.utils.config import get_alert_mode JSON = Dict[str, Any] NoneType = type(None) @@ -138,7 +139,7 @@ def parse(cls, json: JSON) -> 'Alert': id=json.get('id', None), resource=json.get('resource', None), event=json.get('event', None), - environment=json.get('environment', None), + environment=get_alert_mode(json.get('resource')), severity=json.get('severity', None), correlate=json.get('correlate', list()), status=json.get('status', None), diff --git a/alerta/utils/config.py b/alerta/utils/config.py index 61556c061..e842a4659 100644 --- a/alerta/utils/config.py +++ b/alerta/utils/config.py @@ -1,6 +1,8 @@ import os from flask import Flask +import json +import logging class Config: @@ -140,3 +142,13 @@ def get_config(key, default=None, type=None, **kwargs): except KeyError: rv = default return rv + + +def get_alert_mode(alert_name): + try: + ALERT_METADATA_CONFIG_FILE = os.environ.get('ALERT_METADATA_CONFIG_FILE') + with open(ALERT_METADATA_CONFIG_FILE, 'r') as f: + alert_metadata = json.load(f)['alertMetadata'] + return alert_metadata.get(alert_name).get('alertMode', 'PROXYMODE') + except Exception as e: + logging.error(f"Error loading alert metdadata in alerta; {e}", exc_info=True) \ No newline at end of file diff --git a/alerta/webhooks/prometheus.py b/alerta/webhooks/prometheus.py index 096e94980..4e43ce49a 100644 --- a/alerta/webhooks/prometheus.py +++ b/alerta/webhooks/prometheus.py @@ -10,6 +10,7 @@ from . import WebhookBase from alerta.models.alert import get_rudder_resource_from_tags import json +from alerta.utils.config import get_alert_mode JSON = Dict[str, Any] dt = datetime.datetime @@ -46,7 +47,7 @@ def parse_prometheus(alert: JSON, external_url: str) -> Alert: # labels resource = labels.pop('resource', None) or labels.pop('alertname') - environment = labels.pop('environment', current_app.config['DEFAULT_ENVIRONMENT']) + environment = get_alert_mode(resource) customer = labels.pop('customer', None) correlate = labels.pop('correlate').split(',') if 'correlate' in labels else None service = labels.pop('service', '').split(',') diff --git a/package.json b/package.json index 44ef67363..60ac106c7 100644 --- a/package.json +++ b/package.json @@ -1,3 +1,3 @@ { - "version":"2.0.10" + "version":"2.0.11" }