/
stomp.go
98 lines (84 loc) · 2.2 KB
/
stomp.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
//go:generate ../../../tools/readme_config_includer/generator
package stomp
import (
"crypto/tls"
_ "embed"
"fmt"
"net"
"time"
"github.com/go-stomp/stomp"
"github.com/influxdata/telegraf"
"github.com/influxdata/telegraf/config"
commontls "github.com/influxdata/telegraf/plugins/common/tls"
"github.com/influxdata/telegraf/plugins/outputs"
"github.com/influxdata/telegraf/plugins/serializers"
)
//go:embed sample.conf
var sampleConfig string
type STOMP struct {
Host string `toml:"host"`
Username string `toml:"username"`
Password string `toml:"password"`
QueueName string `toml:"queueName"`
Log telegraf.Logger `toml:"-"`
HeartBeatSend config.Duration `toml:"heartbeat_timeout_send"`
HeartBeatRec config.Duration `toml:"heartbeat_timeout_receive"`
commontls.ClientConfig
conn net.Conn
stomp *stomp.Conn
serialize serializers.Serializer
}
func (q *STOMP) Connect() error {
tlsConfig, err := q.ClientConfig.TLSConfig()
if err != nil {
return err
}
if tlsConfig != nil {
q.conn, err = tls.Dial("tcp", q.Host, tlsConfig)
if err != nil {
return err
}
} else {
q.conn, err = net.Dial("tcp", q.Host)
if err != nil {
return err
}
}
q.stomp, err = stomp.Connect(q.conn, stomp.ConnOpt.HeartBeat(time.Duration(q.HeartBeatSend), time.Duration(q.HeartBeatRec)), stomp.ConnOpt.Login(q.Username, q.Password))
if err != nil {
return err
}
q.Log.Debug("STOMP Connected...")
return nil
}
func (q *STOMP) SetSerializer(serializer serializers.Serializer) {
q.serialize = serializer
}
func (q *STOMP) Write(metrics []telegraf.Metric) error {
for _, metric := range metrics {
values, err := q.serialize.Serialize(metric)
if err != nil {
q.Log.Errorf("Serializing metric %v failed: %s", metric, err)
continue
}
err = q.stomp.Send(q.QueueName, "text/plain", values, nil)
if err != nil {
return fmt.Errorf("sending metric failed: %s", err)
}
}
return nil
}
func (q *STOMP) SampleConfig() string {
return sampleConfig
}
func (q *STOMP) Close() error {
return q.stomp.Disconnect()
}
func init() {
outputs.Add("stomp", func() telegraf.Output {
return &STOMP{
Host: "localhost:61613",
QueueName: "telegraf",
}
})
}