feat(logs): metrics 指标代理端点(admin) + 快捷查询选中 App/Pod 即插入并执行

- POST /logs/metrics:LogQL 指标查询代理,[$step] 占位符按跨度自适应步长,返回多序列(httptest 假 Loki 单测)
- 快捷查询:选中 App/Pod 即写入输入框并立即查询,面板保持打开支持 Pod 级联;命名空间仅级联筛选不插入,切换时清理失效标签
- 删除冗余的「查询该范围日志」「指定副本日志」按钮;Ingress 按钮仅在选择 ingress 命名空间/app 时显示
- /logs 支持 ?q= 预填自动执行(表格「查看日志」跳转入口)
- 清理无引用死组件 LogResults/LogHistogram
This commit is contained in:
opsx committed 2026-10-28 22:43:57 +08:00
1 parent 5ce90fe2e5
commit 16306a9532
10 files changed
+264 -295

No files matched your search

+66 -1
View File
@@ -17,9 +17,9 @@ import (
"strings"
"time"
"k8s.io/client-go/rest"
"lokistack/models"
"lokistack/repository"
"k8s.io/client-go/rest"
)
const maxLogQueryLimit = 1000
@@ -449,6 +449,71 @@ func LokiHistogram(ctx context.Context, src *models.LogSource, query string, sta
return buckets, nil
}
// MetricSeriesPoint 指标序列上的一个采样点
type MetricSeriesPoint struct {
Timestamp string `json:"timestamp"`
Value float64 `json:"value"`
}
// MetricSeries 一条按标签分组的指标序列(如 sum by (app)(...) 的每个 app 一条)
type MetricSeries struct {
Labels map[string]string `json:"labels"`
Points []MetricSeriesPoint `json:"points"`
}
// LokiMetrics 通用 LogQL 指标查询(日志分析页用):query 为完整指标表达式,
// 其中所有 [$step] 占位符替换为按时间跨度自适应的步长后走 query_range,
// 返回矩阵多序列(直方图只取单序列,这里按 by (...) 分组整表返回)
func LokiMetrics(ctx context.Context, src *models.LogSource, query string, start, end int64) ([]MetricSeries, error) {
if end <= 0 {
end = time.Now().UnixNano()
}
if start <= 0 {
start = end - int64(time.Hour)
}
step := pickHistogramStep(end - start)
wrapped := strings.ReplaceAll(query, "[$step]", "["+step+"]")
q := url.Values{}
q.Set("query", wrapped)
q.Set("start", strconv.FormatInt(start, 10))
q.Set("end", strconv.FormatInt(end, 10))
q.Set("step", step)
body, _, err := lokiProxyGET(ctx, src, "/loki/api/v1/query_range", q)
if err != nil {
return nil, err
}
var resp struct {
Data struct {
Result []struct {
Metric map[string]string `json:"metric"`
Values [][2]interface{} `json:"values"`
} `json:"result"`
} `json:"data"`
}
if err := json.Unmarshal(body, &resp); err != nil {
return nil, err
}
series := make([]MetricSeries, 0, len(resp.Data.Result))
for _, item := range resp.Data.Result {
points := make([]MetricSeriesPoint, 0, len(item.Values))
for _, v := range item.Values {
// metric API 的 ts 为秒级浮点数(如 1.79e+09)
tsf, _ := strconv.ParseFloat(fmt.Sprintf("%v", v[0]), 64)
cnt, _ := strconv.ParseFloat(fmt.Sprintf("%v", v[1]), 64)
points = append(points, MetricSeriesPoint{
Timestamp: time.Unix(int64(tsf), 0).Format(time.RFC3339),
Value: cnt,
})
}
if len(points) == 0 {
continue
}
series = append(series, MetricSeries{Labels: item.Metric, Points: points})
}
return series, nil
}
// pickHistogramStep 按时间跨度自适应步长(aiops 同款)
func pickHistogramStep(spanNano int64) string {
span := time.Duration(spanNano)