This commit is contained in:
jiangyong 2026-11-18 00:42:41 +08:00
parent 24ddf2fe42
commit 3f241610d0
2 changed files with 32 additions and 4 deletions

View File

@ -9,6 +9,7 @@ func Value(name string, value int64, tags map[string]string) {
return
}
serv.add(&metric{
Type: metricTypeValue,
Metric: name,
Value: value,
Count: 1,
@ -16,3 +17,16 @@ func Value(name string, value int64, tags map[string]string) {
Timestamp: time.Now().UnixMilli(),
})
}
func Count(name string, tags map[string]string) {
if serv == nil {
return
}
serv.add(&metric{
Type: metricTypeCount,
Count: 1,
Metric: name,
Tags: tags,
Timestamp: time.Now().UnixMilli(),
})
}

View File

@ -15,7 +15,13 @@ import (
const reportBatchSize = 500
const (
metricTypeValue = "value"
metricTypeCount = "count"
)
type metric struct {
Type string `json:"type"`
Timestamp int64 `json:"timestamp"`
Metric string `json:"metric"`
Count int64 `json:"count"`
@ -196,11 +202,19 @@ func (s *service) retain(items []*aggregateItem) {
}
func (s *service) reportFields(m *metric) map[string]any {
var avg int64 = 0
fields := make(map[string]any)
switch m.Type {
case metricTypeCount:
fields["count"] = m.Count
case metricTypeValue:
fields["value"] = m.Value
fields["count"] = m.Count
if m.Count > 0 && m.Value != 0 {
avg = m.Value / m.Count
fields["avg"] = m.Value / m.Count
}
return map[string]any{"count": m.Count, "value": m.Value, "avg": avg}
}
return fields
}
func (s *service) send(points []*reportPoint) error {