/
plugin.go
161 lines (144 loc) · 3.65 KB
/
plugin.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
158
159
160
161
package statsd
import (
"fmt"
"time"
"github.com/gin-gonic/gin"
"github.com/influxdata/telegraf"
"github.com/influxdata/telegraf/agent"
"github.com/influxdata/telegraf/plugins/inputs/statsd"
"github.com/influxdata/telegraf/plugins/serializers/influx"
"github.com/sirupsen/logrus"
"github.com/taosdata/taosadapter/db/commonpool"
"github.com/taosdata/taosadapter/log"
"github.com/taosdata/taosadapter/monitor"
"github.com/taosdata/taosadapter/plugin"
"github.com/taosdata/taosadapter/schemaless/inserter"
)
var logger = log.GetLogger("statsd")
type Plugin struct {
conf Config
ac telegraf.Accumulator
input *statsd.Statsd
closeChan chan struct{}
metricChan chan telegraf.Metric
}
func (p *Plugin) Init(_ gin.IRouter) error {
p.conf.setValue()
if !p.conf.Enable {
logger.Info("statsd disabled")
return nil
}
return nil
}
func (p *Plugin) Start() error {
if !p.conf.Enable {
return nil
}
p.closeChan = make(chan struct{})
p.metricChan = make(chan telegraf.Metric, 2*p.conf.Worker)
for i := 0; i < p.conf.Worker; i++ {
go func() {
serializer := influx.NewSerializer()
for {
select {
case metric := <-p.metricChan:
p.HandleMetrics(serializer, metric)
case <-p.closeChan:
return
}
}
}()
}
p.input = &statsd.Statsd{
Protocol: p.conf.Protocol,
ServiceAddress: fmt.Sprintf(":%d", p.conf.Port),
MaxTCPConnections: p.conf.MaxTCPConnections,
TCPKeepAlive: p.conf.TCPKeepAlive,
AllowedPendingMessages: p.conf.AllowedPendingMessages,
DeleteCounters: p.conf.DeleteCounters,
DeleteGauges: p.conf.DeleteGauges,
DeleteSets: p.conf.DeleteSets,
DeleteTimings: p.conf.DeleteTimings,
Log: logger,
}
p.ac = agent.NewAccumulator(&MetricMaker{logger: logger}, p.metricChan)
err := p.input.Start(p.ac)
if err != nil {
return err
}
ticker := time.NewTicker(p.conf.GatherInterval)
go func() {
for {
select {
case <-ticker.C:
if monitor.AllPaused() {
return
}
err := p.input.Gather(p.ac)
if err != nil {
logger.WithError(err).Error("gather error")
}
case <-p.closeChan:
ticker.Stop()
ticker = nil
return
}
}
}()
return nil
}
func (p *Plugin) Stop() error {
if !p.conf.Enable {
return nil
}
p.input.Stop()
close(p.closeChan)
return nil
}
func (p *Plugin) String() string {
return "statsd"
}
func (p *Plugin) Version() string {
return "v1"
}
func (p *Plugin) HandleMetrics(serializer *influx.Serializer, metric telegraf.Metric) {
data, err := serializer.Serialize(metric)
if err != nil {
logger.WithError(err).Error("serialize statsd error")
return
}
taosConn, err := commonpool.GetConnection(p.conf.User, p.conf.Password)
if err != nil {
logger.WithError(err).Errorln("connect taosd error")
return
}
defer func() {
putErr := taosConn.Put()
if putErr != nil {
logger.WithError(putErr).Errorln("taos connect pool put error")
}
}()
start := time.Now()
logger.Debugln(start, "insert line", string(data))
result, err := inserter.InsertInfluxdb(taosConn.TaosConnection, data, p.conf.DB, "ns")
logger.Debugln("insert line finish cost:", time.Now().Sub(start), string(data))
if err != nil || result.FailCount != 0 {
logger.WithError(err).WithField("result", result).Errorln("insert lines error", string(data))
return
}
}
type MetricMaker struct {
logger logrus.FieldLogger
}
func (m *MetricMaker) LogName() string {
return "metric"
}
func (m *MetricMaker) MakeMetric(metric telegraf.Metric) telegraf.Metric {
return metric
}
func (m *MetricMaker) Log() telegraf.Logger {
return m.logger
}
func init() {
plugin.Register(&Plugin{})
}