categraf/types/sample.go

32 lines
685 B
Go
Raw Normal View History

package types
import (
2022-07-25 19:27:14 +08:00
"strings"
"time"
)
type Sample struct {
Metric string `json:"metric"`
Timestamp time.Time `json:"timestamp"`
2022-07-25 19:27:14 +08:00
Value interface{} `json:"value"`
Labels map[string]string `json:"labels"`
}
2022-07-25 19:27:14 +08:00
var metricReplacer = strings.NewReplacer("-", "_", ".", "_", " ", "_", "'", "_", "\"", "_")
2022-07-25 19:27:14 +08:00
func NewSample(prefix, metric string, value interface{}, labels ...map[string]string) *Sample {
s := &Sample{
Metric: metric,
2022-07-25 19:27:14 +08:00
Value: value,
Labels: make(map[string]string),
}
2022-07-25 19:27:14 +08:00
if len(prefix) > 0 {
s.Metric = prefix + "_" + metricReplacer.Replace(s.Metric)
} else {
s.Metric = metricReplacer.Replace(s.Metric)
}
return s
}