package metric import ( "encoding/json" "fmt" "os" "sort" "strconv" "strings" "time" "git.u8t.cn/open/gosdk/util" log "github.com/sirupsen/logrus" ) const reportBatchSize = 500 type metric struct { Timestamp int64 `json:"timestamp"` Metric string `json:"metric"` Count int64 `json:"count"` Value int64 `json:"value"` Tags map[string]string `json:"tags"` } type reportPoint struct { Time string `json:"time"` Name string `json:"name"` Tags map[string]string `json:"tags"` Fields map[string]any `json:"fields"` } type reportRequest struct { Metric []*reportPoint `json:"metric"` } type service struct { hostname string serviceName string hostIp string podIp string metrics chan *metric aggregates map[string]*metric config *Config } func newService(c *Config) *service { hostname, _ := os.Hostname() return &service{ metrics: make(chan *metric, 100000), hostname: hostname, serviceName: os.Getenv("SERVICE_NAME"), podIp: os.Getenv("POD_IP"), hostIp: os.Getenv("HOST_IP"), config: c, } } func (s *service) run() { timer := time.NewTicker(time.Duration(s.config.Interval) * time.Second) defer timer.Stop() for { select { case m := <-s.metrics: s.process(m) case <-timer.C: s.report() } } } func (s *service) sortMap(tag map[string]string) string { arr := make([]string, 0, len(tag)) for k, v := range tag { arr = append(arr, fmt.Sprintf("%s=%s", k, v)) } sort.Strings(arr) return strings.Join(arr, ":") } func (s *service) process(m *metric) { if s.aggregates == nil { s.aggregates = make(map[string]*metric) } key := m.Metric if m.Tags != nil { key += "_" + s.sortMap(m.Tags) } if v, ok := s.aggregates[key]; ok { v.Value += m.Value v.Count += m.Count v.Timestamp = m.Timestamp return } s.aggregates[key] = m } func (s *service) defaultTags() map[string]string { return map[string]string{ "hostname": s.hostname, "host_ip": s.hostIp, "pod_ip": s.podIp, "service_name": s.serviceName, } } func (s *service) reportTags(tag map[string]string) map[string]string { tags := make(map[string]string) for k, v := range s.defaultTags() { if validTag(k, v) { tags[k] = v } } for k, v := range tag { if validTag(k, v) { tags[k] = v } } return tags } func validTag(key, value string) bool { return key != "" && key != "time" && key != "count" && key != "value" && value != "" && !strings.ContainsAny(key+value, "\r\n\x00") } type aggregateItem struct { key string metric *metric } func (s *service) report() { if len(s.aggregates) == 0 { return } aggregates := s.aggregates s.aggregates = nil items := make([]*aggregateItem, 0, len(aggregates)) for key, v := range aggregates { items = append(items, &aggregateItem{key: key, metric: v}) } for start := 0; start < len(items); start += reportBatchSize { end := start + reportBatchSize if end > len(items) { end = len(items) } chunk := items[start:end] points := make([]*reportPoint, 0, len(chunk)) for _, item := range chunk { points = append(points, s.toPoint(item.metric)) } if err := s.send(points); err != nil { log.Errorf("report metric error: %s", err.Error()) s.retain(chunk) } } } func (s *service) toPoint(m *metric) *reportPoint { return &reportPoint{ Time: strconv.FormatInt(m.Timestamp, 10), Name: m.Metric, Tags: s.reportTags(m.Tags), Fields: s.reportFields(m), } } func (s *service) retain(items []*aggregateItem) { if s.aggregates == nil { s.aggregates = make(map[string]*metric) } for _, item := range items { if v, ok := s.aggregates[item.key]; ok { v.Value += item.metric.Value v.Count += item.metric.Count v.Timestamp = item.metric.Timestamp continue } s.aggregates[item.key] = item.metric } } func (s *service) reportFields(m *metric) map[string]any { var avg int64 = 0 if m.Count > 0 && m.Value != 0 { avg = m.Value / m.Count } return map[string]any{"count": m.Count, "value": m.Value, "avg": avg} } func (s *service) send(points []*reportPoint) error { body, err := json.Marshal(&reportRequest{Metric: points}) if err != nil { return err } address := strings.TrimRight(s.config.Address, "/") + "/ext/metric" resp, err := util.HttpPostJson(address, map[string]string{"x-token": s.config.Token}, body) if err != nil { return err } var rsp util.Response if err := json.Unmarshal(resp, &rsp); err != nil { return fmt.Errorf("invalid response: %w", err) } if rsp.Code != 0 { return fmt.Errorf("%d:%s", rsp.Code, rsp.Message) } return nil } func (s *service) add(m *metric) { select { case s.metrics <- m: default: log.Errorf("metric chan is full") } }