Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Udp output #364

Merged
merged 3 commits into from
Nov 12, 2015
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
Use the UDP client for writing to InfluxDB
  • Loading branch information
sparrc committed Nov 12, 2015
commit e10394ba3ba1c43a4789629d7acd8c0bbb08c6a3
95 changes: 54 additions & 41 deletions outputs/influxdb/influxdb.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,21 +15,23 @@ import (

type InfluxDB struct {
// URL is only for backwards compatability
URL string
URLs []string `toml:"urls"`
Username string
Password string
Database string
UserAgent string
Precision string
Timeout internal.Duration
URL string
URLs []string `toml:"urls"`
Username string
Password string
Database string
UserAgent string
Precision string
Timeout internal.Duration
UDPPayload int

conns []client.Client
}

var sampleConfig = `
# The full HTTP endpoint URL for your InfluxDB instance
# The full HTTP or UDP endpoint URL for your InfluxDB instance
# Multiple urls can be specified for InfluxDB cluster support.
# urls = ["udp://localhost:8089"] # UDP endpoint example
urls = ["http://localhost:8086"] # required
# The target database for metrics (telegraf will create it if not exists)
database = "telegraf" # required
Expand All @@ -42,51 +44,62 @@ var sampleConfig = `
# timeout = "5s"
# username = "telegraf"
# password = "metricsmetricsmetricsmetrics"
# Set the user agent for the POSTs (can be useful for log differentiation)
# Set the user agent for HTTP POSTs (can be useful for log differentiation)
# user_agent = "telegraf"
# Set UDP payload size, defaults to InfluxDB UDP Client default (512 bytes)
# udp_payload = 512
`

func (i *InfluxDB) Connect() error {
var urls []*url.URL
for _, URL := range i.URLs {
u, err := url.Parse(URL)
if err != nil {
return err
}
var urls []string
for _, u := range i.URLs {
urls = append(urls, u)
}

// Backward-compatability with single Influx URL config files
// This could eventually be removed in favor of specifying the urls as a list
if i.URL != "" {
u, err := url.Parse(i.URL)
if err != nil {
return err
}
urls = append(urls, u)
urls = append(urls, i.URL)
}

var conns []client.Client
for _, parsed_url := range urls {
c := client.NewClient(client.Config{
URL: parsed_url,
Username: i.Username,
Password: i.Password,
UserAgent: i.UserAgent,
Timeout: i.Timeout.Duration,
})
conns = append(conns, c)
}

for _, conn := range conns {
_, e := conn.Query(client.Query{
Command: fmt.Sprintf("CREATE DATABASE %s", i.Database),
})

if e != nil && !strings.Contains(e.Error(), "database already exists") {
log.Println("Database creation failed: " + e.Error())
} else {
break
for _, u := range urls {
switch {
case strings.HasPrefix(u, "udp"):
if i.UDPPayload == 0 {
i.UDPPayload = client.UDPPayloadSize
}
c, err := client.NewUDPClient(client.UDPConfig{
Addr: parsed_url.Host,
PayloadSize: i.UDPPayload,
})
if err != nil {
return err
}
conns = append(conns, c)
default:
// If URL doesn't start with "udp", assume HTTP client
c, err := client.NewHTTPClient(client.HTTPConfig{
Addr: parsed_url.String(),
Username: i.Username,
Password: i.Password,
UserAgent: i.UserAgent,
Timeout: i.Timeout.Duration,
})
if err != nil {
return err
}

// Create Database if it doesn't exist
_, e := c.Query(client.Query{
Command: fmt.Sprintf("CREATE DATABASE %s", i.Database),
})

if e != nil && !strings.Contains(e.Error(), "database already exists") {
log.Println("Database creation failed: " + e.Error())
}

conns = append(conns, c)
}
}

Expand Down