Use list of brokers in amqp output and amqp_consumer

This commit is contained in:
Daniel Nelson 2018-06-03 17:05:33 -07:00 committed by Daniel Nelson
parent e3f1d28908
commit 179bcfdcbb
4 changed files with 128 additions and 32 deletions

View File

@ -15,8 +15,18 @@ The following defaults are known to work with RabbitMQ:
```toml ```toml
# AMQP consumer plugin # AMQP consumer plugin
[[inputs.amqp_consumer]] [[inputs.amqp_consumer]]
## AMQP url ## Broker to consume from.
url = "amqp://localhost:5672/influxdb" ## deprecated in 1.7; use the brokers option
# url = "amqp://localhost:5672/influxdb"
## Brokers to consume from. If multiple brokers are specified a random broker
## will be selected anytime a connection is established. This can be
## helpful for load balancing when not using a dedicated load balancer.
brokers = ["amqp://localhost:5672/influxdb"]
## Authentication credentials for the PLAIN auth_method.
# username = ""
# password = ""
## Exchange to declare and consume from. ## Exchange to declare and consume from.
exchange = "telegraf" exchange = "telegraf"

View File

@ -1,8 +1,10 @@
package amqp_consumer package amqp_consumer
import ( import (
"errors"
"fmt" "fmt"
"log" "log"
"math/rand"
"strings" "strings"
"sync" "sync"
"time" "time"
@ -17,8 +19,10 @@ import (
// AMQPConsumer is the top level struct for this plugin // AMQPConsumer is the top level struct for this plugin
type AMQPConsumer struct { type AMQPConsumer struct {
URL string URL string `toml:"url"` // deprecated in 1.7; use brokers
Brokers []string `toml:"brokers"`
Username string `toml:"username"`
Password string `toml:"password"`
Exchange string `toml:"exchange"` Exchange string `toml:"exchange"`
ExchangeType string `toml:"exchange_type"` ExchangeType string `toml:"exchange_type"`
ExchangeDurability string `toml:"exchange_durability"` ExchangeDurability string `toml:"exchange_durability"`
@ -55,6 +59,8 @@ func (a *externalAuth) Response() string {
const ( const (
DefaultAuthMethod = "PLAIN" DefaultAuthMethod = "PLAIN"
DefaultBroker = "amqp://localhost:5672/influxdb"
DefaultExchangeType = "topic" DefaultExchangeType = "topic"
DefaultExchangeDurability = "durable" DefaultExchangeDurability = "durable"
@ -63,8 +69,18 @@ const (
func (a *AMQPConsumer) SampleConfig() string { func (a *AMQPConsumer) SampleConfig() string {
return ` return `
## AMQP url ## Broker to consume from.
url = "amqp://localhost:5672/influxdb" ## deprecated in 1.7; use the brokers option
# url = "amqp://localhost:5672/influxdb"
## Brokers to consume from. If multiple brokers are specified a random broker
## will be selected anytime a connection is established. This can be
## helpful for load balancing when not using a dedicated load balancer.
brokers = ["amqp://localhost:5672/influxdb"]
## Authentication credentials for the PLAIN auth_method.
# username = ""
# password = ""
## Exchange to declare and consume from. ## Exchange to declare and consume from.
exchange = "telegraf" exchange = "telegraf"
@ -130,16 +146,21 @@ func (a *AMQPConsumer) createConfig() (*amqp.Config, error) {
return nil, err return nil, err
} }
// parse auth method var auth []amqp.Authentication
var sasl []amqp.Authentication // nil by default
if strings.ToUpper(a.AuthMethod) == "EXTERNAL" { if strings.ToUpper(a.AuthMethod) == "EXTERNAL" {
sasl = []amqp.Authentication{&externalAuth{}} auth = []amqp.Authentication{&externalAuth{}}
} else if a.Username != "" || a.Password != "" {
auth = []amqp.Authentication{
&amqp.PlainAuth{
Username: a.Username,
Password: a.Password,
},
}
} }
config := amqp.Config{ config := amqp.Config{
TLSClientConfig: tls, TLSClientConfig: tls,
SASL: sasl, // if nil, it will be PLAIN SASL: auth, // if nil, it will be PLAIN
} }
return &config, nil return &config, nil
} }
@ -187,13 +208,29 @@ func (a *AMQPConsumer) Start(acc telegraf.Accumulator) error {
} }
func (a *AMQPConsumer) connect(amqpConf *amqp.Config) (<-chan amqp.Delivery, error) { func (a *AMQPConsumer) connect(amqpConf *amqp.Config) (<-chan amqp.Delivery, error) {
conn, err := amqp.DialConfig(a.URL, *amqpConf) brokers := a.Brokers
if err != nil { if len(brokers) == 0 {
return nil, err brokers = []string{a.URL}
} }
a.conn = conn
ch, err := conn.Channel() p := rand.Perm(len(brokers))
for _, n := range p {
broker := brokers[n]
log.Printf("D! [amqp_consumer] connecting to %q", broker)
conn, err := amqp.DialConfig(broker, *amqpConf)
if err == nil {
a.conn = conn
log.Printf("D! [amqp_consumer] connected to %q", broker)
break
}
log.Printf("D! [amqp_consumer] error connecting to %q", broker)
}
if a.conn == nil {
return nil, errors.New("could not connect to any broker")
}
ch, err := a.conn.Channel()
if err != nil { if err != nil {
return nil, fmt.Errorf("Failed to open a channel: %s", err) return nil, fmt.Errorf("Failed to open a channel: %s", err)
} }
@ -338,6 +375,7 @@ func (a *AMQPConsumer) Stop() {
func init() { func init() {
inputs.Add("amqp_consumer", func() telegraf.Input { inputs.Add("amqp_consumer", func() telegraf.Input {
return &AMQPConsumer{ return &AMQPConsumer{
URL: DefaultBroker,
AuthMethod: DefaultAuthMethod, AuthMethod: DefaultAuthMethod,
ExchangeType: DefaultExchangeType, ExchangeType: DefaultExchangeType,
ExchangeDurability: DefaultExchangeDurability, ExchangeDurability: DefaultExchangeDurability,

View File

@ -24,8 +24,18 @@ For an introduction to AMQP see:
```toml ```toml
# Configuration for the AMQP server to send metrics to # Configuration for the AMQP server to send metrics to
[[outputs.amqp]] [[outputs.amqp]]
## AMQP url ## Broker to publish to.
url = "amqp://localhost:5672/influxdb" ## deprecated in 1.7; use the brokers option
# url = "amqp://localhost:5672/influxdb"
## Brokers to publish to. If multiple brokers are specified a random broker
## will be selected anytime a connection is established. This can be
## helpful for load balancing when not using a dedicated load balancer.
brokers = ["amqp://localhost:5672/influxdb"]
## Authentication credentials for the PLAIN auth_method.
# username = ""
# password = ""
## Exchange to declare and publish to. ## Exchange to declare and publish to.
exchange = "telegraf" exchange = "telegraf"

View File

@ -1,8 +1,10 @@
package amqp package amqp
import ( import (
"errors"
"fmt" "fmt"
"log" "log"
"math/rand"
"net" "net"
"strings" "strings"
"sync" "sync"
@ -24,9 +26,10 @@ type client struct {
} }
type AMQP struct { type AMQP struct {
// AMQP brokers to send metrics to URL string `toml:"url"` // deprecated in 1.7; use brokers
URL string Brokers []string `toml:"brokers"`
Username string `toml:"username"`
Password string `toml:"password"`
Exchange string `toml:"exchange"` Exchange string `toml:"exchange"`
ExchangeType string `toml:"exchange_type"` ExchangeType string `toml:"exchange_type"`
ExchangeDurability string `toml:"exchange_durability"` ExchangeDurability string `toml:"exchange_durability"`
@ -72,6 +75,8 @@ func (a *externalAuth) Response() string {
const ( const (
DefaultAuthMethod = "PLAIN" DefaultAuthMethod = "PLAIN"
DefaultBroker = "amqp://localhost:5672/influxdb"
DefaultExchangeType = "topic" DefaultExchangeType = "topic"
DefaultExchangeDurability = "durable" DefaultExchangeDurability = "durable"
@ -80,8 +85,18 @@ const (
) )
var sampleConfig = ` var sampleConfig = `
## AMQP url ## Broker to publish to.
url = "amqp://localhost:5672/influxdb" ## deprecated in 1.7; use the brokers option
# url = "amqp://localhost:5672/influxdb"
## Brokers to publish to. If multiple brokers are specified a random broker
## will be selected anytime a connection is established. This can be
## helpful for load balancing when not using a dedicated load balancer.
brokers = ["amqp://localhost:5672/influxdb"]
## Authentication credentials for the PLAIN auth_method.
# username = ""
# password = ""
## Exchange to declare and publish to. ## Exchange to declare and publish to.
exchange = "telegraf" exchange = "telegraf"
@ -158,31 +173,53 @@ func (q *AMQP) Connect() error {
"retention_policy": q.RetentionPolicy, "retention_policy": q.RetentionPolicy,
} }
var connection *amqp.Connection
// make new tls config // make new tls config
tls, err := q.ClientConfig.TLSConfig() tls, err := q.ClientConfig.TLSConfig()
if err != nil { if err != nil {
return err return err
} }
// parse auth method var auth []amqp.Authentication
var sasl []amqp.Authentication // nil by default
if strings.ToUpper(q.AuthMethod) == "EXTERNAL" { if strings.ToUpper(q.AuthMethod) == "EXTERNAL" {
sasl = []amqp.Authentication{&externalAuth{}} auth = []amqp.Authentication{&externalAuth{}}
} else if q.Username != "" || q.Password != "" {
auth = []amqp.Authentication{
&amqp.PlainAuth{
Username: q.Username,
Password: q.Password,
},
}
}
brokers := q.Brokers
if len(brokers) == 0 {
brokers = []string{q.URL}
} }
amqpConf := amqp.Config{ amqpConf := amqp.Config{
TLSClientConfig: tls, TLSClientConfig: tls,
SASL: sasl, // if nil, it will be PLAIN SASL: auth, // if nil, it will be PLAIN
Dial: func(network, addr string) (net.Conn, error) { Dial: func(network, addr string) (net.Conn, error) {
return net.DialTimeout(network, addr, q.Timeout.Duration) return net.DialTimeout(network, addr, q.Timeout.Duration)
}, },
} }
connection, err = amqp.DialConfig(q.URL, amqpConf) var connection *amqp.Connection
if err != nil { p := rand.Perm(len(brokers))
return err for _, n := range p {
broker := brokers[n]
log.Printf("D! Output [amqp] connecting to %q", broker)
conn, err := amqp.DialConfig(broker, amqpConf)
if err == nil {
connection = conn
log.Printf("D! Output [amqp] connected to %q", broker)
break
}
log.Printf("D! Output [amqp] error connecting to %q", broker)
}
if connection == nil {
return errors.New("could not connect to any broker")
} }
channel, err := connection.Channel() channel, err := connection.Channel()
@ -363,6 +400,7 @@ func (q *AMQP) setClient(c *client) {
func init() { func init() {
outputs.Add("amqp", func() telegraf.Output { outputs.Add("amqp", func() telegraf.Output {
return &AMQP{ return &AMQP{
URL: DefaultBroker,
AuthMethod: DefaultAuthMethod, AuthMethod: DefaultAuthMethod,
ExchangeType: DefaultExchangeType, ExchangeType: DefaultExchangeType,
ExchangeDurability: DefaultExchangeDurability, ExchangeDurability: DefaultExchangeDurability,