aggr_rpc.go 11.3 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441
package statsd

import (
	"fmt"
	"sort"
	"strconv"
	"strings"
)

type rpcAggregator struct {
	histogramAggregator
	Counters map[string]float64
	Latencys map[string]float64
}

func (self *rpcAggregator) new(aggregatorNames []string) (aggregator, error) {
	if len(aggregatorNames) < 1 || aggregatorNames[0] != "rpc" {
		return nil, BadAggregatorNameError
	}

	histogramAggregatorNames := []string{"p99", "p95", "p75", "p50"}
	return &rpcAggregator{
		histogramAggregator: histogramAggregator{}.newInstence(histogramAggregatorNames),
		Counters:            map[string]float64{},
		Latencys:            map[string]float64{},
	}, nil
}

// ratio类型可以接受一个或多个(并包模式下) value, 有statusCode字段
// 形如 10.1,ok{"\u2318"}10.2,error{"\u2318"}20.8,ok
func (self *rpcAggregator) collect(values []string, metric string, argLines string) error {
	if len(values) < 1 {
		return fmt.Errorf("bad values")
	}

	for i := range values {
		cvalues := strings.Split(values[i], CodeDelimiter)
		if len(cvalues) < 2 {
			// bad values
			continue
		}

		err := self.histogramAggregator.collect(cvalues[:1], metric, argLines)
		if err != nil {
			return err
		}

		latency, err := strconv.ParseFloat(cvalues[0], 64)
		if err != nil {
			return err
		}

		code := cvalues[1]
		self.Counters[code] += 1

		self.Latencys[code] += latency
	}

	return nil
}

// @input
//		metric: $metric_name(不包含ns)
func (self *rpcAggregator) dump(points []*Point, timestamp int64,
	tags map[string]string, metric, argLines string) ([]*Point, error) {
	var (
		err error
	)

	// 无数据,则不dump点
	if len(self.Counters) == 0 {
		return points, nil
	}

	// 验证tag信息: 必须存在callee caller
	if _, ok := tags["caller"]; !ok {
		return points, nil
	}

	callee, ok := tags["callee"]
	if !ok {
		return points, nil
	}
	tags["callee"] = Func{}.TrimRpcCallee(callee) // 修改callee字段

	// 带tag的rpc统计, 指标名称调整为 by_tags.$metric
	//if len(tags) > 2 {
	//	metric = fmt.Sprintf("by_tags.%s", metric)
	//}

	totalCount := float64(0)
	totalErrorCount := float64(0)
	for code, count := range self.Counters {
		if !(Func{}.IsOk(code)) {
			myTags := map[string]string{}
			for k, v := range tags {
				myTags[k] = v
			}
			myTags["code"] = code
			points = append(points, &Point{
				Name:      metric + ".error.counter",
				Timestamp: timestamp,
				Tags:      myTags,
				Value:     count,
			})
			totalErrorCount += count
		}
		totalCount += count
	}
	points = append(points, &Point{
		Name:      metric + ".counter",
		Timestamp: timestamp,
		Tags:      tags,
		Value:     totalCount,
	})
	if totalCount > 0 {
		points = append(points, &Point{
			Name:      metric + ".error.ratio",
			Timestamp: timestamp,
			Tags:      tags,
			Value:     totalErrorCount / totalCount * 100,
		})
		myTags := map[string]string{}
		for k, v := range tags {
			myTags[k] = v
		}
		myTags["code"] = "<all>"
		points = append(points, &Point{
			Name:      metric + ".error.counter",
			Timestamp: timestamp,
			Tags:      myTags,
			Value:     totalErrorCount,
		})
	}

	// latency
	latencyMetric := fmt.Sprintf("%s.latency", metric)
	{ // avg
		totalLatency := float64(0)
		for _, latency := range self.Latencys {
			totalLatency += latency
		}
		avgLatency := float64(0)
		if totalCount > 0 && totalLatency > 0 {
			avgLatency = totalLatency / totalCount
		}

		myTags := map[string]string{}
		for k, v := range tags {
			myTags[k] = v
		}
		myTags["percentile"] = "avg"

		points = append(points, &Point{
			Name:      latencyMetric,
			Timestamp: timestamp,
			Tags:      myTags,
			Value:     avgLatency,
		})
	}
	points, err = self.histogramAggregator.dump(points, timestamp, tags, latencyMetric, argLines) // percentile

	return points, err
}

func (self *rpcAggregator) summarize(nsmetric, argLines string, newAggrs map[string]aggregator) {
	items, _ := Func{}.TranslateMetricLine(nsmetric)
	//ns := items[0]
	metric := items[1]

	tags, _, err := Func{}.TranslateArgLines(argLines)
	if err != nil {
		return
	}

	// rpc_dirpc_call & rpc_dirpc_called
	if metric == MetricToBeSummarized_DirpcCallConst || metric == MetricToBeSummarized_DirpcCalledConst {
		if len(tags) != 5 {
			return
		}
		callee, _ := tags["callee"]
		calleef, _ := tags["callee-func"]
		caller, _ := tags["caller"]
		callerf, _ := tags["caller-func"]
		su, _ := tags["su"]
		if !(caller != "" && callerf != "" && callee != "" && calleef != "" && su != "") {
			return
		}

		formator := "callee=%s\ncallee-func=%s\ncaller=%s\ncaller-func=%s\nsu=%s\nrpc"
		if calleef != "<all>" {
			summarizedCalleef := fmt.Sprintf(formator, callee, "<all>", caller, callerf, su)
			rpcAggregator{}.addSummarizeAggregator(summarizedCalleef, self, newAggrs)
		}
		if callerf != "<all>" {
			summarizedCallerf := fmt.Sprintf(formator, callee, calleef, caller, "<all>", su)
			rpcAggregator{}.addSummarizeAggregator(summarizedCallerf, self, newAggrs)
		}
		if calleef != "<all>" && callerf != "<all>" {
			summarizedCalleefCallerf := fmt.Sprintf(formator, callee, "<all>", caller, "<all>", su)
			rpcAggregator{}.addSummarizeAggregator(summarizedCalleefCallerf, self, newAggrs)
		}

		return
	}

	// rpcdisf
	if metric == MetricToBeSummarized_RpcdisfConst {
		if len(tags) != 7 {
			return
		}
		callee, _ := tags["callee"]
		calleec, _ := tags["callee-cluster"]
		calleef, _ := tags["callee-func"]
		caller, _ := tags["caller"]
		callerc, _ := tags["caller-cluster"]
		callerf, _ := tags["caller-func"]
		su, _ := tags["su"]
		if !(caller != "" && callerc != "" && callerf != "" &&
			callee != "" && calleec != "" && calleef != "" && su != "") {
			return
		}

		formator := "callee=%s\ncallee-cluster=%s\ncallee-func=%s\ncaller=%s\ncaller-cluster=%s\ncaller-func=%s\nsu=%s\nrpc"
		if calleef != "<all>" {
			summarizedCalleef := fmt.Sprintf(formator, callee, calleec, "<all>", caller, callerc, callerf, su)
			rpcAggregator{}.addSummarizeAggregator(summarizedCalleef, self, newAggrs)
		}
		if callerf != "<all>" {
			summarizedCallerf := fmt.Sprintf(formator, callee, calleec, calleef, caller, callerc, "<all>", su)
			rpcAggregator{}.addSummarizeAggregator(summarizedCallerf, self, newAggrs)
		}
		summarizedCalleefCallerf := fmt.Sprintf(formator, callee, calleec, "<all>", caller, callerc, "<all>", su)
		rpcAggregator{}.addSummarizeAggregator(summarizedCalleefCallerf, self, newAggrs)

		return
	}

	// rpcdfe
	if metric == MetricToBeSummarized_RpcdfeConst {
		if len(tags) != 5 {
			return
		}
		callee, _ := tags["callee"]
		caller, _ := tags["caller"]
		domain, _ := tags["domain"]
		scheme, _ := tags["scheme"]
		upstream, _ := tags["upstream"]
		if !(callee != "" && caller != "" && domain != "" &&
			scheme != "" && upstream != "") {
			return
		}

		formator := "callee=%s\ncaller=%s\ndomain=%s\nscheme=%s\nupstream=%s\nrpc"
		if domain != "<all>" {
			summarizedDomain := fmt.Sprintf(formator, callee, caller, "<all>", scheme, upstream)
			rpcAggregator{}.addSummarizeAggregator(summarizedDomain, self, newAggrs)
		}
		if scheme != "<all>" {
			summarizedScheme := fmt.Sprintf(formator, callee, caller, domain, "<all>", upstream)
			rpcAggregator{}.addSummarizeAggregator(summarizedScheme, self, newAggrs)
		}
		if upstream != "<all>" {
			summarizedUpstream := fmt.Sprintf(formator, callee, caller, domain, scheme, "<all>")
			rpcAggregator{}.addSummarizeAggregator(summarizedUpstream, self, newAggrs)
		}
		summarizedDomainSchemeUp := fmt.Sprintf(formator, callee, caller, "<all>", "<all>", "<all>")
		rpcAggregator{}.addSummarizeAggregator(summarizedDomainSchemeUp, self, newAggrs)
		return
	}

	// 黑名单

	// 只做默认聚合
	self.doAggr(tags, newAggrs)
	// 本机聚合

	return
}

func (self *rpcAggregator) merge(toMerge aggregator) (aggregator, error) {
	that, ok := toMerge.(*rpcAggregator)
	if !ok {
		return nil, BadSummarizeAggregatorError
	}

	_, err := self.histogramAggregator.merge(&that.histogramAggregator)
	if err != nil {
		return nil, err
	}

	for k, v2 := range that.Counters {
		_, found := self.Counters[k]
		if found {
			self.Counters[k] += v2
		} else {
			self.Counters[k] = v2
		}
	}
	for k, v2 := range that.Latencys {
		_, found := self.Latencys[k]
		if found {
			self.Latencys[k] += v2
		} else {
			self.Latencys[k] = v2
		}
	}
	return self, nil
}

func (self *rpcAggregator) toMap() (map[string]interface{}, error) {
	counters := map[string]interface{}{}
	for k, v := range self.Counters {
		counters[k] = v
	}

	latencys := map[string]interface{}{}
	for k, v := range self.Latencys {
		latencys[k] = v
	}

	hm, err := self.histogramAggregator.toMap()
	if err != nil {
		return nil, err
	}
	return map[string]interface{}{
		"__aggregator__": "rpc",
		"counters":       counters,
		"latencys":       latencys,
		"histogram":      hm,
	}, nil
}

func (self rpcAggregator) fromMap(serialized map[string]interface{}) (aggregator, error) {
	aggregator := &rpcAggregator{Counters: map[string]float64{}, Latencys: map[string]float64{}}
	counters := (serialized["counters"]).(map[string]interface{})
	for k, v := range counters {
		aggregator.Counters[k] = v.(float64)
	}

	latencys := (serialized["latencys"]).(map[string]interface{})
	for k, v := range latencys {
		aggregator.Latencys[k] = v.(float64)
	}

	histogram := (serialized["histogram"]).(map[string]interface{})
	hm, err := self.histogramAggregator.fromMap(histogram)
	if err != nil {
		return nil, err
	}

	hmaggr, ok := hm.(*histogramAggregator)
	if !ok {
		return nil, BadDeserializeError
	}

	aggregator.histogramAggregator = *hmaggr
	return aggregator, nil
}

// internal functions
func (self rpcAggregator) addSummarizeAggregator(argLines string, toMerge *rpcAggregator, newAggrs map[string]aggregator) {
	aggr, ok := newAggrs[argLines]
	if !(ok && aggr != nil) {
		nAggr, err := toMerge.clone()
		if err == nil {
			newAggrs[argLines] = nAggr
		}
	} else {
		aggr.merge(toMerge)
	}
}

func (self *rpcAggregator) clone() (aggregator, error) {
	maps, err := self.toMap()
	if err != nil {
		return nil, err
	}

	aggr, err := rpcAggregator{}.fromMap(maps)
	if err != nil {
		return nil, err
	}

	return aggr, nil
}

func (self *rpcAggregator) doAggr(tags map[string]string, newAggrs map[string]aggregator, aggrTagksList ...[][]string) {
	tagks := make([]string, 0)
	for k, _ := range tags {
		tagks = append(tagks, k)
	}

	tagkNum := len(tagks)
	if tagkNum == 0 {
		return
	}
	sort.Strings(tagks)

	// get formator
	formator := ""
	for i := 0; i < tagkNum; i++ {
		formator += tagks[i] + "=%s\n"
	}
	formator += "rpc"

	// 聚合所有维度
	ntagvs_all := make([]interface{}, tagkNum)
	for i := 0; i < tagkNum; i++ {
		ntagvs_all[i] = "<all>"
	}
	summarizedTags := fmt.Sprintf(formator, ntagvs_all...)
	rpcAggregator{}.addSummarizeAggregator(summarizedTags, self, newAggrs)

	// 聚合指定维度
	if len(aggrTagksList) > 0 {
		for i := 0; i < len(aggrTagksList[0]); i++ {
			aggrTagks := aggrTagksList[0][i]
			// 判断合法性
			if !(len(aggrTagks) > 0 && len(aggrTagks) < tagkNum && // ==tagsNum 会造成 所有维度 的重复聚合
				(Func{}).IsSubKeys(aggrTagks, tags)) { // 监控数据 有 指定的聚合维度
				continue
			}
			// 聚合
			sometagks := make([]interface{}, tagkNum)
			for i, tk := range tagks {
				sometagks[i] = tags[tk]
			}
			for _, tk := range aggrTagks {
				for i := 0; i < tagkNum; i++ {
					if tk == tagks[i] {
						sometagks[i] = "<all>"
						break
					}
				}
			}
			summarizedTags := fmt.Sprintf(formator, sometagks...)
			rpcAggregator{}.addSummarizeAggregator(summarizedTags, self, newAggrs)
		}
	}
}