forked from reyhanfahlevi/pkg
-
Notifications
You must be signed in to change notification settings - Fork 0
/
init.go
54 lines (44 loc) · 1.17 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
package tracer
import (
"strings"
"github.com/ennobelprakoso/pkg/go/tracer/nr"
)
// Init to initialize tracing package
func Init(cfg Config) error {
nrCfg := nr.Config{
AppName: cfg.Appname,
SecretKey: cfg.NewRelic.SecretKey,
LogLevel: cfg.NewRelic.LogLevel,
}
err := nr.Init(nrCfg)
if err != nil {
return err
}
return nil
}
// getOperationFromSQLQuery to get DDL / DML operation
// example: query `SELECT $1 FROM table_name`, this func will return SELECT
// newrelic pkg will send both operation name and raw query to their data, so we can explore queries that slow and so on...
func getOperationFromSQLQuery(query string) string {
// replace /t with space
query = strings.Replace(query, "\t", " ", -1)
// replace /n with space
query = strings.Replace(query, "\n", " ", -1)
// uppercase all
query = strings.ToUpper(query)
// trim space
query = strings.TrimSpace(query)
if strings.Contains(query, "INSERT ") {
return "INSERT"
}
if strings.Contains(query, "UPDATE ") {
return "UPDATE"
}
if strings.Contains(query, "DELETE ") {
return "DELETE"
}
if strings.Contains(query, "SELECT ") {
return "SELECT"
}
return strings.Split(query, " ")[0]
}