Add time-limited caching
Update:
- add caching 2MB size max, kept for 5 minutes
- add re-auth for Salesforce connection error
- remove unused "status" from create_case method
Change-Id: I84bec7ce86ef389ddfcad2d26613f135b80b682e
Related-bug: PROD-25695 (PROD:25695)
diff --git a/requirements.txt b/requirements.txt
index 94ceb82..b797b8a 100644
--- a/requirements.txt
+++ b/requirements.txt
@@ -1,4 +1,5 @@
asn1crypto==0.24.0
+cachetools==3.0.0
certifi==2018.10.15
cffi==1.11.5
chardet==3.0.4
diff --git a/sf_notifier/helpers.py b/sf_notifier/helpers.py
index d1a655b..295c566 100644
--- a/sf_notifier/helpers.py
+++ b/sf_notifier/helpers.py
@@ -44,6 +44,5 @@
# Order of keys matters
fields.append(_format_subject(alert))
fields.append(_format_description(alert))
- fields.append(alert['status'])
fields.append(alert['labels'])
return fields, action
diff --git a/sf_notifier/salesforce/client.py b/sf_notifier/salesforce/client.py
index ae265bc..63d138f 100644
--- a/sf_notifier/salesforce/client.py
+++ b/sf_notifier/salesforce/client.py
@@ -18,9 +18,12 @@
import os
import uuid
+from cachetools import TTLCache
+
from prometheus_client import Counter, Gauge
-import requests
+from requests import Session
+from requests import exceptions as req_exceptions
from simple_salesforce import Salesforce
from simple_salesforce import exceptions as sf_exceptions
@@ -59,6 +62,9 @@
except sf_exceptions.SalesforceExpiredSession:
logger.warning('Salesforce session expired.')
self._auth()
+ except req_exceptions.ConnectionError:
+ logger.error('Salesforce connection error.')
+ self._auth()
return method(self, *args, **kwargs)
return wrapper
@@ -75,12 +81,12 @@
'sf_error_count': Counter('sf_error_count', 'sf-notifier'),
'sf_request_count': Counter('sf_request_count', 'sf-notifier')
}
- self.session = requests.Session()
+ self.session = Session()
self.config = self._validate_config(config)
self.hash_func = self._hash_func()
self.environment = self.config.pop('environment_id')
self._auth()
- self._registered_alerts = {}
+ self._registered_alerts = TTLCache(maxsize=2048, ttl=300)
@staticmethod
def _hash_func():
@@ -202,19 +208,20 @@
logger.warning('Alert ID: {} was already solved.'.format(alert_id))
- def create_case(self, subject, body, status, labels):
+ def create_case(self, subject, body, labels):
alert_id = self._get_alert_id(labels)
error_code, case_id = self._create_case(subject, body,
labels, alert_id)
- response = {'case_id': case_id, 'alert_id': alert_id,
- 'status': 'created'}
+ self._create_feed_item(subject, body, case_id)
- if error_code != 2:
- self._create_feed_item(subject, body, case_id)
+ response = {'case_id': case_id, 'alert_id': alert_id}
+
if error_code == 1:
response['status'] = 'duplicate'
+ else:
+ response['status'] = 'created'
return response
def close_case(self, labels):