Grafana+Prometheus监控篇-SpringBoot服务

一、SpringBoot配置.

   ①、引入依赖

<dependency>
   <groupId>org.springframework.boot</groupId>
   <artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<dependency>
    <groupId>io.micrometer</groupId>
    <artifactId>micrometer-registry-prometheus</artifactId>
</dependency>

   ②、开启监控

management:
  endpoints:
    web:
      exposure:
        include: prometheus

   ③、启动服务,查看监控采集:

http://test.code.com/skywalk/actuator/prometheus

二、定义Prometheus监控任务.

   ①、prometheus.yml

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: 'Windows'
    static_configs:
    - targets: ['192.168.1.6:9182']
      labels:
        instance: Windows
  # 新添加的采集目标
  - job_name: "skywalk-boot"
    metrics_path: '/skywalk/actuator/prometheus'
    static_configs:
      - targets: ["192.168.1.6:80"] 

   ②、启动prometheus

  ③、 查看界面

 三、启动Grafana

  ①、直接启动即可.

springboot监控面板,推荐第一个非常不错.

Dashboards | Grafana Labs

 

 设置时间段:

源码简单介绍

例如采集磁盘数据:

 采集Tomcat信息:

 根据Tomcat的包来获取:

package org.apache.catalina;

 例如Kafka的监控收集指标,封装了一整套的采集-发送框架,可扩展各种中间件监控,对于自研一套内部的业务采集监控指标很有参考意义.

对比一下Dubbo的监控采集数据,基本是一样的采集数据监控的轮子.

附:监控HikariCP数据库连接池

可观测性-Metrics-数据库连接池HikariCP监控_hikari连接池监控_lakernote的博客-CSDN博客

/*
 * Copyright (C) 2015 Brett Wooldridge
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.zaxxer.hikari.metrics;

import static com.zaxxer.hikari.util.ClockSource.currentTime;
import static com.zaxxer.hikari.util.ClockSource.plusMillis;

import java.util.concurrent.atomic.AtomicLong;

/**
 *
 * @author Brett Wooldridge
 */
public abstract class PoolStats
{
   private final AtomicLong reloadAt;
   private final long timeoutMs;

   protected volatile int totalConnections;
   protected volatile int idleConnections;
   protected volatile int activeConnections;
   protected volatile int pendingThreads;
   protected volatile int maxConnections;
   protected volatile int minConnections;

   public PoolStats(final long timeoutMs)
   {
      this.timeoutMs = timeoutMs;
      this.reloadAt = new AtomicLong();
   }

   public int getTotalConnections()
   {
      if (shouldLoad()) {
         update();
      }

      return totalConnections;
   }

   public int getIdleConnections()
   {
      if (shouldLoad()) {
         update();
      }

      return idleConnections;
   }

   public int getActiveConnections()
   {
      if (shouldLoad()) {
         update();
      }

      return activeConnections;
   }

   public int getPendingThreads()
   {
      if (shouldLoad()) {
         update();
      }

      return pendingThreads;
   }

   public int getMaxConnections() {
      if (shouldLoad()) {
         update();
      }

      return maxConnections;
   }

   public int getMinConnections() {
      if (shouldLoad()) {
         update();
      }

      return minConnections;
   }

   protected abstract void update();

   private boolean shouldLoad()
   {
      for (; ; ) {
          final long now = currentTime();
          final long reloadTime = reloadAt.get();
          if (reloadTime > now) {
              return false;
          }
          else if (reloadAt.compareAndSet(reloadTime, plusMillis(now, timeoutMs))) {
              return true;
          }
      }
  }
}

 指标详解
hikaricp.connections    当前总连接数,包括空闲的连接和使用中的连接。(4 = 3 + 1)对应上面日志;
Connections = activeConnection + idleConnections,会随着连接使用情况变化。
hikaricp.connections.active    正在使用中活跃连接数 (1),会随着连接使用情况变化。
hikaricp.connections.idle    空闲连接数 (3) ,会随着连接使用情况变化。
hikaricp.connections.max    最大连接数 (10),初始配置。
hikaricp.connections.min    最小连接数 (3),初始配置。
hikaricp.connections.pending    正在等待连接的线程数量(0)。重点:一般来说,这里应该都是0,如果存在这个数据并且时间较长要触发告警,视情况加大最大连接数。
hikaricp.connections.acquire    获取每个连接需要时间,单位为ns。
hikaricp.connections.creation    连接创建时间,单位为ms。
hikaricp.connections.timeout    创建连接超时次数。
hikaricp.connections.usage    连接从池中取出到返回的时间,单位为ms。即连接被业务占用时间(3.017s)。重点:这个时间长的话,
可能是慢SQL或者长事务导致连接被占用问题。

JAVA包监控指标采集

HikariPoolMXBean
package com.zaxxer.hikari;

import javax.sql.DataSource;

/**
 * The javax.management MBean for a Hikari pool instance.
 *
 * @author Brett Wooldridge
 */
public interface HikariPoolMXBean
{
   /**
    * Get the number of currently idle connections in the pool.
    * <p>
    * The return value is extremely transient and is a point-in-time measurement.  Therefore, due to a time
    * difference between invoking this method and {@link #getActiveConnections()}, it is possible for the sum
    * of idle plus active connections to be either less than or greater than the value returned by
    * {@link #getTotalConnections()}.
    *
    * @return the current number of idle connections in the pool
    */
   int getIdleConnections();

   /**
    * Get the number of currently active connections in the pool.
    * <p>
    * The return value is extremely transient and is a point-in-time measurement.  Therefore, due to a time
    * difference between invoking this method and {@link #getIdleConnections()}, it is possible for the sum
    * of idle plus active connections to be either less than or greater than the value returned by
    * {@link #getTotalConnections()}.
    *
    * @return the current number of active (in-use) connections in the pool
    */
   int getActiveConnections();

   /**
    * Get the total number of connections currently in the pool.  The return value is transient and is a
    * point-in-time measurement.
    *
    * @return the total number of connections in the pool
    */
   int getTotalConnections();

   /**
    * Get the number of threads awaiting connections from the pool.  The return value is extremely transient and is
    * a point-in-time measurement.
    *
    * @return the number of threads awaiting a connection from the pool
    */
   int getThreadsAwaitingConnection();

   /**
    * Evict currently idle connections from the pool, and mark active (in-use) connections for eviction when they are
    * returned to the pool.
    */
   void softEvictConnections();

   /**
    * Suspend the pool.  When the pool is suspended, threads calling {@link DataSource#getConnection()} will be
    * blocked <i>with no timeout</i> until the pool is resumed via the {@link #resumePool()} method.
    * <br>
    * This method has no effect unless the {@link HikariConfig#setAllowPoolSuspension(boolean)} method or equivalent
    * property has been set to {@code true}.
    */
   void suspendPool();

   /**
    * Resume the pool.  Enables connection borrowing to resume on a pool that has been suspended via the
    * {@link #suspendPool()} method.
    * <br>
    * This method has no effect unless the {@link HikariConfig#setAllowPoolSuspension(boolean)} method or equivalent
    * property has been set to {@code true}.
    */
   void resumePool();
}

监控采集HttpClient连接池.

  • 0
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

大道之简

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值