Skip to content

Commit

Permalink
split metrics based on UDPPayload size (#2795)
Browse files Browse the repository at this point in the history
  • Loading branch information
sebito91 authored and danielnelson committed May 12, 2017
1 parent 0ed404e commit a871b64
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 8 deletions.
10 changes: 7 additions & 3 deletions plugins/outputs/influxdb/client/udp.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ type UDPConfig struct {
PayloadSize int
}

// NewUDP will return an instance of the telegraf UDP output plugin for influxdb
func NewUDP(config UDPConfig) (Client, error) {
p, err := url.Parse(config.URL)
if err != nil {
Expand Down Expand Up @@ -55,20 +56,22 @@ type udpClient struct {
buffer []byte
}

// Query will send the provided query command to the client, returning an error if any issues arise
func (c *udpClient) Query(command string) error {
return nil
}

// Write will send the byte stream to the given UDP client endpoint
func (c *udpClient) Write(b []byte) (int, error) {
return c.WriteStream(bytes.NewReader(b), -1)
}

// write params are ignored by the UDP client
// WriteWithParams are ignored by the UDP client, will forward to WriteStream
func (c *udpClient) WriteWithParams(b []byte, wp WriteParams) (int, error) {
return c.WriteStream(bytes.NewReader(b), -1)
}

// contentLength is ignored by the UDP client.
// WriteStream will send the provided data through to the client, contentLength is ignored by the UDP client
func (c *udpClient) WriteStream(r io.Reader, contentLength int) (int, error) {
var totaln int
for {
Expand All @@ -88,12 +91,13 @@ func (c *udpClient) WriteStream(r io.Reader, contentLength int) (int, error) {
return totaln, nil
}

// contentLength is ignored by the UDP client.
// WriteStreamWithParams will forward the stream to the client backend, contentLength is ignored by the UDP client
// write params are ignored by the UDP client
func (c *udpClient) WriteStreamWithParams(r io.Reader, contentLength int, wp WriteParams) (int, error) {
return c.WriteStream(r, -1)
}

// Close will terminate the provided client connection
func (c *udpClient) Close() error {
return c.conn.Close()
}
16 changes: 11 additions & 5 deletions plugins/outputs/influxdb/influxdb.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import (
"github.com/influxdata/telegraf/plugins/outputs/influxdb/client"
)

// InfluxDB struct is the primary data structure for the plugin
type InfluxDB struct {
// URL is only for backwards compatability
URL string
Expand Down Expand Up @@ -79,11 +80,10 @@ var sampleConfig = `
# insecure_skip_verify = false
`

// Connect initiates the primary connection to the range of provided URLs
func (i *InfluxDB) Connect() error {
var urls []string
for _, u := range i.URLs {
urls = append(urls, u)
}
urls = append(urls, i.URLs...)

// Backward-compatability with single Influx URL config files
// This could eventually be removed in favor of specifying the urls as a list
Expand Down Expand Up @@ -144,26 +144,32 @@ func (i *InfluxDB) Connect() error {
return nil
}

// Close will terminate the session to the backend, returning error if an issue arises
func (i *InfluxDB) Close() error {
return nil
}

// SampleConfig returns the formatted sample configuration for the plugin
func (i *InfluxDB) SampleConfig() string {
return sampleConfig
}

// Description returns the human-readable function definition of the plugin
func (i *InfluxDB) Description() string {
return "Configuration for influxdb server to send metrics to"
}

// Choose a random server in the cluster to write to until a successful write
// Write will choose a random server in the cluster to write to until a successful write
// occurs, logging each unsuccessful. If all servers fail, return error.
func (i *InfluxDB) Write(metrics []telegraf.Metric) error {
bufsize := 0
splitData := make([]telegraf.Metric, 0)

for _, m := range metrics {
bufsize += m.Len()
splitData = append(splitData, m.Split(i.UDPPayload)...)
}
r := metric.NewReader(metrics)
r := metric.NewReader(splitData)

// This will get set to nil if a successful write occurs
err := fmt.Errorf("Could not write to any InfluxDB server in cluster")
Expand Down

0 comments on commit a871b64

Please sign in to comment.