mirror of https://gitee.com/stylefeng/roses
新增prometheus监控
parent
0c595a6cb0
commit
c1ade42b55
|
@ -34,6 +34,12 @@
|
||||||
<artifactId>commons-logging</artifactId>
|
<artifactId>commons-logging</artifactId>
|
||||||
</dependency>
|
</dependency>
|
||||||
|
|
||||||
|
<!-- https://mvnrepository.com/artifact/javax.activation/activation -->
|
||||||
|
<dependency>
|
||||||
|
<groupId>javax.activation</groupId>
|
||||||
|
<artifactId>activation</artifactId>
|
||||||
|
<version>1.1.1</version>
|
||||||
|
</dependency>
|
||||||
</dependencies>
|
</dependencies>
|
||||||
|
|
||||||
</project>
|
</project>
|
||||||
|
|
|
@ -0,0 +1,44 @@
|
||||||
|
package cn.stylefeng.roses.kernel.monitor.api;
|
||||||
|
|
||||||
|
import cn.stylefeng.roses.kernel.monitor.api.pojo.prometheus.PromResultInfo;
|
||||||
|
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 监控管理prometheus
|
||||||
|
*
|
||||||
|
* @author chenli
|
||||||
|
* @date 2021/1/10 16:09
|
||||||
|
*/
|
||||||
|
public interface PrometheusApi {
|
||||||
|
|
||||||
|
/**
|
||||||
|
* prometheus采集监控指标
|
||||||
|
*
|
||||||
|
* @param promURL prometheus地址
|
||||||
|
* @param promQL prometheus查询表达式
|
||||||
|
* @param isRate prometheus是否需要计算周期内数值函数
|
||||||
|
* @param promQL prometheus需要计算的指标
|
||||||
|
* @return Map<String, Object> 指标名称与指标数据的集合
|
||||||
|
* @author chenli
|
||||||
|
* @date 2021/1/10 17:37
|
||||||
|
*/
|
||||||
|
List<PromResultInfo> getMetricInfo(String promURL, String promQL, String isRate, String rateMetric);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 配置prometheus无效链接则关闭prometheus相关菜单
|
||||||
|
*
|
||||||
|
* @author chenli
|
||||||
|
* @date 2021/3/3 16:08
|
||||||
|
*/
|
||||||
|
void closePrometheusMenu();
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 配置prometheus有效链接无效则显示prometheus相关菜单
|
||||||
|
*
|
||||||
|
* @author chenli
|
||||||
|
* @date 2021/3/3 16:08
|
||||||
|
*/
|
||||||
|
void displayPrometheusMenu();
|
||||||
|
|
||||||
|
}
|
|
@ -18,4 +18,28 @@ public interface MonitorConstants {
|
||||||
*/
|
*/
|
||||||
String MONITOR_EXCEPTION_STEP_CODE = "27";
|
String MONITOR_EXCEPTION_STEP_CODE = "27";
|
||||||
|
|
||||||
|
/**
|
||||||
|
* prometheus查询命令
|
||||||
|
*/
|
||||||
|
String MONITOR_PROMETHEUS_QUERY = "query";
|
||||||
|
|
||||||
|
/**
|
||||||
|
* prometheus查询区间向量命令
|
||||||
|
*/
|
||||||
|
String MONITOR_PROMETHEUS_QUERY_RANGE = "query_range";
|
||||||
|
|
||||||
|
/**
|
||||||
|
* prometheus查询开始时间
|
||||||
|
*/
|
||||||
|
String MONITOR_PROMETHEUS_START = "start";
|
||||||
|
|
||||||
|
/**
|
||||||
|
* prometheus查询结束时间
|
||||||
|
*/
|
||||||
|
String MONITOR_PROMETHEUS_END = "end";
|
||||||
|
|
||||||
|
/**
|
||||||
|
* prometheus查询步长
|
||||||
|
*/
|
||||||
|
String MONITOR_PROMETHEUS_STEP = "step";
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,6 +1,5 @@
|
||||||
package cn.stylefeng.roses.kernel.monitor.api.exception.enums;
|
package cn.stylefeng.roses.kernel.monitor.api.exception.enums;
|
||||||
|
|
||||||
import cn.stylefeng.roses.kernel.monitor.api.constants.MonitorConstants;
|
|
||||||
import cn.stylefeng.roses.kernel.rule.constants.RuleConstants;
|
import cn.stylefeng.roses.kernel.rule.constants.RuleConstants;
|
||||||
import cn.stylefeng.roses.kernel.rule.exception.AbstractExceptionEnum;
|
import cn.stylefeng.roses.kernel.rule.exception.AbstractExceptionEnum;
|
||||||
import lombok.Getter;
|
import lombok.Getter;
|
||||||
|
@ -15,9 +14,10 @@ import lombok.Getter;
|
||||||
public enum MonitorExceptionEnum implements AbstractExceptionEnum {
|
public enum MonitorExceptionEnum implements AbstractExceptionEnum {
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 演示环境无法操作
|
* prometheus配置异常
|
||||||
*/
|
*/
|
||||||
DEMO_OPERATE(RuleConstants.BUSINESS_ERROR_TYPE_CODE + MonitorConstants.MONITOR_EXCEPTION_STEP_CODE + "01", "演示环境无法操作!");
|
PROMETHEUS_CONFIG_ERROR(RuleConstants.THIRD_ERROR_TYPE_CODE + RuleConstants.RULE_EXCEPTION_STEP_CODE + "02", "prometheus配置异常,具体信息为:{}");
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 错误编码
|
* 错误编码
|
||||||
|
|
|
@ -0,0 +1,31 @@
|
||||||
|
package cn.stylefeng.roses.kernel.monitor.api.pojo.prometheus;
|
||||||
|
|
||||||
|
import lombok.Data;
|
||||||
|
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* prometheus查询返回结果信息
|
||||||
|
*
|
||||||
|
* @author chenli
|
||||||
|
* @date 2021/1/10 19:10
|
||||||
|
*/
|
||||||
|
@Data
|
||||||
|
public class PromDataInfo {
|
||||||
|
|
||||||
|
/**
|
||||||
|
* prometheus结果类型:
|
||||||
|
* <p>
|
||||||
|
* vector -- 瞬时向量
|
||||||
|
* matrix -- 区间向量
|
||||||
|
* scalar -- 标量
|
||||||
|
* string -- 字符串
|
||||||
|
*/
|
||||||
|
private String resultType;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* prometheus指标属性和值
|
||||||
|
*/
|
||||||
|
private List<PromResultInfo> result;
|
||||||
|
|
||||||
|
}
|
|
@ -0,0 +1,54 @@
|
||||||
|
package cn.stylefeng.roses.kernel.monitor.api.pojo.prometheus;
|
||||||
|
|
||||||
|
import lombok.Data;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* prometheus返回监控指标信息
|
||||||
|
*
|
||||||
|
* @author chenli
|
||||||
|
* @date 2021/1/10 18:53
|
||||||
|
*/
|
||||||
|
@Data
|
||||||
|
public class PromMetricInfo {
|
||||||
|
|
||||||
|
/**
|
||||||
|
* prometheus监控指标id
|
||||||
|
*/
|
||||||
|
private String id;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* prometheus监控指标名称
|
||||||
|
*/
|
||||||
|
private String __name__;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* prometheus实例名称
|
||||||
|
*/
|
||||||
|
private String instance;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* prometheus任务名称
|
||||||
|
*/
|
||||||
|
private String job;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* prometheus jvm区域指标
|
||||||
|
*/
|
||||||
|
private String area;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* prometheus 日志监控级别
|
||||||
|
*/
|
||||||
|
private String level;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* prometheus 是否需要计算周期内数据
|
||||||
|
*/
|
||||||
|
private String isRate;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* prometheus 指定监控指标
|
||||||
|
*/
|
||||||
|
private String rateMetric;
|
||||||
|
}
|
||||||
|
|
|
@ -0,0 +1,26 @@
|
||||||
|
package cn.stylefeng.roses.kernel.monitor.api.pojo.prometheus;
|
||||||
|
|
||||||
|
import lombok.Data;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* prometheus http响应信息
|
||||||
|
*
|
||||||
|
* @author chenli
|
||||||
|
* @date 2021/1/10 19:00
|
||||||
|
*/
|
||||||
|
@Data
|
||||||
|
public class PromResponseInfo {
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 状态:
|
||||||
|
* <p>
|
||||||
|
* 成功-success
|
||||||
|
*/
|
||||||
|
private String status;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* prometheus指标属性和值
|
||||||
|
*/
|
||||||
|
private PromDataInfo data;
|
||||||
|
|
||||||
|
}
|
|
@ -0,0 +1,24 @@
|
||||||
|
package cn.stylefeng.roses.kernel.monitor.api.pojo.prometheus;
|
||||||
|
|
||||||
|
import lombok.Data;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* prometheus结果
|
||||||
|
*
|
||||||
|
* @author chenli
|
||||||
|
* @date 2021/1/10 18:58
|
||||||
|
*/
|
||||||
|
@Data
|
||||||
|
public class PromResultInfo {
|
||||||
|
|
||||||
|
/**
|
||||||
|
* prometheus指标属性
|
||||||
|
*/
|
||||||
|
private PromMetricInfo metric;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* prometheus指标值
|
||||||
|
*/
|
||||||
|
private String[] values;
|
||||||
|
|
||||||
|
}
|
|
@ -0,0 +1,43 @@
|
||||||
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
|
<project xmlns="http://maven.apache.org/POM/4.0.0"
|
||||||
|
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||||
|
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||||
|
<parent>
|
||||||
|
<artifactId>kernel-o-monitor</artifactId>
|
||||||
|
<groupId>cn.stylefeng.roses</groupId>
|
||||||
|
<version>7.0.1</version>
|
||||||
|
</parent>
|
||||||
|
<modelVersion>4.0.0</modelVersion>
|
||||||
|
|
||||||
|
<artifactId>monitor-sdk-prometheus</artifactId>
|
||||||
|
<dependencies>
|
||||||
|
<dependency>
|
||||||
|
<groupId>cn.stylefeng.roses</groupId>
|
||||||
|
<artifactId>monitor-api</artifactId>
|
||||||
|
<version>7.0.1</version>
|
||||||
|
<scope>compile</scope>
|
||||||
|
</dependency>
|
||||||
|
|
||||||
|
<!-- actuator monitor -->
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.springframework.boot</groupId>
|
||||||
|
<artifactId>spring-boot-starter-actuator</artifactId>
|
||||||
|
</dependency>
|
||||||
|
|
||||||
|
<!-- prometheus meter -->
|
||||||
|
<dependency>
|
||||||
|
<groupId>io.micrometer</groupId>
|
||||||
|
<artifactId>micrometer-registry-prometheus</artifactId>
|
||||||
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.springframework</groupId>
|
||||||
|
<artifactId>spring-tx</artifactId>
|
||||||
|
</dependency>
|
||||||
|
</dependencies>
|
||||||
|
|
||||||
|
<properties>
|
||||||
|
<maven.compiler.source>8</maven.compiler.source>
|
||||||
|
<maven.compiler.target>8</maven.compiler.target>
|
||||||
|
</properties>
|
||||||
|
|
||||||
|
</project>
|
|
@ -0,0 +1,26 @@
|
||||||
|
package cn.stylefeng.roses.kernel.monitor.prometheus.mapper;
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 关闭或者打开prometheus菜单接口
|
||||||
|
*
|
||||||
|
* @author chenli
|
||||||
|
* @date 2021/3/3 16:12
|
||||||
|
*/
|
||||||
|
public interface PrometheusMenuMapper {
|
||||||
|
|
||||||
|
/***
|
||||||
|
* 功能描述: 关闭或者打开prometheus菜单
|
||||||
|
* 创建时间: 2021/3/3 16:12
|
||||||
|
* @author chenli
|
||||||
|
*/
|
||||||
|
/**
|
||||||
|
* 关闭或者打开prometheus菜单
|
||||||
|
* @param statusFlag 1启用2关闭
|
||||||
|
* @author chenli
|
||||||
|
* @date 2021/3/3 16:12
|
||||||
|
*/
|
||||||
|
void displayOrClosePrometheusMenu(int statusFlag);
|
||||||
|
|
||||||
|
}
|
|
@ -0,0 +1,8 @@
|
||||||
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
|
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
|
||||||
|
<mapper namespace="cn.stylefeng.roses.kernel.monitor.prometheus.mapper.PrometheusMenuMapper">
|
||||||
|
|
||||||
|
<update id="displayOrClosePrometheusMenu" parameterType="int">
|
||||||
|
update sys_menu set status_flag=#{statusFlag} where menu_code in ('monitor_quality','monitor_redis','monitor_tomcat','monitor_jvm','monitor_trace','monitor_disk')
|
||||||
|
</update>
|
||||||
|
</mapper>
|
|
@ -0,0 +1,13 @@
|
||||||
|
package cn.stylefeng.roses.kernel.monitor.prometheus.service;
|
||||||
|
|
||||||
|
import cn.stylefeng.roses.kernel.monitor.api.PrometheusApi;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 监控管理prometheus
|
||||||
|
*
|
||||||
|
* @author chenli
|
||||||
|
* @date 2021/1/10 16:09
|
||||||
|
*/
|
||||||
|
public interface PrometheusService extends PrometheusApi {
|
||||||
|
|
||||||
|
}
|
|
@ -0,0 +1,127 @@
|
||||||
|
package cn.stylefeng.roses.kernel.monitor.prometheus.service.impl;
|
||||||
|
|
||||||
|
import cn.hutool.core.io.IORuntimeException;
|
||||||
|
import cn.hutool.core.util.ObjectUtil;
|
||||||
|
import cn.hutool.core.util.StrUtil;
|
||||||
|
import cn.hutool.http.HttpUtil;
|
||||||
|
import cn.stylefeng.roses.kernel.monitor.api.constants.MonitorConstants;
|
||||||
|
import cn.stylefeng.roses.kernel.monitor.api.exception.MonitorException;
|
||||||
|
import cn.stylefeng.roses.kernel.monitor.api.exception.enums.MonitorExceptionEnum;
|
||||||
|
import cn.stylefeng.roses.kernel.monitor.api.pojo.prometheus.PromResponseInfo;
|
||||||
|
import cn.stylefeng.roses.kernel.monitor.api.pojo.prometheus.PromResultInfo;
|
||||||
|
import cn.stylefeng.roses.kernel.monitor.prometheus.mapper.PrometheusMenuMapper;
|
||||||
|
import cn.stylefeng.roses.kernel.monitor.prometheus.service.PrometheusService;
|
||||||
|
import cn.stylefeng.roses.kernel.rule.enums.StatusEnum;
|
||||||
|
import com.alibaba.fastjson.JSON;
|
||||||
|
import lombok.extern.slf4j.Slf4j;
|
||||||
|
import org.springframework.stereotype.Service;
|
||||||
|
import org.springframework.transaction.annotation.Transactional;
|
||||||
|
|
||||||
|
import javax.annotation.Resource;
|
||||||
|
import java.util.Calendar;
|
||||||
|
import java.util.Date;
|
||||||
|
import java.util.HashMap;
|
||||||
|
import java.util.List;
|
||||||
|
import java.util.Map;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 监控管理prometheus
|
||||||
|
*
|
||||||
|
* @author chenli
|
||||||
|
* @date 2021/1/10 16:09
|
||||||
|
*/
|
||||||
|
@Service
|
||||||
|
@Slf4j
|
||||||
|
public class PrometheusServiceImpl implements PrometheusService {
|
||||||
|
|
||||||
|
@Resource
|
||||||
|
private PrometheusMenuMapper mapper;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* prometheus采集监控指标
|
||||||
|
*
|
||||||
|
* @author chenli
|
||||||
|
* @date 2021/1/10 16:09
|
||||||
|
*/
|
||||||
|
@Override
|
||||||
|
public List<PromResultInfo> getMetricInfo(String promURL, String promQL, String isRate, String rateMetric) {
|
||||||
|
Map<String, Object> paramMap = new HashMap<>();
|
||||||
|
String httpRes = "";
|
||||||
|
// prometheus查询命令
|
||||||
|
if(!StrUtil.isEmpty(isRate)){
|
||||||
|
paramMap.put(MonitorConstants.MONITOR_PROMETHEUS_QUERY, getRatePromQl(isRate,rateMetric,promQL));
|
||||||
|
} else {
|
||||||
|
paramMap.put(MonitorConstants.MONITOR_PROMETHEUS_QUERY, promQL);
|
||||||
|
}
|
||||||
|
|
||||||
|
// 查询5分钟数据
|
||||||
|
Calendar calendar = Calendar.getInstance();
|
||||||
|
calendar.set(Calendar.MINUTE, calendar.get(Calendar.MINUTE) - 5);
|
||||||
|
|
||||||
|
// 查询开始时间
|
||||||
|
paramMap.put(MonitorConstants.MONITOR_PROMETHEUS_START, (calendar.getTime().getTime()) / 1000L);
|
||||||
|
|
||||||
|
// 查询结束时间
|
||||||
|
paramMap.put(MonitorConstants.MONITOR_PROMETHEUS_END, (new Date().getTime()) / 1000L);
|
||||||
|
|
||||||
|
// 查询步长
|
||||||
|
paramMap.put(MonitorConstants.MONITOR_PROMETHEUS_STEP, 15);
|
||||||
|
// 获取查询结果
|
||||||
|
try {
|
||||||
|
httpRes = HttpUtil.get(promURL, paramMap);
|
||||||
|
} catch (IORuntimeException e) {
|
||||||
|
log.error(MonitorExceptionEnum.PROMETHEUS_CONFIG_ERROR.getUserTip(),"","");
|
||||||
|
//log.error("prometheus配置异常,具体信息为:{}", e.getMessage());
|
||||||
|
// log.error(DictExceptionEnum.DICT_CODE_REPEAT.getUserTip(), "", dictCode);
|
||||||
|
// throw new MonitorException(MonitorExceptionEnum.PROMETHEUS_CONFIG_ERROR);
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
PromResponseInfo responseInfo = JSON.parseObject(httpRes, PromResponseInfo.class);
|
||||||
|
if (ObjectUtil.isEmpty(responseInfo)) {
|
||||||
|
// 监控指标未产生数据
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
if (StrUtil.isEmpty(responseInfo.getStatus()) || !"success".equals(responseInfo.getStatus())) {
|
||||||
|
// prometheus查询失败
|
||||||
|
log.error("prometheus配置异常,具体信息为:{}", responseInfo.getStatus());
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
return responseInfo.getData().getResult();
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 组装prometheus rate promql
|
||||||
|
*
|
||||||
|
* @author chenli
|
||||||
|
* @date 2021/1/10 16:10
|
||||||
|
*/
|
||||||
|
private String getRatePromQl(String isRate,String rateMetric,String promQl){
|
||||||
|
StringBuilder ratePromQlBuilder = new StringBuilder(isRate);
|
||||||
|
ratePromQlBuilder.append("(").append(rateMetric).append(promQl).append(")");
|
||||||
|
return ratePromQlBuilder.toString();
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 配置prometheus无效链接则关闭prometheus相关菜单
|
||||||
|
*
|
||||||
|
* @author chenli
|
||||||
|
* @date 2021/3/3 16:08
|
||||||
|
*/
|
||||||
|
@Override
|
||||||
|
@Transactional(rollbackFor = Exception.class)
|
||||||
|
public void closePrometheusMenu(){ mapper.displayOrClosePrometheusMenu(StatusEnum.DISABLE.getCode());
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 配置prometheus有效链接无效则显示prometheus相关菜单
|
||||||
|
*
|
||||||
|
* @author chenli
|
||||||
|
* @date 2021/3/3 16:08
|
||||||
|
*/
|
||||||
|
@Override
|
||||||
|
@Transactional(rollbackFor = Exception.class)
|
||||||
|
public void displayPrometheusMenu(){
|
||||||
|
mapper.displayOrClosePrometheusMenu(StatusEnum.ENABLE.getCode());
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
|
@ -17,13 +17,20 @@
|
||||||
|
|
||||||
<dependencies>
|
<dependencies>
|
||||||
|
|
||||||
<!--监控模块的业务-->
|
<!--系统监控-->
|
||||||
<dependency>
|
<dependency>
|
||||||
<groupId>cn.stylefeng.roses</groupId>
|
<groupId>cn.stylefeng.roses</groupId>
|
||||||
<artifactId>monitor-sdk-system-info</artifactId>
|
<artifactId>monitor-sdk-system-info</artifactId>
|
||||||
<version>7.0.1</version>
|
<version>7.0.1</version>
|
||||||
</dependency>
|
</dependency>
|
||||||
|
|
||||||
|
<!--prometheus监控-->
|
||||||
|
<dependency>
|
||||||
|
<groupId>cn.stylefeng.roses</groupId>
|
||||||
|
<artifactId>monitor-sdk-prometheus</artifactId>
|
||||||
|
<version>7.0.1</version>
|
||||||
|
</dependency>
|
||||||
|
|
||||||
</dependencies>
|
</dependencies>
|
||||||
|
|
||||||
</project>
|
</project>
|
||||||
|
|
|
@ -19,6 +19,7 @@
|
||||||
<module>monitor-api</module>
|
<module>monitor-api</module>
|
||||||
<module>monitor-sdk-system-info</module>
|
<module>monitor-sdk-system-info</module>
|
||||||
<module>monitor-spring-boot-starter</module>
|
<module>monitor-spring-boot-starter</module>
|
||||||
|
<module>monitor-sdk-prometheus</module>
|
||||||
</modules>
|
</modules>
|
||||||
|
|
||||||
<dependencies>
|
<dependencies>
|
||||||
|
|
Loading…
Reference in New Issue