Add support for gzip compression to amqp plugins (#5830)

This commit is contained in:
Daniel Nelson
2019-05-20 14:36:23 -07:00
committed by GitHub
parent 1b2773a762
commit b5cd9a9ff2
7 changed files with 250 additions and 12 deletions

View File

@@ -92,6 +92,14 @@ For an introduction to AMQP see:
## Recommended to set to true.
# use_batch_format = false
## Content encoding for message payloads, can be set to "gzip" to or
## "identity" to apply no encoding.
##
## Please note that when use_batch_format = false each amqp message contains only
## a single metric, it is recommended to use compression with batch format
## for best results.
# content_encoding = "identity"
## Data format to output.
## Each data format has its own unique set of configuration options, read
## more about them here:

View File

@@ -54,6 +54,7 @@ type AMQP struct {
Headers map[string]string `toml:"headers"`
Timeout internal.Duration `toml:"timeout"`
UseBatchFormat bool `toml:"use_batch_format"`
ContentEncoding string `toml:"content_encoding"`
tls.ClientConfig
serializer serializers.Serializer
@@ -61,6 +62,7 @@ type AMQP struct {
client Client
config *ClientConfig
sentMessages int
encoder internal.ContentEncoder
}
type Client interface {
@@ -149,6 +151,14 @@ var sampleConfig = `
## Recommended to set to true.
# use_batch_format = false
## Content encoding for message payloads, can be set to "gzip" to or
## "identity" to apply no encoding.
##
## Please note that when use_batch_format = false each amqp message contains only
## a single metric, it is recommended to use compression with batch format
## for best results.
# content_encoding = "identity"
## Data format to output.
## Each data format has its own unique set of configuration options, read
## more about them here:
@@ -177,11 +187,16 @@ func (q *AMQP) Connect() error {
q.config = config
}
client, err := q.connect(q.config)
var err error
q.encoder, err = internal.NewContentEncoder(q.ContentEncoding)
if err != nil {
return err
}
q.client, err = q.connect(q.config)
if err != nil {
return err
}
q.client = client
return nil
}
@@ -227,6 +242,11 @@ func (q *AMQP) Write(metrics []telegraf.Metric) error {
return err
}
body, err = q.encoder.Encode(body)
if err != nil {
return err
}
err = q.publish(key, body)
if err != nil {
// If this is the first attempt to publish and the connection is
@@ -298,6 +318,7 @@ func (q *AMQP) makeClientConfig() (*ClientConfig, error) {
exchange: q.Exchange,
exchangeType: q.ExchangeType,
exchangePassive: q.ExchangePassive,
encoding: q.ContentEncoding,
timeout: q.Timeout.Duration,
}

View File

@@ -19,6 +19,7 @@ type ClientConfig struct {
exchangePassive bool
exchangeDurable bool
exchangeArguments amqp.Table
encoding string
headers amqp.Table
deliveryMode uint8
tlsConfig *tls.Config
@@ -114,10 +115,11 @@ func (c *client) Publish(key string, body []byte) error {
false, // mandatory
false, // immediate
amqp.Publishing{
Headers: c.config.headers,
ContentType: "text/plain",
Body: body,
DeliveryMode: c.config.deliveryMode,
Headers: c.config.headers,
ContentType: "text/plain",
ContentEncoding: c.config.encoding,
Body: body,
DeliveryMode: c.config.deliveryMode,
})
}