forked from ReactiveX/RxJava
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request ReactiveX#88 from resilience4j/ratelimiter_metrics
Issue ReactiveX#72: rate limiter metrics initial
- Loading branch information
Showing
7 changed files
with
294 additions
and
28 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,8 @@ | ||
dependencies { | ||
compile (libraries.metrics) | ||
compileOnly project(':resilience4j-circuitbreaker') | ||
compileOnly project(':resilience4j-ratelimiter') | ||
testCompile project(':resilience4j-test') | ||
testCompile project(':resilience4j-circuitbreaker') | ||
testCompile project(':resilience4j-ratelimiter') | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
121 changes: 121 additions & 0 deletions
121
resilience4j-metrics/src/main/java/io/github/resilience4j/metrics/RateLimiterMetrics.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,121 @@ | ||
/* | ||
* | ||
* Copyright 2017: Bohdan Storozhuk | ||
* | ||
* 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.metrics; | ||
|
||
import static com.codahale.metrics.MetricRegistry.name; | ||
import static java.util.Objects.requireNonNull; | ||
|
||
import com.codahale.metrics.Gauge; | ||
import com.codahale.metrics.Metric; | ||
import com.codahale.metrics.MetricRegistry; | ||
import com.codahale.metrics.MetricSet; | ||
import io.github.resilience4j.ratelimiter.RateLimiter; | ||
import io.github.resilience4j.ratelimiter.RateLimiterRegistry; | ||
import javaslang.collection.Array; | ||
|
||
import java.util.Map; | ||
|
||
/** | ||
* An adapter which exports {@link RateLimiter.Metrics} as Dropwizard Metrics Gauges. | ||
*/ | ||
public class RateLimiterMetrics implements MetricSet { | ||
|
||
private static final String PREFIX_NULL = "Prefix must not be null"; | ||
private static final String ITERABLE_NULL = "RateLimiters iterable must not be null"; | ||
|
||
private static final String DEFAULT_PREFIX = "resilience4j.ratelimiter"; | ||
|
||
private final MetricRegistry metricRegistry = new MetricRegistry(); | ||
|
||
private RateLimiterMetrics(Iterable<RateLimiter> rateLimiters) { | ||
this(DEFAULT_PREFIX, rateLimiters); | ||
} | ||
|
||
private RateLimiterMetrics(String prefix, Iterable<RateLimiter> rateLimiters) { | ||
requireNonNull(prefix, PREFIX_NULL); | ||
requireNonNull(rateLimiters, ITERABLE_NULL); | ||
|
||
rateLimiters.forEach(rateLimiter -> { | ||
String name = rateLimiter.getName(); | ||
RateLimiter.Metrics metrics = rateLimiter.getMetrics(); | ||
|
||
metricRegistry.register(name(prefix, name, "number_of_waiting_threads"), | ||
(Gauge<Integer>) metrics::getNumberOfWaitingThreads); | ||
metricRegistry.register(name(prefix, name, "available_permissions"), | ||
(Gauge<Integer>) metrics::getAvailablePermissions); | ||
} | ||
); | ||
} | ||
|
||
/** | ||
* Creates a new instance {@link RateLimiterMetrics} with specified metrics names prefix and | ||
* a {@link RateLimiterRegistry} as a source. | ||
* | ||
* @param prefix the prefix of metrics names | ||
* @param rateLimiterRegistry the registry of rate limiters | ||
*/ | ||
public static RateLimiterMetrics ofRateLimiterRegistry(String prefix, RateLimiterRegistry rateLimiterRegistry) { | ||
return new RateLimiterMetrics(prefix, rateLimiterRegistry.getAllRateLimiters()); | ||
} | ||
|
||
/** | ||
* Creates a new instance {@link RateLimiterMetrics} with | ||
* a {@link RateLimiterRegistry} as a source. | ||
* | ||
* @param rateLimiterRegistry the registry of rate limiters | ||
*/ | ||
public static RateLimiterMetrics ofRateLimiterRegistry(RateLimiterRegistry rateLimiterRegistry) { | ||
return new RateLimiterMetrics(rateLimiterRegistry.getAllRateLimiters()); | ||
} | ||
|
||
/** | ||
* Creates a new instance {@link RateLimiterMetrics} with | ||
* an {@link Iterable} of rate limiters as a source. | ||
* | ||
* @param rateLimiters the rate limiters | ||
*/ | ||
public static RateLimiterMetrics ofIterable(Iterable<RateLimiter> rateLimiters) { | ||
return new RateLimiterMetrics(rateLimiters); | ||
} | ||
|
||
/** | ||
* Creates a new instance {@link RateLimiterMetrics} with | ||
* an {@link Iterable} of rate limiters as a source. | ||
* | ||
* @param rateLimiters the rate limiters | ||
*/ | ||
public static RateLimiterMetrics ofIterable(String prefix, Iterable<RateLimiter> rateLimiters) { | ||
return new RateLimiterMetrics(prefix, rateLimiters); | ||
} | ||
|
||
|
||
/** | ||
* Creates a new instance of {@link RateLimiterMetrics} with a rate limiter as a source. | ||
* | ||
* @param rateLimiter the rate limiter | ||
*/ | ||
public static RateLimiterMetrics ofRateLimiter(RateLimiter rateLimiter) { | ||
return new RateLimiterMetrics(Array.of(rateLimiter)); | ||
} | ||
|
||
@Override | ||
public Map<String, Metric> getMetrics() { | ||
return metricRegistry.getMetrics(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
93 changes: 93 additions & 0 deletions
93
...lience4j-metrics/src/test/java/io/github/resilience4j/metrics/RateLimiterMetricsTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,93 @@ | ||
/* | ||
* | ||
* Copyright 2017: Robert Winkler | ||
* | ||
* 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.metrics; | ||
|
||
import static org.assertj.core.api.Assertions.assertThat; | ||
import static org.mockito.Mockito.mock; | ||
import static org.mockito.Mockito.times; | ||
|
||
import com.codahale.metrics.MetricRegistry; | ||
import io.github.resilience4j.ratelimiter.RateLimiter; | ||
import io.github.resilience4j.ratelimiter.RateLimiterConfig; | ||
import io.github.resilience4j.ratelimiter.RateLimiterRegistry; | ||
import io.github.resilience4j.test.HelloWorldService; | ||
import org.junit.Before; | ||
import org.junit.Test; | ||
import org.mockito.BDDMockito; | ||
|
||
public class RateLimiterMetricsTest { | ||
|
||
private static final int DEFAULT_LIMIT_FOR_PERIOD = RateLimiterConfig.ofDefaults().getLimitForPeriod(); | ||
private MetricRegistry metricRegistry; | ||
private HelloWorldService helloWorldService; | ||
|
||
@Before | ||
public void setUp() { | ||
metricRegistry = new MetricRegistry(); | ||
helloWorldService = mock(HelloWorldService.class); | ||
} | ||
|
||
@Test | ||
public void shouldRegisterMetrics() throws Throwable { | ||
//Given | ||
RateLimiterRegistry rateLimiterRegistry = RateLimiterRegistry.ofDefaults(); | ||
RateLimiter rateLimiter = rateLimiterRegistry.rateLimiter("testLimit"); | ||
metricRegistry.registerAll(RateLimiterMetrics.ofRateLimiterRegistry(rateLimiterRegistry)); | ||
|
||
// Given the HelloWorldService returns Hello world | ||
BDDMockito.given(helloWorldService.returnHelloWorld()).willReturn("Hello world"); | ||
|
||
//When | ||
String value = rateLimiter.executeSupplier(helloWorldService::returnHelloWorld); | ||
|
||
//Then | ||
assertThat(value).isEqualTo("Hello world"); | ||
// Then the helloWorldService should be invoked 1 time | ||
BDDMockito.then(helloWorldService).should(times(1)).returnHelloWorld(); | ||
assertThat(metricRegistry.getMetrics()).hasSize(2); | ||
assertThat(metricRegistry.getGauges().get("resilience4j.ratelimiter.testLimit.number_of_waiting_threads") | ||
.getValue()).isEqualTo(0); | ||
assertThat(metricRegistry.getGauges().get("resilience4j.ratelimiter.testLimit.available_permissions").getValue()) | ||
.isIn(DEFAULT_LIMIT_FOR_PERIOD, DEFAULT_LIMIT_FOR_PERIOD - 1); | ||
} | ||
|
||
@Test | ||
public void shouldUseCustomPrefix() throws Throwable { | ||
//Given | ||
RateLimiterRegistry rateLimiterRegistry = RateLimiterRegistry.ofDefaults(); | ||
RateLimiter rateLimiter = rateLimiterRegistry.rateLimiter("testLimit"); | ||
metricRegistry.registerAll(RateLimiterMetrics.ofIterable("testPre", rateLimiterRegistry.getAllRateLimiters())); | ||
|
||
// Given the HelloWorldService returns Hello world | ||
BDDMockito.given(helloWorldService.returnHelloWorld()).willReturn("Hello world"); | ||
|
||
//When | ||
String value = rateLimiter.executeSupplier(helloWorldService::returnHelloWorld); | ||
|
||
//Then | ||
assertThat(value).isEqualTo("Hello world"); | ||
// Then the helloWorldService should be invoked 1 time | ||
BDDMockito.then(helloWorldService).should(times(1)).returnHelloWorld(); | ||
assertThat(metricRegistry.getMetrics()).hasSize(2); | ||
assertThat(metricRegistry.getGauges().get("testPre.testLimit.number_of_waiting_threads") | ||
.getValue()).isEqualTo(0); | ||
assertThat(metricRegistry.getGauges().get("testPre.testLimit.available_permissions").getValue()) | ||
.isIn(DEFAULT_LIMIT_FOR_PERIOD, DEFAULT_LIMIT_FOR_PERIOD - 1); | ||
} | ||
} |
Oops, something went wrong.