This commit is contained in:
jiangyong 2026-11-13 16:12:09 +08:00
parent d5c2b9ed5b
commit 645ab41b62
5 changed files with 149 additions and 130 deletions

1
go.mod
View File

@ -10,7 +10,6 @@ require (
github.com/go-redis/redis v6.15.9+incompatible github.com/go-redis/redis v6.15.9+incompatible
github.com/gomodule/redigo v1.9.2 github.com/gomodule/redigo v1.9.2
github.com/google/uuid v1.6.0 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/minio/minio-go v6.0.14+incompatible
github.com/qiniu/go-sdk/v7 v7.21.0 github.com/qiniu/go-sdk/v7 v7.21.0
github.com/sirupsen/logrus v1.9.4 github.com/sirupsen/logrus v1.9.4

View File

@ -1,24 +1,23 @@
package metric package metric
import (
"errors"
)
var ( var (
serv *service serv *service
TypeCount = "count"
TypeDuration = "duration"
) )
type Config struct { type Config struct {
Address string Address string
Username string Token string
Password string
Database string
Interval int Interval int
} }
func NewConfg() *Config { func NewConfg() *Config {
return &Config{ return &Config{
Address: "http://10.0.2.157:8086", Address: "http://t.batiao8.com",
Interval: 10, Interval: 10,
Database: "telegraf",
} }
} }
@ -26,8 +25,20 @@ func Init(c *Config) error {
if serv != nil { if serv != nil {
return 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() go serv.run()
return nil return nil
} }

View File

@ -4,31 +4,15 @@ import (
"time" "time"
) )
func Count(name string, value int64, tag map[string]string) { func Value(name string, value int64, tags map[string]string) {
if serv == nil { if serv == nil {
return return
} }
serv.add(&metric{ serv.add(&metric{
Type: TypeCount,
Metric: name, Metric: name,
Value: value, Value: value,
Count: 1, Count: 1,
Tags: tag, Tags: tags,
Timestamp: time.Now().Unix(), Timestamp: time.Now().UnixMilli(),
})
}
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(),
}) })
} }

View File

@ -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",
})
}

View File

@ -1,48 +1,52 @@
package metric package metric
import ( import (
"encoding/json"
"fmt" "fmt"
influxdb "github.com/influxdata/influxdb1-client"
"net/url"
"os" "os"
"sort" "sort"
"strconv"
"strings" "strings"
"time" "time"
"git.u8t.cn/open/gosdk/util"
log "github.com/sirupsen/logrus"
) )
const reportBatchSize = 500
type metric struct { type metric struct {
Type string `json:"type"`
Timestamp int64 `json:"timestamp"` Timestamp int64 `json:"timestamp"`
Metric string `json:"metric"` Metric string `json:"metric"`
Count int64 `json:"count"` Count int64 `json:"count"`
Value int64 `json:"value"` Value int64 `json:"value"`
Max int64 `json:"max"`
Min int64 `json:"min"`
Tags map[string]string `json:"tags"` 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 { type service struct {
hostname string hostname string
serviceName string serviceName string
hostIp string hostIp string
podIp string podIp string
metrics chan *metric metrics chan *metric
megers map[string]*metric aggregates map[string]*metric
config *Config config *Config
client *influxdb.Client
} }
func NewService(c *Config) *service { func newService(c *Config) *service {
hostname, _ := os.Hostname() 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{ return &service{
metrics: make(chan *metric, 100000), metrics: make(chan *metric, 100000),
hostname: hostname, hostname: hostname,
@ -50,12 +54,13 @@ func NewService(c *Config) *service {
podIp: os.Getenv("POD_IP"), podIp: os.Getenv("POD_IP"),
hostIp: os.Getenv("HOST_IP"), hostIp: os.Getenv("HOST_IP"),
config: c, config: c,
client: client,
} }
} }
func (s *service) run() { func (s *service) run() {
timer := time.NewTicker(time.Duration(s.config.Interval) * time.Second) timer := time.NewTicker(time.Duration(s.config.Interval) * time.Second)
defer timer.Stop()
for { for {
select { select {
case m := <-s.metrics: case m := <-s.metrics:
@ -68,7 +73,7 @@ func (s *service) run() {
} }
func (s *service) sortMap(tag map[string]string) string { func (s *service) sortMap(tag map[string]string) string {
arr := make([]string, 0) arr := make([]string, 0, len(tag))
for k, v := range tag { for k, v := range tag {
arr = append(arr, fmt.Sprintf("%s=%s", k, v)) 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) { func (s *service) process(m *metric) {
if s.megers == nil { if s.aggregates == nil {
s.megers = make(map[string]*metric) s.aggregates = make(map[string]*metric)
} }
key := m.Metric key := m.Metric
if m.Tags != nil { if m.Tags != nil {
key += "_" + s.sortMap(m.Tags) key += "_" + s.sortMap(m.Tags)
} }
if v, ok := s.megers[key]; ok { if v, ok := s.aggregates[key]; ok {
v.Value += m.Value v.Value += m.Value
v.Count += m.Count v.Count += m.Count
v.Timestamp = m.Timestamp v.Timestamp = m.Timestamp
if m.Value > v.Max {
v.Max = m.Value
}
if m.Value < v.Min {
v.Value = m.Min
}
return return
} }
m.Max = m.Value s.aggregates[key] = m
m.Min = m.Value
s.megers[key] = m
} }
func (s *service) defaultTags() map[string]string { 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() { func (s *service) report() {
if s.megers == nil { if len(s.aggregates) == 0 {
return return
} }
aggregates := s.aggregates
s.aggregates = nil
var bp influxdb.BatchPoints items := make([]*aggregateItem, 0, len(aggregates))
for _, v := range s.megers { for key, v := range aggregates {
var p influxdb.Point items = append(items, &aggregateItem{key: key, metric: v})
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)
} }
bp.Database = s.config.Database for start := 0; start < len(items); start += reportBatchSize {
bp.Tags = s.defaultTags() 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) func (s *service) toPoint(m *metric) *reportPoint {
s.megers = nil 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) { func (s *service) add(m *metric) {
select { select {
case s.metrics <- m: case s.metrics <- m:
default: 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
}