Mateusz Matuszkowiak | 2820c66 | 2018-11-21 12:07:25 +0100 | [diff] [blame] | 1 | import json |
| 2 | from logging.config import dictConfig |
| 3 | |
| 4 | from flask import Flask, Response, jsonify, request |
Michal Kobus | 4104c10 | 2019-02-22 17:05:11 +0100 | [diff] [blame] | 5 | |
Michal Kobus | 7187d6b | 2023-07-11 11:56:06 +0200 | [diff] [blame] | 6 | from prometheus_client import (CollectorRegistry, CONTENT_TYPE_LATEST, |
| 7 | generate_latest, multiprocess) |
Michal Kobus | aa3accf | 2019-06-05 12:25:09 +0200 | [diff] [blame] | 8 | |
| 9 | from requests.exceptions import ConnectionError as RequestsConnectionError |
Michal Kobus | fc1e773 | 2019-05-16 18:30:48 +0200 | [diff] [blame] | 10 | |
Michal Kobus | 211ee92 | 2019-04-15 17:44:06 +0200 | [diff] [blame] | 11 | from sf_notifier.helpers import alert_fields_and_action, create_file |
| 12 | from sf_notifier.salesforce.client import SESSION_FILE, SalesforceClient |
Michal Kobus | aa3accf | 2019-06-05 12:25:09 +0200 | [diff] [blame] | 13 | |
| 14 | from simple_salesforce.exceptions import SalesforceError |
| 15 | |
| 16 | from simple_settings import settings |
| 17 | |
Mateusz Matuszkowiak | 2820c66 | 2018-11-21 12:07:25 +0100 | [diff] [blame] | 18 | |
| 19 | dictConfig(settings.LOGGING) |
| 20 | |
Michal Kobus | afbf4d0 | 2018-11-28 14:18:05 +0100 | [diff] [blame] | 21 | app = Flask(__name__) |
Michal Kobus | afbf4d0 | 2018-11-28 14:18:05 +0100 | [diff] [blame] | 22 | |
Michal Kobus | 7187d6b | 2023-07-11 11:56:06 +0200 | [diff] [blame] | 23 | registry = CollectorRegistry() |
| 24 | multiprocess.MultiProcessCollector(registry) |
Michal Kobus | 211ee92 | 2019-04-15 17:44:06 +0200 | [diff] [blame] | 25 | |
| 26 | create_file(SESSION_FILE) |
Michal Kobus | 7187d6b | 2023-07-11 11:56:06 +0200 | [diff] [blame] | 27 | sf_cli = SalesforceClient(settings.SF_CONFIG, prometheus_registry=registry) |
| 28 | |
| 29 | |
| 30 | @app.route('/metrics', methods=['GET']) |
| 31 | def metrics(): |
| 32 | return Response(generate_latest(registry), |
| 33 | mimetype=CONTENT_TYPE_LATEST) |
Mateusz Matuszkowiak | 2820c66 | 2018-11-21 12:07:25 +0100 | [diff] [blame] | 34 | |
| 35 | |
Michal Kobus | 819cf02 | 2018-11-29 16:39:22 +0100 | [diff] [blame] | 36 | @app.route('/info', methods=['GET']) |
| 37 | def info(): |
Michal Kobus | 73d3352 | 2018-12-10 11:41:13 +0100 | [diff] [blame] | 38 | return jsonify({ |
| 39 | 'version': settings.VERSION, |
| 40 | 'hashing': sf_cli.hash_func.__name__ |
| 41 | }) |
Michal Kobus | 819cf02 | 2018-11-29 16:39:22 +0100 | [diff] [blame] | 42 | |
| 43 | |
Mateusz Matuszkowiak | 2820c66 | 2018-11-21 12:07:25 +0100 | [diff] [blame] | 44 | @app.route('/hook', methods=['POST']) |
| 45 | def webhook_receiver(): |
| 46 | |
| 47 | try: |
| 48 | data = json.loads(request.data) |
| 49 | except ValueError: |
Michal Kobus | 17726ae | 2018-11-27 12:59:55 +0100 | [diff] [blame] | 50 | msg = 'Invalid request data: {}.'.format(request.data) |
| 51 | app.logger.error(msg) |
| 52 | return Response(json.dumps({'error': msg}), |
Mateusz Matuszkowiak | 2820c66 | 2018-11-21 12:07:25 +0100 | [diff] [blame] | 53 | status=400, |
| 54 | mimetype='application/json') |
| 55 | |
| 56 | app.logger.info('Received requests: {}'.format(data)) |
| 57 | |
Michal Kobus | aa3accf | 2019-06-05 12:25:09 +0200 | [diff] [blame] | 58 | cases = [] |
Mateusz Matuszkowiak | 2820c66 | 2018-11-21 12:07:25 +0100 | [diff] [blame] | 59 | for alert in data['alerts']: |
Michal Kobus | aa3accf | 2019-06-05 12:25:09 +0200 | [diff] [blame] | 60 | try: |
| 61 | alert['labels']['env_id'] = sf_cli.environment |
Michal Kobus | e7589f7 | 2020-09-11 14:29:37 +0200 | [diff] [blame] | 62 | fields, action = alert_fields_and_action( |
Michal Kobus | 28b37f9 | 2022-06-14 11:06:24 +0200 | [diff] [blame] | 63 | alert, |
| 64 | add_links=settings.ADD_LINKS, |
| 65 | cluster_id=settings.CLUSTER_ID) |
Michal Kobus | aa3accf | 2019-06-05 12:25:09 +0200 | [diff] [blame] | 66 | except KeyError as key: |
| 67 | msg = 'Alert misses {} key.'.format(key) |
| 68 | app.logger.error(msg) |
| 69 | return Response(json.dumps({'error': msg}), |
| 70 | status=400, |
| 71 | mimetype='application/json') |
Mateusz Matuszkowiak | 2820c66 | 2018-11-21 12:07:25 +0100 | [diff] [blame] | 72 | |
Michal Kobus | aa3accf | 2019-06-05 12:25:09 +0200 | [diff] [blame] | 73 | if fields: |
| 74 | try: |
| 75 | cases.append(getattr(sf_cli, action)(*fields)) |
| 76 | except (SalesforceError, RequestsConnectionError) as err: |
| 77 | msg = 'Salesforce request failure: {}.'.format(err) |
| 78 | sf_cli.metrics['sf_error_count'].inc() |
| 79 | app.logger.error(msg) |
| 80 | return Response(json.dumps({'error': msg}), |
| 81 | status=500, |
| 82 | mimetype='application/json') |
Mateusz Matuszkowiak | 2820c66 | 2018-11-21 12:07:25 +0100 | [diff] [blame] | 83 | |
Michal Kobus | aa3accf | 2019-06-05 12:25:09 +0200 | [diff] [blame] | 84 | if len(cases) == 1: |
| 85 | return jsonify(cases[0]) |
Mateusz Matuszkowiak | 2820c66 | 2018-11-21 12:07:25 +0100 | [diff] [blame] | 86 | return jsonify(cases) |
| 87 | |
| 88 | |
| 89 | if __name__ == '__main__': |
| 90 | app.run() |