Search in sources :

Example 11 with CollectorRegistry

use of io.prometheus.client.CollectorRegistry in project resilience4j by resilience4j.

the class CircuitBreakerExportsTest method testExportsCircuitBreakerMetrics.

@Test
public void testExportsCircuitBreakerMetrics() {
    // Given
    final CollectorRegistry registry = new CollectorRegistry();
    final CircuitBreaker circuitBreaker = CircuitBreaker.ofDefaults("foo");
    CircuitBreakerExports.ofIterable("boo_circuit_breaker", singletonList(circuitBreaker)).register(registry);
    final Supplier<Map<String, Double>> values = () -> HashSet.of("successful", "failed", "not_permitted", "buffered", "buffered_max").map(callType -> Tuple.of(callType, registry.getSampleValue("boo_circuit_breaker_calls", new String[] { "name", "call_result" }, new String[] { "foo", callType }))).toMap(t -> t);
    // When
    final Map<String, Double> initialValues = values.get();
    circuitBreaker.executeRunnable(() -> {
    });
    final Map<String, Double> afterSuccessValues = values.get();
    try {
        circuitBreaker.executeRunnable(() -> {
            throw new SomeAppException("Some exception");
        });
    } catch (RuntimeException e) {
    // expected
    }
    final Map<String, Double> afterFailureValues = values.get();
    circuitBreaker.transitionToOpenState();
    try {
        circuitBreaker.executeRunnable(() -> {
        });
    } catch (CircuitBreakerOpenException e) {
    // expected
    }
    final Map<String, Double> afterDeclinedValues = values.get();
    // Then
    assertThat(initialValues).isEqualTo(HashMap.of("successful", 0.0, "failed", 0.0, "not_permitted", 0.0, "buffered", 0.0, "buffered_max", 100.0));
    assertThat(afterSuccessValues).isEqualTo(HashMap.of("successful", 1.0, "failed", 0.0, "not_permitted", 0.0, "buffered", 1.0, "buffered_max", 100.0));
    assertThat(afterFailureValues).isEqualTo(HashMap.of("successful", 1.0, "failed", 1.0, "not_permitted", 0.0, "buffered", 2.0, "buffered_max", 100.0));
    assertThat(afterDeclinedValues).isEqualTo(HashMap.of("successful", 1.0, "failed", 1.0, "not_permitted", 1.0, "buffered", 2.0, "buffered_max", 100.0));
}
Also used : Tuple(io.vavr.Tuple) CollectorRegistry(io.prometheus.client.CollectorRegistry) CircuitBreaker(io.github.resilience4j.circuitbreaker.CircuitBreaker) InMemoryCircuitBreakerRegistry(io.github.resilience4j.circuitbreaker.internal.InMemoryCircuitBreakerRegistry) HashMap(io.vavr.collection.HashMap) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) Test(org.junit.Test) Supplier(java.util.function.Supplier) CircuitBreakerOpenException(io.github.resilience4j.circuitbreaker.CircuitBreakerOpenException) CircuitBreakerRegistry(io.github.resilience4j.circuitbreaker.CircuitBreakerRegistry) Collections.singletonList(java.util.Collections.singletonList) Collections.singleton(java.util.Collections.singleton) HashSet(io.vavr.collection.HashSet) Map(io.vavr.collection.Map) CircuitBreaker(io.github.resilience4j.circuitbreaker.CircuitBreaker) CollectorRegistry(io.prometheus.client.CollectorRegistry) HashMap(io.vavr.collection.HashMap) Map(io.vavr.collection.Map) CircuitBreakerOpenException(io.github.resilience4j.circuitbreaker.CircuitBreakerOpenException) Test(org.junit.Test)

Example 12 with CollectorRegistry

use of io.prometheus.client.CollectorRegistry in project resilience4j by resilience4j.

the class RateLimiterExportsTest method testExportsRateLimiterMetrics.

@Test
public void testExportsRateLimiterMetrics() {
    // Given
    final CollectorRegistry registry = new CollectorRegistry();
    final RateLimiter rateLimiter = RateLimiter.ofDefaults("foo");
    RateLimiterExports.ofIterable("boo_rate_limiter", singletonList(rateLimiter)).register(registry);
    final Supplier<Map<String, Double>> values = () -> HashSet.of("available_permissions", "waiting_threads").map(param -> Tuple.of(param, registry.getSampleValue("boo_rate_limiter", new String[] { "name", "param" }, new String[] { "foo", param }))).toMap(t -> t);
    // When
    final Map<String, Double> initialValues = values.get();
    // Then
    assertThat(initialValues).isEqualTo(HashMap.of("available_permissions", 50.0, "waiting_threads", 0.0));
}
Also used : Tuple(io.vavr.Tuple) CollectorRegistry(io.prometheus.client.CollectorRegistry) RateLimiter(io.github.resilience4j.ratelimiter.RateLimiter) HashMap(io.vavr.collection.HashMap) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) Test(org.junit.Test) InMemoryRateLimiterRegistry(io.github.resilience4j.ratelimiter.internal.InMemoryRateLimiterRegistry) Supplier(java.util.function.Supplier) Collections.singletonList(java.util.Collections.singletonList) Collections.singleton(java.util.Collections.singleton) HashSet(io.vavr.collection.HashSet) RateLimiterConfig(io.github.resilience4j.ratelimiter.RateLimiterConfig) RateLimiterRegistry(io.github.resilience4j.ratelimiter.RateLimiterRegistry) Map(io.vavr.collection.Map) CollectorRegistry(io.prometheus.client.CollectorRegistry) HashMap(io.vavr.collection.HashMap) Map(io.vavr.collection.Map) RateLimiter(io.github.resilience4j.ratelimiter.RateLimiter) Test(org.junit.Test)

Example 13 with CollectorRegistry

use of io.prometheus.client.CollectorRegistry in project infrautils by opendaylight.

the class MetricsPrometheusExampleMain method main.

public static void main(String[] args) throws IOException {
    // see also OsgiWebInitializer
    CollectorRegistry collectorRegistry = new CollectorRegistrySingleton();
    PrometheusMetricProviderImpl metricProvider = new PrometheusMetricProviderImpl(collectorRegistry);
    MetricsExample metricsExample = new MetricsExample(metricProvider);
    metricsExample.init();
    HTTPServer server = new HTTPServer(new InetSocketAddress("localhost", 1234), collectorRegistry);
    System.in.read();
    server.stop();
    metricsExample.close();
    metricProvider.close();
}
Also used : HTTPServer(io.prometheus.client.exporter.HTTPServer) PrometheusMetricProviderImpl(org.opendaylight.infrautils.metrics.prometheus.impl.PrometheusMetricProviderImpl) InetSocketAddress(java.net.InetSocketAddress) CollectorRegistry(io.prometheus.client.CollectorRegistry) CollectorRegistrySingleton(org.opendaylight.infrautils.metrics.prometheus.impl.CollectorRegistrySingleton)

Aggregations

CollectorRegistry (io.prometheus.client.CollectorRegistry)13 Test (org.junit.Test)7 Tuple (io.vavr.Tuple)3 HashMap (io.vavr.collection.HashMap)3 HashSet (io.vavr.collection.HashSet)3 Map (io.vavr.collection.Map)3 Collections.singleton (java.util.Collections.singleton)3 Collections.singletonList (java.util.Collections.singletonList)3 Supplier (java.util.function.Supplier)3 Assertions.assertThat (org.assertj.core.api.Assertions.assertThat)3 CircuitBreaker (io.github.resilience4j.circuitbreaker.CircuitBreaker)2 CircuitBreakerOpenException (io.github.resilience4j.circuitbreaker.CircuitBreakerOpenException)2 CircuitBreakerRegistry (io.github.resilience4j.circuitbreaker.CircuitBreakerRegistry)2 InMemoryCircuitBreakerRegistry (io.github.resilience4j.circuitbreaker.internal.InMemoryCircuitBreakerRegistry)2 HTTPServer (io.prometheus.client.exporter.HTTPServer)2 InetSocketAddress (java.net.InetSocketAddress)2 Provides (dagger.Provides)1 RateLimiter (io.github.resilience4j.ratelimiter.RateLimiter)1 RateLimiterConfig (io.github.resilience4j.ratelimiter.RateLimiterConfig)1 RateLimiterRegistry (io.github.resilience4j.ratelimiter.RateLimiterRegistry)1