Files
LokiStack/backend/models/collector.go
T
opsx 41633f0b38 feat(collector): 采集命名空间多选 + app 标签来源可自定义 + app 白名单
=== 一、采集命名空间改为多选(原来只有文本输入)===
- CollectorEditDialog 重写:勾选式多选面板,从
  GET /clusters/:id/namespaces 拉取真实命名空间列表
- "所有命名空间(全集群采集)"一键全选(targetNamespaces 置空即全集群)
- 支持搜索过滤命名空间;已选数量实时显示
- 保存时为空数组 → Promtail 不加 namespaces 白名单

=== 二、app 标签取值来源可配置 ===
新增 Collector.AppLabelSource:
  留空(默认)= app.kubernetes.io/name → app → controller 名(去hash) → pod 名 四级回退
  可填任意 Pod 标签,如 app.kubernetes.io/name / app / k8s.kuboard.cn/name
  也可直接写 __meta_kubernetes_pod_label_xxx
  → 作为最高优先级插到回退链最前,其余仍兜底

模板侧新增:
  sanitizePromLabel()      标签名 → Prometheus meta label(. / 等转 _)
  promtailAppSourceLabel() 解析用户配置
  promtailAppIncludeFilter() 生成白名单 keep 规则

=== 三、app 白名单(控制采集量)===
新增 Collector.AppInclude []string:
  填了之后生成
    - source_labels: [app]
      regex: ^(app1|app2)$
      action: keep
  只保留匹配的日志流 —— 这是收敛日志量最直接的手段

service.Update 同步支持 appLabelSource / appInclude(兼容 []string 与逗号分隔字符串)。

前端 CollectorEditDialog:
  采集命名空间 = 勾选多选 + 全选
  app 标签取值来源 = 输入框 + 5 个常用值 datalist 提示
  只采集这些 app = 逗号分隔输入

镜像:lghost/lokistack:latest + v0.1 = sha256:43369d06cb28
2026-10-27 03:55:13 +00:00

77 lines
3.4 KiB
Go
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
package models
import "time"
// Collector 日志采集器(部署客户端),关联一个已部署的 Loki 作为推送目标
type Collector struct {
ID uint `json:"id"`
Name string `json:"name"`
Type string `json:"type"` // host-promtail | k8s-daemonset | k8s-fluentbit-sidecar
LokiDeploymentID uint `json:"lokiDeploymentID"`
LokiDeployment *LokiDeployment `json:"lokiDeployment,omitempty"`
RefType string `json:"refType"` // host | cluster
HostID *uint `json:"hostID,omitempty"`
Host *Host `json:"host,omitempty"`
ClusterID *uint `json:"clusterID,omitempty"`
Cluster *Cluster `json:"cluster,omitempty"`
TargetDeployment string `json:"targetDeployment"` // sidecar 专用:目标 deployment 名
Namespace string `json:"namespace"` // 采集器自身部署所在的 k8s 命名空间
// AppLabelSource 决定 app 标签从哪里取值(可填私有标签,便于按自己的规范匹配)。
// 留空 = 默认:app.kubernetes.io/name → app → Pod 的 controller_name(去 hash) → pod 名
// 可填:controller_name / app.kubernetes.io/name / app / k8s.kuboard.cn/name 等任意 Pod 标签
AppLabelSource string `json:"appLabelSource"`
// AppInclude 只采集这些 app 的日志(白名单,按 AppLabelSource 解析出的 app 值匹配)。
// 留空 = 不限制。配合 TargetNamespaces 可显著收敛日志量。
AppInclude []string `json:"appInclude"`
// TargetNamespaces 只采集这些命名空间的 Pod 日志;为空表示采集全集群。
// 对应 promtail 里 kubernetes_sd_configs.namespaces.names
TargetNamespaces []string `json:"targetNamespaces"`
LogPath string `json:"logPath"` // 采集日志路径
Labels string `json:"labels"` // 额外标签(JSON)
Status string `json:"status"` // pending | active | error
StatusMessage string `json:"statusMessage"`
Config string `json:"config"` // 生成的配置快照
CreatedAt time.Time `json:"createdAt"`
UpdatedAt time.Time `json:"updatedAt"`
}
func (c *Collector) Sanitized() map[string]interface{} {
var loki map[string]interface{}
if c.LokiDeployment != nil {
loki = map[string]interface{}{
"id": c.LokiDeployment.ID,
"name": c.LokiDeployment.Name,
"endpoint": c.LokiDeployment.Endpoint,
"status": c.LokiDeployment.Status,
}
}
var host map[string]interface{}
if c.Host != nil {
host = c.Host.Sanitized()
}
var cluster map[string]interface{}
if c.Cluster != nil {
cluster = c.Cluster.Sanitized()
}
return map[string]interface{}{
"id": c.ID,
"name": c.Name,
"type": c.Type,
"lokiDeploymentID": c.LokiDeploymentID,
"lokiDeployment": loki,
"refType": c.RefType,
"hostID": c.HostID,
"host": host,
"clusterID": c.ClusterID,
"cluster": cluster,
"targetDeployment": c.TargetDeployment,
"namespace": c.Namespace,
"logPath": c.LogPath,
"labels": c.Labels,
"status": c.Status,
"statusMessage": c.StatusMessage,
"createdAt": c.CreatedAt,
"updatedAt": c.UpdatedAt,
}
}