-
Notifications
You must be signed in to change notification settings - Fork 228
/
writers.go
157 lines (132 loc) · 3.24 KB
/
writers.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
package writer
import (
"fmt"
"log"
"sort"
"strings"
"sync"
"time"
"github.com/prometheus/prometheus/prompb"
"flashcat.cloud/categraf/config"
"flashcat.cloud/categraf/types"
)
// Writers manage all writers and metric queue
type Writers struct {
writerMap map[string]Writer
queue *types.SafeListLimited[*prompb.TimeSeries]
}
var writers Writers
func InitWriters() error {
writerMap := map[string]Writer{}
opts := config.Config.Writers
for _, opt := range opts {
writer, err := newWriter(opt)
if err != nil {
return err
}
writerMap[opt.Url] = writer
}
writers = Writers{
writerMap: writerMap,
queue: types.NewSafeListLimited[*prompb.TimeSeries](config.Config.WriterOpt.ChanSize),
}
go writers.LoopRead()
return nil
}
func (ws Writers) LoopRead() {
for {
series := ws.queue.PopBackN(config.Config.WriterOpt.Batch)
if len(series) == 0 {
time.Sleep(time.Millisecond * 400)
continue
}
items := make([]prompb.TimeSeries, len(series))
for i := 0; i < len(series); i++ {
items[i] = *series[i]
}
WriteTimeSeries(items)
}
}
// WriteSample convert sample to prompb.TimeSeries and write to queue
// Note: Use WriteSamples for batch write for better performance
func WriteSample(sample *types.Sample) {
if sample == nil {
return
}
if config.Config.TestMode {
printTestMetric(sample)
return
}
if config.Config.DebugMode {
printTestMetric(sample)
}
item := sample.ConvertTimeSeries(config.Config.Global.Precision)
if item == nil || len(item.Labels) == 0 {
return
}
writers.queue.PushFront(item)
}
// WriteSamples convert samples to []prompb.TimeSeries and batch write to queue
func WriteSamples(samples []*types.Sample) {
if len(samples) == 0 {
return
}
if config.Config.TestMode {
printTestMetrics(samples)
return
}
if config.Config.DebugMode {
printTestMetrics(samples)
}
items := make([]*prompb.TimeSeries, 0, len(samples))
for _, sample := range samples {
item := sample.ConvertTimeSeries(config.Config.Global.Precision)
if item == nil || len(item.Labels) == 0 {
continue
}
items = append(items, item)
}
flag := writers.queue.PushFrontN(items)
if !flag {
log.Printf("E! write %d samples failed, please increase queue size(%d)", len(items), writers.queue.Len())
}
}
// WriteTimeSeries write prompb.TimeSeries to all writers
func WriteTimeSeries(timeSeries []prompb.TimeSeries) {
if len(timeSeries) == 0 {
return
}
wg := sync.WaitGroup{}
for key := range writers.writerMap {
wg.Add(1)
go func(key string) {
defer wg.Done()
writers.writerMap[key].Write(timeSeries)
}(key)
}
wg.Wait()
}
func printTestMetrics(samples []*types.Sample) {
for _, sample := range samples {
printTestMetric(sample)
}
}
// printTestMetric print metric to stdout, only used in debug/test mode
func printTestMetric(sample *types.Sample) {
var sb strings.Builder
sb.WriteString(sample.Timestamp.Format("15:04:05"))
sb.WriteString(" ")
sb.WriteString(sample.Metric)
arr := make([]string, 0, len(sample.Labels))
for key, val := range sample.Labels {
arr = append(arr, fmt.Sprintf("%s=%v", key, val))
}
sort.Strings(arr)
for _, pair := range arr {
sb.WriteString(" ")
sb.WriteString(pair)
}
sb.WriteString(" ")
sb.WriteString(fmt.Sprint(sample.Value))
fmt.Println(sb.String())
}