-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
216 lines (182 loc) · 6.36 KB
/
main.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
package main
import (
"encoding/json"
"flag"
"fmt"
"log"
"os"
"path"
"strconv"
"strings"
"time"
"github.com/kardianos/osext"
"github.com/Clever/analytics-monitor/config"
"github.com/Clever/analytics-monitor/db"
l "github.com/Clever/analytics-monitor/logger"
)
var (
latencyConfigPath string
logger l.Logger
globalDefaultLatency string
)
// Checks stores table checks in a nested map,
// indexed by schema name and then table name
type Checks map[string]map[string]config.TableCheck
func init() {
logger = l.GetLogger()
// kvconfig.yml must live in the same directory as
// the executable file in order for log routing to work
dir, err := osext.ExecutableFolder()
if err != nil {
log.Fatal(err)
}
err = l.SetGlobalRouting(path.Join(dir, "kvconfig.yml"))
if err != nil {
log.Fatal(err)
}
latencyConfigPath = path.Join(dir, "config/example_config.json")
globalDefaultLatency = "24h"
}
func main() {
flag.Parse()
config.Parse()
defer logger.JobFinishedEvent(strings.Join(os.Args[1:], " "), true)
postgresConn, err := db.NewPostgresClient()
fatalIfErr(err, "postgres-failed-init")
configChecks := config.ParseChecks(latencyConfigPath)
postgresChecks := buildLatencyChecks(configChecks.PostgresChecks, postgresConn)
queryLatencyErrors := performLatencyChecks(postgresConn, postgresChecks)
performLoadErrorsCheck(postgresConn)
if len(queryLatencyErrors) > 0 {
var errStrs []string
for _, latencyErr := range queryLatencyErrors {
l.GetKVLogger().CriticalD("query-latency-error", l.M{"errors": latencyErr.Error()})
errStrs = append(errStrs, latencyErr.Error())
}
logger.JobFinishedEvent(strings.Join(os.Args[1:], " "), false)
log.Fatalf("Encountered fatal error querying for latency: %s", strings.Join(errStrs, ","))
}
}
// fatalIfErr logs a critical error. Assumes logger is initialized
func fatalIfErr(err error, title string) {
if err != nil {
logger.JobFinishedEvent(strings.Join(os.Args[1:], " "), false)
l.GetKVLogger().CriticalD(title, l.M{"error": err.Error()})
panic(fmt.Sprintf("Encountered fatal error: %s", err.Error()))
}
}
// buildLatencyChecks constructs the latency checks for a given postgres instance
// Each check can either be declared explicitly (by specifying table latency
// in schemaConfigs), or implicitly (by falling back on the default latency
// values specified at the schema level).
//
// Returns: a map of checks for each cluster.
// Each map of checks is indexed by cluster name, then table name.
// Each check (see: config.TableCheck) contains:
// A.) Latency threshold as a duration string
// B.) Name of the timestamp column
func buildLatencyChecks(schemaConfigs []config.SchemaConfig, postgresClient db.PostgresClient) Checks {
checks := make(Checks)
for _, schemaConfig := range schemaConfigs {
schemaName := schemaConfig.SchemaName
checks[schemaName] = make(map[string]config.TableCheck)
tableMetadata, err := postgresClient.QueryTableMetadata(schemaName)
if err != nil {
l.GetKVLogger().CriticalD("query-table-metadata-error", l.M{"error": err.Error()})
panic("Unable to query table metadata")
}
for tableName, metadata := range tableMetadata {
// Use inferred timestamp column if not specified in schema default
timestampColumn := schemaConfig.DefaultTimestampColumn
if timestampColumn == "" {
timestampColumn = metadata.TimestampColumn
}
// Use global default latency if not specified in schema default
defaultThreshold := schemaConfig.DefaultThreshold
if defaultThreshold == "" {
defaultThreshold = globalDefaultLatency
}
checks[schemaName][tableName] = config.TableCheck{
TableName: tableName,
Latency: config.LatencyInfo{
TimestampColumn: timestampColumn,
Threshold: defaultThreshold,
},
}
}
// Override per-schema thresholds if specified in config
for _, configCheck := range schemaConfig.Checks {
tableName := configCheck.TableName
if _, ok := checks[schemaName][configCheck.TableName]; ok {
checks[schemaName][tableName] = config.TableCheck{
TableName: tableName,
Latency: config.LatencyInfo{
TimestampColumn: configCheck.Latency.TimestampColumn,
Threshold: configCheck.Latency.Threshold,
},
}
} else {
l.GetKVLogger().WarnD("missing-table-in-db", l.M{
"message": fmt.Sprintf("Can't check latency for %s.%s", schemaName, tableName),
})
}
}
// Finally, omit latency checks for specified tables
for _, tableToOmit := range schemaConfig.TablesToOmit {
if _, ok := checks[schemaName][tableToOmit]; ok {
log.Printf("Omitting latency check for %s.%s", schemaName, tableToOmit)
delete(checks[schemaName], tableToOmit)
} else {
l.GetKVLogger().WarnD("missing-table-in-db", l.M{
"message": fmt.Sprintf("Omit latency for %s.%s is a no-op",
schemaName, tableToOmit),
})
}
}
}
return checks
}
func performLoadErrorsCheck(postgresClient db.PostgresClient) {
loadErrors, err := postgresClient.QuerySTLLoadErrors()
if err != nil {
log.Printf("Error with client performing load error check: %v.\n", err)
} else {
if loadErrors != nil && len(loadErrors) > 0 {
loadErrorsJSON, err := json.Marshal(loadErrors)
if err != nil {
log.Printf("Error: %s", err)
}
logger.CheckLoadErrorEvent(1, string(loadErrorsJSON))
} else {
// No load errors in past hour
logger.CheckLoadErrorEvent(0, "")
}
}
}
func performLatencyChecks(postgresClient db.PostgresClient, checks Checks) []error {
var queryLatencyErrors []error
clusterName := postgresClient.GetClusterName()
for schemaName, tableChecks := range checks {
for tableName, check := range tableChecks {
threshold, err := time.ParseDuration(check.Latency.Threshold)
fatalIfErr(err, "parse-duration-error")
latencyHrs, hasRows, err := postgresClient.QueryLatency(check.Latency.TimestampColumn,
schemaName, tableName)
if err != nil {
queryLatencyErrors = append(queryLatencyErrors, err)
continue
}
latencyErrValue := 0
if !hasRows || float64(latencyHrs) > threshold.Hours() {
latencyErrValue = 1
}
reportedLatency := fmt.Sprintf("%sh", strconv.FormatInt(latencyHrs, 10))
if !hasRows {
reportedLatency = "N/A - no rows"
}
fullTableName := fmt.Sprintf("%s.%s.%s", clusterName, schemaName, tableName)
logger.CheckLatencyEvent(latencyErrValue, fullTableName, reportedLatency, check.Latency.Threshold)
}
}
return queryLatencyErrors
}