Newer
Older
sample-prometheus2 / src / main / java / com / sample / config / PrometheusMeterRegistryConfig.java
yhornisse on 29 Apr 2021 1 KB add files
package com.sample.config;

import io.micrometer.core.instrument.binder.jvm.JvmGcMetrics;
import io.micrometer.core.instrument.binder.jvm.JvmMemoryMetrics;
import io.micrometer.core.instrument.binder.jvm.JvmThreadMetrics;
import io.micrometer.core.instrument.binder.logging.LogbackMetrics;
import io.micrometer.core.instrument.binder.system.ProcessorMetrics;
import io.micrometer.core.instrument.composite.CompositeMeterRegistry;
import io.micrometer.core.instrument.config.MeterFilter;
import io.micrometer.prometheus.PrometheusConfig;
import io.micrometer.prometheus.PrometheusMeterRegistry;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.List;

@Configuration
public class PrometheusMeterRegistryConfig {

    @Bean
    PrometheusMeterRegistry prometheusMeterRegistry() {
        final var prometheusMeterRegistry = new PrometheusMeterRegistry(PrometheusConfig.DEFAULT);
        final var meterRegistry = new CompositeMeterRegistry();
        meterRegistry.config().meterFilter(MeterFilter.denyNameStartsWith("http.server.requests"));
        meterRegistry.add(prometheusMeterRegistry);
        List.of(new JvmGcMetrics(),
                new JvmMemoryMetrics(),
                new JvmThreadMetrics(),
                new ProcessorMetrics(),
                new LogbackMetrics()).forEach(x -> x.bindTo(meterRegistry));
        return prometheusMeterRegistry;
    }
}