一、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监控面板,推荐第一个非常不错.
设置时间段:
源码简单介绍
例如采集磁盘数据:
采集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连接池.