Showing
1 changed file
with
49 additions
and
0 deletions
error_reports_go.go
0 → 100644
1 | +package error_reports | ||
2 | + | ||
3 | +import( | ||
4 | + "time" | ||
5 | + "log" | ||
6 | + "os" | ||
7 | + "git.ukko.mx/ukko/influx_client_go.git" | ||
8 | +) | ||
9 | + | ||
10 | +var( | ||
11 | + defaultUsername = os.Getenv("INFLUX_USERNAME") | ||
12 | + defaultPassword = os.Getenv("INFLUX_PASSWORD") | ||
13 | + defaultDbAddress = os.Getenv("INFLUX_DB_ADDRESS") | ||
14 | + defaultSeries = "errors" | ||
15 | + influxClient *influx_client.InfluxClient | ||
16 | + series string | ||
17 | +) | ||
18 | + | ||
19 | +func InitReporting(db string, username string, password string, dbAddress string) { | ||
20 | + influxClient = influx_client.CreateClient(db, username, password, dbAddress) | ||
21 | + series = defaultSeries | ||
22 | +} | ||
23 | + | ||
24 | +func InitDefaultReporting(db string) { | ||
25 | + influxClient = influx_client.CreateClient(db, defaultUsername, defaultPassword, defaultDbAddress) | ||
26 | + series = defaultSeries | ||
27 | +} | ||
28 | + | ||
29 | +func Report(who string, message string, trace string, err error) { | ||
30 | + log.Printf("Error Reporter: %s", trace) | ||
31 | + tags := errorSystemTags(who, message, trace, err) | ||
32 | + fields := map[string]interface{}{ | ||
33 | + "value": 1, | ||
34 | + } | ||
35 | + stamp := time.Now() | ||
36 | + log.Print("Error Reporter: Reporting to InfluxDb...") | ||
37 | + influxClient.CreatePoint(series, tags, fields, stamp) | ||
38 | +} | ||
39 | + | ||
40 | +func errorSystemTags(who string, message string, trace string, err error) map[string]string { | ||
41 | + host, _:= os.Hostname() | ||
42 | + return map[string]string{ | ||
43 | + "hostname": host, | ||
44 | + "process": who, | ||
45 | + "message": message, | ||
46 | + "trace": trace, | ||
47 | + "exception": err.Error(), | ||
48 | + } | ||
49 | +} |
-
Please register or login to post a comment