Skip to content

Commit

Permalink
Issue ReactiveX#354: Use new metrics collectors for spring-boot and s…
Browse files Browse the repository at this point in the history
…pring-boot 2 (ReactiveX#382)
  • Loading branch information
voievodin authored and RobWin committed Mar 29, 2019
1 parent 5f2db94 commit a534cd2
Show file tree
Hide file tree
Showing 11 changed files with 363 additions and 20 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,10 @@

import io.github.resilience4j.bulkhead.BulkheadRegistry;
import io.github.resilience4j.prometheus.BulkheadExports;
import io.github.resilience4j.prometheus.collectors.BulkheadMetricsCollector;
import org.springframework.boot.autoconfigure.AutoConfigureAfter;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

Expand All @@ -30,10 +32,24 @@
@AutoConfigureAfter(value = BulkheadAutoConfiguration.class)
@ConditionalOnClass(BulkheadExports.class)
public class BulkheadPrometheusAutoConfiguration {

@Bean
public BulkheadExports bulkheadPrometheusCollector(BulkheadRegistry bulkheadRegistry){
@ConditionalOnProperty(value = "resilience4j.bulkhead.metrics.use_legacy_collector", havingValue = "true")
public BulkheadExports legacyBulkheadPrometheusCollector(BulkheadRegistry bulkheadRegistry) {
BulkheadExports collector = BulkheadExports.ofBulkheadRegistry(bulkheadRegistry);
collector.register();
return collector;
}

@Bean
@ConditionalOnProperty(
value = "resilience4j.bulkhead.metrics.use_legacy_collector",
havingValue = "false",
matchIfMissing = true
)
public BulkheadMetricsCollector bulkheadPrometheusCollector(BulkheadRegistry bulkheadRegistry) {
BulkheadMetricsCollector collector = BulkheadMetricsCollector.ofBulkheadRegistry(bulkheadRegistry);
collector.register();
return collector;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,10 @@

import io.github.resilience4j.circuitbreaker.CircuitBreakerRegistry;
import io.github.resilience4j.prometheus.CircuitBreakerExports;
import io.github.resilience4j.prometheus.collectors.CircuitBreakerMetricsCollector;
import org.springframework.boot.autoconfigure.AutoConfigureAfter;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

Expand All @@ -30,10 +32,24 @@
@AutoConfigureAfter(value = CircuitBreakerAutoConfiguration.class)
@ConditionalOnClass(CircuitBreakerExports.class)
public class CircuitBreakerPrometheusAutoConfiguration {

@Bean
public CircuitBreakerExports circuitBreakerPrometheusCollector(CircuitBreakerRegistry circuitBreakerRegistry){
@ConditionalOnProperty(value = "resilience4j.circuitbreaker.metrics.use_legacy_collector", havingValue = "true")
public CircuitBreakerExports legacyCircuitBreakerPrometheusCollector(CircuitBreakerRegistry circuitBreakerRegistry) {
CircuitBreakerExports collector = CircuitBreakerExports.ofCircuitBreakerRegistry(circuitBreakerRegistry);
collector.register();
return collector;
}

@Bean
@ConditionalOnProperty(
value = "resilience4j.circuitbreaker.metrics.use_legacy_collector",
havingValue = "false",
matchIfMissing = true
)
public CircuitBreakerMetricsCollector circuitBreakerPrometheusCollector(CircuitBreakerRegistry circuitBreakerRegistry) {
CircuitBreakerMetricsCollector collector = CircuitBreakerMetricsCollector.ofCircuitBreakerRegistry(circuitBreakerRegistry);
collector.register();
return collector;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,11 @@
package io.github.resilience4j.ratelimiter.autoconfigure;

import io.github.resilience4j.prometheus.RateLimiterExports;
import io.github.resilience4j.prometheus.collectors.RateLimiterMetricsCollector;
import io.github.resilience4j.ratelimiter.RateLimiterRegistry;
import org.springframework.boot.autoconfigure.AutoConfigureAfter;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

Expand All @@ -30,10 +32,24 @@
@AutoConfigureAfter(value = RateLimiterAutoConfiguration.class)
@ConditionalOnClass(RateLimiterExports.class)
public class RateLimiterPrometheusAutoConfiguration {

@ConditionalOnProperty(value = "resilience4j.ratelimiter.metrics.use_legacy_collector", havingValue = "true")
@Bean
public RateLimiterExports rateLimiterPrometheusCollector(RateLimiterRegistry rateLimiterRegistry){
public RateLimiterExports legacyRateLimiterPrometheusCollector(RateLimiterRegistry rateLimiterRegistry){
RateLimiterExports collector = RateLimiterExports.ofRateLimiterRegistry(rateLimiterRegistry);
collector.register();
return collector;
}

@Bean
@ConditionalOnProperty(
value = "resilience4j.ratelimiter.metrics.use_legacy_collector",
havingValue = "false",
matchIfMissing = true
)
public RateLimiterMetricsCollector rateLimiterPrometheusCollector(RateLimiterRegistry rateLimiterRegistry) {
RateLimiterMetricsCollector collector = RateLimiterMetricsCollector.ofRateLimiterRegistry(rateLimiterRegistry);
collector.register();
return collector;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
/*
* Copyright 2019 Yevhenii Voievodin
*
* 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;

import io.github.resilience4j.prometheus.BulkheadExports;
import io.github.resilience4j.prometheus.CircuitBreakerExports;
import io.github.resilience4j.prometheus.RateLimiterExports;
import io.github.resilience4j.prometheus.collectors.BulkheadMetricsCollector;
import io.github.resilience4j.prometheus.collectors.CircuitBreakerMetricsCollector;
import io.github.resilience4j.prometheus.collectors.RateLimiterMetricsCollector;
import io.github.resilience4j.service.test.TestApplication;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

import static org.assertj.core.api.Assertions.assertThat;

@RunWith(SpringJUnit4ClassRunner.class)
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT, classes = TestApplication.class)
public class MetricsAutoConfigurationTest {

@Autowired(required = false)
CircuitBreakerExports circuitBreakerExports;

@Autowired(required = false)
BulkheadExports bulkheadExports;

@Autowired(required = false)
RateLimiterExports rateLimiterExports;

@Autowired(required = false)
CircuitBreakerMetricsCollector circuitBreakerMetricsCollector;

@Autowired(required = false)
BulkheadMetricsCollector bulkheadMetricsCollector;

@Autowired(required = false)
RateLimiterMetricsCollector rateLimiterMetricsCollector;

@Test
public void legacyCircuitBreakerCollectorIsNotBound() {
assertThat(circuitBreakerExports).isNull();
}

@Test
public void legacyBulkheadCollectorIsNotBound() {
assertThat(bulkheadExports).isNull();
}

@Test
public void legacyRateLimiterCollectorIsNotBound() {
assertThat(bulkheadExports).isNull();
}

@Test
public void newCircuitBreakerCollectorIsBound() {
assertThat(circuitBreakerMetricsCollector).isNotNull();
}

@Test
public void newBulkheadCollectorIsBound() {
assertThat(bulkheadMetricsCollector).isNotNull();
}

@Test
public void newRateLimiterCollectorIsBound() {
assertThat(rateLimiterMetricsCollector).isNotNull();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@

import io.github.resilience4j.bulkhead.BulkheadRegistry;
import io.github.resilience4j.micrometer.BulkheadMetrics;
import io.github.resilience4j.micrometer.tagged.TaggedBulkheadMetrics;
import org.springframework.boot.actuate.autoconfigure.metrics.MetricsAutoConfiguration;
import org.springframework.boot.autoconfigure.AutoConfigureAfter;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
Expand All @@ -31,10 +32,22 @@
@Configuration
@ConditionalOnClass(MetricsAutoConfiguration.class)
@AutoConfigureAfter(value = {BulkheadMetricsAutoConfiguration.class, MetricsAutoConfiguration.class})
@ConditionalOnProperty(value = "resilience4j.bulkhead.metrics.enabled", matchIfMissing = true)
public class BulkheadMetricsAutoConfiguration {

@Bean
@ConditionalOnProperty(value = "resilience4j.bulkhead.metrics.enabled", matchIfMissing = true)
public BulkheadMetrics registerBulkheadMetrics(BulkheadRegistry bulkheadRegistry){
@ConditionalOnProperty(value = "resilience4j.bulkhead.metrics.use_legacy_binder", havingValue = "true")
public BulkheadMetrics registerLegacyBulkheadMetrics(BulkheadRegistry bulkheadRegistry) {
return BulkheadMetrics.ofBulkheadRegistry(bulkheadRegistry);
}

@Bean
@ConditionalOnProperty(
value = "resilience4j.bulkhead.metrics.use_legacy_binder",
havingValue = "false",
matchIfMissing = true
)
public TaggedBulkheadMetrics registerBulkheadMetrics(BulkheadRegistry bulkheadRegistry) {
return TaggedBulkheadMetrics.ofBulkheadRegistry(bulkheadRegistry);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@

import io.github.resilience4j.circuitbreaker.CircuitBreakerRegistry;
import io.github.resilience4j.micrometer.CircuitBreakerMetrics;
import io.github.resilience4j.micrometer.tagged.TaggedCircuitBreakerMetrics;
import org.springframework.boot.actuate.autoconfigure.metrics.MetricsAutoConfiguration;
import org.springframework.boot.autoconfigure.AutoConfigureAfter;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
Expand All @@ -31,11 +32,22 @@
@Configuration
@ConditionalOnClass(MetricsAutoConfiguration.class)
@AutoConfigureAfter(value = {CircuitBreakerAutoConfiguration.class, MetricsAutoConfiguration.class})
@ConditionalOnProperty(value = "resilience4j.circuitbreaker.metrics.enabled", matchIfMissing = true)
public class CircuitBreakerMetricsAutoConfiguration {

@Bean
@ConditionalOnProperty(value = "resilience4j.circuitbreaker.metrics.enabled", matchIfMissing = true)
public CircuitBreakerMetrics registerCircuitBreakerMetrics(CircuitBreakerRegistry circuitBreakerRegistry) {
@ConditionalOnProperty(value = "resilience4j.circuitbreaker.metrics.use_legacy_binder", havingValue = "true")
public CircuitBreakerMetrics registerLegacyCircuitBreakerMetrics(CircuitBreakerRegistry circuitBreakerRegistry) {
return CircuitBreakerMetrics.ofCircuitBreakerRegistry(circuitBreakerRegistry);
}

@Bean
@ConditionalOnProperty(
value = "resilience4j.circuitbreaker.metrics.use_legacy_binder",
havingValue = "false",
matchIfMissing = true
)
public TaggedCircuitBreakerMetrics registerCircuitBreakerMetrics(CircuitBreakerRegistry circuitBreakerRegistry) {
return TaggedCircuitBreakerMetrics.ofCircuitBreakerRegistry(circuitBreakerRegistry);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,12 @@

import io.github.resilience4j.circuitbreaker.autoconfigure.CircuitBreakerAutoConfiguration;
import io.github.resilience4j.micrometer.RateLimiterMetrics;
import io.github.resilience4j.micrometer.tagged.TaggedRateLimiterMetrics;
import io.github.resilience4j.ratelimiter.RateLimiterRegistry;
import org.springframework.boot.actuate.autoconfigure.metrics.MetricsAutoConfiguration;
import org.springframework.boot.autoconfigure.AutoConfigureAfter;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

Expand All @@ -31,9 +33,22 @@
@Configuration
@ConditionalOnClass(MetricsAutoConfiguration.class)
@AutoConfigureAfter(value = {CircuitBreakerAutoConfiguration.class, MetricsAutoConfiguration.class})
@ConditionalOnProperty(value = "resilience4j.ratelimiter.metrics.enabled", matchIfMissing = true)
public class RateLimiterMetricsAutoConfiguration {

@Bean
public RateLimiterMetrics registerRateLimiterMetrics(RateLimiterRegistry rateLimiterRegistry) {
@ConditionalOnProperty(value = "resilience4j.ratelimiter.metrics.use_legacy_binder", havingValue = "true")
public RateLimiterMetrics registerLegacyRateLimiterMetrics(RateLimiterRegistry rateLimiterRegistry) {
return RateLimiterMetrics.ofRateLimiterRegistry(rateLimiterRegistry);
}

@Bean
@ConditionalOnProperty(
value = "resilience4j.ratelimiter.metrics.use_legacy_binder",
havingValue = "false",
matchIfMissing = true
)
public TaggedRateLimiterMetrics registerRateLimiterMetrics(RateLimiterRegistry rateLimiterRegistry) {
return TaggedRateLimiterMetrics.ofRateLimiterRegistry(rateLimiterRegistry);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
/*
* Copyright 2019 Yevhenii Voievodin
*
* 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.retry.autoconfigure;

import io.github.resilience4j.micrometer.AsyncRetryMetrics;
import io.github.resilience4j.micrometer.tagged.TaggedAsyncRetryMetrics;
import io.github.resilience4j.retry.AsyncRetryRegistry;
import org.springframework.boot.actuate.autoconfigure.metrics.MetricsAutoConfiguration;
import org.springframework.boot.autoconfigure.AutoConfigureAfter;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
@ConditionalOnClass(MetricsAutoConfiguration.class)
@AutoConfigureAfter(value = {RetryAutoConfiguration.class, MetricsAutoConfiguration.class})
@ConditionalOnProperty(value = "resilience4j.async_retry.metrics.enabled", matchIfMissing = true)
public class AsyncRetryMetricsAutoConfiguration {

@Bean
@ConditionalOnProperty(value = "resilience4j.async_retry.metrics.use_legacy_binder", havingValue = "true")
public AsyncRetryMetrics registerLegacyAsyncRetryMetrics(AsyncRetryRegistry asyncRetryRegistry) {
return AsyncRetryMetrics.ofRetryRegistry(asyncRetryRegistry);
}

@Bean
@ConditionalOnProperty(
value = "resilience4j.async_retry.metrics.use_legacy_binder",
havingValue = "false",
matchIfMissing = true
)
public TaggedAsyncRetryMetrics registerAsyncRetryMetrics(AsyncRetryRegistry asyncRetryRegistry) {
return TaggedAsyncRetryMetrics.ofAsyncRetryRegistry(asyncRetryRegistry);
}
}
Loading

0 comments on commit a534cd2

Please sign in to comment.