This repository has been archived by the owner on Jan 19, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 13
/
init.go
298 lines (255 loc) · 8.7 KB
/
init.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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
/*
Copyright (c) 2016, Percona LLC and/or its affiliates. All rights reserved.
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Affero General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Affero General Public License for more details.
You should have received a copy of the GNU Affero General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>
*/
package app
import (
"fmt"
"math/rand"
"net/http"
"os"
"runtime"
"strings"
"time"
"github.com/cactus/go-statsd-client/statsd"
"github.com/percona/pmm/proto"
"github.com/percona/qan-api/app/agent"
"github.com/percona/qan-api/app/auth"
"github.com/percona/qan-api/app/controllers"
agentCtrl "github.com/percona/qan-api/app/controllers/agent"
"github.com/percona/qan-api/app/db"
"github.com/percona/qan-api/app/instance"
"github.com/percona/qan-api/app/query"
"github.com/percona/qan-api/app/shared"
"github.com/percona/qan-api/config"
queryService "github.com/percona/qan-api/service/query"
"github.com/percona/qan-api/stats"
"github.com/revel/revel"
)
// Do not set this var. It's set by scripts/build. The official version is set
// in conf/app.conf.
var APP_VERSION = ""
func init() {
runtime.GOMAXPROCS(runtime.NumCPU())
// CLOUD_API_HOSTNAME is only used for testing and should override api.hostname.
hostname := os.Getenv("CLOUD_API_HOSTNAME")
if hostname == "" {
hostname = config.Get("api.hostname")
if hostname == "" {
panic("Set CLOUD_API_HOSTNAME env var or api.hostname in the config file")
}
}
// Use real stats clients only in stage and prod.
statsEnv := config.Get("stats.env")
if statsEnv == "stage" || statsEnv == "prod" {
for _, service := range []string{"", "route"} {
statsClient, err := statsd.NewBufferedClient(
config.Get("statsd.server"),
"", // prefix -- DO NOT SET HERE
250*time.Millisecond, // buffer time
8932, // MTU for gigabit ethernet
)
if err != nil {
panic(fmt.Sprintf("ERROR: statsd.NewBufferedClient: %s", err))
}
s := stats.NewStats(
statsClient,
statsEnv,
config.Get("api.alias"),
service,
config.Get("stats.rate"),
)
switch service {
case "": // internal: agent, api
shared.InternalStats = s
case "route":
shared.RouteStats = s
}
}
}
shared.AgentDirectory = agent.NewLocalDirectory()
go func() {
t := time.NewTicker(1 * time.Minute)
for _ = range t.C {
shared.AgentDirectory.Refresh(20 * time.Second)
}
}()
// Run the query abstracter, used to consume QAN data.
shared.QueryAbstracter = queryService.NewMini(config.ApiRootDir + "/service/query") // tables + abstract using Perl
go shared.QueryAbstracter.Run()
shared.TableParser = queryService.NewMini("") // only tables
go shared.TableParser.Run()
revel.Filters = []revel.Filter{
revel.PanicFilter, // Recover from panics and display an error page instead.
revel.RouterFilter, // Use the routing table to select the right Action
revel.FilterConfiguringFilter, // A hook for adding or removing per-Action filters.
revel.ParamsFilter, // Parse parameters into Controller.Params.
revel.ValidationFilter, // Restore kept validation errors and save new ones from cookie.
revel.InterceptorFilter, // Run interceptors around the action.
revel.ActionInvoker, // Invoke the action.
}
// Tasks to be run at the begin and end of every request
revel.InterceptFunc(beforeController, revel.BEFORE, revel.ALL_CONTROLLERS)
revel.InterceptFunc(afterController, revel.FINALLY, revel.ALL_CONTROLLERS)
// All access to agent resources (/agents/:uuid/*) must specify a valid agent.
revel.InterceptFunc(authAgent, revel.BEFORE, &agentCtrl.Agent{})
revel.InterceptFunc(getInstanceId, revel.BEFORE, &controllers.QAN{})
revel.InterceptFunc(getQueryId, revel.BEFORE, &controllers.Query{})
}
// Copied from github.com/cactus/go-statsd-client/statsd/main.go
func includeStat(rate float32) bool {
if rate < 1 {
if rand.Float32() < rate {
return true
}
return false
}
return true
}
func beforeController(c *revel.Controller) revel.Result {
if c.Action == "Home.Options" {
return nil
}
if includeStat(shared.RouteStats.SampleRate) {
c.Args["t"] = time.Now()
}
if c.Action == "Home.Ping" {
c.Response.Out.Header().Set("X-Percona-QAN-API-Version", APP_VERSION)
}
// Create a MySQL db manager for the controller because most need it, but
// don't open the connection yet, let the controller do that when it's
// ready because it might return early (e.g. on invalid input).
// The controller doesn't need to close it; we do that in afterController.
c.Args["dbm"] = db.NewMySQLManager()
// Args for various controllers/routes.
apiBasePath := os.Getenv("BASE_PATH")
if apiBasePath == "" {
apiBasePath = config.Get("api.base.path")
}
schema := "http"
if strings.Contains(strings.ToLower(c.Request.Request.Proto), "https") {
schema = "https"
}
c.Args["wsBase"] = "ws://" + c.Request.Request.Host + apiBasePath
c.Args["httpBase"] = schema + "://" + c.Request.Request.Host + apiBasePath
agentVersion := c.Request.Header.Get("X-Percona-QAN-Agent-Version")
if agentVersion == "" {
agentVersion = "0.0.9"
}
c.Args["agentVersion"] = agentVersion
// Set common headers before Revel sets the response code and writes
// the response body. This avoids "multiple calls to WriterHeader" errors.
c.Response.Out.Header().Set("Access-Control-Allow-Headers", "Content-Type,Authorization")
c.Response.Out.Header().Set("Access-Control-Allow-Methods", "GET,PUT,POST,DELETE")
c.Response.Out.Header().Set("Access-Control-Allow-Origin", "*")
return nil
}
func afterController(c *revel.Controller) revel.Result {
if c.Action == "Home.Options" {
return nil
}
dbm := c.Args["dbm"].(db.Manager)
if err := dbm.Close(); err != nil {
revel.ERROR.Println(err)
}
if c.Args["t"] != nil {
t := c.Args["t"].(time.Time)
d := time.Now().Sub(t)
shared.RouteStats.TimingDuration( // response time
shared.RouteStats.Metric(c.Action+".t"),
d,
1, // sampling handled in beforeController()
)
shared.RouteStats.Inc( // call rate
shared.RouteStats.Metric(c.Action+".call"),
1,
1, // sampling handled in beforeController()
)
}
return nil
}
func authAgent(c *revel.Controller) revel.Result {
// We don't need a valid agent UUID for these routes.
if c.Action == "Agent.Create" || c.Action == "Agent.List" || c.Action == "Home.Options" {
return nil
}
var agentUuid string
c.Params.Bind(&agentUuid, "uuid")
dbm := c.Args["dbm"].(db.Manager)
dbh := auth.NewMySQLHandler(dbm)
authHandler := auth.NewAuthDb(dbh)
agentId, res, err := authHandler.Agent(agentUuid)
if err != nil {
switch err {
case shared.ErrNotFound:
revel.INFO.Printf("auth agent: not found: %s", agentUuid)
default:
revel.ERROR.Printf("auth agent: %s", err)
}
c.Response.Status = int(res.Code)
return c.RenderText(res.Error)
}
c.Args["agentId"] = agentId
return nil // success
}
func getInstanceId(c *revel.Controller) revel.Result {
// Get the internal (auto-inc) instance ID of the UUID.
var uuid string
c.Params.Bind(&uuid, "uuid")
dbm := c.Args["dbm"].(db.Manager)
if err := dbm.Open(); err != nil {
return internalError(c, "init.getInstanceId: dbm.Open", err)
}
instanceId, err := instance.GetInstanceId(dbm.DB(), uuid)
if err != nil {
switch err {
case shared.ErrNotFound:
c.Response.Status = http.StatusNotFound
return c.RenderText("")
default:
return internalError(c, "init.getInstanceId: ih.GetInstanceId", err)
}
}
c.Args["instanceId"] = instanceId
return nil // success
}
func getQueryId(c *revel.Controller) revel.Result {
// Get the internal (auto-inc) query ID.
var queryId string
c.Params.Bind(&queryId, "id")
dbm := c.Args["dbm"].(db.Manager)
if err := dbm.Open(); err != nil {
return internalError(c, "init.getQueryId: dbm.Open", err)
}
// 92F3B1B361FB0E5B -> 5
classId, err := query.GetClassId(dbm.DB(), queryId)
if err != nil {
switch err {
case shared.ErrNotFound:
c.Response.Status = http.StatusNotFound
return c.RenderText("")
default:
return internalError(c, "init.getQueryId: query.GetClassId", err)
}
}
c.Args["classId"] = classId
return nil // success
}
func internalError(c *revel.Controller, op string, err error) revel.Result {
errMsg := fmt.Sprintf("%s: %s", op, err)
revel.ERROR.Printf(errMsg)
res := proto.Error{
Error: errMsg,
}
c.Response.Status = http.StatusInternalServerError
return c.RenderJson(res)
}