-
Notifications
You must be signed in to change notification settings - Fork 149
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Refactors NotificationBase to be concerned only with sending the message. Notifications now have no knowledge of the celery queue or Project/Service models * Alert model handles expanding alert json based on Project/Service model but has no knowledge about about configured sender plugins * Incoming alerts are saved to the database for auditing purposes and queued to be processed * Processing alerts expands the alert data, and de-duplicates sender targets before queuing to be sent
- Loading branch information
Showing
9 changed files
with
159 additions
and
112 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
# Copyright (c) 2018 LINE Corporation | ||
# These sources are released under the terms of the MIT license: see LICENSE | ||
|
||
import json | ||
import logging | ||
|
||
from django.core.management.base import BaseCommand | ||
from django.test import override_settings | ||
from promgen import models, tasks, tests | ||
|
||
|
||
class Command(BaseCommand): | ||
@override_settings(CELERY_TASK_ALWAYS_EAGER=True) | ||
def handle(self, **kwargs): | ||
logging._handlers = [] | ||
logging.basicConfig(level=logging.DEBUG) | ||
|
||
data = tests.PromgenTest.data_json('examples', 'alertmanager.json') | ||
|
||
shard, _ = models.Shard.objects.get_or_create(name='Shard Test') | ||
service, _ = models.Service.objects.get_or_create( | ||
shard=shard, name=data['commonLabels']['service'] | ||
) | ||
project, _ = models.Project.objects.get_or_create( | ||
service=service, name=data['commonLabels']['project'] | ||
) | ||
|
||
alert = models.Alert.objects.create( | ||
body=json.dumps(data) | ||
) | ||
tasks.process_alert(alert.pk) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
# Generated by Django 2.0.7 on 2018-07-26 08:15 | ||
|
||
from django.db import migrations, models | ||
import django.db.models.deletion | ||
import django.utils.timezone | ||
|
||
|
||
class Migration(migrations.Migration): | ||
|
||
dependencies = [ | ||
('promgen', '0002_auto_20180316_0525'), | ||
] | ||
|
||
operations = [ | ||
migrations.CreateModel( | ||
name='Alert', | ||
fields=[ | ||
('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')), | ||
('created', models.DateTimeField(default=django.utils.timezone.now)), | ||
('body', models.TextField()), | ||
], | ||
), | ||
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,25 +1,54 @@ | ||
# Copyright (c) 2017 LINE Corporation | ||
# These sources are released under the terms of the MIT license: see LICENSE | ||
|
||
import json | ||
import collections | ||
import logging | ||
|
||
from celery import shared_task | ||
|
||
from promgen import plugins, prometheus, signals # NOQA | ||
from promgen import models, plugins, prometheus, signals # NOQA | ||
|
||
logger = logging.getLogger(__name__) | ||
|
||
|
||
@shared_task | ||
def send_notification(sender, body): | ||
body = json.loads(body) | ||
logger.info('Attempting to send alert for %s', sender) | ||
def process_alert(alert_pk): | ||
''' | ||
Process alert for routing and notifications | ||
We load our Alert from the database and expand it to determine which labels are routable | ||
Next we loop through all senders configured and de-duplicate sender:target pairs before | ||
queing the notification to actually be sent | ||
''' | ||
alert = models.Alert.objects.get(pk=alert_pk) | ||
routable, data = alert.expand() | ||
|
||
# Now that we have our routable items, we want to check which senders are | ||
# configured and expand those as needed | ||
senders = collections.defaultdict(set) | ||
for label, obj in routable.items(): | ||
logger.debug('Processing %s %s', label, obj) | ||
for sender in models.Sender.filter(obj): | ||
if hasattr(sender.driver, 'splay'): | ||
for splay in sender.driver.splay(sender.value): | ||
senders[splay.sender].add(splay.value) | ||
else: | ||
senders[sender.sender].add(sender.value) | ||
|
||
for driver in senders: | ||
for target in senders[driver]: | ||
send_alert.delay(driver, target, data, alert.pk) | ||
|
||
|
||
@shared_task | ||
def send_alert(sender, target, data, alert_pk=None): | ||
''' | ||
Send alert to specific target | ||
alert_pk is used for debugging purposes | ||
''' | ||
logger.debug('Sending %s %s', sender, target) | ||
for plugin in plugins.notifications(): | ||
if sender == plugin.module_name: | ||
try: | ||
instance = plugin.load()() | ||
count = instance.send(body) | ||
logger.info('Sent %d alerts with %s', count, sender) | ||
except: | ||
logger.exception('Error sending message') | ||
instance = plugin.load()() | ||
instance._send(target, data) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters