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 | |
Michal Kobus | 2536327 | 2023-09-19 12:13:17 +0200 | [diff] [blame] | 30 | with app.app_context(): |
| 31 | sf_cli.auth() |
| 32 | |
| 33 | |
Michal Kobus | 7187d6b | 2023-07-11 11:56:06 +0200 | [diff] [blame] | 34 | @app.route('/metrics', methods=['GET']) |
| 35 | def metrics(): |
| 36 | return Response(generate_latest(registry), |
| 37 | mimetype=CONTENT_TYPE_LATEST) |
Mateusz Matuszkowiak | 2820c66 | 2018-11-21 12:07:25 +0100 | [diff] [blame] | 38 | |
| 39 | |
Michal Kobus | 819cf02 | 2018-11-29 16:39:22 +0100 | [diff] [blame] | 40 | @app.route('/info', methods=['GET']) |
| 41 | def info(): |
Michal Kobus | 73d3352 | 2018-12-10 11:41:13 +0100 | [diff] [blame] | 42 | return jsonify({ |
| 43 | 'version': settings.VERSION, |
| 44 | 'hashing': sf_cli.hash_func.__name__ |
| 45 | }) |
Michal Kobus | 819cf02 | 2018-11-29 16:39:22 +0100 | [diff] [blame] | 46 | |
| 47 | |
Mateusz Matuszkowiak | 2820c66 | 2018-11-21 12:07:25 +0100 | [diff] [blame] | 48 | @app.route('/hook', methods=['POST']) |
| 49 | def webhook_receiver(): |
| 50 | |
| 51 | try: |
| 52 | data = json.loads(request.data) |
| 53 | except ValueError: |
Michal Kobus | 17726ae | 2018-11-27 12:59:55 +0100 | [diff] [blame] | 54 | msg = 'Invalid request data: {}.'.format(request.data) |
| 55 | app.logger.error(msg) |
| 56 | return Response(json.dumps({'error': msg}), |
Mateusz Matuszkowiak | 2820c66 | 2018-11-21 12:07:25 +0100 | [diff] [blame] | 57 | status=400, |
| 58 | mimetype='application/json') |
| 59 | |
| 60 | app.logger.info('Received requests: {}'.format(data)) |
| 61 | |
Michal Kobus | aa3accf | 2019-06-05 12:25:09 +0200 | [diff] [blame] | 62 | cases = [] |
Mateusz Matuszkowiak | 2820c66 | 2018-11-21 12:07:25 +0100 | [diff] [blame] | 63 | for alert in data['alerts']: |
Michal Kobus | aa3accf | 2019-06-05 12:25:09 +0200 | [diff] [blame] | 64 | try: |
| 65 | alert['labels']['env_id'] = sf_cli.environment |
Michal Kobus | e7589f7 | 2020-09-11 14:29:37 +0200 | [diff] [blame] | 66 | fields, action = alert_fields_and_action( |
Michal Kobus | 28b37f9 | 2022-06-14 11:06:24 +0200 | [diff] [blame] | 67 | alert, |
| 68 | add_links=settings.ADD_LINKS, |
| 69 | cluster_id=settings.CLUSTER_ID) |
Michal Kobus | aa3accf | 2019-06-05 12:25:09 +0200 | [diff] [blame] | 70 | except KeyError as key: |
| 71 | msg = 'Alert misses {} key.'.format(key) |
| 72 | app.logger.error(msg) |
| 73 | return Response(json.dumps({'error': msg}), |
| 74 | status=400, |
| 75 | mimetype='application/json') |
Mateusz Matuszkowiak | 2820c66 | 2018-11-21 12:07:25 +0100 | [diff] [blame] | 76 | |
Michal Kobus | aa3accf | 2019-06-05 12:25:09 +0200 | [diff] [blame] | 77 | if fields: |
| 78 | try: |
| 79 | cases.append(getattr(sf_cli, action)(*fields)) |
| 80 | except (SalesforceError, RequestsConnectionError) as err: |
| 81 | msg = 'Salesforce request failure: {}.'.format(err) |
| 82 | sf_cli.metrics['sf_error_count'].inc() |
| 83 | app.logger.error(msg) |
| 84 | return Response(json.dumps({'error': msg}), |
| 85 | status=500, |
| 86 | mimetype='application/json') |
Mateusz Matuszkowiak | 2820c66 | 2018-11-21 12:07:25 +0100 | [diff] [blame] | 87 | |
Michal Kobus | aa3accf | 2019-06-05 12:25:09 +0200 | [diff] [blame] | 88 | if len(cases) == 1: |
| 89 | return jsonify(cases[0]) |
Mateusz Matuszkowiak | 2820c66 | 2018-11-21 12:07:25 +0100 | [diff] [blame] | 90 | return jsonify(cases) |
| 91 | |
| 92 | |
| 93 | if __name__ == '__main__': |
| 94 | app.run() |