Beispiel #1
0
func (r *Raper) saveBindData(c DomainBindInfo) {

	var newBindData map[string][]string = make(map[string][]string)

	for _, v1 := range c.Data {
		for _, v2 := range v1 {
			domain := v2.Domain
			path := v2.Path
			if ok := utils.StrInStrings(path, newBindData[domain]); !ok {
				newBindData[domain] = append(newBindData[domain], path)
			}

		}
	}

	r.mutexBindPath.Lock()
	if ok := utils.MapStrStringsCmp(r.domainBindPath, newBindData); !ok {
		logger.Println("save new domain bind data")

		r.domainBindPath = newBindData

	}
	r.mutexBindPath.Unlock()

	var domains []string

	for k := range r.domainBindPath {
		domains = append(domains, k)
	}
	for _, d := range domains {
		r.mutexBindPath.RLock()
		a := make([]string, len(r.domainBindPath[d]))
		copy(a, r.domainBindPath[d])
		r.mutexBindPath.RUnlock()
		bcc := sorted.ByCharCount(a, "/")
		sort.Sort(bcc)
		r.mutexBindPath.Lock()
		r.domainBindPath[d] = bcc.List
		r.mutexBindPath.Unlock()
	}

}
Beispiel #2
0
func (r *Raper) distDomainMertic() {

	r.domainsChanMap = make(map[string]chan domainMetricTsdbMap)
	for {
		select {
		case AD := <-r.aggreMessageChan:
			domain, data := AD.domain, AD.data
			dataInfo := make(domainMetricTsdbMap)
			r.mutexDomains.Lock()
			if !utils.StrInStrings(domain, r.domains) {
				r.domains = append(r.domains, domain)
			}
			r.mutexDomains.Unlock()

			key, tags := "", make(map[string]string)

			configTags := r.getDomainTags(domain)

			tags, key = utils.TagsToKey(configTags, data)

			length, err := utils.ParseFloat64(data["length"])
			if err != nil {
				logger.Printf("parse length error: %+v\n", data)
				break
			}

			requestTime, err := utils.ParseFloat64(data["reqtime"])
			if err != nil {
				logger.Printf("request time parse error: %+v", data)
				requestTime = 0
			}

			upstreamTime, err := utils.ParseFloat64(data["upstream_resptime"])
			if err != nil {
				upstreamTime = requestTime
			}

			/*
				code 408 means server wait for client request timeout, so ignore here.
			*/
			if c := data["code"]; c == "408" || c == "499" && requestTime < config.Code499Timeout {
				break
			}

			for _, v := range config.TotalUrlMetric {
				k := key + "|" + v
				if _, ok := dataInfo[k]; !ok {
					dataInfo[k] = &TsdbPutInfo{
						Metric: v,
						Tags:   tags,
					}
				}
			}
			dataInfo[key+"|"+config.UrlQpsMetric].Value += 1 / float64(config.UploadFrequency)
			dataInfo[key+"|"+config.UrlCodeMetric].Value += 1
			dataInfo[key+"|"+config.UrlUpstreamMetric].Value += upstreamTime * 1000
			dataInfo[key+"|"+config.UrlTimeMetric].Value += requestTime * 1000
			dataInfo[key+"|"+config.UrlTrafficMetric].Value += length / float64(config.UploadFrequency)

			var channel chan domainMetricTsdbMap
			r.mutexDomainMap.RLock()
			_, ok := r.domainsChanMap[domain]
			r.mutexDomainMap.RUnlock()
			if !ok {
				r.mutexDomainMap.Lock()
				_, ok := r.domainsChanMap[domain]
				if !ok {
					r.domainsChanMap[domain] = make(chan domainMetricTsdbMap)
					go r.updateDomainMetric(domain, r.domainsChanMap[domain])
				}
				channel = r.domainsChanMap[domain]
				r.mutexDomainMap.Unlock()

			} else {
				r.mutexDomainMap.RLock()
				channel = r.domainsChanMap[domain]
				r.mutexDomainMap.RUnlock()
			}

			channel <- dataInfo
		case <-r.Dying():
			return
		}
	}

}