首页 > 解决方案 > Spring Boot 未在 promethes 端点上注册

问题描述

我正在尝试使用 spring boot 配置 Prometheus 和 Grafana。

@Configuration
@EnableSpringBootMetricsCollector
public class MetricsConfiguration {

    /**
     * Register common tags application instead of job. This application tag is
     * needed for Grafana dashboard.
     *
     * @return registry with registered tags.
     */

    @Value("${spring.application.name}")
    private String applicationName;

    @Value("${spring.profiles.active}")
    private String environment;


    @Bean
    public MeterRegistryCustomizer<MeterRegistry> metricsCommonTags() {
        return registry -> {
            registry.config().commonTags("application", applicationName, "environment", environment)
                    .meterFilter(getDefualtConfig());
        };
    }

    private MeterFilter getDefualtConfig() {
        return new MeterFilter() {
            @Override
            public DistributionStatisticConfig configure(Meter.Id id, DistributionStatisticConfig config) {
                return DistributionStatisticConfig.builder().percentilesHistogram(true).percentiles(0.95, 0.99).build()
                        .merge(config);
            }
        };
    }

}

在运行应用程序时,我可以在localhost:8080/prometheus url 上看到训练。

但同样我无法在localhost:9090/metrics url 上看到它是 Prometheus URL。

我已经在 prometheus.yml 中添加了配置并重新启动了 Prometheus。

- job_name: 'my-api'
    scrape_interval: 10s
    metrics_path: '/prometheus'
    target_groups:
       - targets: ['localhost:8080']

标签: spring-bootgrafanaprometheus

解决方案


在花了 2 个小时找到解决方案后,我们也对所有健康点使用基本身份验证问题是我没有在我的 proemtheus.yml 中设置基本身份验证

- job_name: 'my-api'
    scrape_interval: 10s
    metrics_path: '/prometheus'
    target_groups:
       - targets: ['localhost:8080']
    basic_auth:
      username: test
      password: test

推荐阅读