func (r *Raper) updateDomainMetric(domain string, c chan domainMetricTsdbMap) { stamp, err := utils.CurrentStamp(config.UploadFrequency) if err != nil { logger.Fatalf("UploadFrequency must be integer times of 60, err:%d\n", config.UploadFrequency) } dataInfo := &specifiledDataInfo{ data: make(map[string]*TsdbPutInfo), sTime: stamp, } pathList := &domainPathList{ data: make(map[string]int), } go r.createAndUploadData(domain, pathList, dataInfo) for { select { case tsdb := <-c: for k, v := range tsdb { path := v.Tags["path"] if v.Metric == config.UrlCodeMetric { pathList.rmux.Lock() if _, ok := pathList.data[path]; !ok { pathList.data[path] = 1 } else { pathList.data[path]++ } pathList.rmux.Unlock() } dataInfo.mutex.Lock() if _, ok := dataInfo.data[k]; !ok { dataInfo.data[k] = v dataInfo.data[k].TimeStamp = dataInfo.sTime } else { dataInfo.data[k].Value += v.Value } dataInfo.mutex.Unlock() } case <-r.Dying(): return } } }
func (r *Raper) createAndUploadData(domain string, pathList *domainPathList, dataInfo *specifiledDataInfo) { hostName, _ := os.Hostname() var conftags []string for { select { case <-time.After(time.Duration(dataInfo.sTime-utils.CurrentMilliSecond()+int64(config.UploadFrequency)*1000) * time.Millisecond): dataInfo.mutex.Lock() tsdbMap := dataInfo.data dataInfo.data = make(map[string]*TsdbPutInfo) dataInfo.sTime, _ = utils.CurrentStamp(config.UploadFrequency) dataInfo.mutex.Unlock() pathList.rmux.Lock() countMap := pathList.data pathList.data = make(map[string]int) pathList.rmux.Unlock() finalTsdbMap := make(domainMetricTsdbMap) paths := utils.UTF8Filter(AggregationPath(countMap)) r.mutexBindPath.RLock() domainBinds := r.domainBindPath[domain] r.mutexBindPath.RUnlock() paths = utils.AppendListToList(domainBinds, paths) conftags = r.getDomainTags(domain) var total float64 for key, tsdb := range tsdbMap { oldPath := tsdb.Tags["path"] tsdb.Tags["path"], _ = utils.FindConfigPath(oldPath, paths) _, newKey := utils.TagsToKey(conftags, tsdb.Tags) parts := strings.Split(key, "|") lastPart := parts[len(parts)-1] finalKey := newKey + "|" + lastPart if lastPart == config.UrlCodeMetric { total += tsdb.Value } if _, ok := finalTsdbMap[finalKey]; ok { finalTsdbMap[finalKey].Value += tsdbMap[key].Value } else { finalTsdbMap[finalKey] = tsdbMap[key] } } logger.Printf("domain: %s, total: %d\n", domain, int64(total)) go func() { for _, v := range finalTsdbMap { var data string intPart, frac := math.Modf(v.Value) if frac == 0 { data = fmt.Sprintf("put %s %d %d consumer=%s", v.Metric, v.TimeStamp, int64(intPart), hostName) } else { data = fmt.Sprintf("put %s %d %.2f consumer=%s", v.Metric, v.TimeStamp, v.Value, hostName) } for key, value := range v.Tags { data += fmt.Sprintf(" %s=%s", key, value) } // put test.url.code 1440492540000 2000 domain=api.wandoujia.com path=/sre-test code=200 data += "\n" r.tsdb.MessageChan <- data } }() case <-r.Dying(): return } } }