Fix InfluxDB output UDP line splitting (#5439)
This commit is contained in:
committed by
Daniel Nelson
parent
e4d084fbc3
commit
5dfa3fa769
@@ -1,6 +1,8 @@
|
||||
package influxdb
|
||||
|
||||
import (
|
||||
"bufio"
|
||||
"bytes"
|
||||
"context"
|
||||
"fmt"
|
||||
"log"
|
||||
@@ -45,9 +47,9 @@ func NewUDPClient(config *UDPConfig) (*udpClient, error) {
|
||||
serializer := config.Serializer
|
||||
if serializer == nil {
|
||||
s := influx.NewSerializer()
|
||||
s.SetMaxLineBytes(config.MaxPayloadSize)
|
||||
serializer = s
|
||||
}
|
||||
serializer.SetMaxLineBytes(size)
|
||||
|
||||
dialer := config.Dialer
|
||||
if dialer == nil {
|
||||
@@ -96,7 +98,11 @@ func (c *udpClient) Write(ctx context.Context, metrics []telegraf.Metric) error
|
||||
continue
|
||||
}
|
||||
|
||||
_, err = c.conn.Write(octets)
|
||||
scanner := bufio.NewScanner(bytes.NewReader(octets))
|
||||
scanner.Split(scanLines)
|
||||
for scanner.Scan() {
|
||||
_, err = c.conn.Write(scanner.Bytes())
|
||||
}
|
||||
if err != nil {
|
||||
c.conn.Close()
|
||||
c.conn = nil
|
||||
@@ -118,3 +124,15 @@ type netDialer struct {
|
||||
func (d *netDialer) DialContext(ctx context.Context, network, address string) (Conn, error) {
|
||||
return d.Dialer.DialContext(ctx, network, address)
|
||||
}
|
||||
|
||||
func scanLines(data []byte, atEOF bool) (advance int, token []byte, err error) {
|
||||
if atEOF && len(data) == 0 {
|
||||
return 0, nil, nil
|
||||
}
|
||||
if i := bytes.IndexByte(data, '\n'); i >= 0 {
|
||||
// We have a full newline-terminated line.
|
||||
return i + 1, data[0 : i+1], nil
|
||||
|
||||
}
|
||||
return 0, nil, nil
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user