解决内存占用异常高的问题
This commit is contained in:
@@ -5,6 +5,7 @@ import (
|
||||
"fmt"
|
||||
"log"
|
||||
"math/rand"
|
||||
"strconv"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
@@ -251,8 +252,8 @@ func (s *Storage) WriteLogMetric(ctx context.Context, deviceID string, sequence
|
||||
return s.writeData(ctx, "logs", allTags, fields, deviceID, "log")
|
||||
}
|
||||
|
||||
// QueryMetrics 查询监控指标
|
||||
func (s *Storage) QueryMetrics(ctx context.Context, deviceID, metricType, startTime, endTime string) ([]MetricPoint, error) {
|
||||
// QueryMetrics 查询监控指标,支持采样
|
||||
func (s *Storage) QueryMetrics(ctx context.Context, deviceID, metricType, startTime, endTime string, limit ...int) ([]MetricPoint, error) {
|
||||
queryAPI := s.client.QueryAPI(s.org)
|
||||
|
||||
// 处理时间参数
|
||||
@@ -324,6 +325,15 @@ func (s *Storage) QueryMetrics(ctx context.Context, deviceID, metricType, startT
|
||||
|> filter(fn: (r) => r["type"] == "` + metricType + `")
|
||||
|> sort(columns: ["_time"])`
|
||||
|
||||
// 设置默认限制
|
||||
maxLimit := 10000
|
||||
if len(limit) > 0 && limit[0] > 0 {
|
||||
maxLimit = limit[0]
|
||||
}
|
||||
|
||||
query += `
|
||||
|> limit(n: ` + strconv.Itoa(maxLimit) + `)` // 限制返回的数据点数量,防止内存溢出
|
||||
|
||||
// 执行查询
|
||||
result, err := queryAPI.Query(ctx, query)
|
||||
if err != nil {
|
||||
@@ -591,11 +601,11 @@ func (s *Storage) QueryLogMetrics(ctx context.Context, deviceID string, startTim
|
||||
|
||||
// 构建日志数据
|
||||
logData := map[string]interface{}{
|
||||
"time": record.Time(),
|
||||
"device_id": record.ValueByKey("device_id"),
|
||||
"source": record.ValueByKey("source"),
|
||||
"sequence": record.ValueByKey("sequence"),
|
||||
"message": record.ValueByKey("message"),
|
||||
"time": record.Time(),
|
||||
"device_id": record.ValueByKey("device_id"),
|
||||
"source": record.ValueByKey("source"),
|
||||
"sequence": record.ValueByKey("sequence"),
|
||||
"message": record.ValueByKey("message"),
|
||||
"agent_name": record.ValueByKey("agent_name"),
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user