forked from pingcap/tidb
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmetrics_schema.go
161 lines (147 loc) · 4.7 KB
/
metrics_schema.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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
// Copyright 2019 PingCAP, Inc.
//
// 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 infoschema
import (
"bytes"
"fmt"
"sort"
"strconv"
"strings"
"github.com/pingcap/errors"
"github.com/pingcap/parser/model"
"github.com/pingcap/parser/mysql"
"github.com/pingcap/tidb/meta/autoid"
"github.com/pingcap/tidb/sessionctx"
"github.com/pingcap/tidb/table"
"github.com/pingcap/tidb/util"
"github.com/pingcap/tidb/util/set"
)
const (
promQLQuantileKey = "$QUANTILE"
promQLLabelConditionKey = "$LABEL_CONDITIONS"
promQRangeDurationKey = "$RANGE_DURATION"
)
func init() {
// Initialize the metric schema database and register the driver to `drivers`.
dbID := autoid.MetricSchemaDBID
tableID := dbID + 1
metricTables := make([]*model.TableInfo, 0, len(MetricTableMap))
for name, def := range MetricTableMap {
cols := def.genColumnInfos()
tableInfo := buildTableMeta(name, cols)
tableInfo.ID = tableID
tableInfo.Comment = def.Comment
tableID++
metricTables = append(metricTables, tableInfo)
}
dbInfo := &model.DBInfo{
ID: dbID,
Name: model.NewCIStr(util.MetricSchemaName.O),
Charset: mysql.DefaultCharset,
Collate: mysql.DefaultCollationName,
Tables: metricTables,
}
RegisterVirtualTable(dbInfo, tableFromMeta)
}
// MetricTableDef is the metric table define.
type MetricTableDef struct {
PromQL string
Labels []string
Quantile float64
Comment string
}
// IsMetricTable uses to checks whether the table is a metric table.
func IsMetricTable(lowerTableName string) bool {
_, ok := MetricTableMap[lowerTableName]
return ok
}
// GetMetricTableDef gets the metric table define.
func GetMetricTableDef(lowerTableName string) (*MetricTableDef, error) {
def, ok := MetricTableMap[lowerTableName]
if !ok {
return nil, errors.Errorf("can not find metric table: %v", lowerTableName)
}
return &def, nil
}
func (def *MetricTableDef) genColumnInfos() []columnInfo {
cols := []columnInfo{
{name: "time", tp: mysql.TypeDatetime, size: 19, deflt: "CURRENT_TIMESTAMP"},
}
for _, label := range def.Labels {
cols = append(cols, columnInfo{name: label, tp: mysql.TypeVarchar, size: 512})
}
if def.Quantile > 0 {
defaultValue := strconv.FormatFloat(def.Quantile, 'f', -1, 64)
cols = append(cols, columnInfo{name: "quantile", tp: mysql.TypeDouble, size: 22, deflt: defaultValue})
}
cols = append(cols, columnInfo{name: "value", tp: mysql.TypeDouble, size: 22})
return cols
}
// GenPromQL generates the promQL.
func (def *MetricTableDef) GenPromQL(sctx sessionctx.Context, labels map[string]set.StringSet, quantile float64) string {
promQL := def.PromQL
promQL = strings.Replace(promQL, promQLQuantileKey, strconv.FormatFloat(quantile, 'f', -1, 64), -1)
promQL = strings.Replace(promQL, promQLLabelConditionKey, def.genLabelCondition(labels), -1)
promQL = strings.Replace(promQL, promQRangeDurationKey, strconv.FormatInt(sctx.GetSessionVars().MetricSchemaRangeDuration, 10)+"s", -1)
return promQL
}
func (def *MetricTableDef) genLabelCondition(labels map[string]set.StringSet) string {
var buf bytes.Buffer
index := 0
for _, label := range def.Labels {
values := labels[label]
if len(values) == 0 {
continue
}
if index > 0 {
buf.WriteByte(',')
}
switch len(values) {
case 1:
buf.WriteString(fmt.Sprintf("%s=\"%s\"", label, GenLabelConditionValues(values)))
default:
buf.WriteString(fmt.Sprintf("%s=~\"%s\"", label, GenLabelConditionValues(values)))
}
index++
}
return buf.String()
}
// GenLabelConditionValues generates the label condition values.
func GenLabelConditionValues(values set.StringSet) string {
vs := make([]string, 0, len(values))
for k := range values {
vs = append(vs, k)
}
sort.Strings(vs)
return strings.Join(vs, "|")
}
// metricSchemaTable stands for the fake table all its data is in the memory.
type metricSchemaTable struct {
infoschemaTable
}
func tableFromMeta(alloc autoid.Allocators, meta *model.TableInfo) (table.Table, error) {
columns := make([]*table.Column, 0, len(meta.Columns))
for _, colInfo := range meta.Columns {
col := table.ToColumn(colInfo)
columns = append(columns, col)
}
t := &metricSchemaTable{
infoschemaTable: infoschemaTable{
meta: meta,
cols: columns,
tp: table.VirtualTable,
},
}
return t, nil
}