-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathglobal_routes.go
162 lines (140 loc) · 3.72 KB
/
global_routes.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
package main
import (
"fmt"
"regexp"
"strconv"
"strings"
"github.com/Clever/amazon-kinesis-client-go/decode"
)
const defaultValueField = "value"
const statTypeCounter = "counter"
const statTypeGauge = "gauge"
const statTypeEvent = "event"
func globalRoutes(fields map[string]interface{}) []decode.AlertRoute {
routes := []decode.AlertRoute{}
// chain and append all global routes here
// TODO: After initial migration, revisit these routes and ensure they all
// emit hostname+env via default dimensions
routes = append(routes, processMetricsRoutes(fields)...)
routes = append(routes, rdsSlowQueries(fields)...)
return routes
}
// globalRoutesWithCustomFields some global routes inject custom dimensions by adding values to the
// fields map
func globalRoutesWithCustomFields(fields *map[string]interface{}) []decode.AlertRoute {
routes := []decode.AlertRoute{}
routes = append(routes, mongoSlowQueries(fields)...)
return routes
}
// Metrics emitted by node-process-metrics and go-process-metrics libraries
func processMetricsRoutes(fields map[string]interface{}) []decode.AlertRoute {
via, ok := fields["via"].(string)
if !ok {
return []decode.AlertRoute{}
}
if via != "process-metrics" {
return []decode.AlertRoute{}
}
_, ok = fields["source"].(string)
if !ok {
return []decode.AlertRoute{}
}
title, ok := fields["title"].(string)
if !ok {
return []decode.AlertRoute{}
}
statType, ok := fields["type"].(string)
if !ok {
return []decode.AlertRoute{}
}
if statType == "guage" {
statType = "gauge" // Here to fix typo in library
}
return []decode.AlertRoute{
{
Series: fmt.Sprintf("process-metrics.%s", title),
Dimensions: []string{"Hostname", "env", "source"},
StatType: statType,
ValueField: defaultValueField,
RuleName: "global-process-metrics",
},
}
}
var allowedLifecycleEvents = []string{
"app_deploying",
"app_scaling",
"app_rollback",
"app_stopping",
//"app_freezing",
//"app_unfreezing",
"app_restarting",
"app_canary",
"app_canaryscaling",
}
var reMongoSlowQuery = regexp.MustCompile(`^\[conn\d+\]\s([a-z]+)\s([^\s]+?)\s.*\s(\d+)ms$`)
func mongoSlowQueries(fields *map[string]interface{}) []decode.AlertRoute {
rawlog, ok := (*fields)["rawlog"].(string)
if !ok {
return []decode.AlertRoute{}
}
matches := reMongoSlowQuery.FindStringSubmatch(rawlog)
if len(matches) < 4 {
return []decode.AlertRoute{}
}
millis, err := strconv.ParseFloat(matches[3], 64)
if err != nil {
return []decode.AlertRoute{}
}
(*fields)["operation"] = matches[1]
(*fields)["namespace"] = matches[2]
(*fields)["is_collscan"] = strings.Contains(rawlog, "COLLSCAN")
(*fields)["millis"] = millis
return []decode.AlertRoute{
{
Series: "mongo.slow-query",
Dimensions: []string{
"hostname",
"operation",
"namespace",
"is_collscan",
},
StatType: statTypeCounter,
RuleName: "global-mongo-slow-query-count",
},
{
Series: "mongo.slow-query-millis",
Dimensions: []string{
"hostname",
"operation",
"namespace",
"is_collscan",
},
StatType: statTypeGauge,
ValueField: "millis",
RuleName: "global-mongo-slow-query-gauge",
},
}
}
func rdsSlowQueries(fields map[string]interface{}) []decode.AlertRoute {
hostname, ok := fields["hostname"].(string)
if !ok {
return []decode.AlertRoute{}
}
if hostname != "aws-rds" {
return []decode.AlertRoute{}
}
// filter out slowqueries by rdsadmin
user, ok := fields["user"].(string)
if !ok || user == "rdsadmin[rdsadmin]" {
return []decode.AlertRoute{}
}
return []decode.AlertRoute{
{
Series: "rds.slow-query",
Dimensions: []string{"env", "programname"},
StatType: statTypeCounter,
ValueField: defaultValueField,
RuleName: "global-rds-slow-query-count",
},
}
}