Skip to content

Commit

Permalink
Issue ReactiveX#509: Added Spring support for custom global tags (Rea…
Browse files Browse the repository at this point in the history
  • Loading branch information
Romeh authored and RobWin committed Dec 13, 2019
1 parent 98ac127 commit 6d2f870
Show file tree
Hide file tree
Showing 36 changed files with 575 additions and 266 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,22 @@ static BulkheadRegistry of(Map<String, BulkheadConfig> configs,
return new InMemoryBulkheadRegistry(configs, registryEventConsumer);
}

/**
* Creates a BulkheadRegistry with a Map of shared Bulkhead configurations and a Bulkhead
* registry event consumer.
*
* @param configs a Map of shared Bulkhead configurations.
* @param registryEventConsumer a Bulkhead registry event consumer.
* @param tags default tags to add to the registry
* @return a BulkheadRegistry with a Map of shared Bulkhead configurations and a Bulkhead
* registry event consumer.
*/
static BulkheadRegistry of(Map<String, BulkheadConfig> configs,
RegistryEventConsumer<Bulkhead> registryEventConsumer,
io.vavr.collection.Map<String, String> tags) {
return new InMemoryBulkheadRegistry(configs, registryEventConsumer, tags);
}

/**
* Creates a BulkheadRegistry with a Map of shared Bulkhead configurations and a list of
* Bulkhead registry event consumers.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
import io.github.resilience4j.core.registry.EntryRemovedEvent;
import io.github.resilience4j.core.registry.EntryReplacedEvent;
import io.github.resilience4j.core.registry.RegistryEventConsumer;
import io.vavr.Tuple;
import org.assertj.core.api.Assertions;
import org.junit.Before;
import org.junit.Test;
Expand Down Expand Up @@ -54,6 +55,13 @@ public void setUp() {
.build();
}

@Test
public void shouldInitRegistryTags() {
BulkheadRegistry registry = BulkheadRegistry.of(config,io.vavr.collection.HashMap.of("Tag1Key","Tag1Value"));
assertThat(registry.getTags()).isNotEmpty();
assertThat(registry.getTags()).containsOnly(Tuple.of("Tag1Key","Tag1Value"));
}

@Test
public void shouldReturnCustomConfig() {
BulkheadRegistry registry = BulkheadRegistry.of(config);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,23 @@ static CircuitBreakerRegistry of(Map<String, CircuitBreakerConfig> configs,
return new InMemoryCircuitBreakerRegistry(configs, registryEventConsumer);
}

/**
* Creates a CircuitBreakerRegistry with a Map of shared CircuitBreaker configurations and a
* CircuitBreaker registry event consumer.
*
* @param configs a Map of shared CircuitBreaker configurations.
* @param registryEventConsumer a CircuitBreaker registry event consumer.
* @param tags default tags to add to the registry
* @return a CircuitBreakerRegistry with a Map of shared CircuitBreaker configurations and a
* CircuitBreaker registry event consumer.
*/
static CircuitBreakerRegistry of(Map<String, CircuitBreakerConfig> configs,
RegistryEventConsumer<CircuitBreaker> registryEventConsumer,
io.vavr.collection.Map<String, String> tags) {
return new InMemoryCircuitBreakerRegistry(configs, registryEventConsumer, tags);
}


/**
* Creates a CircuitBreakerRegistry with a Map of shared CircuitBreaker configurations and a
* list of CircuitBreaker registry event consumers.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,14 @@ public InMemoryCircuitBreakerRegistry(Map<String, CircuitBreakerConfig> configs,
this.configurations.putAll(configs);
}

public InMemoryCircuitBreakerRegistry(Map<String, CircuitBreakerConfig> configs,
RegistryEventConsumer<CircuitBreaker> registryEventConsumer,
io.vavr.collection.Map<String, String> tags) {
this(configs.getOrDefault(DEFAULT_CONFIG, CircuitBreakerConfig.ofDefaults()),
registryEventConsumer, tags);
this.configurations.putAll(configs);
}

public InMemoryCircuitBreakerRegistry(Map<String, CircuitBreakerConfig> configs,
List<RegistryEventConsumer<CircuitBreaker>> registryEventConsumers) {
this(configs.getOrDefault(DEFAULT_CONFIG, CircuitBreakerConfig.ofDefaults()),
Expand Down Expand Up @@ -100,6 +108,12 @@ public InMemoryCircuitBreakerRegistry(CircuitBreakerConfig defaultConfig,
super(defaultConfig, registryEventConsumer);
}

public InMemoryCircuitBreakerRegistry(CircuitBreakerConfig defaultConfig,
RegistryEventConsumer<CircuitBreaker> registryEventConsumer,
io.vavr.collection.Map<String, String> tags) {
super(defaultConfig, registryEventConsumer, tags);
}

public InMemoryCircuitBreakerRegistry(CircuitBreakerConfig defaultConfig,
List<RegistryEventConsumer<CircuitBreaker>> registryEventConsumers) {
super(defaultConfig, registryEventConsumers);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
import io.github.resilience4j.core.registry.EntryRemovedEvent;
import io.github.resilience4j.core.registry.EntryReplacedEvent;
import io.github.resilience4j.core.registry.RegistryEventConsumer;
import io.vavr.Tuple;
import org.junit.Test;

import java.util.*;
Expand All @@ -43,6 +44,16 @@ private static Optional<EventProcessor<?>> getEventProcessor(
return Optional.empty();
}

@Test
public void shouldInitRegistryTags() {
CircuitBreakerConfig circuitBreakerConfig = CircuitBreakerConfig.ofDefaults();
Map<String, CircuitBreakerConfig> circuitBreakerConfigs = Collections
.singletonMap("default", circuitBreakerConfig);
CircuitBreakerRegistry registry = CircuitBreakerRegistry.of(circuitBreakerConfigs,new NoOpCircuitBreakerEventConsumer(),io.vavr.collection.HashMap.of("Tag1Key","Tag1Value"));
assertThat(registry.getTags()).isNotEmpty();
assertThat(registry.getTags()).containsOnly(Tuple.of("Tag1Key","Tag1Value"));
}

@Test
public void shouldReturnTheCorrectName() {
CircuitBreakerRegistry circuitBreakerRegistry = CircuitBreakerRegistry.ofDefaults();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import io.github.resilience4j.core.registry.EntryRemovedEvent;
import io.github.resilience4j.core.registry.EntryReplacedEvent;
import io.github.resilience4j.core.registry.RegistryEvent;
import io.vavr.collection.Map;

import java.util.Optional;

Expand Down Expand Up @@ -75,6 +76,11 @@ public interface Registry<E, C> {
*/
C getDefaultConfig();

/**
* @return global configured registry tags
*/
Map<String, String> getTags();

/**
* Returns an EventPublisher which can be used to register event consumers.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,11 @@
import io.vavr.collection.HashMap;
import io.vavr.collection.Map;

import java.util.*;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Objects;
import java.util.Optional;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentMap;
import java.util.function.Supplier;
Expand Down Expand Up @@ -132,6 +136,11 @@ public C getDefaultConfig() {
return configurations.get(DEFAULT_CONFIG);
}

@Override
public Map<String, String> getTags() {
return registryTags;
}

@Override
public EventPublisher<E> getEventPublisher() {
return eventProcessor;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package io.github.resilience4j.core.registry;

import io.vavr.Tuple;
import io.vavr.collection.Map;
import org.junit.Test;

import java.util.ArrayList;
Expand All @@ -13,6 +15,14 @@

public class AbstractRegistryTest {

@Test
public void shouldInitRegistryTags(){
TestRegistry testRegistry = new TestRegistry(io.vavr.collection.HashMap.of("Tag1","Tag1Value"));
assertThat(testRegistry.getTags()).isNotEmpty();
assertThat(testRegistry.getTags()).containsOnly(Tuple.of("Tag1","Tag1Value"));
}


@Test
public void shouldContainDefaultAndCustomConfiguration() {
TestRegistry testRegistry = new TestRegistry();
Expand Down Expand Up @@ -150,6 +160,11 @@ static class TestRegistry extends AbstractRegistry<String, String> {
super("default", registryEventConsumer);
this.configurations.put(DEFAULT_CONFIG, "default");
}

TestRegistry(Map<String,String> tags) {
super("default", tags);
this.configurations.put(DEFAULT_CONFIG, "default");
}
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
/*
* Copyright Mahmoud Romeh
*
* 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 io.github.resilience4j.common;

import java.util.HashMap;
import java.util.Map;

/**
* common properties between different spring resilience4j supported types
*/
public class CommonProperties {

/**
* The Optional configured global registry tags if any that can be used with the exported
* metrics
*/
Map<String, String> tags = new HashMap<>();

/**
* @return the Optional configured registry global tags if any that can be used with the
* exported metrics
*/
public Map<String, String> getTags() {
return tags;
}

/**
* @param tags the optional configured tags values into registry
*/
public void setTags(Map<String, String> tags) {
this.tags = tags;
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2019 Dan Maas
* Copyright 2019 Dan Maas , Mahmoud Romeh
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand All @@ -16,6 +16,7 @@
package io.github.resilience4j.common.bulkhead.configuration;

import io.github.resilience4j.bulkhead.BulkheadConfig;
import io.github.resilience4j.common.CommonProperties;
import io.github.resilience4j.common.utils.ConfigUtils;
import io.github.resilience4j.core.ConfigurationNotFoundException;
import io.github.resilience4j.core.StringUtils;
Expand All @@ -26,7 +27,7 @@
import java.util.Map;
import java.util.Objects;

public class BulkheadConfigurationProperties {
public class BulkheadConfigurationProperties extends CommonProperties {

private Map<String, InstanceProperties> instances = new HashMap<>();
private Map<String, InstanceProperties> configs = new HashMap<>();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2019 Dan Maas
* Copyright 2019 Dan Maas, Mahmoud Romeh
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand All @@ -17,6 +17,7 @@

import io.github.resilience4j.bulkhead.Bulkhead;
import io.github.resilience4j.bulkhead.ThreadPoolBulkheadConfig;
import io.github.resilience4j.common.CommonProperties;
import io.github.resilience4j.core.ConfigurationNotFoundException;
import io.github.resilience4j.core.StringUtils;
import io.github.resilience4j.core.lang.Nullable;
Expand All @@ -26,7 +27,7 @@
import java.util.Map;
import java.util.Objects;

public class ThreadPoolBulkheadConfigurationProperties {
public class ThreadPoolBulkheadConfigurationProperties extends CommonProperties {

private Map<String, InstanceProperties> instances = new HashMap<>();
private Map<String, InstanceProperties> configs = new HashMap<>();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import io.github.resilience4j.circuitbreaker.CircuitBreakerConfig;
import io.github.resilience4j.circuitbreaker.CircuitBreakerConfig.Builder;
import io.github.resilience4j.circuitbreaker.CircuitBreakerConfig.SlidingWindowType;
import io.github.resilience4j.common.CommonProperties;
import io.github.resilience4j.common.utils.ConfigUtils;
import io.github.resilience4j.core.ClassUtils;
import io.github.resilience4j.core.ConfigurationNotFoundException;
Expand All @@ -36,7 +37,7 @@
import static io.github.resilience4j.circuitbreaker.CircuitBreakerConfig.custom;
import static io.github.resilience4j.circuitbreaker.CircuitBreakerConfig.from;

public class CircuitBreakerConfigurationProperties {
public class CircuitBreakerConfigurationProperties extends CommonProperties {

private Map<String, InstanceProperties> instances = new HashMap<>();
private Map<String, InstanceProperties> configs = new HashMap<>();
Expand Down Expand Up @@ -300,7 +301,6 @@ public static class InstanceProperties {
*/
private Double randomizedWaitFactor;


/**
* Returns the failure rate threshold for the circuit breaker as percentage.
*
Expand Down Expand Up @@ -474,7 +474,8 @@ public InstanceProperties setEventConsumerBufferSize(Integer eventConsumerBuffer
}

/**
* @return the flag that controls if health indicators are allowed to go into a failed (DOWN) status.
* @return the flag that controls if health indicators are allowed to go into a failed
* (DOWN) status.
* @see #setAllowHealthIndicatorToFail(Boolean)
*/
@Nullable
Expand All @@ -483,13 +484,15 @@ public Boolean getAllowHealthIndicatorToFail() {
}

/**
* When set to true, it allows the health indicator to go to a failed (DOWN) status.
* By default, health indicators for circuit breakers will never go into an unhealthy state.
* When set to true, it allows the health indicator to go to a failed (DOWN) status. By
* default, health indicators for circuit breakers will never go into an unhealthy state.
*
* @param allowHealthIndicatorToFail flag to control if the health indicator is allowed to fail
* @param allowHealthIndicatorToFail flag to control if the health indicator is allowed to
* fail
* @return the InstanceProperties
*/
public InstanceProperties setAllowHealthIndicatorToFail(Boolean allowHealthIndicatorToFail) {
public InstanceProperties setAllowHealthIndicatorToFail(
Boolean allowHealthIndicatorToFail) {
this.allowHealthIndicatorToFail = allowHealthIndicatorToFail;
return this;
}
Expand Down
Loading

0 comments on commit 6d2f870

Please sign in to comment.