java 我如何使用prometheus绘制在某个端点上每个API命中所花费的时间?

ulmd4ohb  于 2023-04-19  发布在  Java
关注(0)|答案(1)|浏览(120)

我尝试在Prometheus中创建一些指标。我想测量当我到达某个端点时需要多长时间才能返回响应。因此我用@Timed注解了路由。
这是我的控制器:

import io.micrometer.core.annotation.Timed;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class MyController {

    @GetMapping("/ping")
    @Timed(value = "ping.request.time", description = "time pIng")
    public String ping(){
        return "pIng";
    }

    @GetMapping("/pong")
    @Timed(value = "pong.request.time", description = "time pOng")
    public String pong(){
        return "pOng";
    }
}

但是在http://localhost:8080/actuator/prometheus中没有出现任何提到pingpong的内容。但是在点击这些API至少一次之后,它们出现了。响应看起来像这样:

...
# HELP http_server_requests_seconds  
# TYPE http_server_requests_seconds summary
http_server_requests_seconds_count{error="none",exception="none",method="GET",outcome="SUCCESS",status="200",uri="/actuator/prometheus",} 2.0
http_server_requests_seconds_sum{error="none",exception="none",method="GET",outcome="SUCCESS",status="200",uri="/actuator/prometheus",} 0.5708052
http_server_requests_seconds_count{error="none",exception="none",method="GET",outcome="SUCCESS",status="200",uri="/ping",} 1.0
http_server_requests_seconds_sum{error="none",exception="none",method="GET",outcome="SUCCESS",status="200",uri="/ping",} 0.0085949
http_server_requests_seconds_count{error="none",exception="none",method="GET",outcome="SUCCESS",status="200",uri="/actuator/metrics",} 1.0
http_server_requests_seconds_sum{error="none",exception="none",method="GET",outcome="SUCCESS",status="200",uri="/actuator/metrics",} 0.1374681
http_server_requests_seconds_count{error="none",exception="none",method="GET",outcome="CLIENT_ERROR",status="404",uri="/**",} 1.0
http_server_requests_seconds_sum{error="none",exception="none",method="GET",outcome="CLIENT_ERROR",status="404",uri="/**",} 0.0148576
# HELP http_server_requests_seconds_max  
# TYPE http_server_requests_seconds_max gauge
http_server_requests_seconds_max{error="none",exception="none",method="GET",outcome="SUCCESS",status="200",uri="/actuator/prometheus",} 0.0508929
http_server_requests_seconds_max{error="none",exception="none",method="GET",outcome="SUCCESS",status="200",uri="/ping",} 0.0085949
http_server_requests_seconds_max{error="none",exception="none",method="GET",outcome="SUCCESS",status="200",uri="/actuator/metrics",} 0.0
...

但是当我尝试执行这个查询时:rate(http_server_requests_seconds_sum{uri="/ping"}[5m])/rate(http_server_requests_seconds_count{uri="/ping"}[5m]),它给了我**Empty query result**
我还尝试了其他查询,例如:

  • http_server_requests_seconds_sum{uri="/ping"}
  • http_server_requests_seconds_count{uri="/ping"}但结果是一样的。

如何使用prometheus在/ping上绘制每个API命中所花费的时间?
下面是我的prometheus.yaml文件:

global:
  scrape_interval:     15s # Default is every 1 minute.
  evaluation_interval: 15s # Evaluate rules every 15 seconds. The default is every 1 minute.

rule_files:
# - "first_rules.yml"
# - "second_rules.yml"

# A scrape configuration containing exactly one endpoint to scrape:
scrape_configs:
  - job_name: 'prometheus'

    static_configs:
      - targets: ['127.0.0.1:9090']

  - job_name: 'spring-actuator'
    metrics_path: '/actuator/prometheus'
    scrape_interval: 5s
    static_configs:
      - targets: ['SYSTEM_IP_ADDRESS:8080'] # refer system ip address rather that localhost

下面是我如何运行系统**docker run -d --name=prometheus -p 9090:9090 -v C:\Users\Admin\Desktop\prometheus-test\prometheus.yaml:/etc/prometheus/prometheus.yml prom/prometheus --config.file=/etc/prometheus/prometheus.yml**

zkure5ic

zkure5ic1#

验证Prometheus是否实际显示在问题指标中:如果它们存在,您将通过查询得到一些结果。您可以通过查询http_server_requests_seconds_sum{uri="/ping"}或仅查询http_server_requests_seconds_sum来验证它们是否存在。
如果没有返回-检查目标页面,看看你的目标是否可以被Prometheus访问,并且实际上是被抓取的。
如果目标不可达-重新检查配置。此行

- targets: ['SYSTEM_IP_ADDRESS:8080'] # refer system ip address rather that localhost

似乎很可疑

相关问题