io.github.resilience4j.bulkhead.Bulkhead Java Examples

The following examples show how to use io.github.resilience4j.bulkhead.Bulkhead. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: DecoratorsTest.java    From resilience4j with Apache License 2.0 6 votes vote down vote up
@Test
public void testDecorateSupplier() {
    given(helloWorldService.returnHelloWorld()).willReturn("Hello world");
    CircuitBreaker circuitBreaker = CircuitBreaker.ofDefaults("helloBackend");
    Supplier<String> decoratedSupplier = Decorators
        .ofSupplier(() -> helloWorldService.returnHelloWorld())
        .withCircuitBreaker(circuitBreaker)
        .withRetry(Retry.ofDefaults("id"))
        .withRateLimiter(RateLimiter.ofDefaults("testName"))
        .withBulkhead(Bulkhead.ofDefaults("testName"))
        .decorate();

    String result = decoratedSupplier.get();

    assertThat(result).isEqualTo("Hello world");
    CircuitBreaker.Metrics metrics = circuitBreaker.getMetrics();
    assertThat(metrics.getNumberOfBufferedCalls()).isEqualTo(1);
    assertThat(metrics.getNumberOfSuccessfulCalls()).isEqualTo(1);
    then(helloWorldService).should(times(1)).returnHelloWorld();
}
 
Example #2
Source File: DecoratorsTest.java    From resilience4j with Apache License 2.0 6 votes vote down vote up
@Test
public void testDecorateCompletionStage() throws ExecutionException, InterruptedException {
    given(helloWorldService.returnHelloWorld()).willReturn("Hello world");
    CircuitBreaker circuitBreaker = CircuitBreaker.ofDefaults("helloBackend");
    Supplier<CompletionStage<String>> completionStageSupplier =
        () -> CompletableFuture.supplyAsync(helloWorldService::returnHelloWorld);
    CompletionStage<String> completionStage = Decorators
        .ofCompletionStage(completionStageSupplier)
        .withCircuitBreaker(circuitBreaker)
        .withRetry(Retry.ofDefaults("id"), Executors.newSingleThreadScheduledExecutor())
        .withBulkhead(Bulkhead.ofDefaults("testName"))
        .get();

    String value = completionStage.toCompletableFuture().get();

    assertThat(value).isEqualTo("Hello world");
    CircuitBreaker.Metrics metrics = circuitBreaker.getMetrics();
    assertThat(metrics.getNumberOfBufferedCalls()).isEqualTo(1);
    assertThat(metrics.getNumberOfSuccessfulCalls()).isEqualTo(1);
    then(helloWorldService).should(times(1)).returnHelloWorld();
}
 
Example #3
Source File: DecoratorsTest.java    From resilience4j with Apache License 2.0 6 votes vote down vote up
@Test
public void testDecorateRunnable() {
    CircuitBreaker circuitBreaker = CircuitBreaker.ofDefaults("helloBackend");
    Runnable decoratedRunnable = Decorators
        .ofRunnable(() -> helloWorldService.sayHelloWorld())
        .withCircuitBreaker(circuitBreaker)
        .withRetry(Retry.ofDefaults("id"))
        .withRateLimiter(RateLimiter.ofDefaults("testName"))
        .withBulkhead(Bulkhead.ofDefaults("testName"))
        .decorate();

    decoratedRunnable.run();

    CircuitBreaker.Metrics metrics = circuitBreaker.getMetrics();
    assertThat(metrics.getNumberOfBufferedCalls()).isEqualTo(1);
    assertThat(metrics.getNumberOfSuccessfulCalls()).isEqualTo(1);
    then(helloWorldService).should(times(1)).sayHelloWorld();
}
 
Example #4
Source File: AbstractBulkheadMetricsTest.java    From resilience4j with Apache License 2.0 6 votes vote down vote up
@Test
public void bulkheadConfigChangeAffectsTheMaxAllowedConcurrentCallsValue() {
    Bulkhead bulkhead = givenMetricRegistry("testPre", metricRegistry);
    // Then make sure that configured value is reported as max allowed concurrent calls
    assertThat(
        metricRegistry.getGauges().get("testPre.testBulkhead.max_allowed_concurrent_calls")
            .getValue())
        .isEqualTo(DEFAULT_MAX_CONCURRENT_CALLS);

    // And when the config is changed
    BulkheadConfig newConfig = BulkheadConfig.custom()
        .maxConcurrentCalls(DEFAULT_MAX_CONCURRENT_CALLS + 50)
        .build();
    bulkhead.changeConfig(newConfig);

    // Then the new config value gets reported
    assertThat(
        metricRegistry.getGauges().get("testPre.testBulkhead.max_allowed_concurrent_calls")
            .getValue())
        .isEqualTo(newConfig.getMaxConcurrentCalls());
}
 
Example #5
Source File: BulkheadMetricsCollector.java    From resilience4j with Apache License 2.0 6 votes vote down vote up
@Override
public List<MetricFamilySamples> collect() {
    GaugeMetricFamily availableCallsFamily = new GaugeMetricFamily(
        names.getAvailableConcurrentCallsMetricName(),
        "The number of available concurrent calls",
        LabelNames.NAME
    );
    GaugeMetricFamily maxAllowedCallsFamily = new GaugeMetricFamily(
        names.getMaxAllowedConcurrentCallsMetricName(),
        "The maximum number of allowed concurrent calls",
        LabelNames.NAME
    );

    for (Bulkhead bulkhead : bulkheadRegistry.getAllBulkheads()) {
        List<String> labelValues = singletonList(bulkhead.getName());
        availableCallsFamily
            .addMetric(labelValues, bulkhead.getMetrics().getAvailableConcurrentCalls());
        maxAllowedCallsFamily
            .addMetric(labelValues, bulkhead.getMetrics().getMaxAllowedConcurrentCalls());
    }

    return asList(availableCallsFamily, maxAllowedCallsFamily);
}
 
Example #6
Source File: DecoratorsTest.java    From resilience4j with Apache License 2.0 6 votes vote down vote up
@Test
public void testExecuteConsumer() {
    CircuitBreaker circuitBreaker = CircuitBreaker.ofDefaults("helloBackend");
    Decorators.DecorateConsumer<String> decoratedConsumer =
        Decorators.ofConsumer((String input) -> helloWorldService
            .sayHelloWorldWithName(input))
            .withCircuitBreaker(circuitBreaker)
            .withBulkhead(Bulkhead.ofDefaults("testName"))
            .withRateLimiter(RateLimiter.ofDefaults("testName"));

    decoratedConsumer.accept("test");

    CircuitBreaker.Metrics metrics = circuitBreaker.getMetrics();
    assertThat(metrics.getNumberOfBufferedCalls()).isEqualTo(1);
    assertThat(metrics.getNumberOfSuccessfulCalls()).isEqualTo(1);
    then(helloWorldService).should(times(1)).sayHelloWorldWithName("test");
}
 
Example #7
Source File: DecoratorsTest.java    From resilience4j with Apache License 2.0 6 votes vote down vote up
@Test
public void testDecorateFunction() {
    given(helloWorldService.returnHelloWorldWithName("Name")).willReturn("Hello world Name");
    CircuitBreaker circuitBreaker = CircuitBreaker.ofDefaults("helloBackend");
    Function<String, String> decoratedFunction = Decorators
        .ofFunction(helloWorldService::returnHelloWorldWithName)
        .withCircuitBreaker(circuitBreaker)
        .withRetry(Retry.ofDefaults("id"))
        .withRateLimiter(RateLimiter.ofDefaults("testName"))
        .withBulkhead(Bulkhead.ofDefaults("testName"))
        .decorate();

    String result = decoratedFunction.apply("Name");

    assertThat(result).isEqualTo("Hello world Name");
    CircuitBreaker.Metrics metrics = circuitBreaker.getMetrics();
    assertThat(metrics.getNumberOfBufferedCalls()).isEqualTo(1);
    assertThat(metrics.getNumberOfSuccessfulCalls()).isEqualTo(1);
}
 
Example #8
Source File: DecoratorsTest.java    From resilience4j with Apache License 2.0 6 votes vote down vote up
@Test
public void testDecoratorBuilderWithRetry() {
    given(helloWorldService.returnHelloWorld()).willThrow(new RuntimeException("BAM!"));
    CircuitBreaker circuitBreaker = CircuitBreaker.ofDefaults("helloBackend");
    Supplier<String> decoratedSupplier = Decorators
        .ofSupplier(() -> helloWorldService.returnHelloWorld())
        .withCircuitBreaker(circuitBreaker)
        .withRetry(Retry.ofDefaults("id"))
        .withBulkhead(Bulkhead.ofDefaults("testName"))
        .decorate();

    Try.of(decoratedSupplier::get);

    CircuitBreaker.Metrics metrics = circuitBreaker.getMetrics();
    assertThat(metrics.getNumberOfBufferedCalls()).isEqualTo(3);
    assertThat(metrics.getNumberOfFailedCalls()).isEqualTo(3);
    then(helloWorldService).should(times(3)).returnHelloWorld();
}
 
Example #9
Source File: DecoratorsTest.java    From resilience4j with Apache License 2.0 6 votes vote down vote up
@Test
public void testDecorateCallable() throws Exception {
    given(helloWorldService.returnHelloWorldWithException()).willReturn("Hello world");
    CircuitBreaker circuitBreaker = CircuitBreaker.ofDefaults("helloBackend");
    Callable<String> decoratedCallable = Decorators
        .ofCallable(() -> helloWorldService.returnHelloWorldWithException())
        .withCircuitBreaker(circuitBreaker)
        .withRetry(Retry.ofDefaults("id"))
        .withRateLimiter(RateLimiter.ofDefaults("testName"))
        .withBulkhead(Bulkhead.ofDefaults("testName"))
        .decorate();

    String result = decoratedCallable.call();

    assertThat(result).isEqualTo("Hello world");
    CircuitBreaker.Metrics metrics = circuitBreaker.getMetrics();
    assertThat(metrics.getNumberOfBufferedCalls()).isEqualTo(1);
    assertThat(metrics.getNumberOfSuccessfulCalls()).isEqualTo(1);
    then(helloWorldService).should(times(1)).returnHelloWorldWithException();
}
 
Example #10
Source File: BulkheadMetricsPublisher.java    From resilience4j with Apache License 2.0 6 votes vote down vote up
@Override
public void publishMetrics(Bulkhead bulkhead) {
    String name = bulkhead.getName();

    //number of available concurrent calls as an integer
    String availableConcurrentCalls = name(prefix, name, AVAILABLE_CONCURRENT_CALLS);
    String maxAllowedConcurrentCalls = name(prefix, name, MAX_ALLOWED_CONCURRENT_CALLS);

    metricRegistry.register(availableConcurrentCalls,
        (Gauge<Integer>) () -> bulkhead.getMetrics().getAvailableConcurrentCalls());
    metricRegistry.register(maxAllowedConcurrentCalls,
        (Gauge<Integer>) () -> bulkhead.getMetrics().getMaxAllowedConcurrentCalls());

    List<String> metricNames = Arrays
        .asList(availableConcurrentCalls, maxAllowedConcurrentCalls);
    metricsNameMap.put(name, new HashSet<>(metricNames));
}
 
Example #11
Source File: TaggedBulkheadMetricsTest.java    From resilience4j with Apache License 2.0 6 votes vote down vote up
@Test
public void shouldAddMetricsForANewlyCreatedRetry() {
    Bulkhead newBulkhead = bulkheadRegistry.bulkhead("backendB");

    assertThat(taggedBulkheadMetrics.meterIdMap).containsKeys("backendA", "backendB");
    assertThat(taggedBulkheadMetrics.meterIdMap.get("backendA")).hasSize(2);
    assertThat(taggedBulkheadMetrics.meterIdMap.get("backendB")).hasSize(2);

    List<Meter> meters = meterRegistry.getMeters();
    assertThat(meters).hasSize(4);

    Collection<Gauge> gauges = meterRegistry
        .get(DEFAULT_BULKHEAD_MAX_ALLOWED_CONCURRENT_CALLS_METRIC_NAME).gauges();

    Optional<Gauge> successful = findMeterByNamesTag(gauges, newBulkhead.getName());
    assertThat(successful).isPresent();
    assertThat(successful.get().value())
        .isEqualTo(newBulkhead.getMetrics().getMaxAllowedConcurrentCalls());
}
 
Example #12
Source File: ReactorBulkheadAspectExt.java    From resilience4j with Apache License 2.0 6 votes vote down vote up
/**
 * handle the Spring web flux (Flux /Mono) return types AOP based into reactor bulk head See
 * {@link Bulkhead} for details.
 *
 * @param proceedingJoinPoint Spring AOP proceedingJoinPoint
 * @param bulkhead            the configured bulkhead
 * @param methodName          the method name
 * @return the result object
 * @throws Throwable exception in case of faulty flow
 */
@Override
public Object handle(ProceedingJoinPoint proceedingJoinPoint, Bulkhead bulkhead,
    String methodName) throws Throwable {
    Object returnValue = proceedingJoinPoint.proceed();
    if (Flux.class.isAssignableFrom(returnValue.getClass())) {
        Flux<?> fluxReturnValue = (Flux<?>) returnValue;
        return fluxReturnValue.transformDeferred(BulkheadOperator.of(bulkhead));
    } else if (Mono.class.isAssignableFrom(returnValue.getClass())) {
        Mono<?> monoReturnValue = (Mono<?>) returnValue;
        return monoReturnValue.transformDeferred(BulkheadOperator.of(bulkhead));
    } else {
        logger.error("Unsupported type for Reactor BulkHead {}",
            returnValue.getClass().getTypeName());
        throw new IllegalArgumentException(
            "Not Supported type for the BulkHead in Reactor :" + returnValue.getClass()
                .getName());

    }
}
 
Example #13
Source File: TaggedBulkheadMetricsTest.java    From resilience4j with Apache License 2.0 6 votes vote down vote up
@Test
public void shouldReplaceMetrics() {
    Collection<Gauge> gauges = meterRegistry
        .get(DEFAULT_BULKHEAD_MAX_ALLOWED_CONCURRENT_CALLS_METRIC_NAME).gauges();

    Optional<Gauge> successful = findMeterByNamesTag(gauges, bulkhead.getName());
    assertThat(successful).isPresent();
    assertThat(successful.get().value())
        .isEqualTo(bulkhead.getMetrics().getMaxAllowedConcurrentCalls());

    Bulkhead newBulkhead = Bulkhead.of(bulkhead.getName(), BulkheadConfig.custom()
        .maxConcurrentCalls(100).build());

    bulkheadRegistry.replace(bulkhead.getName(), newBulkhead);

    gauges = meterRegistry.get(DEFAULT_BULKHEAD_MAX_ALLOWED_CONCURRENT_CALLS_METRIC_NAME)
        .gauges();

    successful = findMeterByNamesTag(gauges, newBulkhead.getName());
    assertThat(successful).isPresent();
    assertThat(successful.get().value())
        .isEqualTo(newBulkhead.getMetrics().getMaxAllowedConcurrentCalls());

}
 
Example #14
Source File: TaggedBulkheadMetricsPublisherTest.java    From resilience4j with Apache License 2.0 6 votes vote down vote up
@Test
public void shouldAddMetricsForANewlyCreatedRetry() {
    Bulkhead newBulkhead = bulkheadRegistry.bulkhead("backendB");

    assertThat(taggedBulkheadMetricsPublisher.meterIdMap).containsKeys("backendA", "backendB");
    assertThat(taggedBulkheadMetricsPublisher.meterIdMap.get("backendA")).hasSize(2);
    assertThat(taggedBulkheadMetricsPublisher.meterIdMap.get("backendB")).hasSize(2);

    List<Meter> meters = meterRegistry.getMeters();
    assertThat(meters).hasSize(4);

    Collection<Gauge> gauges = meterRegistry
        .get(DEFAULT_BULKHEAD_MAX_ALLOWED_CONCURRENT_CALLS_METRIC_NAME).gauges();

    Optional<Gauge> successful = findMeterByNamesTag(gauges, newBulkhead.getName());
    assertThat(successful).isPresent();
    assertThat(successful.get().value())
        .isEqualTo(newBulkhead.getMetrics().getMaxAllowedConcurrentCalls());
}
 
Example #15
Source File: TaggedBulkheadMetricsPublisherTest.java    From resilience4j with Apache License 2.0 6 votes vote down vote up
@Test
public void shouldReplaceMetrics() {
    Collection<Gauge> gauges = meterRegistry
        .get(DEFAULT_BULKHEAD_MAX_ALLOWED_CONCURRENT_CALLS_METRIC_NAME).gauges();

    Optional<Gauge> successful = findMeterByNamesTag(gauges, bulkhead.getName());
    assertThat(successful).isPresent();
    assertThat(successful.get().value())
        .isEqualTo(bulkhead.getMetrics().getMaxAllowedConcurrentCalls());

    Bulkhead newBulkhead = Bulkhead.of(bulkhead.getName(), BulkheadConfig.custom()
        .maxConcurrentCalls(100).build());

    bulkheadRegistry.replace(bulkhead.getName(), newBulkhead);

    gauges = meterRegistry.get(DEFAULT_BULKHEAD_MAX_ALLOWED_CONCURRENT_CALLS_METRIC_NAME)
        .gauges();

    successful = findMeterByNamesTag(gauges, newBulkhead.getName());
    assertThat(successful).isPresent();
    assertThat(successful.get().value())
        .isEqualTo(newBulkhead.getMetrics().getMaxAllowedConcurrentCalls());

}
 
Example #16
Source File: Resilience4jUnitTest.java    From tutorials with MIT License 5 votes vote down vote up
@Test
public void whenBulkheadIsUsed_thenItWorksAsExpected() throws InterruptedException {
    BulkheadConfig config = BulkheadConfig.custom().maxConcurrentCalls(1).build();
    BulkheadRegistry registry = BulkheadRegistry.of(config);
    Bulkhead bulkhead = registry.bulkhead("my");
    Function<Integer, Integer> decorated = Bulkhead.decorateFunction(bulkhead, service::process);

    Future<?> taskInProgress = callAndBlock(decorated);
    try {
        assertThat(bulkhead.isCallPermitted()).isFalse();
    } finally {
        taskInProgress.cancel(true);
    }
}
 
Example #17
Source File: BulkheadMetricsPublisherTest.java    From resilience4j with Apache License 2.0 5 votes vote down vote up
@Override
protected Bulkhead givenMetricRegistry(String prefix, MetricRegistry metricRegistry) {
    BulkheadRegistry bulkheadRegistry =
        BulkheadRegistry.of(BulkheadConfig.ofDefaults(),
            new BulkheadMetricsPublisher(prefix, metricRegistry));

    return bulkheadRegistry.bulkhead("testBulkhead");
}
 
Example #18
Source File: AbstractBulkheadConfigurationOnMissingBean.java    From resilience4j with Apache License 2.0 5 votes vote down vote up
@Bean
@ConditionalOnMissingBean
public BulkheadRegistry bulkheadRegistry(
    BulkheadConfigurationProperties bulkheadConfigurationProperties,
    EventConsumerRegistry<BulkheadEvent> bulkheadEventConsumerRegistry,
    RegistryEventConsumer<Bulkhead> bulkheadRegistryEventConsumer,
    @Qualifier("compositeBulkheadCustomizer") CompositeCustomizer<BulkheadConfigCustomizer> compositeBulkheadCustomizer) {
    return bulkheadConfiguration
        .bulkheadRegistry(bulkheadConfigurationProperties, bulkheadEventConsumerRegistry,
            bulkheadRegistryEventConsumer, compositeBulkheadCustomizer);
}
 
Example #19
Source File: BulkheadMetrics.java    From resilience4j with Apache License 2.0 5 votes vote down vote up
private BulkheadMetrics(String prefix, Iterable<Bulkhead> bulkheads,
    MetricRegistry metricRegistry) {
    requireNonNull(prefix);
    requireNonNull(bulkheads);
    requireNonNull(metricRegistry);
    this.metricRegistry = metricRegistry;
    bulkheads.forEach(bulkhead -> {
        String name = bulkhead.getName();
        //number of available concurrent calls as an integer
        metricRegistry.register(name(prefix, name, AVAILABLE_CONCURRENT_CALLS),
            (Gauge<Integer>) () -> bulkhead.getMetrics().getAvailableConcurrentCalls());
        metricRegistry.register(name(prefix, name, MAX_ALLOWED_CONCURRENT_CALLS),
            (Gauge<Integer>) () -> bulkhead.getMetrics().getMaxAllowedConcurrentCalls());
    });
}
 
Example #20
Source File: ReactorBulkheadAspectExtTest.java    From resilience4j with Apache License 2.0 5 votes vote down vote up
@Test
public void testReactorTypes() throws Throwable {
    Bulkhead bulkhead = Bulkhead.ofDefaults("test");

    when(proceedingJoinPoint.proceed()).thenReturn(Mono.just("Test"));
    assertThat(reactorBulkheadAspectExt.handle(proceedingJoinPoint, bulkhead, "testMethod"))
        .isNotNull();

    when(proceedingJoinPoint.proceed()).thenReturn(Flux.just("Test"));
    assertThat(reactorBulkheadAspectExt.handle(proceedingJoinPoint, bulkhead, "testMethod"))
        .isNotNull();
}
 
Example #21
Source File: ConcurrentBulkheadTest.java    From resilience4j with Apache License 2.0 5 votes vote down vote up
public ConcurrentBulkheadTest() {

        bulkhead = Bulkhead.of("test", BulkheadConfig.custom().maxConcurrentCalls(1).build());

        callRejectectedEventSubscriber = RxJava2Adapter.toFlowable(bulkhead.getEventPublisher())
            .filter(event -> event.getEventType() == Type.CALL_REJECTED)
            .map(BulkheadEvent::getEventType)
            .test();
    }
 
Example #22
Source File: SemaphoreBulkheadTest.java    From resilience4j with Apache License 2.0 5 votes vote down vote up
@Before
public void setUp() {
    BulkheadConfig config = BulkheadConfig.custom()
        .maxConcurrentCalls(2)
        .maxWaitDuration(Duration.ofMillis(0))
        .build();
    bulkhead = Bulkhead.of("test", config);
    testSubscriber = RxJava2Adapter.toFlowable(bulkhead.getEventPublisher())
        .map(BulkheadEvent::getEventType)
        .test();
}
 
Example #23
Source File: BulkheadMetricsPublisherTest.java    From resilience4j with Apache License 2.0 5 votes vote down vote up
@Override
protected Bulkhead givenMetricRegistry(MetricRegistry metricRegistry) {
    BulkheadRegistry bulkheadRegistry = BulkheadRegistry
        .of(BulkheadConfig.ofDefaults(), new BulkheadMetricsPublisher(metricRegistry));

    return bulkheadRegistry.bulkhead("testBulkhead");
}
 
Example #24
Source File: Resilience4jFeignBulkheadTest.java    From resilience4j with Apache License 2.0 5 votes vote down vote up
@Before
public void setUp() {
    bulkhead = spy(Bulkhead.of("bulkheadTest", BulkheadConfig.ofDefaults()));
    final FeignDecorators decorators = FeignDecorators.builder()
            .withBulkhead(bulkhead)
            .build();
    testService = Resilience4jFeign.builder(decorators)
            .target(TestService.class, MOCK_URL);

}
 
Example #25
Source File: BulkheadSubscriber.java    From resilience4j with Apache License 2.0 5 votes vote down vote up
BulkheadSubscriber(Bulkhead bulkhead,
    CoreSubscriber<? super T> downstreamSubscriber,
    boolean singleProducer) {
    super(downstreamSubscriber);
    this.bulkhead = requireNonNull(bulkhead);
    this.singleProducer = singleProducer;
}
 
Example #26
Source File: RxJava2BulkheadAspectExt.java    From resilience4j with Apache License 2.0 5 votes vote down vote up
/**
 * @param proceedingJoinPoint Spring AOP proceedingJoinPoint
 * @param bulkhead            the configured bulkhead
 * @param methodName          the method name
 * @return the result object
 * @throws Throwable exception in case of faulty flow
 */
@Override
public Object handle(ProceedingJoinPoint proceedingJoinPoint, Bulkhead bulkhead,
    String methodName) throws Throwable {
    BulkheadOperator<?> bulkheadOperator = BulkheadOperator.of(bulkhead);
    Object returnValue = proceedingJoinPoint.proceed();
    return executeRxJava2Aspect(bulkheadOperator, returnValue);
}
 
Example #27
Source File: TaggedBulkheadMetrics.java    From resilience4j with Apache License 2.0 5 votes vote down vote up
@Override
public void bindTo(MeterRegistry registry) {
    for (Bulkhead bulkhead : bulkheadRegistry.getAllBulkheads()) {
        addMetrics(registry, bulkhead);
    }
    bulkheadRegistry.getEventPublisher()
        .onEntryAdded(event -> addMetrics(registry, event.getAddedEntry()));
    bulkheadRegistry.getEventPublisher()
        .onEntryRemoved(event -> removeMetrics(registry, event.getRemovedEntry().getName()));
    bulkheadRegistry.getEventPublisher().onEntryReplaced(event -> {
        removeMetrics(registry, event.getOldEntry().getName());
        addMetrics(registry, event.getNewEntry());
    });
}
 
Example #28
Source File: TaggedBulkheadMetricsTest.java    From resilience4j with Apache License 2.0 5 votes vote down vote up
@Test
public void customTagsShouldBeAdded() {
    Bulkhead bulkheadC = bulkheadRegistry.bulkhead("backendC", io.vavr.collection.HashMap.of("key1", "value1"));
    // record some basic stats
    bulkheadC.tryAcquirePermission();
    bulkheadC.tryAcquirePermission();

    List<Meter> meters = meterRegistry.getMeters();
    assertThat(meters).hasSize(4);
    final RequiredSearch match = meterRegistry.get(DEFAULT_BULKHEAD_MAX_ALLOWED_CONCURRENT_CALLS_METRIC_NAME).tags("key1", "value1");
    assertThat(match).isNotNull();
}
 
Example #29
Source File: TaggedBulkheadMetricsPublisherTest.java    From resilience4j with Apache License 2.0 5 votes vote down vote up
@Test
public void testReplaceNewMeter(){
    Bulkhead oldOne = Bulkhead.of("backendC", BulkheadConfig.ofDefaults());
    // add meters of old
    taggedBulkheadMetricsPublisher.addMetrics(meterRegistry, oldOne);
    // one success call
    oldOne.tryAcquirePermission();

    assertThat(taggedBulkheadMetricsPublisher.meterIdMap).containsKeys("backendC");
    assertThat(taggedBulkheadMetricsPublisher.meterIdMap.get("backendC")).hasSize(2);
    Collection<Gauge> gauges = meterRegistry
        .get(DEFAULT_BULKHEAD_MAX_ALLOWED_CONCURRENT_CALLS_METRIC_NAME).gauges();
    Optional<Gauge> successful = findMeterByNamesTag(gauges, oldOne.getName());
    assertThat(successful).isPresent();
    assertThat(successful.get().value())
        .isEqualTo(oldOne.getMetrics().getMaxAllowedConcurrentCalls());

    Bulkhead newOne = Bulkhead.of("backendC", BulkheadConfig.ofDefaults());

    // add meters of new
    taggedBulkheadMetricsPublisher.addMetrics(meterRegistry, newOne);
    // three success call
    newOne.tryAcquirePermission();
    newOne.tryAcquirePermission();
    newOne.tryAcquirePermission();

    assertThat(taggedBulkheadMetricsPublisher.meterIdMap).containsKeys("backendC");
    assertThat(taggedBulkheadMetricsPublisher.meterIdMap.get("backendC")).hasSize(2);
    gauges = meterRegistry
        .get(DEFAULT_BULKHEAD_MAX_ALLOWED_CONCURRENT_CALLS_METRIC_NAME).gauges();
    successful = findMeterByNamesTag(gauges, newOne.getName());
    assertThat(successful).isPresent();
    assertThat(successful.get().value())
        .isEqualTo(newOne.getMetrics().getMaxAllowedConcurrentCalls());

}
 
Example #30
Source File: BulkheadConfiguration.java    From resilience4j with Apache License 2.0 5 votes vote down vote up
@Bean
@Primary
public RegistryEventConsumer<Bulkhead> bulkheadRegistryEventConsumer(
    Optional<List<RegistryEventConsumer<Bulkhead>>> optionalRegistryEventConsumers) {
    return new CompositeRegistryEventConsumer<>(
        optionalRegistryEventConsumers.orElseGet(ArrayList::new));
}