public void registerAll(MetricSet metrics) { registry.registerAll(metrics); }
/** * Given a metric set, registers them. * * @param metrics a set of metrics * @throws IllegalArgumentException if any of the names are already registered */ public void registerAll(MetricSet metrics) throws IllegalArgumentException { registerAll(null, metrics); }
public static void safelyRegisterAll(MetricRegistry metricRegistry, MetricSet metrics) throws IllegalArgumentException { try { metricRegistry.registerAll(metrics); } catch (IllegalArgumentException e) { log.error("Duplicate metric set registered", e); } }
/** * Creates a new {@link JvmSource} and register all JVM metrics. */ public JvmSource() { mMetricRegistry = new MetricRegistry(); mMetricRegistry.registerAll(new GarbageCollectorMetricSet()); mMetricRegistry.registerAll(new MemoryUsageGaugeSet()); }
@Bean public CircuitBreakerMetrics registerCircuitBreakerMetrics(CircuitBreakerRegistry circuitBreakerRegistry, MetricRegistry metricRegistry){ CircuitBreakerMetrics circuitBreakerMetrics = CircuitBreakerMetrics.ofCircuitBreakerRegistry(circuitBreakerRegistry); metricRegistry.registerAll(circuitBreakerMetrics); return circuitBreakerMetrics; } }
@Bean public RateLimiterMetrics registerRateLimiterMetrics(RateLimiterRegistry rateLimiterRegistry, MetricRegistry metricRegistry) { RateLimiterMetrics rateLimiterMetrics = RateLimiterMetrics.ofRateLimiterRegistry(rateLimiterRegistry); metricRegistry.registerAll(rateLimiterMetrics); return rateLimiterMetrics; } }
/** * Register all wanted metrics to {@link #metricRegistry}. * <p> * {@inheritDoc} */ @Override protected void init(ReportingInitializationContext config) { metricRegistry = new MetricRegistry(); currentStatusReference = new AtomicReference<>(); metricRegistry.registerAll(new MemoryUsageGaugeSet()); metricRegistry.registerAll(new FlowMetricSet(currentStatusReference)); }
private void registerAll(String prefix, MetricSet metrics) throws IllegalArgumentException { for (Map.Entry<String, Metric> entry : metrics.getMetrics().entrySet()) { if (entry.getValue() instanceof MetricSet) { registerAll(name(prefix, entry.getKey()), (MetricSet) entry.getValue()); } else { register(name(prefix, entry.getKey()), entry.getValue()); } } }
private static void registerSystemMetrics() { REGISTRY.registerAll(new GarbageCollectorMetricSet()); REGISTRY.registerAll(new BufferPoolMetricSet(ManagementFactory.getPlatformMBeanServer())); REGISTRY.registerAll(new MemoryUsageGaugeSet()); REGISTRY.registerAll(new ThreadStatesGaugeSet()); REGISTRY.registerAll(new CpuGaugeSet()); register("fd.usage", new FileDescriptorRatioGauge()); }
private BoneCPDataSource initMetrics(BoneCPDataSource ds) { final MetricRegistry registry = Metrics.getRegistry(); if (registry != null) { registry.registerAll(new BoneCPMetrics(ds)); } return ds; }
@Test public void shouldRegisterMetrics() throws Throwable { //Given CircuitBreakerRegistry circuitBreakerRegistry = CircuitBreakerRegistry.ofDefaults(); CircuitBreaker circuitBreaker = circuitBreakerRegistry.circuitBreaker("testName"); metricRegistry.registerAll(CircuitBreakerMetrics.ofCircuitBreakerRegistry(circuitBreakerRegistry)); // Given the HelloWorldService returns Hello world BDDMockito.given(helloWorldService.returnHelloWorld()).willReturn("Hello world"); //When String value = circuitBreaker.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(6); assertThat(metricRegistry.getGauges().get("resilience4j.circuitbreaker.testName.state").getValue()).isEqualTo(0); assertThat(metricRegistry.getGauges().get("resilience4j.circuitbreaker.testName.buffered").getValue()).isEqualTo(1); assertThat(metricRegistry.getGauges().get("resilience4j.circuitbreaker.testName.successful").getValue()).isEqualTo(1); assertThat(metricRegistry.getGauges().get("resilience4j.circuitbreaker.testName.failed").getValue()).isEqualTo(0); assertThat(metricRegistry.getGauges().get("resilience4j.circuitbreaker.testName.not_permitted").getValue()).isEqualTo(0L); assertThat(metricRegistry.getGauges().get("resilience4j.circuitbreaker.testName.buffered_max").getValue()).isEqualTo(100); }
@Test public void shouldRegisterMetricsWithoutRetry() throws Throwable { //Given RetryRegistry retryRegistry = RetryRegistry.ofDefaults(); Retry retry = retryRegistry.retry("testName"); metricRegistry.registerAll(RetryMetrics.ofRetryRegistry(retryRegistry)); // Given the HelloWorldService returns Hello world BDDMockito.given(helloWorldService.returnHelloWorld()).willReturn("Hello world"); // Setup circuitbreaker with retry String value = retry.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(4); assertThat(metricRegistry.getGauges().get("resilience4j.retry.testName." + SUCCESSFUL_CALLS_WITH_RETRY).getValue()).isEqualTo(0L); assertThat(metricRegistry.getGauges().get("resilience4j.retry.testName." + SUCCESSFUL_CALLS_WITHOUT_RETRY).getValue()).isEqualTo(1L); assertThat(metricRegistry.getGauges().get("resilience4j.retry.testName." + FAILED_CALLS_WITH_RETRY).getValue()).isEqualTo(0L); assertThat(metricRegistry.getGauges().get("resilience4j.retry.testName." + FAILED_CALLS_WITHOUT_RETRY).getValue()).isEqualTo(0L); }
@Test public void shouldUseCustomPrefix() throws Throwable { //Given CircuitBreakerRegistry circuitBreakerRegistry = CircuitBreakerRegistry.ofDefaults(); CircuitBreaker circuitBreaker = circuitBreakerRegistry.circuitBreaker("testName"); metricRegistry.registerAll(CircuitBreakerMetrics.ofCircuitBreakerRegistry("testPrefix", circuitBreakerRegistry)); // Given the HelloWorldService returns Hello world BDDMockito.given(helloWorldService.returnHelloWorld()).willReturn("Hello world"); //When String value = circuitBreaker.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(6); assertThat(metricRegistry.getGauges().get("testPrefix.testName.state").getValue()).isEqualTo(0); assertThat(metricRegistry.getGauges().get("testPrefix.testName.buffered").getValue()).isEqualTo(1); assertThat(metricRegistry.getGauges().get("testPrefix.testName.successful").getValue()).isEqualTo(1); assertThat(metricRegistry.getGauges().get("testPrefix.testName.failed").getValue()).isEqualTo(0); assertThat(metricRegistry.getGauges().get("testPrefix.testName.not_permitted").getValue()).isEqualTo(0L); assertThat(metricRegistry.getGauges().get("testPrefix.testName.buffered_max").getValue()).isEqualTo(100); } }
@Test public void getMetrics() { final int FILES_PINNED_TEST_VALUE = 100; String filesPinnedProperty = MetricsSystem.getMetricName(MasterMetrics.FILES_PINNED); Gauge<Integer> filesPinnedGauge = () -> FILES_PINNED_TEST_VALUE; MetricSet mockMetricsSet = mock(MetricSet.class); Map<String, Metric> map = new HashMap<>(); map.put(filesPinnedProperty, filesPinnedGauge); when(mockMetricsSet.getMetrics()).thenReturn(map); MetricsSystem.METRIC_REGISTRY.registerAll(mockMetricsSet); Response response = mHandler.getMetrics(); try { assertNotNull("Response must be not null!", response); assertNotNull("Response must have a entry!", response.getEntity()); assertTrue("Entry must be a SortedMap!", (response.getEntity() instanceof SortedMap)); SortedMap<String, Long> metricsMap = (SortedMap<String, Long>) response.getEntity(); assertFalse("Metrics Map must be not empty!", (metricsMap.isEmpty())); assertTrue("Map must contain key " + filesPinnedProperty + "!", metricsMap.containsKey(filesPinnedProperty)); assertEquals(FILES_PINNED_TEST_VALUE, metricsMap.get(filesPinnedProperty).longValue()); } finally { response.close(); } }
@Test public void shouldUseCustomPrefix() { //Given AsyncRetryRegistry retryRegistry = AsyncRetryRegistry.ofDefaults(); AsyncRetry retry = retryRegistry.retry("testName"); metricRegistry.registerAll(AsyncRetryMetrics.ofAsyncRetryRegistry("testPrefix",retryRegistry)); // Given the HelloWorldService returns Hello world BDDMockito.given(helloWorldService.returnHelloWorld()).willReturn(CompletableFuture.completedFuture("Hello world")); String value = awaitResult(retry.executeCompletionStage(scheduler, 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(4); assertThat(metricRegistry.getGauges().get("testPrefix.testName." + SUCCESSFUL_CALLS_WITH_RETRY).getValue()).isEqualTo(0L); assertThat(metricRegistry.getGauges().get("testPrefix.testName." + SUCCESSFUL_CALLS_WITHOUT_RETRY).getValue()).isEqualTo(1L); assertThat(metricRegistry.getGauges().get("testPrefix.testName." + FAILED_CALLS_WITH_RETRY).getValue()).isEqualTo(0L); assertThat(metricRegistry.getGauges().get("testPrefix.testName." + FAILED_CALLS_WITHOUT_RETRY).getValue()).isEqualTo(0L); }
@Test public void shouldRegisterMetricsWithoutRetry() { //Given AsyncRetryRegistry retryRegistry = AsyncRetryRegistry.ofDefaults(); AsyncRetry retry = retryRegistry.retry("testName"); metricRegistry.registerAll(AsyncRetryMetrics.ofAsyncRetryRegistry(retryRegistry)); // Given the HelloWorldService returns Hello world BDDMockito.given(helloWorldService.returnHelloWorld()) .willReturn(CompletableFuture.completedFuture("Hello world")); // Setup circuitbreaker with retry String value = awaitResult(retry.executeCompletionStage(scheduler, 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(4); assertThat(metricRegistry.getGauges().get("resilience4j.retry.testName." + SUCCESSFUL_CALLS_WITH_RETRY).getValue()).isEqualTo(0L); assertThat(metricRegistry.getGauges().get("resilience4j.retry.testName." + SUCCESSFUL_CALLS_WITHOUT_RETRY).getValue()).isEqualTo(1L); assertThat(metricRegistry.getGauges().get("resilience4j.retry.testName." + FAILED_CALLS_WITH_RETRY).getValue()).isEqualTo(0L); assertThat(metricRegistry.getGauges().get("resilience4j.retry.testName." + FAILED_CALLS_WITHOUT_RETRY).getValue()).isEqualTo(0L); }
@Test public void shouldUseCustomPrefix() throws Throwable { //Given RetryRegistry retryRegistry = RetryRegistry.ofDefaults(); Retry retry = retryRegistry.retry("testName"); metricRegistry.registerAll(RetryMetrics.ofRetryRegistry("testPrefix",retryRegistry)); // Given the HelloWorldService returns Hello world BDDMockito.given(helloWorldService.returnHelloWorld()).willReturn("Hello world"); String value = retry.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(4); assertThat(metricRegistry.getGauges().get("testPrefix.testName." + SUCCESSFUL_CALLS_WITH_RETRY).getValue()).isEqualTo(0L); assertThat(metricRegistry.getGauges().get("testPrefix.testName." + SUCCESSFUL_CALLS_WITHOUT_RETRY).getValue()).isEqualTo(1L); assertThat(metricRegistry.getGauges().get("testPrefix.testName." + FAILED_CALLS_WITH_RETRY).getValue()).isEqualTo(0L); assertThat(metricRegistry.getGauges().get("testPrefix.testName." + FAILED_CALLS_WITHOUT_RETRY).getValue()).isEqualTo(0L); } }
@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); } }
@Before public void setUp() throws Exception { CircuitBreakerConfig config = CircuitBreakerConfig.custom() .waitDurationInOpenState(Duration.ofSeconds(1)) .failureRateThreshold(50) .ringBufferSizeInHalfOpenState(3) .ringBufferSizeInClosedState(10) .build(); circuitBreaker = CircuitBreakerRegistry.ofDefaults().circuitBreaker("test", config); metricRegistry.registerAll(CircuitBreakerMetrics.ofCircuitBreaker(circuitBreaker)); }