//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 }
func PanicLogger(panicMsg interface{}, serviceID string, requestURI string, curTime time.Time) { syncOnce.Do(func() { isLog = conf.Bool("monitor.panic_log", false) }) if isLog == true { sTime := time.Now().UTC() currentDay = sTime.Day() if currentDay != prevDay { if prevDay != 0 && logFp != nil { logFp.Close() } createPanicLog(sTime, panicMsg) prevDay = currentDay } if logFp != nil { logPanic(panicMsg, serviceID, requestURI, curTime) } else { fmt.Println("lost the pointer to the log file, creating again") createPanicLog(sTime, panicMsg) logPanic(panicMsg, serviceID, requestURI, curTime) } } else { panic(panicMsg) } }
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) } } } }