error_reports_go.go 1.29 KB
package error_reports

import(
  "time"
  "log"
  "os"
  "git.ukko.mx/ukko/influx_client_go.git"
)

var(
  defaultUsername = os.Getenv("INFLUX_USERNAME")
  defaultPassword = os.Getenv("INFLUX_PASSWORD")
  defaultDbAddress = os.Getenv("INFLUX_DB_ADDRESS")
  defaultSeries = "errors"
  influxClient *influx_client.InfluxClient
  series string
)

func InitReporting(db string, username string, password string, dbAddress string) {
  influxClient = influx_client.CreateClient(db, username, password, dbAddress)
  series = defaultSeries
}

func InitDefaultReporting(db string) {
  influxClient = influx_client.CreateClient(db, defaultUsername, defaultPassword, defaultDbAddress)
  series = defaultSeries
}

func Report(who string, message string, trace string, err error) {
  log.Printf("Error Reporter: %s", trace)
  tags := errorSystemTags(who, message, trace, err)
  fields := map[string]interface{}{
    "value": 1,
  }
  stamp := time.Now()
  log.Print("Error Reporter: Reporting to InfluxDb...")
  influxClient.CreatePoint(series, tags, fields, stamp)
}

func errorSystemTags(who string, message string, trace string, err error) map[string]string {
  host, _:= os.Hostname()
  return map[string]string{
    "hostname": host,
    "process": who,
    "message": message,
    "trace": trace,
    "exception": err.Error(),
  }
}