Skip to content

Commit

Permalink
feat(event): Add circuit breaker for events sending. (#1233) (#1236)
Browse files Browse the repository at this point in the history
* feat(event): Add circuit breaker for events sending.

* refactor: Move send event without Circuit Breaker to the event service.

Co-authored-by: mergify[bot] <37929162+mergify[bot]@users.noreply.github.com>
(cherry picked from commit 28d0b16)

Co-authored-by: armory-abedonik <106548537+armory-abedonik@users.noreply.github.com>
  • Loading branch information
mergify[bot] and armory-abedonik committed Dec 14, 2022
1 parent 8dcc957 commit eec8bf5
Show file tree
Hide file tree
Showing 3 changed files with 80 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,6 @@
import com.netflix.spinnaker.echo.api.events.EventListener;
import com.netflix.spinnaker.echo.config.RestUrls;
import com.netflix.spinnaker.echo.jackson.EchoObjectMapper;
import com.netflix.spinnaker.kork.core.RetrySupport;
import java.time.Duration;
import java.util.HashMap;
import java.util.Map;
import org.slf4j.Logger;
Expand All @@ -44,25 +42,28 @@ class RestEventListener implements EventListener {

private RestUrls restUrls;
private RestEventTemplateEngine restEventTemplateEngine;
private RestEventService restEventService;
private Registry registry;
private RetrySupport retrySupport;

@Value("${rest.default-event-name:spinnaker_events}")
private String eventName;

@Value("${rest.default-field-name:payload}")
private String fieldName;

@Value("${rest.circuit-breaker-enabled}")
private boolean circuitBreakerEnabled;

@Autowired
RestEventListener(
RestUrls restUrls,
RestEventTemplateEngine restEventTemplateEngine,
Registry registry,
RetrySupport retrySupport) {
RestEventService restEventService,
Registry registry) {
this.restUrls = restUrls;
this.restEventTemplateEngine = restEventTemplateEngine;
this.restEventService = restEventService;
this.registry = registry;
this.retrySupport = retrySupport;
}

@Override
Expand Down Expand Up @@ -102,12 +103,11 @@ public void processEvent(Event event) {
}
}

Map<String, Object> finalEventMap = eventMap;
retrySupport.retry(
() -> service.getClient().recordEvent(finalEventMap),
service.getConfig().getRetryCount(),
Duration.ofMillis(200),
false);
if (circuitBreakerEnabled) {
restEventService.sendEventWithCircuitBreaker(eventMap, service);
} else {
restEventService.sendEvent(eventMap, service);
}
} catch (Exception e) {
if (event != null
&& event.getDetails() != null
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
/*
* Copyright 2022 Armory, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package com.netflix.spinnaker.echo.events;

import com.netflix.spinnaker.echo.config.RestUrls;
import com.netflix.spinnaker.kork.core.RetrySupport;
import io.github.resilience4j.circuitbreaker.annotation.CircuitBreaker;
import java.time.Duration;
import java.util.Map;
import lombok.RequiredArgsConstructor;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.stereotype.Component;

@Component
@RequiredArgsConstructor
@ConditionalOnProperty("rest.enabled")
public class RestEventService {

private final RetrySupport retrySupport;

@CircuitBreaker(name = "sendEvent")
public void sendEventWithCircuitBreaker(Map<String, Object> event, RestUrls.Service service) {
sendEvent(event, service);
}

public void sendEvent(Map<String, Object> event, RestUrls.Service service) {
retrySupport.retry(
() -> service.getClient().recordEvent(event),
service.getConfig().getRetryCount(),
Duration.ofMillis(200),
false);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,8 @@ import spock.lang.Subject
class RestEventListenerSpec extends Specification {

@Subject
RestEventListener listener = new RestEventListener(null, null, new NoopRegistry(), new RetrySupport())
RestEventService restEventService = new RestEventService(new RetrySupport())
RestEventListener listener = new RestEventListener(null, null, restEventService, new NoopRegistry())
Event event = new Event(content: ['uno': 'dos'])
RestService restService

Expand Down Expand Up @@ -191,4 +192,23 @@ class RestEventListenerSpec extends Specification {
it == listener.mapper.convertValue(event, Map)
})
}

void 'should send event when circuit breaker is enabled'() {
given:
RestProperties.RestEndpointConfiguration config = new RestProperties.RestEndpointConfiguration()

RestUrls.Service service = RestUrls.Service.builder()
.client(restService)
.config(config)
.build()

listener.circuitBreakerEnabled = true
listener.restUrls.services = [service]

when:
listener.processEvent(event)

then:
1 * restService.recordEvent({ it == listener.mapper.convertValue(event, Map) })
}
}

0 comments on commit eec8bf5

Please sign in to comment.