forked from influxdata/telegraf
-
Notifications
You must be signed in to change notification settings - Fork 0
/
nats.go
128 lines (101 loc) · 2.69 KB
/
nats.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
package nats
import (
"fmt"
"log"
"strings"
"github.com/influxdata/telegraf"
"github.com/influxdata/telegraf/plugins/common/tls"
"github.com/influxdata/telegraf/plugins/outputs"
"github.com/influxdata/telegraf/plugins/serializers"
"github.com/nats-io/nats.go"
)
type NATS struct {
Servers []string `toml:"servers"`
Secure bool `toml:"secure"`
Username string `toml:"username"`
Password string `toml:"password"`
Credentials string `toml:"credentials"`
Subject string `toml:"subject"`
tls.ClientConfig
conn *nats.Conn
serializer serializers.Serializer
}
var sampleConfig = `
## URLs of NATS servers
servers = ["nats://localhost:4222"]
## Optional credentials
# username = ""
# password = ""
## Optional NATS 2.0 and NATS NGS compatible user credentials
# credentials = "/etc/telegraf/nats.creds"
## NATS subject for producer messages
subject = "telegraf"
## Use Transport Layer Security
# secure = false
## Optional TLS Config
# tls_ca = "/etc/telegraf/ca.pem"
# tls_cert = "/etc/telegraf/cert.pem"
# tls_key = "/etc/telegraf/key.pem"
## Use TLS but skip chain & host verification
# insecure_skip_verify = false
## Data format to output.
## Each data format has its own unique set of configuration options, read
## more about them here:
## https://github.com/influxdata/telegraf/blob/master/docs/DATA_FORMATS_OUTPUT.md
data_format = "influx"
`
func (n *NATS) SetSerializer(serializer serializers.Serializer) {
n.serializer = serializer
}
func (n *NATS) Connect() error {
var err error
opts := []nats.Option{
nats.MaxReconnects(-1),
}
// override authentication, if any was specified
if n.Username != "" {
opts = append(opts, nats.UserInfo(n.Username, n.Password))
}
if n.Secure {
tlsConfig, err := n.ClientConfig.TLSConfig()
if err != nil {
return err
}
opts = append(opts, nats.Secure(tlsConfig))
}
// try and connect
n.conn, err = nats.Connect(strings.Join(n.Servers, ","), opts...)
return err
}
func (n *NATS) Close() error {
n.conn.Close()
return nil
}
func (n *NATS) SampleConfig() string {
return sampleConfig
}
func (n *NATS) Description() string {
return "Send telegraf measurements to NATS"
}
func (n *NATS) Write(metrics []telegraf.Metric) error {
if len(metrics) == 0 {
return nil
}
for _, metric := range metrics {
buf, err := n.serializer.Serialize(metric)
if err != nil {
log.Printf("D! [outputs.nats] Could not serialize metric: %v", err)
continue
}
err = n.conn.Publish(n.Subject, buf)
if err != nil {
return fmt.Errorf("FAILED to send NATS message: %s", err)
}
}
return nil
}
func init() {
outputs.Add("nats", func() telegraf.Output {
return &NATS{}
})
}