1.prometheus接入springboot

prometheus安装后,在安装目录有一个默认的配置文件prometheus.yml

# my global config
global:
  scrape_interval: 15s # Set the scrape interval to every 15 seconds. Default is every 1 minute.
  evaluation_interval: 15s # Evaluate rules every 15 seconds. The default is every 1 minute.
  # scrape_timeout is set to the global default (10s).

# Alertmanager configuration
alerting:
  alertmanagers:
    - static_configs:
        - targets:
          # - alertmanager:9093

# Load rules once and periodically evaluate them according to the global 'evaluation_interval'.
rule_files:
  # - "first_rules.yml"
  # - "second_rules.yml"

# A scrape configuration containing exactly one endpoint to scrape:
# Here it's Prometheus itself.
scrape_configs:
  # The job name is added as a label `job=<job_name>` to any timeseries scraped from this config.
  - job_name: "prometheus"

    # metrics_path defaults to '/metrics'
    # scheme defaults to 'http'.

    static_configs:
      - targets: ["localhost:9090"]

默认配置了一个job_name,监控prometheus本身。需要增加一个监控springboot项目

- job_name: "custom_spring_boot"
    metrics_path: '/actuator/prometheus'
    static_configs:
      - targets: ["localhost:9595"]
1f5c659520e2b7ed8bb03098cc7ab628.png
  • metrics_path 默认采集metrics的路径是/metrics;需要改成/actuator/prometheus

  • scheme 默认是http;如果是https需要自定义配置

  • targets 获取metrics的地址和端口列表

2.访问prometheus

http://127.0.0.1:9090/
beec1a161d63857e2952e6eec056734b.png

出现自定义需要监控的springboot端点列表

d57a2a2e55c3421ad3eb70983a70e1f8.png

在首页,可以查询各种不同的指标0f4a694986bdce1b5570a87180a16678.png

比如查询custom_http_request_time_seconds_count指标

b7c4d9896dac5656b1014a7bcdfd85cb.png

3.grafana接入prometheus

访问

http://127.0.0.1:3000/

配置数据源

c3db11350480b31b7f5bee1bc7a4c426.png

添加一个数据

a33ef729a9dc41c52e71425d2dd6d639.png

选择prometheus

70956234cada0903aa5118a0ce2eb018.png

设置名称和prometheus服务地址

090b85f118c47c5e627d54561b5ed669.png258ade3c99f6ae961007ad07d29226bd.png

4.配置仪表盘

c9d1855b38b7715acf564b40fc2edec8.png

点击Add a new panel;新建一个Panel

3304c45a4d46cbedd0552eb2ad6dbf14.png

平均时间查询

sum by(api) (rate(custom_http_request_time_seconds_count{job="custom_spring_boot", api="/order"}[5m]))
c55bf5b0cc7c00b0e54085719d4c1b2a.png

保存,最终显示

0a47f121f9714e8262a206e22b1c1f68.png

a0df979142b74eb880d910c7da0cbcfb.gif

Logo

魔乐社区(Modelers.cn) 是一个中立、公益的人工智能社区,提供人工智能工具、模型、数据的托管、展示与应用协同服务,为人工智能开发及爱好者搭建开放的学习交流平台。社区通过理事会方式运作,由全产业链共同建设、共同运营、共同享有,推动国产AI生态繁荣发展。

更多推荐