2022-07-03 22:01:41 +08:00
|
|
|
package types
|
|
|
|
|
|
|
|
import (
|
2022-07-25 19:27:14 +08:00
|
|
|
"strings"
|
2022-07-03 22:01:41 +08:00
|
|
|
"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"`
|
2022-07-03 22:01:41 +08:00
|
|
|
Labels map[string]string `json:"labels"`
|
|
|
|
}
|
|
|
|
|
2022-07-25 19:27:14 +08:00
|
|
|
var metricReplacer = strings.NewReplacer("-", "_", ".", "_", " ", "_", "'", "_", "\"", "_")
|
2022-07-03 22:01:41 +08:00
|
|
|
|
2022-07-25 19:27:14 +08:00
|
|
|
func NewSample(prefix, metric string, value interface{}, labels ...map[string]string) *Sample {
|
2022-07-03 22:01:41 +08:00
|
|
|
s := &Sample{
|
|
|
|
Metric: metric,
|
2022-07-25 19:27:14 +08:00
|
|
|
Value: value,
|
2022-07-03 22:01:41 +08:00
|
|
|
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)
|
2022-07-03 22:01:41 +08:00
|
|
|
}
|
|
|
|
|
2022-07-26 16:15:55 +08:00
|
|
|
for i := 0; i < len(labels); i++ {
|
|
|
|
for k, v := range labels[i] {
|
|
|
|
s.Labels[k] = v
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-07-03 22:01:41 +08:00
|
|
|
return s
|
|
|
|
}
|