def compute(slo_config, error_budget_policy, timestamp=None, client=None, do_export=False, delete=False): """Run pipeline to compute SLO, Error Budget and Burn Rate, and export the results (if exporters are specified in the SLO config). Args: slo_config (dict): SLO configuration. error_budget_policy (dict): Error Budget policy configuration. timestamp (int, optional): UNIX timestamp. Defaults to now. client (obj, optional): Existing metrics backend client. do_export (bool, optional): Enable / Disable export. Default: False. delete (bool, optional): Enable / Disable delete mode. Default: False. """ start = time.time() if timestamp is None: timestamp = time.time() # Compute SLO, Error Budget, Burn rates and make report exporters = slo_config.get('exporters') reports = [] for step in error_budget_policy: report = SLOReport(config=slo_config, step=step, timestamp=timestamp, client=client, delete=delete) if not report.valid: continue if delete: # delete mode is enabled continue LOGGER.info(report) json_report = report.to_json() if exporters is not None and do_export is True: responses = export(json_report, exporters) json_report['exporters'] = responses reports.append(json_report) end = time.time() run_duration = round(end - start, 1) LOGGER.debug(pprint.pformat(reports)) LOGGER.debug(f'Run finished successfully in {run_duration}s.') return reports
def test_report_valid_sli_value(self): report_cfg = mock_slo_report("valid_sli_value") report = SLOReport(**report_cfg) self.assertTrue(report.valid) self.assertEqual(report.sli_measurement, report_cfg['config']['backend']['sli']) self.assertEqual(report.alert, False)
def compute(slo_config, error_budget_policy, timestamp=None, client=None, do_export=False, delete=False): """Run pipeline to compute SLO, Error Budget and Burn Rate, and export the results (if exporters are specified in the SLO config). Args: slo_config (dict): SLO configuration. error_budget_policy (dict): Error Budget policy configuration. timestamp (int, optional): UNIX timestamp. Defaults to now. client (obj, optional): Existing metrics backend client. do_export (bool, optional): Enable / Disable export. Default: False. delete (bool, optional): Enable / Disable delete mode. Default: False. """ if timestamp is None: timestamp = time.time() # Compute SLO, Error Budget, Burn rates and make report exporters = slo_config.get('exporters') reports = [] for step in error_budget_policy: report = SLOReport(config=slo_config, step=step, timestamp=timestamp, client=client, delete=delete) if report.is_empty(): # report is empty continue if delete: # delete mode is enabled continue LOGGER.info(report) json_report = report.to_json() reports.append(json_report) if exporters is not None and do_export is True: export(json_report, exporters) return reports
def test_report_invalid_backend_response_type(self): report_cfg = mock_slo_report("invalid_backend_response_type") report = SLOReport(**report_cfg) self.assertFalse(report.valid)
def test_report_no_backend_response_ratio(self): report_cfg = mock_slo_report("no_backend_response_ratio") report = SLOReport(**report_cfg) self.assertFalse(report.valid)
def test_report_no_sli_value(self): report_cfg = mock_slo_report("no_sli_value") report = SLOReport(**report_cfg) self.assertFalse(report.valid)
def test_report_not_enough_events(self): report_cfg = mock_slo_report("not_enough_events") report = SLOReport(**report_cfg) self.assertFalse(report.valid)
def test_report_no_bad_events(self): report_cfg = mock_slo_report("no_bad_events") report = SLOReport(**report_cfg) self.assertTrue(report.valid) self.assertEqual(report.sli_measurement, 1)
def test_report_enough_events(self): report_cfg = mock_slo_report("enough_events") report = SLOReport(**report_cfg) self.assertTrue(report.valid) self.assertEqual(report.sli_measurement, 0.5) self.assertEqual(report.alert, True)