//TODO: handle errnous cases func GetDataDogAgent() *DataDogAgent { once.Do(func() { agentObj = new(DataDogAgent) var errObj error enabled := conf.Bool("monitor.enabled", false) if enabled && agentObj.ClientExists() == false { host := conf.String("monitor.host", "") port := conf.String("monitor.port", "") if host == "" || port == "" { errObj = errors.New("Datadog config host and port missing") } else { client, err := dogstatsd.New(host + ":" + port) if err != nil { errObj = err } else { namespace := conf.String("monitor.namespace", "") if namespace != "" { client.Namespace = namespace } agentObj.Client = client } } } if errObj != nil { //log error message } }) return agentObj }
// debug // - inner // - type // - .. // - read-file // - write-file func DebugFromConfig(container string) Cacher { return NewDebugCache( conf.String(container+".write-file", ""), conf.String(container+".read-file", ""), FromConfig(container+".inner"), ) }
func initMongo() { lookup := "database.mongo" if conf.Exists(lookup) { connMongo = GetMongoConnFromConfig(lookup) dbMongo = conf.String("database.mongo.db", "") } }
func GetTimeIntervalTag(respTime float64) (ret []string) { interval := conf.String("monitor.interval", "") if interval != "" { intervalArr := strings.Split(interval, ",") var lowerLimit, higherLimit float64 var matched bool for i := range intervalArr { higherLimit, _ = strconv.ParseFloat(intervalArr[i], 64) if respTime <= higherLimit { matched = true break } else { lowerLimit = higherLimit } } var intervalGroup string if matched == true { intervalGroup = "from_" + strconv.FormatInt(int64(lowerLimit), 10) + "_to_" + strconv.FormatInt(int64(higherLimit), 10) } else { intervalGroup = "above_" + strconv.FormatInt(int64(higherLimit), 10) } ret = append(ret, intervalGroup) } return }
func createPanicLog(sTime time.Time, panicMsg interface{}) { path := conf.String("logs.panic_log", "panic_log") path = fmt.Sprintf("%s_%d-%d-%d.log", path, sTime.Day(), sTime.Month(), sTime.Year()) if logFp, fileErr = os.OpenFile(path, os.O_RDWR|os.O_CREATE|os.O_APPEND, 0666); fileErr != nil { fmt.Println("Could not create the panic log file") panic(panicMsg) } }
func GetMongoConnFromConfig(container string) string { if !conf.Exists(container) { panic("Container for mongo configuration not found") } username := conf.String(container+".username", "") password := conf.String(container+".password", "") host := conf.String(container+".host", "") port := conf.String(container+".port", "") db := conf.String(container+".db", "") replicas := conf.String(container+".replicas", "") options := conf.String(container+".options", "") if port != "" { port = ":" + port } if replicas != "" { replicas = "," + replicas } if options != "" { options = "?" + options } auth := "" if username != "" || password != "" { auth = username + ":" + password + "@" } return fmt.Sprintf("mongodb://%s%s%s%s/%s%s", auth, host, port, replicas, db, options, ) }
// create the mongo connection string func getMongoConnStr(container string) (conn string, db string, err error) { username := conf.String(container+".username", "") password := conf.String(container+".password", "") host := conf.String(container+".host", "") port := conf.String(container+".port", "") db = conf.String(container+".db", "") replicas := conf.String(container+".replicas", "") options := conf.String(container+".options", "") if db == "" { err = errors.New("mongo database name missing") return } if port != "" { port = ":" + port } if replicas != "" { replicas = "," + replicas } if options != "" { options = "?" + options } auth := "" if username != "" || password != "" { auth = username + ":" + password + "@" } conn = fmt.Sprintf("mongodb://%s%s%s%s/%s%s", auth, host, port, replicas, db, options) return }
// memcache: // - host // - port func MemcacheFromConfig(container string) Cacher { host := conf.String(container+".host", "") panik.If(host == "", "memcache host not specified") port := conf.Int(container+".port", 0) panik.If(port == 0, "memcache port not specified") return NewMemcache(host, port) }
// redis: // - host // - port // - db func RedisFromConfig(container string) Cacher { host := conf.String(container+".host", "") panik.If(host == "", "redis host not specified") port := conf.Int(container+".port", 0) panik.If(port == 0, "redis port not specified") db := conf.Int(container+".db", 0) return NewRedis(host, port, db) }
func MonitorMe(params MonitorParams) { if params.ServiceId != "" { dataDogAgent := GetDataDogAgent() dataDogAgent.Count("throughput", 1, nil, 1) dataDogAgent.Count(params.ServiceId, 1, nil, 1) dataDogAgent.Histogram("resptime", params.RespTime, nil, 1) dataDogAgent.Histogram(params.ServiceId+".resptime", params.RespTime, nil, 1) intervalTag := GetTimeIntervalTag(params.RespTime) dataDogAgent.Histogram("resptimeinterval", params.RespTime, intervalTag, 1) dataDogAgent.Histogram(params.ServiceId+".resptimeinterval", params.RespTime, intervalTag, 1) if params.CacheHit { dataDogAgent.Count("cachehit", 1, nil, 1) dataDogAgent.Count(params.ServiceId+".cachehit", 1, nil, 1) dataDogAgent.Histogram("resptime.c", params.RespTime, nil, 1) dataDogAgent.Histogram(params.ServiceId+".resptime.c", params.RespTime, nil, 1) } else { dataDogAgent.Histogram("resptime.nc", params.RespTime, nil, 1) dataDogAgent.Histogram(params.ServiceId+".resptime.nc", params.RespTime, nil, 1) } if params.ResponseCode > 0 { statusCode := FormatHttpStatusCode(int64(params.ResponseCode)) dataDogAgent.Count(statusCode, 1, nil, 1) dataDogAgent.Count(params.ServiceId+"."+statusCode, 1, nil, 1) } enablelog := conf.Bool("monitor.enablelog", false) if enablelog { logfile := conf.String("monitor.filelog", "") if logfile != "" { t := time.Now() logfileSuffix := fmt.Sprintf("%d-%d-%d", t.Month(), t.Day(), t.Hour()) f, err := os.OpenFile(logfile+logfileSuffix+".csv", os.O_RDWR|os.O_CREATE|os.O_APPEND, 0666) panik.On(err) l := log.New(f, "", log.LstdFlags) l.Printf("%s %f %s %d %t", params.ServiceId, params.RespTime, intervalTag, params.ResponseCode, params.CacheHit) } } } }
func getMySqlConnString(container string) string { if !conf.Exists(container) { panic("Container for mysql configuration not found") } username := conf.String(container+".username", "") password := conf.String(container+".password", "") host := conf.String(container+".host", "") port := conf.String(container+".port", "") db := conf.String(container+".db", "") timezone := conf.String(container+".timezone", "") return fmt.Sprintf("%s:%s@tcp(%s:%s)/%s?parseTime=true&loc=%s", username, password, host, port, db, url.QueryEscape(timezone), ) }
func FromConfig(container string) (out Cacher) { cType := conf.String(container+".type", "") panik.If(cType == "", "cache type is not specified") switch cType { case "memcache": out = MemcacheFromConfig(container) case "redis": out = RedisFromConfig(container) case "inmem": out = InmemFromConfig(container) case "debug": out = DebugFromConfig(container) default: panic("unknown cache type specifed: " + cType) } return out }