Iker Narvaez

Refactor package & improve error logging

1 -package error_reporter
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, err error) {
30 - log.Fatalf("%s: %s", message, err)
31 - tags := errorSystemTags(who, message, err)
32 - fields := map[string]interface{}{
33 - "value": 1,
34 - }
35 - stamp := time.Now()
36 - influxClient.CreatePoint(series, tags, fields, stamp)
37 -}
38 -
39 -func errorSystemTags(who string, message string, err error) map[string]string {
40 - host, _:= os.Hostname()
41 - return map[string]string{
42 - "hostname": host,
43 - "process": who,
44 - "message": message,
45 - "exception": err.Error(),
46 - }
47 -}