categraf/inputs/inputs.go

71 lines
1.4 KiB
Go
Raw Normal View History

package inputs
import (
2022-04-17 12:54:32 +08:00
"flashcat.cloud/categraf/config"
2022-04-16 23:29:59 +08:00
"flashcat.cloud/categraf/pkg/conv"
"flashcat.cloud/categraf/types"
2022-04-25 15:34:15 +08:00
"github.com/toolkits/pkg/container/list"
)
type Input interface {
2022-04-15 12:35:45 +08:00
Init() error
2022-04-18 17:35:16 +08:00
Drop()
2022-04-16 16:52:23 +08:00
GetInputName() string
2022-04-17 12:54:32 +08:00
GetInterval() config.Duration
2022-04-25 15:34:15 +08:00
Gather(slist *list.SafeList)
}
type Creator func() Input
var InputCreators = map[string]Creator{}
func Add(name string, creator Creator) {
InputCreators[name] = creator
}
2022-04-14 18:34:27 +08:00
2022-04-19 18:09:16 +08:00
func NewSample(metric string, value interface{}, labels ...map[string]string) *types.Sample {
floatValue, err := conv.ToFloat64(value)
if err != nil {
return nil
}
2022-04-14 18:34:27 +08:00
s := &types.Sample{
Metric: metric,
2022-04-19 18:09:16 +08:00
Value: floatValue,
2022-04-14 18:34:27 +08:00
Labels: make(map[string]string),
}
for i := 0; i < len(labels); i++ {
for k, v := range labels[i] {
s.Labels[k] = v
}
}
return s
}
2022-04-15 11:00:18 +08:00
func NewSamples(fields map[string]interface{}, labels ...map[string]string) []*types.Sample {
count := len(fields)
samples := make([]*types.Sample, 0, count)
for metric, value := range fields {
2022-04-16 23:29:59 +08:00
floatValue, err := conv.ToFloat64(value)
2022-04-15 11:00:18 +08:00
if err != nil {
continue
}
2022-04-15 11:03:33 +08:00
samples = append(samples, NewSample(metric, floatValue, labels...))
2022-04-15 11:00:18 +08:00
}
return samples
}
2022-04-25 15:34:15 +08:00
func PushSamples(slist *list.SafeList, fields map[string]interface{}, labels ...map[string]string) {
for metric, value := range fields {
floatValue, err := conv.ToFloat64(value)
if err != nil {
continue
}
slist.PushFront(NewSample(metric, floatValue, labels...))
}
}