impl: 添加初步的监测数据查询实现

ref: 移动部分数据类
lensfrex 7 months ago
parent 5070304883
commit eee3a9523d
Signed by: lensfrex
GPG Key ID: B1E395B3C6CA0356
  1. 5
      rition-center/api/pom.xml
  2. 2
      rition-center/api/src/main/java/rition/backend/api/v1/collector/DataCollectingController.java
  3. 34
      rition-center/api/src/main/java/rition/backend/api/v1/panel/MetricsViewController.java
  4. 4
      rition-center/api/src/main/java/rition/backend/service/MetricDataCollectingService.java
  5. 2
      rition-center/api/src/main/resources/application.yml
  6. 10
      rition-center/common/src/main/java/rition/common/data/dao/mapper/MetricRecordMapper.java
  7. 2
      rition-center/common/src/main/java/rition/common/data/dto/service/MetricDataDto.java
  8. 2
      rition-center/common/src/main/java/rition/common/data/entity/RuleEntity.java
  9. 35
      rition-center/common/src/main/resources/mapper/MetricRecordMapper.xml
  10. 2
      rition-center/service/collector/src/main/java/rition/service/collector/MetricCollectingService.java
  11. 2
      rition-center/service/monitor/src/main/java/rition/service/monitor/MonitorService.java
  12. 2
      rition-center/service/monitor/src/main/resources/application-monitor.yml
  13. 73
      rition-center/service/panel/src/main/java/rition/service/panel/MetricService.java
  14. 12
      rition-center/service/panel/src/main/resources/application-panel.yml

@ -43,6 +43,11 @@
<artifactId>notify</artifactId> <artifactId>notify</artifactId>
<version>${revision}</version> <version>${revision}</version>
</dependency> </dependency>
<dependency>
<groupId>net.rition</groupId>
<artifactId>panel</artifactId>
<version>${revision}</version>
</dependency>
<dependency> <dependency>
<groupId>org.springframework.boot</groupId> <groupId>org.springframework.boot</groupId>

@ -9,7 +9,7 @@ import rition.backend.annotation.paramter.RequestId;
import rition.backend.api.v1.dto.request.MetricDataUploadRequest; import rition.backend.api.v1.dto.request.MetricDataUploadRequest;
import rition.backend.api.v1.dto.response.Response; import rition.backend.api.v1.dto.response.Response;
import rition.backend.service.MetricDataCollectingService; import rition.backend.service.MetricDataCollectingService;
import rition.common.data.dto.MetricDataDto; import rition.common.data.dto.service.MetricDataDto;
import java.util.HashMap; import java.util.HashMap;
import java.util.List; import java.util.List;

@ -5,14 +5,44 @@ import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController; import org.springframework.web.bind.annotation.RestController;
import rition.backend.api.v1.dto.request.MetricDataRequest; import rition.backend.api.v1.dto.request.MetricDataRequest;
import rition.backend.api.v1.dto.response.MetricDataResponse;
import rition.backend.api.v1.dto.response.Response; import rition.backend.api.v1.dto.response.Response;
import rition.common.data.entity.MetricRecordEntity;
import rition.service.panel.MetricService;
import java.time.Instant;
import java.util.ArrayList;
import java.util.List;
@RestController @RestController
@RequestMapping("/panel/metrics") @RequestMapping("/panel/metrics")
public class MetricsViewController { public class MetricsViewController {
private final MetricService metricService;
public MetricsViewController(MetricService metricService) {
this.metricService = metricService;
}
@GetMapping("/list") @GetMapping("/list")
public Response<Object> getMetrics(@RequestBody MetricDataRequest metricDataRequest) { public Response<List<MetricDataResponse>> getMetrics(@RequestBody MetricDataRequest metricDataRequest) {
return Response.success(); var entityResult = metricService.getMetricDataRange(
metricDataRequest.getInstanceId(),
metricDataRequest.getMetricItems(),
Instant.ofEpochMilli(metricDataRequest.getStart()),
Instant.ofEpochMilli(metricDataRequest.getEnd())
);
// 转换数据
List<MetricDataResponse> responseList = new ArrayList<>(entityResult.size());
for (MetricRecordEntity entity : entityResult) {
responseList.add(MetricDataResponse.builder()
.metricData(entity.getMetricData())
.time(entity.getTime())
.build()
);
}
return Response.success(responseList);
} }
} }

@ -4,12 +4,10 @@ import lombok.extern.slf4j.Slf4j;
import org.springframework.data.redis.core.RedisTemplate; import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.scheduling.annotation.Async; import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
import rition.common.data.dto.MetricDataDto;
import rition.common.data.dto.log.RequestProcessTraceRecord; import rition.common.data.dto.log.RequestProcessTraceRecord;
import rition.common.data.dto.service.MetricDataDto;
import rition.service.collector.MetricCollectingService; import rition.service.collector.MetricCollectingService;
import java.time.Duration;
import java.time.temporal.ChronoUnit;
import java.util.Map; import java.util.Map;
/** /**

@ -5,7 +5,7 @@ server:
spring: spring:
profiles: profiles:
include: collector, monitor include: collector, monitor, panel
data: data:
redis: redis:
host: ${REDIS_HOST:127.0.0.1} host: ${REDIS_HOST:127.0.0.1}

@ -2,11 +2,21 @@ package rition.common.data.dao.mapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper; import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import org.apache.ibatis.annotations.Mapper; import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.springframework.stereotype.Component;
import rition.common.data.entity.MetricRecordEntity; import rition.common.data.entity.MetricRecordEntity;
import java.time.Instant;
import java.util.List; import java.util.List;
@Mapper @Mapper
@Component
public interface MetricRecordMapper extends BaseMapper<MetricRecordEntity> { public interface MetricRecordMapper extends BaseMapper<MetricRecordEntity> {
int insertBatchSomeColumn(List<MetricRecordEntity> entityList); int insertBatchSomeColumn(List<MetricRecordEntity> entityList);
List<MetricRecordEntity> getMetricDataGroupByHour(
@Param("instanceId") String instanceId,
@Param("metricItems") List<String> metricItems,
@Param("startTime") Instant startTime, @Param("endTime") Instant endTime
);
} }

@ -1,5 +1,5 @@
package rition.common.data.dto; package rition.common.data.dto.service;
import lombok.AllArgsConstructor; import lombok.AllArgsConstructor;
import lombok.Builder; import lombok.Builder;

@ -40,7 +40,7 @@ public class RuleEntity {
/** /**
* 阈值 * 阈值
*/ */
private String threshold; private Double threshold;
/** /**
* 触发方法实时计算或定时计算 * 触发方法实时计算或定时计算

@ -1,5 +1,40 @@
<?xml version="1.0" encoding="UTF-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" > <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd" >
<mapper namespace="rition.common.data.dao.mapper.MetricRecordMapper"> <mapper namespace="rition.common.data.dao.mapper.MetricRecordMapper">
<resultMap id="metricDataEntity" type="rition.common.data.entity.MetricRecordEntity">
<result property="metricData" column="metric_data" typeHandler="com.baomidou.mybatisplus.extension.handlers.JacksonTypeHandler"/>
</resultMap>
<select id="getMetricDataGroupByHour" resultMap="metricDataEntity" resultType="rition.common.data.entity.MetricRecordEntity">
SELECT JSON_OBJECT(
<foreach collection="metricItems" item="metricItem" separator=",">
"${metricItem}", ${metricItem}
</foreach>
) AS metric_data,
t AS time
FROM (
SELECT
<foreach collection="metricItems" item="metricItem">
avg(JSON_EXTRACT(metric_data, "$.${metricItem}")) as ${metricItem},
</foreach>
DATE_FORMAT(time,'%Y-%m-%d %H:00:00') as t
FROM record WHERE `time` BETWEEN #{startTime} and #{endTime} and `instance_id` = #{instanceId}
GROUP BY t ORDER BY t
) AS tab;
<!--
SELECT
JSON_OBJECT('node_sockstat_TCP_tw', node_sockstat_TCP_tw),
t AS time
FROM (
SELECT
avg(JSON_EXTRACT(metric_data, "$.node_sockstat_TCP_tw")) node_sockstat_TCP_tw,
DATE_FORMAT(time,'%Y-%m-%d %H:00:00') as t
FROM record
WHERE `time` BETWEEN '2024-04-17 14:00:00' and '2024-04-17 15:00:00'
GROUP BY t ORDER BY t
) AS tab
-->
</select>
</mapper> </mapper>

@ -4,7 +4,7 @@ import com.github.yitter.idgen.YitIdHelper;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;
import org.springframework.kafka.core.KafkaTemplate; import org.springframework.kafka.core.KafkaTemplate;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
import rition.common.data.dto.MetricDataDto; import rition.common.data.dto.service.MetricDataDto;
import rition.common.data.entity.MetricRecordEntity; import rition.common.data.entity.MetricRecordEntity;
import rition.service.collector.configure.CollectorServiceKafkaConfigure; import rition.service.collector.configure.CollectorServiceKafkaConfigure;
import rition.common.data.dao.mapper.MetricRecordMapper; import rition.common.data.dao.mapper.MetricRecordMapper;

@ -3,7 +3,7 @@ package rition.service.monitor;
import org.apache.kafka.clients.consumer.ConsumerRecord; import org.apache.kafka.clients.consumer.ConsumerRecord;
import org.springframework.kafka.annotation.KafkaListener; import org.springframework.kafka.annotation.KafkaListener;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
import rition.common.data.dto.MetricDataDto; import rition.common.data.dto.service.MetricDataDto;
@Service @Service
public class MonitorService { public class MonitorService {

@ -13,7 +13,7 @@ spring:
spring: spring:
json: json:
trusted: trusted:
packages: "rition.common.data.dto" packages: "rition.common.data.*"
producer: producer:
retries: 4 retries: 4
compression-type: zstd compression-type: zstd

@ -0,0 +1,73 @@
package rition.service.panel;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import rition.common.data.dao.mapper.MetricRecordMapper;
import rition.common.data.entity.MetricRecordEntity;
import java.time.Instant;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
@Slf4j
@Service
public class MetricService {
private final MetricRecordMapper metricRecordMapper;
public MetricService(MetricRecordMapper metricRecordMapper) {
this.metricRecordMapper = metricRecordMapper;
}
public List<MetricRecordEntity> getMetricDataRange(String instanceId,
List<String> metricItems,
Instant start, Instant end) {
var filteredMetricItems = this.filterMetricItems(metricItems);
if (filteredMetricItems.isEmpty()) {
return new ArrayList<>();
}
return metricRecordMapper.getMetricDataGroupByHour(instanceId, metricItems, start, end);
}
private static final HashSet<String> allowedMetricItems = new HashSet<>(22);
static {
allowedMetricItems.add("up");
allowedMetricItems.add("node_load5");
allowedMetricItems.add("node_sockstat_TCP_tw");
allowedMetricItems.add("node_cpu_seconds_total");
allowedMetricItems.add("node_memory_Cached_bytes");
allowedMetricItems.add("node_memory_Buffers_bytes");
allowedMetricItems.add("node_memory_MemFree_bytes");
allowedMetricItems.add("node_disk_read_bytes_total");
allowedMetricItems.add("node_filesystem_free_bytes");
allowedMetricItems.add("node_filesystem_size_bytes");
allowedMetricItems.add("node_memory_MemTotal_bytes");
allowedMetricItems.add("node_netstat_Tcp_CurrEstab");
allowedMetricItems.add("node_filesystem_avail_bytes");
allowedMetricItems.add("node_disk_written_bytes_total");
allowedMetricItems.add("node_disk_reads_completed_total");
allowedMetricItems.add("node_network_receive_drop_total");
allowedMetricItems.add("node_disk_writes_completed_total");
allowedMetricItems.add("node_network_receive_bytes_total");
allowedMetricItems.add("node_network_transmit_drop_total");
allowedMetricItems.add("node_network_transmit_bytes_total");
allowedMetricItems.add("node_network_receive_packets_total");
allowedMetricItems.add("node_network_transmit_packets_total");
}
public List<String> filterMetricItems(List<String> metricItems) {
List<String> filteredMetricItems = new ArrayList<>(metricItems.size());
for (String metricItem : metricItems) {
if (allowedMetricItems.contains(metricItem)) {
filteredMetricItems.add(metricItem);
} else {
log.warn("[MetricService]: 含有非允许的指标值:{}", metricItem);
}
}
return filteredMetricItems;
}
}

@ -0,0 +1,12 @@
spring:
datasource:
driver-class-name: com.mysql.cj.jdbc.Driver
url: jdbc:mysql://127.0.0.1:3306/rition?useUnicode=true&characterEncoding=utf-8&useSSL=true&serverTimezone=Asia/Shanghai&rewriteBatchedStatements=true
username: root
password: Test2333!
mybatis:
configuration:
log-impl: org.apache.ibatis.logging.stdout.StdOutImpl
mybatis-plus:
configuration:
log-impl: org.apache.ibatis.logging.stdout.StdOutImpl
Loading…
Cancel
Save