+2
-2
cmd/querycheck/main.go
+2
-2
cmd/querycheck/main.go
···
49
&cli.StringFlag{
50
Name: "auth-token",
51
Usage: "auth token for accessing the querycheck api",
52
-
Value: "localdev",
53
EnvVars: []string{"AUTH_TOKEN"},
54
},
55
}
···
136
137
e.Use(func(next echo.HandlerFunc) echo.HandlerFunc {
138
return func(c echo.Context) error {
139
-
if c.Request().Header.Get("Authorization") != cctx.String("auth-token") {
140
return c.String(http.StatusUnauthorized, "unauthorized")
141
}
142
return next(c)
···
49
&cli.StringFlag{
50
Name: "auth-token",
51
Usage: "auth token for accessing the querycheck api",
52
+
Value: "",
53
EnvVars: []string{"AUTH_TOKEN"},
54
},
55
}
···
136
137
e.Use(func(next echo.HandlerFunc) echo.HandlerFunc {
138
return func(c echo.Context) error {
139
+
if cctx.String("auth-token") != "" && c.Request().Header.Get("Authorization") != cctx.String("auth-token") {
140
return c.String(http.StatusUnauthorized, "unauthorized")
141
}
142
return next(c)
+28
-24
querycheck/check.go
+28
-24
querycheck/check.go
···
17
18
// Query is a query to check
19
type Query struct {
20
-
Name string
21
-
Query string
22
-
LastPlan *QueryPlan
23
-
LastChecked time.Time
24
-
LastError error
25
-
CheckEvery time.Duration
26
27
lk sync.RWMutex
28
in chan struct{}
···
103
for _, qu := range q.Queries {
104
if qu.Name == name {
105
return &Query{
106
-
Name: qu.Name,
107
-
Query: qu.Query,
108
-
LastPlan: qu.LastPlan,
109
-
LastChecked: qu.LastChecked,
110
-
LastError: qu.LastError,
111
-
CheckEvery: qu.CheckEvery,
112
}
113
}
114
}
···
125
queries := make([]*Query, len(q.Queries))
126
for i, qu := range q.Queries {
127
queries[i] = &Query{
128
-
Name: qu.Name,
129
-
Query: qu.Query,
130
-
LastPlan: qu.LastPlan,
131
-
LastChecked: qu.LastChecked,
132
-
LastError: qu.LastError,
133
-
CheckEvery: qu.CheckEvery,
134
}
135
}
136
···
177
ticker := time.NewTicker(query.CheckEvery)
178
defer ticker.Stop()
179
180
-
query.LastPlan, err = q.CheckQueryPlan(ctx, query.Query)
181
if err != nil {
182
log.Errorf("failed to check query plan: %+v\n", err)
183
}
184
185
-
if query.LastPlan != nil {
186
-
log.Infof("Initial plan:\n%+v\n", query.LastPlan.String())
187
-
query.RecordPlanMetrics(*query.LastPlan)
188
query.LastChecked = time.Now()
189
}
190
···
216
}
217
218
query.lk.RLock()
219
-
lastPlan := *query.LastPlan
220
query.lk.RUnlock()
221
222
query.RecordPlanMetrics(*qp)
···
231
log.Infof("query plan has changed (%s%.03fms): \n%+v\n", sign, diff, qp.String())
232
233
query.lk.Lock()
234
-
query.LastPlan = qp
235
query.lk.Unlock()
236
}
237
case <-query.in:
···
17
18
// Query is a query to check
19
type Query struct {
20
+
Name string
21
+
Query string
22
+
LatestPlan *QueryPlan
23
+
PreviousPlan *QueryPlan
24
+
LastChecked time.Time
25
+
LastError error
26
+
CheckEvery time.Duration
27
28
lk sync.RWMutex
29
in chan struct{}
···
104
for _, qu := range q.Queries {
105
if qu.Name == name {
106
return &Query{
107
+
Name: qu.Name,
108
+
Query: qu.Query,
109
+
LatestPlan: qu.LatestPlan,
110
+
PreviousPlan: qu.PreviousPlan,
111
+
LastChecked: qu.LastChecked,
112
+
LastError: qu.LastError,
113
+
CheckEvery: qu.CheckEvery,
114
}
115
}
116
}
···
127
queries := make([]*Query, len(q.Queries))
128
for i, qu := range q.Queries {
129
queries[i] = &Query{
130
+
Name: qu.Name,
131
+
Query: qu.Query,
132
+
LatestPlan: qu.LatestPlan,
133
+
PreviousPlan: qu.PreviousPlan,
134
+
LastChecked: qu.LastChecked,
135
+
LastError: qu.LastError,
136
+
CheckEvery: qu.CheckEvery,
137
}
138
}
139
···
180
ticker := time.NewTicker(query.CheckEvery)
181
defer ticker.Stop()
182
183
+
query.LatestPlan, err = q.CheckQueryPlan(ctx, query.Query)
184
if err != nil {
185
log.Errorf("failed to check query plan: %+v\n", err)
186
}
187
188
+
if query.LatestPlan != nil {
189
+
log.Infof("Initial plan:\n%+v\n", query.LatestPlan.String())
190
+
query.RecordPlanMetrics(*query.LatestPlan)
191
query.LastChecked = time.Now()
192
}
193
···
219
}
220
221
query.lk.RLock()
222
+
lastPlan := *query.LatestPlan
223
query.lk.RUnlock()
224
225
query.RecordPlanMetrics(*qp)
···
234
log.Infof("query plan has changed (%s%.03fms): \n%+v\n", sign, diff, qp.String())
235
236
query.lk.Lock()
237
+
query.PreviousPlan = query.LatestPlan
238
+
query.LatestPlan = qp
239
query.lk.Unlock()
240
}
241
case <-query.in:
+21
-18
querycheck/handlers.go
+21
-18
querycheck/handlers.go
···
7
)
8
9
type RetQuery struct {
10
-
Name string `json:"name"`
11
-
Query string `json:"query"`
12
-
LastPlan *QueryPlan `json:"last_plan"`
13
-
LastChecked time.Time `json:"last_checked"`
14
-
LastError error `json:"last_error"`
15
-
CheckEvery string `json:"check_every"`
16
}
17
18
func (q *Querychecker) HandleGetQueries(c echo.Context) error {
···
20
retQueries := []RetQuery{}
21
for _, query := range queries {
22
retQueries = append(retQueries, RetQuery{
23
-
Name: query.Name,
24
-
Query: query.Query,
25
-
LastPlan: query.LastPlan,
26
-
LastChecked: query.LastChecked,
27
-
LastError: query.LastError,
28
-
CheckEvery: query.CheckEvery.String(),
29
})
30
}
31
···
41
}
42
43
retQuery := RetQuery{
44
-
Name: query.Name,
45
-
Query: query.Query,
46
-
LastPlan: query.LastPlan,
47
-
LastChecked: query.LastChecked,
48
-
LastError: query.LastError,
49
-
CheckEvery: query.CheckEvery.String(),
50
}
51
52
return c.JSON(200, retQuery)
···
7
)
8
9
type RetQuery struct {
10
+
Name string `json:"name"`
11
+
Query string `json:"query"`
12
+
LatestPlan *QueryPlan `json:"latest_plan"`
13
+
PreviousPlan *QueryPlan `json:"previous_plan"`
14
+
LastChecked time.Time `json:"last_checked"`
15
+
LastError error `json:"last_error"`
16
+
CheckEvery string `json:"check_every"`
17
}
18
19
func (q *Querychecker) HandleGetQueries(c echo.Context) error {
···
21
retQueries := []RetQuery{}
22
for _, query := range queries {
23
retQueries = append(retQueries, RetQuery{
24
+
Name: query.Name,
25
+
Query: query.Query,
26
+
LatestPlan: query.LatestPlan,
27
+
PreviousPlan: query.PreviousPlan,
28
+
LastChecked: query.LastChecked,
29
+
LastError: query.LastError,
30
+
CheckEvery: query.CheckEvery.String(),
31
})
32
}
33
···
43
}
44
45
retQuery := RetQuery{
46
+
Name: query.Name,
47
+
Query: query.Query,
48
+
LatestPlan: query.LatestPlan,
49
+
PreviousPlan: query.PreviousPlan,
50
+
LastChecked: query.LastChecked,
51
+
LastError: query.LastError,
52
+
CheckEvery: query.CheckEvery.String(),
53
}
54
55
return c.JSON(200, retQuery)