diff --git a/echo-rest/src/main/java/com/netflix/spinnaker/echo/events/RestEventListener.java b/echo-rest/src/main/java/com/netflix/spinnaker/echo/events/RestEventListener.java index d30c8c037..039bce661 100644 --- a/echo-rest/src/main/java/com/netflix/spinnaker/echo/events/RestEventListener.java +++ b/echo-rest/src/main/java/com/netflix/spinnaker/echo/events/RestEventListener.java @@ -59,8 +59,11 @@ class RestEventListener implements EventListener { @Value("${rest.default-field-name:payload}") private String fieldName; + @Value("${rest.circuit-breaker-enabled:false}") + private boolean circuitBreakerEnabled; + @Autowired - public RestEventListener( + RestEventListener( RestUrls restUrls, RestEventTemplateEngine restEventTemplateEngine, RestEventService restEventService, @@ -84,7 +87,7 @@ public void processEvent(Event event) { .forEach( (service) -> { try { - if (service.getConfig().getCircuitBreakerEnabled()) { + if (service.getConfig().getCircuitBreakerEnabled() || circuitBreakerEnabled) { processEventWithCircuitBreaker(event, service); } else { Map eventMap = transformEventToMap(event, service); diff --git a/echo-rest/src/main/java/com/netflix/spinnaker/echo/events/RestEventService.java b/echo-rest/src/main/java/com/netflix/spinnaker/echo/events/RestEventService.java index 712be23c5..58144718b 100644 --- a/echo-rest/src/main/java/com/netflix/spinnaker/echo/events/RestEventService.java +++ b/echo-rest/src/main/java/com/netflix/spinnaker/echo/events/RestEventService.java @@ -72,7 +72,7 @@ public void sendEvent(Map event, RestUrls.Service service) { */ public CircuitBreaker getCircuitBreakerInstance(RestUrls.Service service) { String circuitBreakerInstance = - StringUtils.defaultString(service.getConfig().getEventName(), "default"); + StringUtils.defaultString(service.getConfig().getEventName(), "sendEvent"); return circuitBreakerRegistry.circuitBreaker(circuitBreakerInstance); } diff --git a/echo-rest/src/test/java/com/netflix/spinnaker/echo/events/RestEventListenerTest.java b/echo-rest/src/test/java/com/netflix/spinnaker/echo/events/RestEventListenerTest.java index 8ca1a77da..286f44325 100644 --- a/echo-rest/src/test/java/com/netflix/spinnaker/echo/events/RestEventListenerTest.java +++ b/echo-rest/src/test/java/com/netflix/spinnaker/echo/events/RestEventListenerTest.java @@ -44,6 +44,7 @@ class RestEventListenerTest { private RestEventListener listener; private Event event; private RestService restService; + private RestEventService restEventService; private CircuitBreakerRegistry circuitBreakerRegistry; @@ -63,12 +64,11 @@ void setup() { // Get the CircuitBreaker from the CircuitBreakerRegistry with a custom configuration circuitBreakerRegistry.circuitBreaker("circuitBreakerTest", circuitBreakerConfig); + restEventService = new RestEventService(new RetrySupport(), circuitBreakerRegistry); + listener = new RestEventListener( - new RestUrls(), - new SimpleEventTemplateEngine(), - new RestEventService(new RetrySupport(), circuitBreakerRegistry), - new NoopRegistry()); + new RestUrls(), new SimpleEventTemplateEngine(), restEventService, new NoopRegistry()); event = new Event(); event.setContent(Map.of("uno", "dos")); @@ -248,6 +248,32 @@ void shouldSendEventWhenCircuitBreakerIsEnabled() { Mockito.verify(restService, Mockito.times(1)).recordEvent(expectedEvent); } + /** + * Testing backwards compatibility. Previously if you enabled the circuit breaker via: {@code + * rest.circuit-breaker-enabled=true} all rest events used the circuit breaker with the name + * "sendEvent". This test ensures that users with legacy configs can continue using the circuit + * breaker + */ + @Test + void shouldSendEventWithCircuitBreakerWhenListenerCircuitBreakerFlagIsEnabled() { + RestProperties.RestEndpointConfiguration config = + new RestProperties.RestEndpointConfiguration(); // not enabling circuit breaker per service + + RestUrls.Service service = + RestUrls.Service.builder().client(restService).config(config).build(); + + listener.getRestUrls().setServices(List.of(service)); + listener.setCircuitBreakerEnabled(true); // enabling circuit breaker across all rest services + + Map expectedEvent = listener.getMapper().convertValue(event, Map.class); + + listener.processEvent(event); + + Assertions.assertEquals( + "sendEvent", restEventService.getCircuitBreakerInstance(service).getName()); + Mockito.verify(restService, Mockito.times(1)).recordEvent(expectedEvent); + } + @Test void shouldNotInvokeObjectMapperWhenCircuitBreakerThrowsCallNotPermittedException() { RestProperties.RestEndpointConfiguration config =