'''
Simple webhook bridge
Accepts alert json from Alert Manager and then POSTs individual alerts to
configured webhook destinations
'''

import logging
import requests
from promgen.sender import SenderBase

logger = logging.getLogger(__name__)


class SenderWebhook(SenderBase):
    def _send(self, url, alert, data):
        body = {
            'prometheus': alert['generatorURL'],
            'status': alert['status'],
            'alertmanager': data['externalURL']
        }
        body.update(alert['labels'])
        body.update(alert['annotations'])

        requests.post(url, body).raise_for_status()
        return True

'''
Simple webhook bridge
Accepts alert json from Alert Manager and then POSTs individual alerts to
configured webhook destinations
'''

import logging

import requests

from promgen.sender import SenderBase

logger = logging.getLogger(__name__)


class SenderWebhook(SenderBase):
    def _send(self, url, alert, data):
        body = {
            'prometheus': alert['generatorURL'],
            'status': alert['status'],
            'alertmanager': data['externalURL']
        }
        body.update(alert.get('labels', {}))
        body.update(alert.get('annotations', {}))

        requests.post(url, body).raise_for_status()
        return True

Fix case where annotations may not exist
Fix case where annotations may not exist

  '''
  Simple webhook bridge
  Accepts alert json from Alert Manager and then POSTs individual alerts to
  configured webhook destinations
  '''
  
  import logging
+ 
  import requests
+ 
  from promgen.sender import SenderBase
  
  logger = logging.getLogger(__name__)
  
  
  class SenderWebhook(SenderBase):
      def _send(self, url, alert, data):
          body = {
              'prometheus': alert['generatorURL'],
              'status': alert['status'],
              'alertmanager': data['externalURL']
          }
-         body.update(alert['labels'])
+         body.update(alert.get('labels', {}))
-         body.update(alert['annotations'])
+         body.update(alert.get('annotations', {}))
  
          requests.post(url, body).raise_for_status()
          return True
  
Fix case where annotations may not exist
## Code Before:
'''
Simple webhook bridge
Accepts alert json from Alert Manager and then POSTs individual alerts to
configured webhook destinations
'''

import logging
import requests
from promgen.sender import SenderBase

logger = logging.getLogger(__name__)


class SenderWebhook(SenderBase):
    def _send(self, url, alert, data):
        body = {
            'prometheus': alert['generatorURL'],
            'status': alert['status'],
            'alertmanager': data['externalURL']
        }
        body.update(alert['labels'])
        body.update(alert['annotations'])

        requests.post(url, body).raise_for_status()
        return True

## Instruction:
Fix case where annotations may not exist
## Code After:
'''
Simple webhook bridge
Accepts alert json from Alert Manager and then POSTs individual alerts to
configured webhook destinations
'''

import logging

import requests

from promgen.sender import SenderBase

logger = logging.getLogger(__name__)


class SenderWebhook(SenderBase):
    def _send(self, url, alert, data):
        body = {
            'prometheus': alert['generatorURL'],
            'status': alert['status'],
            'alertmanager': data['externalURL']
        }
        body.update(alert.get('labels', {}))
        body.update(alert.get('annotations', {}))

        requests.post(url, body).raise_for_status()
        return True
