-
Notifications
You must be signed in to change notification settings - Fork 81
/
model_promql_template.go
124 lines (107 loc) · 4.16 KB
/
model_promql_template.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
// Copyright 2022 The kubegems.io Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package models
import (
"fmt"
"time"
"github.com/prometheus/prometheus/pkg/labels"
"kubegems.io/kubegems/pkg/utils/gormdatatypes"
"kubegems.io/kubegems/pkg/utils/prometheus"
"kubegems.io/kubegems/pkg/utils/prometheus/promql"
"kubegems.io/kubegems/pkg/utils/prometheus/templates"
"kubegems.io/kubegems/pkg/utils/set"
)
// PromqlTplScope
type PromqlTplScope struct {
ID uint `gorm:"primarykey" json:"id"`
Name string `gorm:"type:varchar(50);uniqueIndex" binding:"required" json:"name"`
ShowName string `gorm:"type:varchar(50)" json:"showName"`
Namespaced bool `json:"namespaced"`
Resources []*PromqlTplResource `json:"resources" gorm:"foreignKey:ScopeID"`
CreatedAt *time.Time `json:"-"`
UpdatedAt *time.Time `json:"-"`
}
// PromqlTplScope
type PromqlTplResource struct {
ID uint `gorm:"primarykey" json:"id"`
Name string `gorm:"type:varchar(50);uniqueIndex" binding:"required" json:"name"`
ShowName string `gorm:"type:varchar(50)" json:"showName"`
ScopeID *uint `json:"scopeID"`
Scope *PromqlTplScope `gorm:"foreignKey:ScopeID;constraint:OnUpdate:CASCADE,OnDelete:CASCADE;" json:"scope,omitempty"`
Rules []*PromqlTplRule `json:"rules" gorm:"foreignKey:ResourceID"`
CreatedAt *time.Time `json:"-"`
UpdatedAt *time.Time `json:"-"`
}
// PromqlTplScope
type PromqlTplRule struct {
ID uint `gorm:"primarykey" json:"id"`
Name string `gorm:"type:varchar(50)" binding:"required" json:"name"`
ShowName string `gorm:"type:varchar(50)" json:"showName"`
Description string `json:"description"`
Expr string `json:"expr"` // promql expr
Unit string `gorm:"type:varchar(50)" json:"unit"`
Labels gormdatatypes.JSONSlice `json:"labels"`
ResourceID *uint `json:"resourceID"`
Resource *PromqlTplResource `gorm:"foreignKey:ResourceID;constraint:OnUpdate:CASCADE,OnDelete:CASCADE;" json:"resource,omitempty"`
TenantID *uint `json:"tenantID"` // 若为null,则表示系统预置
Tenant *Tenant `gorm:"constraint:OnUpdate:RESTRICT,OnDelete:CASCADE;" json:"tenant,omitempty"`
CreatedAt *time.Time `json:"-"`
UpdatedAt *time.Time `json:"-"`
}
func CheckGraphs(graphs []prometheus.MetricGraph, namespace string, tplGetter templates.TplGetter) error {
// 逐个校验graph
for i, graph := range graphs {
if graph.Name == "" {
return fmt.Errorf("图表名不能为空")
}
if graph.Unit != "" {
if _, err := prometheus.ParseUnit(graph.Unit); err != nil {
return err
}
}
for j, target := range graph.Targets {
targetSet := set.NewSet[string]()
if targetSet.Has(target.TargetName) {
return fmt.Errorf("duplicated target name: %s", target.TargetName)
}
targetSet.Append(target.TargetName)
if target.PromqlGenerator.Notpl() {
if target.Expr == "" {
return fmt.Errorf("模板与原生promql不能同时为空")
}
query, err := promql.New(target.Expr)
if err != nil {
return err
}
if namespace != "" {
// 强制添加namespace selector
graphs[i].Targets[j].Expr = query.AddLabelMatchers(&labels.Matcher{
Type: labels.MatchEqual,
Name: "namespace",
Value: namespace,
}).String()
}
} else {
if err := target.PromqlGenerator.SetTpl(tplGetter); err != nil {
return err
}
if !target.PromqlGenerator.Tpl.Namespaced {
return fmt.Errorf("图表: %s-%s 错误!不能查询集群范围资源", graph.Name, target.TargetName)
}
graphs[i].Unit = target.PromqlGenerator.Unit
}
}
}
return nil
}