@Test public void triggerPagerDutyTest() { Check check = new Check() .withId("123") .withEnabled(true) .withName("test-check") .withState(AlertType.ERROR); Subscription subscription = new Subscription() .withEnabled(true) .withType(SubscriptionType.PAGERDUTY) .withTarget("servicekey123"); Alert alert = new Alert() .withId("890") .withValue(new BigDecimal("1.0")) .withTimestamp(new DateTime()) .withFromType(AlertType.OK) .withToType(AlertType.ERROR); List<Alert> alerts = Arrays.asList(alert); BodyCapture<JsonNode> bodyCapture = new JsonBodyCapture(); clientDriver.addExpectation( onRequestTo("/generic/2010-04-15/create_event.json") .withMethod(ClientDriverRequest.Method.POST) .capturingBodyIn(bodyCapture) .withHeader("content-Type", "application/json"), giveEmptyResponse()); notificationService.sendNotification(check, subscription, alerts); JsonNode node = bodyCapture.getContent(); assertThat(node, hasJsonPath("$.service_key", is("servicekey123"))); assertThat(node, hasJsonPath("$.incident_key", is("MonitoringAlerts_123"))); assertThat(node, hasJsonPath("$.event_type", is("trigger"))); assertThat(node, hasJsonPath("$.description", is("Check 'test-check' has exceeded its threshold."))); assertThat(node, hasJsonPath("$.client", is("Seyren"))); assertThat(node, hasJsonPath("$.client_url", containsString("/#/checks/123"))); assertThat(node, hasJsonPath("$.details.CHECK", containsString("{\"id\":\"123\",\"name\":\"test-check\","))); assertThat(node, hasJsonPath("$.details.ALERTS", containsString("{\"id\":\"890\","))); assertThat(node, hasJsonPath("$.details.SEYREN_URL", containsString("/pagerduty"))); }
@Override public void sendNotification(Check check, Subscription subscription, List<Alert> alerts) throws NotificationFailedException { PagerDuty pagerDuty = PagerDuty.create(subscription.getTarget(), new RestAdapter.Builder() .setEndpoint(Endpoints.newFixedEndpoint(baseUrl)) .build()); NotifyResult result = null; try { if (check.getState() == AlertType.ERROR || check.getState() == AlertType.WARN) { Trigger trigger = new Trigger.Builder("Check '" + check.getName() + "' has exceeded its threshold.") .withIncidentKey(incidentKey(check)) .client("Seyren") .clientUrl(url(check)) .addDetails(details(check, alerts)) .build(); result = pagerDuty.notify(trigger); } else if (check.getState() == AlertType.OK) { Resolution resolution = new Resolution.Builder(incidentKey(check)) .withDescription("Check '" + check.getName() + "' has been resolved.") .addDetails(details(check, alerts)) .build(); result = pagerDuty.notify(resolution); } else { LOGGER.warn("Did not send notification to PagerDuty for check in state: {}", check.getState()); } } catch (Exception e) { throw new NotificationFailedException("Failed to send notification to PagerDuty", e); } if (result != null && !"success".equals(result.status())) { throw new NotificationFailedException("Failed to send notification to PagerDuty: '" + result.status() + "', " + result.message()); } }
PagerDutyNotificationService implements NotificationService { @Override public void sendNotification(Check check, Subscription subscription, List<Alert> alerts) throws NotificationFailedException { PagerDuty pagerDuty = PagerDuty.create(subscription.getTarget(), new RestAdapter.Builder() .setEndpoint(Endpoints.newFixedEndpoint(baseUrl)) .build()); NotifyResult result = null; try { if (check.getState() == AlertType.ERROR || check.getState() == AlertType.WARN) { Trigger trigger = new Trigger.Builder("Check '" + check.getName() + "' has exceeded its threshold.") .withIncidentKey(incidentKey(check)) .client("Seyren") .clientUrl(url(check)) .addDetails(details(check, alerts)) .build(); result = pagerDuty.notify(trigger); } else if (check.getState() == AlertType.OK) { Resolution resolution = new Resolution.Builder(incidentKey(check)) .withDescription("Check '" + check.getName() + "' has been resolved.") .addDetails(details(check, alerts)) .build(); result = pagerDuty.notify(resolution); } else { LOGGER.warn("Did not send notification to PagerDuty for check in state: {}", check.getState()); } } catch (Exception e) { throw new NotificationFailedException("Failed to send notification to PagerDuty", e); } if (result != null && !"success".equals(result.status())) { throw new NotificationFailedException("Failed to send notification to PagerDuty: '" + result.status() + "', " + result.message()); } } }
PagerDutyNotificationService implements NotificationService { @Override public void sendNotification(Check check, Subscription subscription, List<Alert> alerts) throws NotificationFailedException { PagerDuty pagerDuty = PagerDuty.create(subscription.getTarget(), new RestAdapter.Builder() .setEndpoint(Endpoints.newFixedEndpoint(baseUrl)) .build()); NotifyResult result = null; try { if (check.getState() == AlertType.ERROR || check.getState() == AlertType.WARN) { Trigger trigger = new Trigger.Builder("Check '" + check.getName() + "' has exceeded its threshold.") .withIncidentKey(incidentKey(check)) .client("Seyren") .clientUrl(url(check)) .addDetails(details(check, alerts)) .build(); result = pagerDuty.notify(trigger); } else if (check.getState() == AlertType.OK) { Resolution resolution = new Resolution.Builder(incidentKey(check)) .withDescription("Check '" + check.getName() + "' has been resolved.") .addDetails(details(check, alerts)) .build(); result = pagerDuty.notify(resolution); } else { LOGGER.warn("Did not send notification to PagerDuty for check in state: {}", check.getState()); } } catch (Exception e) { throw new NotificationFailedException("Failed to send notification to PagerDuty", e); } if (result != null && !"success".equals(result.status())) { throw new NotificationFailedException("Failed to send notification to PagerDuty: '" + result.status() + "', " + result.message()); } } @Inject  PagerDutyNotificationService(SeyrenConfig seyrenConfig); protected  PagerDutyNotificationService(SeyrenConfig seyrenConfig, String baseUrl); }
PagerDutyNotificationService implements NotificationService { @Override public void sendNotification(Check check, Subscription subscription, List<Alert> alerts) throws NotificationFailedException { PagerDuty pagerDuty = PagerDuty.create(subscription.getTarget(), new RestAdapter.Builder() .setEndpoint(Endpoints.newFixedEndpoint(baseUrl)) .build()); NotifyResult result = null; try { if (check.getState() == AlertType.ERROR || check.getState() == AlertType.WARN) { Trigger trigger = new Trigger.Builder("Check '" + check.getName() + "' has exceeded its threshold.") .withIncidentKey(incidentKey(check)) .client("Seyren") .clientUrl(url(check)) .addDetails(details(check, alerts)) .build(); result = pagerDuty.notify(trigger); } else if (check.getState() == AlertType.OK) { Resolution resolution = new Resolution.Builder(incidentKey(check)) .withDescription("Check '" + check.getName() + "' has been resolved.") .addDetails(details(check, alerts)) .build(); result = pagerDuty.notify(resolution); } else { LOGGER.warn("Did not send notification to PagerDuty for check in state: {}", check.getState()); } } catch (Exception e) { throw new NotificationFailedException("Failed to send notification to PagerDuty", e); } if (result != null && !"success".equals(result.status())) { throw new NotificationFailedException("Failed to send notification to PagerDuty: '" + result.status() + "', " + result.message()); } } @Inject  PagerDutyNotificationService(SeyrenConfig seyrenConfig); protected  PagerDutyNotificationService(SeyrenConfig seyrenConfig, String baseUrl); @Override void sendNotification(Check check, Subscription subscription, List<Alert> alerts); @Override boolean canHandle(SubscriptionType subscriptionType); }
PagerDutyNotificationService implements NotificationService { @Override public void sendNotification(Check check, Subscription subscription, List<Alert> alerts) throws NotificationFailedException { PagerDuty pagerDuty = PagerDuty.create(subscription.getTarget(), new RestAdapter.Builder() .setEndpoint(Endpoints.newFixedEndpoint(baseUrl)) .build()); NotifyResult result = null; try { if (check.getState() == AlertType.ERROR || check.getState() == AlertType.WARN) { Trigger trigger = new Trigger.Builder("Check '" + check.getName() + "' has exceeded its threshold.") .withIncidentKey(incidentKey(check)) .client("Seyren") .clientUrl(url(check)) .addDetails(details(check, alerts)) .build(); result = pagerDuty.notify(trigger); } else if (check.getState() == AlertType.OK) { Resolution resolution = new Resolution.Builder(incidentKey(check)) .withDescription("Check '" + check.getName() + "' has been resolved.") .addDetails(details(check, alerts)) .build(); result = pagerDuty.notify(resolution); } else { LOGGER.warn("Did not send notification to PagerDuty for check in state: {}", check.getState()); } } catch (Exception e) { throw new NotificationFailedException("Failed to send notification to PagerDuty", e); } if (result != null && !"success".equals(result.status())) { throw new NotificationFailedException("Failed to send notification to PagerDuty: '" + result.status() + "', " + result.message()); } } @Inject  PagerDutyNotificationService(SeyrenConfig seyrenConfig); protected  PagerDutyNotificationService(SeyrenConfig seyrenConfig, String baseUrl); @Override void sendNotification(Check check, Subscription subscription, List<Alert> alerts); @Override boolean canHandle(SubscriptionType subscriptionType);  }