def enable_monitoring(self): """ Enable monitoring on this instance. """ if not settings.NEWRELIC_ADMIN_USER_API_KEY: self.logger.warning('Skipping monitoring setup, ' 'NEWRELIC_ADMIN_USER_API_KEY not set') return self.logger.info('Checking New Relic Synthetics monitors') # The notifications for the monitors are now set up using New Relic alert policies. Alert policies # can contain one or more alert conditions and an alert condition can be associated with # one or more Synthetics (aka availability) monitors. # # An alert policy can have one or more existing notification channels added to it. We use email # notification channels for sending out email notifications. # # The alert policy can be configured to create an incident per policy, per alert condition or per monitor and # entity (for each failure). We use the 'per-policy' setting on all the alert policies. try: alert_policy = NewRelicAlertPolicy.objects.get(instance=self) except NewRelicAlertPolicy.DoesNotExist: alert_policy_id = newrelic.add_alert_policy(self.domain) alert_policy = NewRelicAlertPolicy.objects.create( id=alert_policy_id, instance=self) self._update_url_monitors(alert_policy) self._set_email_alerts(alert_policy)
def test_add_alert_policy(self): """ Check that the add_alert_policy function adds an alert """ url = '{}.json'.format(newrelic.ALERTS_POLICIES_API_URL) policy_name = 'Test' policy_id = 1 response_json = {'policy': {'id': policy_id}} responses.add(responses.POST, url, json=response_json, status=201) self.assertEqual(newrelic.add_alert_policy(policy_name), policy_id) self.assertEqual(len(responses.calls), 1) request_json = json.loads(responses.calls[0].request.body.decode()) request_headers = responses.calls[0].request.headers self.assertEqual(request_headers['x-api-key'], 'admin-api-key') self.assertEqual(request_json, { 'policy': { 'incident_preference': 'PER_POLICY', 'name': policy_name } })