Skip to content

Commit e84a012

Browse files
authored
Issue ReactiveX#72: Created a resilience4j-metrics module for Dropwizard metri… (ReactiveX#80)
Issue ReactiveX#72: Created a resilience4j-metrics module for Dropwizard metrics and moved the Stopwatch class into a new resilience4j-core module.
1 parent debb93f commit e84a012

File tree

16 files changed

+569
-23
lines changed

16 files changed

+569
-23
lines changed

libraries.gradle

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -32,23 +32,26 @@ ext {
3232
powermock_api_mockito: "org.powermock:powermock-api-mockito:${powermockVersion}",
3333
powermock_module_junit4: "org.powermock:powermock-module-junit4:${powermockVersion}",
3434
awaitility: "com.jayway.awaitility:awaitility:${awaitilityVersion}",
35-
// vert.x addon
35+
36+
// Vert.x addon
3637
vertx: "io.vertx:vertx-core:${vertxVersion}",
3738
vertx_unit: "io.vertx:vertx-unit:${vertxVersion}",
3839

39-
// spring boot addon
40+
// Spring Boot addon
4041
spring_boot_aop: "org.springframework.boot:spring-boot-starter-aop:${springBootVersion}",
4142
spring_boot_actuator: "org.springframework.boot:spring-boot-starter-actuator:${springBootVersion}",
4243
spring_boot_web: "org.springframework.boot:spring-boot-starter-web:${springBootVersion}",
4344
spring_boot_test: "org.springframework.boot:spring-boot-starter-test:${springBootVersion}",
4445

45-
// retrofit addon
46+
// Retrofit addon
4647
retrofit: "com.squareup.retrofit2:retrofit:${retrofitVersion}",
4748
retrofit_test: "com.squareup.retrofit2:converter-scalars:${retrofitVersion}",
4849
retrofit_wiremock: "com.github.tomakehurst:wiremock:1.58",
4950

50-
// circuitbreaker documentation
51+
// Metrics addon
5152
metrics: "io.dropwizard.metrics:metrics-core:${metricsVersion}",
53+
54+
// CircuitBreaker documentation
5255
metrics_healthcheck: "io.dropwizard.metrics:metrics-healthchecks:${metricsVersion}"
5356
]
5457
}

resilience4j-all/build.gradle

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
apply plugin: 'me.champeau.gradle.jmh'
22

33
dependencies {
4+
compile project(':resilience4j-core')
45
compile project(':resilience4j-ratelimiter')
56
compile project(':resilience4j-circularbuffer')
67
compile project(':resilience4j-circuitbreaker')
78
compile project(':resilience4j-retry')
8-
compile project(':resilience4j-metrics')
99
compile project(':resilience4j-consumer')
1010
compile project(':resilience4j-cache')
1111
testCompile project(':resilience4j-test')

resilience4j-circuitbreaker/build.gradle

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
apply plugin: 'jcstress'
22

33
dependencies {
4-
compile project(':resilience4j-metrics')
4+
compile project(':resilience4j-core')
55
compile ( libraries.rxjava2)
66
testCompile project(':resilience4j-test')
77
testCompile ( libraries.metrics_healthcheck)

resilience4j-circuitbreaker/src/main/java/io/github/resilience4j/circuitbreaker/CircuitBreaker.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
/*
22
*
3-
* Copyright 2016 Robert Winkler
3+
* Copyright 2017: Robert Winkler
44
*
55
* Licensed under the Apache License, Version 2.0 (the "License");
66
* you may not use this file except in compliance with the License.
@@ -29,7 +29,7 @@
2929
import io.github.resilience4j.circuitbreaker.event.CircuitBreakerEvent;
3030
import io.github.resilience4j.circuitbreaker.internal.CircuitBreakerStateMachine;
3131
import io.github.resilience4j.circuitbreaker.utils.CircuitBreakerUtils;
32-
import io.github.resilience4j.metrics.StopWatch;
32+
import io.github.resilience4j.core.StopWatch;
3333
import io.reactivex.Flowable;
3434
import javaslang.control.Try;
3535

resilience4j-circuitbreaker/src/main/java/io/github/resilience4j/circuitbreaker/operator/CircuitBreakerOperator.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
/*
22
*
3-
* Copyright 2016 Robert Winkler
3+
* Copyright 2017: Robert Winkler
44
*
55
* Licensed under the Apache License, Version 2.0 (the "License");
66
* you may not use this file except in compliance with the License.
@@ -28,7 +28,7 @@
2828

2929
import io.github.resilience4j.circuitbreaker.CircuitBreaker;
3030
import io.github.resilience4j.circuitbreaker.CircuitBreakerOpenException;
31-
import io.github.resilience4j.metrics.StopWatch;
31+
import io.github.resilience4j.core.StopWatch;
3232
import io.reactivex.FlowableOperator;
3333
import io.reactivex.ObservableOperator;
3434
import io.reactivex.Observer;

resilience4j-core/build.gradle

Whitespace-only changes.

resilience4j-metrics/src/main/java/io/github/resilience4j/metrics/StopWatch.java renamed to resilience4j-core/src/main/java/io/github/resilience4j/core/StopWatch.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
/*
22
*
3-
* Copyright 2016 Robert Winkler
3+
* Copyright 2017: Robert Winkler
44
*
55
* Licensed under the Apache License, Version 2.0 (the "License");
66
* you may not use this file except in compliance with the License.
@@ -16,7 +16,7 @@
1616
*
1717
*
1818
*/
19-
package io.github.resilience4j.metrics;
19+
package io.github.resilience4j.core;
2020

2121
import java.time.Duration;
2222

@@ -26,7 +26,7 @@
2626
public class StopWatch {
2727

2828
private final String id;
29-
private long startTime;
29+
private final long startTime;
3030
private long elapsedTime;
3131

3232
private StopWatch(String id){
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
/*
22
*
3-
* Copyright 2016 Robert Winkler
3+
* Copyright 2017: Robert Winkler
44
*
55
* Licensed under the Apache License, Version 2.0 (the "License");
66
* you may not use this file except in compliance with the License.
@@ -16,7 +16,7 @@
1616
*
1717
*
1818
*/
19-
package io.github.resilience4j.metrics;
19+
package io.github.resilience4j.core;
2020

2121
import org.assertj.core.api.Assertions;
2222
import org.junit.Test;

resilience4j-metrics/build.gradle

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
dependencies {
2+
compile (libraries.metrics)
3+
testCompile project(':resilience4j-test')
4+
}
Lines changed: 120 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,120 @@
1+
/*
2+
*
3+
* Copyright 2017: Robert Winkler
4+
*
5+
* Licensed under the Apache License, Version 2.0 (the "License");
6+
* you may not use this file except in compliance with the License.
7+
* You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*
17+
*
18+
*/
19+
package io.github.resilience4j.metrics;
20+
21+
import com.codahale.metrics.Timer;
22+
23+
import java.util.function.Function;
24+
import java.util.function.Supplier;
25+
26+
import javaslang.control.Try;
27+
28+
public interface Metrics {
29+
30+
/**
31+
* Creates a timed checked supplier.
32+
33+
* @param timer the timer to use
34+
* @param supplier the original supplier
35+
* @return a timed supplier
36+
*/
37+
static <T> Try.CheckedSupplier<T> decorateCheckedSupplier(Timer timer, Try.CheckedSupplier<T> supplier){
38+
return () -> {
39+
try (Timer.Context context = timer.time()) {
40+
return supplier.get();
41+
}
42+
};
43+
}
44+
45+
/**
46+
* Creates a timed runnable.
47+
48+
* @param timer the timer to use
49+
* @param runnable the original runnable
50+
* @return a timed runnable
51+
*/
52+
static Try.CheckedRunnable decorateCheckedRunnable(Timer timer, Try.CheckedRunnable runnable){
53+
return () -> {
54+
try (Timer.Context context = timer.time()) {
55+
runnable.run();
56+
}
57+
};
58+
}
59+
60+
/**
61+
* Creates a timed checked supplier.
62+
63+
* @param timer the timer to use
64+
* @param supplier the original supplier
65+
* @return a timed supplier
66+
*/
67+
static <T> Supplier<T> decorateSupplier(Timer timer, Supplier<T> supplier){
68+
return () -> {
69+
try (Timer.Context context = timer.time()) {
70+
return supplier.get();
71+
}
72+
};
73+
}
74+
75+
/**
76+
* Creates a timed runnable.
77+
78+
* @param timer the timer to use
79+
* @param runnable the original runnable
80+
* @return a timed runnable
81+
*/
82+
static Runnable decorateRunnable(Timer timer, Runnable runnable){
83+
return () -> {
84+
try (Timer.Context context = timer.time()) {
85+
runnable.run();
86+
}
87+
};
88+
}
89+
90+
91+
/**
92+
* Creates a timed function.
93+
94+
* @param timer the timer to use
95+
* @param function the original function
96+
* @return a timed function
97+
*/
98+
static <T, R> Function<T, R> decorateFunction(Timer timer, Function<T, R> function){
99+
return (T t) -> {
100+
try (Timer.Context context = timer.time()) {
101+
return function.apply(t);
102+
}
103+
};
104+
}
105+
106+
/**
107+
* Creates a timed function.
108+
109+
* @param timer the timer to use
110+
* @param function the original function
111+
* @return a timed function
112+
*/
113+
static <T, R> Try.CheckedFunction<T, R> decorateCheckedFunction(Timer timer, Try.CheckedFunction<T, R> function){
114+
return (T t) -> {
115+
try (Timer.Context context = timer.time()) {
116+
return function.apply(t);
117+
}
118+
};
119+
}
120+
}

0 commit comments

Comments
 (0)