1

我有一个简单的 Spring-Boot 应用程序(使用 Maven),我使用 Prometheus 对其进行了检测,以收集特定于应用程序的指标。默认情况下,检测的 Prometheus 指标是公开http://localhost:8080/prometheus的,但我不确定如何在 Grafana 上可视化这些指标。当我尝试在 Grafana 中设置数据源时,出现错误:

未找到 HTTP 错误

添加新数据源时出现 Grafana 错误

下面是我的工作代码,它公开了关于http:localhost:8080/prometheus

Example.java:简单地公开一些端点。检测以便 Prometheus 计算/homepage端点被命中的次数。

@SpringBootApplication
@RestController
public class Example {

    //Just a logger that keeps track of relevant information:
    private static final Logger LOGGER = Logger.getLogger(Example.class.getName());

    //counter for counting how many times an endpoint has been hit
    static final Counter myCounter = Counter.build()    
                                              .name("CounterName") //note: by convention, counters should have "_total" suffix
                                              .help("Total requests recorded by a specific endpoint")
                                              .labelNames("status")
                                              .register();

    //using a guage to set the time that a dummy process started, store the time it ends, and then calculate
    //the elapsed time between when the process started and when the process ended.
    static final Gauge myGauge =  Gauge.build()
                                .name("GaugeName")
                                .help("This is the Gauge help message")
                                .labelNames("label1")
                                .register();            

    @RequestMapping("/hello")
    String hello() {

        myCounter.labels("customLabel1").inc(); //increment the number of requests by one
        myGauge.labels("customLabel1").inc(3);
        LOGGER.log(Level.INFO, "Number of times /hello has been hit: " + myCounter.labels("customLabel1").get());
        LOGGER.log(Level.INFO, "Size of our Gauge: " + myGauge.labels("customLabel1").get());
        myGauge.labels("customLabel1").dec();
        LOGGER.log(Level.INFO, "size of out Gauge after decrementing by 1: " + myGauge.labels("customLabel1").get());

        return "Hello world! This is an example response!";
    }

    @RequestMapping("/homepage")
    String homePage() {

        myCounter.labels("customLabel2").inc(); //increment the number of requests by one
        LOGGER.log(Level.INFO, "Number of times /homepage has been hit: " + myCounter.labels("customLabel2").get());

        return "this is the home page!!";
    }


    public static void main(String[] args) throws Exception {
        SpringApplication.run(Example.class, args);
    }

}

MonitoringConfig.java:这是 Prometheus 收集应用程序的度量信息的方式,并将它们暴露在localhostL8080/prometheus

@Configuration
public class MonitoringConfig {
    @Bean
    SpringBootMetricsCollector springBootMetricsCollector(Collection<PublicMetrics> publicMetrics){
        SpringBootMetricsCollector springBootMetricsCollector = new SpringBootMetricsCollector(publicMetrics);
        springBootMetricsCollector.register();

        return springBootMetricsCollector;
    }

    @Bean
    ServletRegistrationBean servletRegistrationBean() {
        DefaultExports.initialize();
        return new ServletRegistrationBean(new MetricsServlet(), "/prometheus");
    }
}

pom.xml:项目的 Maven 依赖项

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.example</groupId>
    <artifactId>springboot</artifactId>
    <version>0.0.1-SNAPSHOT</version>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>1.5.8.RELEASE</version>
    </parent>
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <!-- Prometheus dependencies -->
        <!-- The client -->
        <dependency>
            <groupId>io.prometheus</groupId>
            <artifactId>simpleclient</artifactId>
            <version>0.1.0</version>
        </dependency>

        <dependency>
            <groupId>io.prometheus</groupId>
            <artifactId>simpleclient_spring_boot</artifactId>
            <version>0.1.0</version>
        </dependency>

        <dependency>
            <groupId>io.prometheus</groupId>
            <artifactId>simpleclient_servlet</artifactId>
            <version>0.1.0</version>
        </dependency>
        <!-- Hotspot JVM metrics -->
        <dependency>
            <groupId>io.prometheus</groupId>
            <artifactId>simpleclient_hotspot</artifactId>
            <version>0.1.0</version>
        </dependency>
        <!-- Exposition HTTPServer -->
        <dependency>
            <groupId>io.prometheus</groupId>
            <artifactId>simpleclient_httpserver</artifactId>
            <version>0.1.0</version>
        </dependency>
        <!-- Pushgateway exposition -->
        <dependency>
            <groupId>io.prometheus</groupId>
            <artifactId>simpleclient_pushgateway</artifactId>
            <version>0.1.0</version>
        </dependency>

        <!-- Spring Boot Actuator for exposing metrics -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-actuator</artifactId>
            <version>1.5.8.RELEASE</version>
        </dependency>

    </dependencies>


</project>
4

2 回答 2

2

您需要将 grafana 指向 prometheus 服务器,而不是您的应用程序。

有关如何运行 prometheus 以及如何在 grafana 仪表板中可视化 prometheus 指标的更多详细信息,请参阅prometheus 文档。

于 2017-12-04T23:36:47.033 回答
0

Grafana 是一个可视化工具。它无助于监控指标。它有助于为已经被监控的指标创建可视化/图表。

Data Source在 Grafana WebUI 上的添加是正确的,但数据源应指向您的 Prometheus URL。

以下是使用 prometheus 将指标映射到 Grafana 的步骤。

  1. 安装Prometheus后,更改prometheus.yml以包含您的应用程序端点。Prometheus 从该端点抓取监控指标

 scrape_configs
 - job_name: 'my_application'
      metrics_path: '/prometheus'
      static_configs:
      - targets: ['localhost:8080']

  1. 现在prometheus监控这个端点下的所有指标。您可以导航到http://localhost:9090/以观察指标。

  2. 在 Grafana 数据源中提及此 URLDataSources->New

    提及 URL:http://localhost:9090和 Access:Browser并将此数据源命名为 Prometheus

于 2018-07-31T16:05:49.083 回答