=== 一、问题 ===
查看日志上下文时,前后各取几条是**写死**在控制器里的:
span := 5 * time.Minute
QueryLokiLogs(..., ts-span, ts-1, 5, "backward") // 5 条
QueryLokiLogs(..., ts+1, ts+span, 5, "forward") // 5 条
三处写死:条数 5、时间窗 5 分钟、且用户无法调整。
写死的三个实际弊端:
1. 排障时想看更多(比如前后 20 条找调用链)做不到;
2. 只想看紧邻 2 条时,仍然要扫 5 分钟窗口、返回 5 条,浪费;
3. 时间窗与条数不匹配——某服务日志稀疏时,5 分钟窗口里可能一条都没有,
于是"前后各 5 条"实际返回 0 条。
=== 二、改动 ===
1. backend/dto: LogContextRequest 新增 before/after(可选)
2. backend/controllers: normalizeContextLines()
- 不传或 ≤0 → 默认 2 条
- 上限 50 条(防止一次弹窗拉几百条)
- 时间窗改为**按条数自适应**:每条按 30s 估算,最少 1 分钟、最多 2 小时
(不再固定 5 分钟,解决"稀疏服务取不到上下文"的问题)
3. frontend
- 上下文弹窗标题栏加「前后各 [2/5/10/20/50]」选择器,切换后原地重新拉取
- 选择结果写入 localStorage,下次沿用
- 每页条数:数字输入框 → 预设下拉 [100/500/1000/2000/5000]
(原来只能手输且硬顶 1000,现在上限提到 Loki 默认的 5000)
- i18n:去掉"前后各最多 5 条 · 前后 5 分钟"的写死文案
=== 三、实测(平台 API)===
before=2 after=2 → 2 / 2 条
before=5 after=5 → 5 / 5 条
before=10 after=10 → 10 / 10 条
before=50 after=50 → 50 / 50 条
before=999 after=999 → 50 / 50 条(超上限自动收敛)
不传 → 2 / 2 条(默认值生效)
镜像:lghost/lokistack:latest + v0.1 = sha256:d19239fa65f3
53 lines
2.2 KiB
Go
53 lines
2.2 KiB
Go
package dto
|
||
|
||
import "lokistack/models"
|
||
|
||
// 日志查询域
|
||
type LogSourceRequest struct {
|
||
ID uint `json:"id"`
|
||
Name string `json:"name"`
|
||
TargetType string `json:"targetType" binding:"omitempty,oneof=cluster-svc url"`
|
||
ClusterID uint `json:"clusterID"`
|
||
Namespace string `json:"namespace"`
|
||
Service string `json:"service"`
|
||
Port int `json:"port"`
|
||
URL string `json:"url"`
|
||
AuthType string `json:"authType"`
|
||
BasicUser string `json:"basicUser"`
|
||
BasicPass string `json:"basicPass"`
|
||
BearerToken string `json:"bearerToken"`
|
||
Headers map[string]string `json:"headers"`
|
||
SkipTLSVerify bool `json:"skipTLSVerify"`
|
||
IsDefault bool `json:"isDefault"`
|
||
Access []models.LogSourceAccess `json:"access"`
|
||
}
|
||
|
||
type LogQueryRequest struct {
|
||
SourceID uint `json:"sourceId" binding:"required"`
|
||
Query string `json:"query" binding:"required"`
|
||
Start int64 `json:"start"` // epoch 纳秒
|
||
End int64 `json:"end"`
|
||
Limit int `json:"limit"`
|
||
Direction string `json:"direction"`
|
||
Cursor string `json:"cursor"`
|
||
}
|
||
|
||
// LogMetricQueryRequest 日志分析指标查询:Query 为完整 LogQL 指标表达式,
|
||
// 其中的 [$step] 占位符由服务端按时间跨度替换为自适应步长(与直方图同款)
|
||
type LogMetricQueryRequest struct {
|
||
SourceID uint `json:"sourceId" binding:"required"`
|
||
Query string `json:"query" binding:"required"`
|
||
Start int64 `json:"start"` // epoch 纳秒
|
||
End int64 `json:"end"`
|
||
}
|
||
|
||
type LogContextRequest struct {
|
||
SourceID uint `json:"sourceId" binding:"required"`
|
||
TimestampNs string `json:"timestampNs" binding:"required"`
|
||
Labels map[string]string `json:"labels" binding:"required"`
|
||
// 上下文前后各取几条(可选)。不传按 DefaultContextLines,
|
||
// 上限 MaxContextLines —— 原来前后各 5 条是写死的,不能调。
|
||
Before int `json:"before"`
|
||
After int `json:"after"`
|
||
}
|