blob: 2b919e260b871b25de620bef76fa9048a1c13d69 [file] [log] [blame]
Mateusz Matuszkowiak2820c662018-11-21 12:07:25 +01001import json
2from logging.config import dictConfig
3
4from flask import Flask, Response, jsonify, request
Michal Kobus4104c102019-02-22 17:05:11 +01005
Michal Kobus7187d6b2023-07-11 11:56:06 +02006from prometheus_client import (CollectorRegistry, CONTENT_TYPE_LATEST,
7 generate_latest, multiprocess)
Michal Kobusaa3accf2019-06-05 12:25:09 +02008
9from requests.exceptions import ConnectionError as RequestsConnectionError
Michal Kobusfc1e7732019-05-16 18:30:48 +020010
Michal Kobus211ee922019-04-15 17:44:06 +020011from sf_notifier.helpers import alert_fields_and_action, create_file
12from sf_notifier.salesforce.client import SESSION_FILE, SalesforceClient
Michal Kobusaa3accf2019-06-05 12:25:09 +020013
14from simple_salesforce.exceptions import SalesforceError
15
16from simple_settings import settings
17
Mateusz Matuszkowiak2820c662018-11-21 12:07:25 +010018
19dictConfig(settings.LOGGING)
20
Michal Kobusafbf4d02018-11-28 14:18:05 +010021app = Flask(__name__)
Michal Kobusafbf4d02018-11-28 14:18:05 +010022
Michal Kobus7187d6b2023-07-11 11:56:06 +020023registry = CollectorRegistry()
24multiprocess.MultiProcessCollector(registry)
Michal Kobus211ee922019-04-15 17:44:06 +020025
26create_file(SESSION_FILE)
Michal Kobus7187d6b2023-07-11 11:56:06 +020027sf_cli = SalesforceClient(settings.SF_CONFIG, prometheus_registry=registry)
28
29
30@app.route('/metrics', methods=['GET'])
31def metrics():
32 return Response(generate_latest(registry),
33 mimetype=CONTENT_TYPE_LATEST)
Mateusz Matuszkowiak2820c662018-11-21 12:07:25 +010034
35
Michal Kobus819cf022018-11-29 16:39:22 +010036@app.route('/info', methods=['GET'])
37def info():
Michal Kobus73d33522018-12-10 11:41:13 +010038 return jsonify({
39 'version': settings.VERSION,
40 'hashing': sf_cli.hash_func.__name__
41 })
Michal Kobus819cf022018-11-29 16:39:22 +010042
43
Mateusz Matuszkowiak2820c662018-11-21 12:07:25 +010044@app.route('/hook', methods=['POST'])
45def webhook_receiver():
46
47 try:
48 data = json.loads(request.data)
49 except ValueError:
Michal Kobus17726ae2018-11-27 12:59:55 +010050 msg = 'Invalid request data: {}.'.format(request.data)
51 app.logger.error(msg)
52 return Response(json.dumps({'error': msg}),
Mateusz Matuszkowiak2820c662018-11-21 12:07:25 +010053 status=400,
54 mimetype='application/json')
55
56 app.logger.info('Received requests: {}'.format(data))
57
Michal Kobusaa3accf2019-06-05 12:25:09 +020058 cases = []
Mateusz Matuszkowiak2820c662018-11-21 12:07:25 +010059 for alert in data['alerts']:
Michal Kobusaa3accf2019-06-05 12:25:09 +020060 try:
61 alert['labels']['env_id'] = sf_cli.environment
Michal Kobuse7589f72020-09-11 14:29:37 +020062 fields, action = alert_fields_and_action(
Michal Kobus28b37f92022-06-14 11:06:24 +020063 alert,
64 add_links=settings.ADD_LINKS,
65 cluster_id=settings.CLUSTER_ID)
Michal Kobusaa3accf2019-06-05 12:25:09 +020066 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 Matuszkowiak2820c662018-11-21 12:07:25 +010072
Michal Kobusaa3accf2019-06-05 12:25:09 +020073 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 Matuszkowiak2820c662018-11-21 12:07:25 +010083
Michal Kobusaa3accf2019-06-05 12:25:09 +020084 if len(cases) == 1:
85 return jsonify(cases[0])
Mateusz Matuszkowiak2820c662018-11-21 12:07:25 +010086 return jsonify(cases)
87
88
89if __name__ == '__main__':
90 app.run()