blob: 0996d4abd92ce5fe0615d739e6b76b99b8f1c346 [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
Michal Kobus25363272023-09-19 12:13:17 +020030with app.app_context():
31 sf_cli.auth()
32
33
Michal Kobus7187d6b2023-07-11 11:56:06 +020034@app.route('/metrics', methods=['GET'])
35def metrics():
36 return Response(generate_latest(registry),
37 mimetype=CONTENT_TYPE_LATEST)
Mateusz Matuszkowiak2820c662018-11-21 12:07:25 +010038
39
Michal Kobus819cf022018-11-29 16:39:22 +010040@app.route('/info', methods=['GET'])
41def info():
Michal Kobus73d33522018-12-10 11:41:13 +010042 return jsonify({
43 'version': settings.VERSION,
44 'hashing': sf_cli.hash_func.__name__
45 })
Michal Kobus819cf022018-11-29 16:39:22 +010046
47
Mateusz Matuszkowiak2820c662018-11-21 12:07:25 +010048@app.route('/hook', methods=['POST'])
49def webhook_receiver():
50
51 try:
52 data = json.loads(request.data)
53 except ValueError:
Michal Kobus17726ae2018-11-27 12:59:55 +010054 msg = 'Invalid request data: {}.'.format(request.data)
55 app.logger.error(msg)
56 return Response(json.dumps({'error': msg}),
Mateusz Matuszkowiak2820c662018-11-21 12:07:25 +010057 status=400,
58 mimetype='application/json')
59
60 app.logger.info('Received requests: {}'.format(data))
61
Michal Kobusaa3accf2019-06-05 12:25:09 +020062 cases = []
Mateusz Matuszkowiak2820c662018-11-21 12:07:25 +010063 for alert in data['alerts']:
Michal Kobusaa3accf2019-06-05 12:25:09 +020064 try:
65 alert['labels']['env_id'] = sf_cli.environment
Michal Kobuse7589f72020-09-11 14:29:37 +020066 fields, action = alert_fields_and_action(
Michal Kobus28b37f92022-06-14 11:06:24 +020067 alert,
68 add_links=settings.ADD_LINKS,
69 cluster_id=settings.CLUSTER_ID)
Michal Kobusaa3accf2019-06-05 12:25:09 +020070 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 Matuszkowiak2820c662018-11-21 12:07:25 +010076
Michal Kobusaa3accf2019-06-05 12:25:09 +020077 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 Matuszkowiak2820c662018-11-21 12:07:25 +010087
Michal Kobusaa3accf2019-06-05 12:25:09 +020088 if len(cases) == 1:
89 return jsonify(cases[0])
Mateusz Matuszkowiak2820c662018-11-21 12:07:25 +010090 return jsonify(cases)
91
92
93if __name__ == '__main__':
94 app.run()