1
- package gopromql
1
+ package vtm
2
2
3
3
import (
4
4
"fmt"
@@ -98,9 +98,9 @@ type BinaryOp struct {
98
98
type Matcher string
99
99
100
100
const (
101
- EqualMacther Matcher = "="
101
+ EqualMatcher Matcher = "="
102
102
NotEqualMatcher Matcher = "!="
103
- LikeMatcher Matcher = "!= "
103
+ LikeMatcher Matcher = "=~ "
104
104
NotLikeMatcher Matcher = "!~"
105
105
)
106
106
@@ -110,7 +110,7 @@ type Label struct {
110
110
Matcher
111
111
}
112
112
113
- type PromqlBuilder struct {
113
+ type PromQLBuilder struct {
114
114
metricName string
115
115
funcName string
116
116
labels []Label
@@ -120,92 +120,92 @@ type PromqlBuilder struct {
120
120
agg Aggregation
121
121
}
122
122
123
- func NewPromqlBuilder (metricName string ) * PromqlBuilder {
124
- return & PromqlBuilder {metricName : metricName }
123
+ func NewPromQLBuilder (metricName string ) * PromQLBuilder {
124
+ return & PromQLBuilder {metricName : metricName }
125
125
}
126
126
127
127
// see prometheus functions: https://prometheus.io/docs/prometheus/latest/querying/functions/
128
- // WithFuncName, basicQL will be format as "funcName(basicql )"
129
- func (pb * PromqlBuilder ) WithFuncName (funcName string ) * PromqlBuilder {
128
+ // WithFuncName, basicQL will be format as "funcName(basicQL )"
129
+ func (pb * PromQLBuilder ) WithFuncName (funcName string ) * PromQLBuilder {
130
130
pb .funcName = funcName
131
131
return pb
132
132
}
133
133
134
134
// WithLabels basicQL will be format as `{labelName1}={labelValue1},{labelName2}=~{labelValue2|labelValue2x}...{labelNameN}!={labelValueN}`
135
135
// and append to metric name
136
- func (pb * PromqlBuilder ) WithLabels (labels Label ) * PromqlBuilder {
136
+ func (pb * PromQLBuilder ) WithLabels (labels Label ) * PromQLBuilder {
137
137
pb .labels = append (pb .labels , labels )
138
138
139
139
return pb
140
140
}
141
141
142
142
// WithOffset basicQL will be format as "offset {offset}"
143
- func (pb * PromqlBuilder ) WithOffset (offset string ) * PromqlBuilder {
143
+ func (pb * PromQLBuilder ) WithOffset (offset string ) * PromQLBuilder {
144
144
pb .offset = offset
145
145
return pb
146
146
}
147
147
148
148
// WithWindow basicQL will be format as "[window]"
149
- func (pb * PromqlBuilder ) WithWindow (window string ) * PromqlBuilder {
149
+ func (pb * PromQLBuilder ) WithWindow (window string ) * PromQLBuilder {
150
150
pb .window = window
151
151
return pb
152
152
}
153
153
154
154
// WithComp basicQL will be format as "comp1 and comp2 and ... and compN"
155
155
// when compare length > 1, combine with "and" as default
156
- func (pb * PromqlBuilder ) WithComp (comp Compare ) * PromqlBuilder {
156
+ func (pb * PromQLBuilder ) WithComp (comp Compare ) * PromQLBuilder {
157
157
pb .compOps = append (pb .compOps , comp )
158
158
return pb
159
159
}
160
160
161
- // WithAgg basicQL will be format as "agg(basicql ) by/without (labelName1,labelName2,...,labelNameN)"
162
- func (pb * PromqlBuilder ) WithAgg (agg Aggregation ) * PromqlBuilder {
161
+ // WithAgg basicQL will be format as "agg(basicQL ) by/without (labelName1,labelName2,...,labelNameN)"
162
+ func (pb * PromQLBuilder ) WithAgg (agg Aggregation ) * PromQLBuilder {
163
163
pb .agg = agg
164
164
return pb
165
165
}
166
166
167
- // Build build basicql
168
- func (pb * PromqlBuilder ) Build () (string , error ) {
167
+ // Build build basicQL
168
+ func (pb * PromQLBuilder ) Build () (string , error ) {
169
169
if pb .metricName == "" {
170
170
return "" , fmt .Errorf ("metric name is required" )
171
171
}
172
- basicql := fmt . Sprintf ( pb .metricName )
172
+ basicQL := pb .metricName
173
173
if len (pb .labels ) > 0 {
174
174
tmp := make ([]string , len (pb .labels ))
175
175
for index , label := range pb .labels {
176
176
tmp [index ] = fmt .Sprintf (`%s%s"%s"` , label .Name , label .Matcher , label .Value )
177
177
}
178
- basicql += fmt .Sprintf ("{%s}" , strings .Join (tmp , "," ))
178
+ basicQL += fmt .Sprintf ("{%s}" , strings .Join (tmp , "," ))
179
179
}
180
180
181
181
if pb .window != "" {
182
- basicql += fmt .Sprintf ("[%s]" , pb .window )
182
+ basicQL += fmt .Sprintf ("[%s]" , pb .window )
183
183
}
184
184
if len (pb .compOps ) > 0 {
185
185
tmp := make ([]string , len (pb .compOps ))
186
186
for index , comp := range pb .compOps {
187
- tmp [index ] = fmt .Sprintf ("%s%s%.0f" , basicql , comp .Op , comp .Value )
187
+ tmp [index ] = fmt .Sprintf ("%s%s%.0f" , basicQL , comp .Op , comp .Value )
188
188
}
189
189
fmt .Println (tmp )
190
190
fmt .Println (len (tmp ))
191
- basicql = fmt .Sprintf (" %s" , strings .Join (tmp , " and " ))
191
+ basicQL = fmt .Sprintf (" %s" , strings .Join (tmp , " and " ))
192
192
193
193
}
194
194
if pb .funcName != "" {
195
- basicql = fmt .Sprintf ("%s(%s)" , pb .funcName , basicql )
195
+ basicQL = fmt .Sprintf ("%s(%s)" , pb .funcName , basicQL )
196
196
}
197
197
198
198
if pb .offset != "" {
199
- basicql += fmt .Sprintf (" offset %s" , pb .offset )
199
+ basicQL += fmt .Sprintf (" offset %s" , pb .offset )
200
200
}
201
201
202
202
if ! reflect .DeepEqual (pb .agg , Aggregation {}) {
203
203
if pb .agg .Op != "" {
204
- basicql = fmt .Sprintf ("%s(%s)" , pb .agg .Op , basicql )
204
+ basicQL = fmt .Sprintf ("%s(%s)" , pb .agg .Op , basicQL )
205
205
}
206
206
if pb .agg .AggWay != "" && pb .agg .By != nil {
207
- basicql = fmt .Sprintf ("%s %s (%s)" , basicql , pb .agg .AggWay , strings .Join (pb .agg .By , "," ))
207
+ basicQL = fmt .Sprintf ("%s %s (%s)" , basicQL , pb .agg .AggWay , strings .Join (pb .agg .By , "," ))
208
208
}
209
209
}
210
- return basicql , nil
210
+ return basicQL , nil
211
211
}
0 commit comments