新增prometheus监控

pull/3/head
陈立 2021-03-05 17:51:18 +08:00
parent 0c595a6cb0
commit c1ade42b55
15 changed files with 438 additions and 4 deletions

View File

@ -34,6 +34,12 @@
<artifactId>commons-logging</artifactId>
</dependency>
<!-- https://mvnrepository.com/artifact/javax.activation/activation -->
<dependency>
<groupId>javax.activation</groupId>
<artifactId>activation</artifactId>
<version>1.1.1</version>
</dependency>
</dependencies>
</project>

View File

@ -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);
/**
* prometheusprometheus
*
* @author chenli
* @date 2021/3/3 16:08
*/
void closePrometheusMenu();
/**
* prometheusprometheus
*
* @author chenli
* @date 2021/3/3 16:08
*/
void displayPrometheusMenu();
}

View File

@ -18,4 +18,28 @@ public interface MonitorConstants {
*/
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";
}

View File

@ -1,6 +1,5 @@
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.exception.AbstractExceptionEnum;
import lombok.Getter;
@ -15,9 +14,10 @@ import lombok.Getter;
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配置异常具体信息为{}");
/**
*

View File

@ -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;
}

View File

@ -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 {
/**
* prometheusid
*/
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;
}

View File

@ -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;
}

View File

@ -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;
}

View File

@ -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>

View File

@ -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 12
* @author chenli
* @date 2021/3/3 16:12
*/
void displayOrClosePrometheusMenu(int statusFlag);
}

View File

@ -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>

View File

@ -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 {
}

View File

@ -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();
}
/**
* prometheusprometheus
*
* @author chenli
* @date 2021/3/3 16:08
*/
@Override
@Transactional(rollbackFor = Exception.class)
public void closePrometheusMenu(){ mapper.displayOrClosePrometheusMenu(StatusEnum.DISABLE.getCode());
}
/**
* prometheusprometheus
*
* @author chenli
* @date 2021/3/3 16:08
*/
@Override
@Transactional(rollbackFor = Exception.class)
public void displayPrometheusMenu(){
mapper.displayOrClosePrometheusMenu(StatusEnum.ENABLE.getCode());
}
}

View File

@ -17,13 +17,20 @@
<dependencies>
<!--监控模块的业务-->
<!--系统监控-->
<dependency>
<groupId>cn.stylefeng.roses</groupId>
<artifactId>monitor-sdk-system-info</artifactId>
<version>7.0.1</version>
</dependency>
<!--prometheus监控-->
<dependency>
<groupId>cn.stylefeng.roses</groupId>
<artifactId>monitor-sdk-prometheus</artifactId>
<version>7.0.1</version>
</dependency>
</dependencies>
</project>

View File

@ -19,6 +19,7 @@
<module>monitor-api</module>
<module>monitor-sdk-system-info</module>
<module>monitor-spring-boot-starter</module>
<module>monitor-sdk-prometheus</module>
</modules>
<dependencies>