Springboot 网关 Prometheus 收集海量数据

问题描述

我使用 springboot 作为微服务。

我的 k8s 集群中有大约 20 个微服务

我正在使用 promethues 收集数据以在 Grafana 中显示

在该应用程序中,有一些 url 使用 Path 变量,如下所示

  1. /v1/contacts/{id}
  2. /v1/users/{id}

还有几个网址,如果我考虑所有微服务中的所有此类网址,那么使用路径变量的网址可能大约为 60 到 70 个

问题:

现在每当请求任何网址时,例如

  1. /v1/contacts/10
  2. /v1/contacts/111
  3. /v1/contacts/51
  4. /v1/users/91

等等...

然后,promethus 正在收集所有此类 url 的指标。一段时间后,它有大量指标,最后我增加了从普罗米修斯收集数据的响应时间。

所以基本上我想在我的 springboot 应用程序一段时间后清除 prometheus 日志。

我不确定它是否可能。

有人可以帮我解决这个问题吗?

谢谢

解决方法

Prometheus 有几个用于配置本地存储的标志。

--storage.tsdb.path:Prometheus 在何处写入其数据库。默认为 data/
--storage.tsdb.retention.time:何时删除旧数据。默认为 15 天。如果此标志设置为默认值以外的任何值,则覆盖 storage.tsdb.retention。
--storage.tsdb.retention.size:[实验] 要保留的存储块的最大字节数。最旧的数据将首先被删除。默认为 0 或禁用。此标志是实验性的,可能会在未来版本中更改。支持的单位:B、KB、MB、GB、TB、PB、EB。例如:“512MB”

Prometheus 存储 link

步骤:

  1. Spring Boot 应用暴露监控指标,添加 以下依赖项。
    <artifactId>spring-boot-starter-actuator</artifactId>

    <groupId>io.prometheus</groupId>
    artifactId>simpleclient_spring_boot</artifactId>

  2. Prometheus 收集 Spring Boot 指标数据。首先,得到 Prometheus 的 Docker 镜像:docker pull prom/prometheus

    然后,编写配置文件prometheus.yml:

    global:
      scrape_interval: 10s
      scrape_timeout: 10s
      evaluation_interval: 10m
    
    scrape_configs:
      - job_name: prometheus
        scrape_interval: 5s
        scrape_timeout: 5s
        metrics_path: /metrics
        scheme: http
    

    您可以为其添加更多值。示例here

  3. 接下来,启动 Prometheus:

    docker run -d -p 9090:9090 \
    -u root \
    -v /opt/prometheus/tsdb:/etc/prometheus/tsdb \
    -v /opt/prometheus/prometheus.yml:/etc/prometheus/prometheus.yml \
    --privileged=true prom/prometheus \
    --storage.tsdb.path=/etc/prometheus/tsdb \
    --storage.tsdb.retention.time=7d \
    --storage.tsdb.retention.size = 300MB \
    --config.file=/etc/prometheus/prometheus.yml
    

另一种方式,您可以使用此链接Monitoring Spring Boot projects with Prometheus
但请确保当您使用 Docker Compose 运行 Prometheus 服务器时,您必须使用大小或时间属性更新 commands 部分。