Search in sources :

Example 1 with CollectorRegistry

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

the class CallMeterTest method testInstrumentsFailedCall.

@Test
public void testInstrumentsFailedCall() throws Exception {
    // Given
    final CollectorRegistry registry = new CollectorRegistry();
    final CallMeter timer = CallMeter.ofCollectorRegistry("some_call", "Some help", registry);
    try {
        // When
        timer.executeRunnable(() -> {
            try {
                Thread.sleep(50);
                throw new SomeAppException("Test Exception");
            } catch (InterruptedException e) {
                fail();
            }
        });
    } catch (SomeAppException e) {
        assertThat(e.getMessage()).isEqualTo("Test Exception");
    // ignore
    }
    // Then
    assertThat(registry.getSampleValue("some_call_total", new String[] {}, new String[] {})).isEqualTo(1.0);
    assertThat(registry.getSampleValue("some_call_failures_total", new String[] {}, new String[] {})).isEqualTo(1.0);
    assertThat(registry.getSampleValue("some_call_latency_count", new String[] {}, new String[] {})).isEqualTo(0.0);
}
Also used : CollectorRegistry(io.prometheus.client.CollectorRegistry) Test(org.junit.Test)

Example 2 with CollectorRegistry

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

the class CircuitBreakerExportsTest method testExportsCircuitBreakerStates.

@Test
public void testExportsCircuitBreakerStates() {
    // 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("closed", "open", "half_open").map(state -> Tuple.of(state, registry.getSampleValue("boo_circuit_breaker_states", new String[] { "name", "state" }, new String[] { "foo", state }))).toMap(t -> t);
    // When
    final Map<String, Double> closedStateValues = values.get();
    circuitBreaker.transitionToOpenState();
    final Map<String, Double> openStateValues = values.get();
    circuitBreaker.transitionToHalfOpenState();
    final Map<String, Double> halfOpenStateValues = values.get();
    circuitBreaker.transitionToClosedState();
    final Map<String, Double> closedStateValues2 = values.get();
    // Then
    assertThat(closedStateValues).isEqualTo(HashMap.of("closed", 1.0, "open", 0.0, "half_open", 0.0));
    assertThat(openStateValues).isEqualTo(HashMap.of("closed", 0.0, "open", 1.0, "half_open", 0.0));
    assertThat(halfOpenStateValues).isEqualTo(HashMap.of("closed", 0.0, "open", 0.0, "half_open", 1.0));
    assertThat(closedStateValues2).isEqualTo(HashMap.of("closed", 1.0, "open", 0.0, "half_open", 0.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) Test(org.junit.Test)

Example 3 with CollectorRegistry

use of io.prometheus.client.CollectorRegistry in project modules-extra by CubeEngine.

the class Stats method onPreInit.

@Listener
public void onPreInit(GamePreInitializationEvent event) {
    final CollectorRegistry registry = monitoring.getRegistry();
    final InetSocketAddress bindAddr = new InetSocketAddress(config.bindAddress, config.bindPort);
    registerDefaults(registry);
    players.register(registry);
    tps.register(registry);
    loadedChunks.register(registry);
    playersOnline.register(registry);
    entities.register(registry);
    tileEntities.register(registry);
    try {
        this.exporter = new HTTPServer(bindAddr, registry, true);
        this.scheduler.scheduleAtFixedRate(this::sampleServer, config.samplingInterval.toMillis(), config.samplingInterval.toMillis(), TimeUnit.MILLISECONDS);
    } catch (IOException e) {
        e.printStackTrace();
    }
}
Also used : HTTPServer(io.prometheus.client.exporter.HTTPServer) InetSocketAddress(java.net.InetSocketAddress) CollectorRegistry(io.prometheus.client.CollectorRegistry) IOException(java.io.IOException) Listener(org.spongepowered.api.event.Listener)

Example 4 with CollectorRegistry

use of io.prometheus.client.CollectorRegistry in project dcos-commons by mesosphere.

the class Metrics method configureMetricsEndpoints.

/**
 * Appends endpoint servlets to the provided {@code context} which will serve codahale-style and prometheus-style
 * metrics.
 */
public static void configureMetricsEndpoints(ServletContextHandler context, String codahaleMetricsEndpoint, String prometheusEndpoint) {
    // Metrics
    ServletHolder codahaleMetricsServlet = new ServletHolder("default", new com.codahale.metrics.servlets.MetricsServlet(metrics));
    context.addServlet(codahaleMetricsServlet, codahaleMetricsEndpoint);
    // Prometheus
    CollectorRegistry collectorRegistry = new CollectorRegistry();
    collectorRegistry.register(new DropwizardExports(metrics));
    ServletHolder prometheusServlet = new ServletHolder("prometheus", new io.prometheus.client.exporter.MetricsServlet(collectorRegistry));
    context.addServlet(prometheusServlet, prometheusEndpoint);
}
Also used : DropwizardExports(io.prometheus.client.dropwizard.DropwizardExports) ServletHolder(org.eclipse.jetty.servlet.ServletHolder) CollectorRegistry(io.prometheus.client.CollectorRegistry)

Example 5 with CollectorRegistry

use of io.prometheus.client.CollectorRegistry in project oxAuth by GluuFederation.

the class StatWS method createOpenMetricsResponse.

public static String createOpenMetricsResponse(StatResponse statResponse) throws IOException {
    Writer writer = new StringWriter();
    CollectorRegistry registry = new CollectorRegistry();
    final Counter usersCounter = Counter.build().name("monthly_active_users").labelNames("month").help("Monthly active users").register(registry);
    final Counter accessTokenCounter = Counter.build().name(StatService.ACCESS_TOKEN_KEY).labelNames("month", "grantType").help("Access Token").register(registry);
    final Counter idTokenCounter = Counter.build().name(StatService.ID_TOKEN_KEY).labelNames("month", "grantType").help("Id Token").register(registry);
    final Counter refreshTokenCounter = Counter.build().name(StatService.REFRESH_TOKEN_KEY).labelNames("month", "grantType").help("Refresh Token").register(registry);
    final Counter umaTokenCounter = Counter.build().name(StatService.UMA_TOKEN_KEY).labelNames("month", "grantType").help("UMA Token").register(registry);
    for (Map.Entry<String, StatResponseItem> entry : statResponse.getResponse().entrySet()) {
        final String month = entry.getKey();
        final StatResponseItem item = entry.getValue();
        usersCounter.labels(month).inc(item.getMonthlyActiveUsers());
        for (Map.Entry<String, Map<String, Long>> tokenEntry : item.getTokenCountPerGrantType().entrySet()) {
            final String grantType = tokenEntry.getKey();
            final Map<String, Long> tokenMap = tokenEntry.getValue();
            accessTokenCounter.labels(month, grantType).inc(getToken(tokenMap, StatService.ACCESS_TOKEN_KEY));
            idTokenCounter.labels(month, grantType).inc(getToken(tokenMap, StatService.ID_TOKEN_KEY));
            refreshTokenCounter.labels(month, grantType).inc(getToken(tokenMap, StatService.REFRESH_TOKEN_KEY));
            umaTokenCounter.labels(month, grantType).inc(getToken(tokenMap, StatService.UMA_TOKEN_KEY));
        }
    }
    TextFormat.write004(writer, registry.metricFamilySamples());
    return writer.toString();
}
Also used : Counter(io.prometheus.client.Counter) StringWriter(java.io.StringWriter) CollectorRegistry(io.prometheus.client.CollectorRegistry) Map(java.util.Map) StringWriter(java.io.StringWriter) Writer(java.io.Writer)

Aggregations

CollectorRegistry (io.prometheus.client.CollectorRegistry)16 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