diff --git a/go.mod b/go.mod index fcbdda8..e371ed8 100644 --- a/go.mod +++ b/go.mod @@ -10,7 +10,6 @@ require ( github.com/go-redis/redis v6.15.9+incompatible github.com/gomodule/redigo v1.9.2 github.com/google/uuid v1.6.0 - github.com/influxdata/influxdb1-client v0.0.0-20220302092344-a9ab5670611c github.com/minio/minio-go v6.0.14+incompatible github.com/qiniu/go-sdk/v7 v7.21.0 github.com/sirupsen/logrus v1.9.4 diff --git a/metric/config.go b/metric/config.go index 6db43b7..502a411 100644 --- a/metric/config.go +++ b/metric/config.go @@ -1,24 +1,23 @@ package metric +import ( + "errors" +) + var ( - serv *service - TypeCount = "count" - TypeDuration = "duration" + serv *service ) type Config struct { Address string - Username string - Password string - Database string + Token string Interval int } func NewConfg() *Config { return &Config{ - Address: "http://10.0.2.157:8086", + Address: "http://t.batiao8.com", Interval: 10, - Database: "telegraf", } } @@ -26,8 +25,20 @@ func Init(c *Config) error { if serv != nil { return nil } + if c == nil { + return errors.New("metric config is nil") + } + if c.Address == "" { + return errors.New("metric address is required") + } + if c.Token == "" { + return errors.New("metric token is required") + } + if c.Interval <= 0 { + c.Interval = 10 + } - serv = NewService(c) + serv = newService(c) go serv.run() return nil } diff --git a/metric/metric.go b/metric/metric.go index 51670a1..1725340 100644 --- a/metric/metric.go +++ b/metric/metric.go @@ -4,31 +4,15 @@ import ( "time" ) -func Count(name string, value int64, tag map[string]string) { +func Value(name string, value int64, tags map[string]string) { if serv == nil { return } - serv.add(&metric{ - Type: TypeCount, Metric: name, Value: value, Count: 1, - Tags: tag, - Timestamp: time.Now().Unix(), - }) -} - -func Duration(name string, costMs int64, tag map[string]string) { - if serv == nil { - return - } - serv.add(&metric{ - Type: TypeDuration, - Metric: name, - Value: costMs, - Count: 1, - Tags: tag, - Timestamp: time.Now().Unix(), + Tags: tags, + Timestamp: time.Now().UnixMilli(), }) } diff --git a/metric/metric_test.go b/metric/metric_test.go deleted file mode 100644 index 2800c66..0000000 --- a/metric/metric_test.go +++ /dev/null @@ -1,36 +0,0 @@ -package metric - -import ( - "math/rand" - "testing" - "time" -) - -func TestName(t *testing.T) { - c := NewConfg() - c.Address = "http://influx.dev.batiao8.com" - c.Username = "batiao" - c.Password = "Batiao12B" - - c.Interval = 1 - Init(c) - for i := 0; i < 100; i++ { - Duration("test.test.a1", int64(rand.Int()%100), map[string]string{ - "a": "b", - "c": "d", - }) - time.Sleep(time.Second) - } -} - -func TestMetric(t *testing.T) { - c := NewConfg() - c.Address = "https://monitor.batiao8.com" - Init(c) - - Count("test.test.b", 12, map[string]string{ - "a": "b", - "c": "d", - }) - -} diff --git a/metric/service.go b/metric/service.go index 8d24e3b..d19b890 100644 --- a/metric/service.go +++ b/metric/service.go @@ -1,48 +1,52 @@ package metric import ( + "encoding/json" "fmt" - influxdb "github.com/influxdata/influxdb1-client" - "net/url" "os" "sort" + "strconv" "strings" "time" + + "git.u8t.cn/open/gosdk/util" + log "github.com/sirupsen/logrus" ) +const reportBatchSize = 500 + type metric struct { - Type string `json:"type"` Timestamp int64 `json:"timestamp"` Metric string `json:"metric"` Count int64 `json:"count"` Value int64 `json:"value"` - Max int64 `json:"max"` - Min int64 `json:"min"` 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 - megers map[string]*metric + aggregates map[string]*metric config *Config - client *influxdb.Client } -func NewService(c *Config) *service { +func newService(c *Config) *service { hostname, _ := os.Hostname() - infUrl, _ := url.Parse(c.Address) - infCfg := influxdb.NewConfig() - infCfg.Username = c.Username - infCfg.Password = c.Password - infCfg.URL = *infUrl - - client, _ := influxdb.NewClient(infCfg) - return &service{ metrics: make(chan *metric, 100000), hostname: hostname, @@ -50,12 +54,13 @@ func NewService(c *Config) *service { podIp: os.Getenv("POD_IP"), hostIp: os.Getenv("HOST_IP"), config: c, - client: client, } } func (s *service) run() { timer := time.NewTicker(time.Duration(s.config.Interval) * time.Second) + defer timer.Stop() + for { select { case m := <-s.metrics: @@ -68,7 +73,7 @@ func (s *service) run() { } func (s *service) sortMap(tag map[string]string) string { - arr := make([]string, 0) + arr := make([]string, 0, len(tag)) for k, v := range tag { arr = append(arr, fmt.Sprintf("%s=%s", k, v)) } @@ -77,30 +82,22 @@ func (s *service) sortMap(tag map[string]string) string { } func (s *service) process(m *metric) { - if s.megers == nil { - s.megers = make(map[string]*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.megers[key]; ok { + if v, ok := s.aggregates[key]; ok { v.Value += m.Value v.Count += m.Count v.Timestamp = m.Timestamp - if m.Value > v.Max { - v.Max = m.Value - } - if m.Value < v.Min { - v.Value = m.Min - } return } - m.Max = m.Value - m.Min = m.Value - s.megers[key] = m + s.aggregates[key] = m } func (s *service) defaultTags() map[string]string { @@ -112,53 +109,117 @@ func (s *service) defaultTags() map[string]string { } } +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 s.megers == nil { + if len(s.aggregates) == 0 { return } + aggregates := s.aggregates + s.aggregates = nil - var bp influxdb.BatchPoints - for _, v := range s.megers { - var p influxdb.Point - p.Measurement = v.Metric - p.Tags = v.Tags - p.Fields = s.getField(v) - p.Time = time.Unix(v.Timestamp, 0) - bp.Points = append(bp.Points, p) + items := make([]*aggregateItem, 0, len(aggregates)) + for key, v := range aggregates { + items = append(items, &aggregateItem{key: key, metric: v}) } - bp.Database = s.config.Database - bp.Tags = s.defaultTags() + 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) + } + } +} - s.client.Write(bp) - s.megers = nil +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: - fmt.Println("chan is full") + log.Errorf("metric chan is full") } } - -func (s *service) getField(m *metric) map[string]interface{} { - - if m.Type == TypeCount { - return map[string]interface{}{ - "count": m.Value, - } - } - - if m.Type == TypeDuration { - avgMs := int64(float64(m.Value) / float64(m.Count)) - return map[string]interface{}{ - "avg": avgMs, - "count": m.Count, - "max": m.Max, - "min": m.Min, - } - } - - return nil -}