Removed useHttp in favor of parsing host string to determine the right API to use for sending metrics. Also renamed BatchSize to HttpBatchSize to better convey that it is only used when using Http API.

This commit is contained in:
Eric 2016-07-22 13:02:26 -04:00
parent 433a76acef
commit 2000f06727
2 changed files with 32 additions and 22 deletions

View File

@ -3,6 +3,7 @@ package opentsdb
import ( import (
"fmt" "fmt"
"net" "net"
"net/url"
"sort" "sort"
"strconv" "strconv"
"strings" "strings"
@ -17,8 +18,7 @@ type OpenTSDB struct {
Host string Host string
Port int Port int
UseHttp bool HttpBatchSize int
BatchSize int
Debug bool Debug bool
} }
@ -30,19 +30,17 @@ var sampleConfig = `
## prefix for metrics keys ## prefix for metrics keys
prefix = "my.specific.prefix." prefix = "my.specific.prefix."
## Telnet Mode ##
## DNS name of the OpenTSDB server ## DNS name of the OpenTSDB server
## Using "opentsdb.example.com" or "tcp://opentsdb.example.com" will use the
## telnet API. "http://opentsdb.example.com" will use the Http API.
host = "opentsdb.example.com" host = "opentsdb.example.com"
## Port of the OpenTSDB server in telnet mode ## Port of the OpenTSDB server
port = 4242 port = 4242
## Use Http PUT API
useHttp = false
## Number of data points to send to OpenTSDB in Http requests. ## Number of data points to send to OpenTSDB in Http requests.
## Not used when useHttp is false. ## Not used with telnet API.
batchSize = 50 httpBatchSize = 50
## Debug true - Prints OpenTSDB communication ## Debug true - Prints OpenTSDB communication
debug = false debug = false
@ -63,7 +61,12 @@ func (t TagSet) ToLineFormat() string {
func (o *OpenTSDB) Connect() error { func (o *OpenTSDB) Connect() error {
// Test Connection to OpenTSDB Server // Test Connection to OpenTSDB Server
uri := fmt.Sprintf("%s:%d", o.Host, o.Port) u, err := url.Parse(o.Host)
if err != nil {
return fmt.Errorf("Error in parsing host url: %s", err.Error())
}
uri := fmt.Sprintf("%s:%d", u.Host, o.Port)
tcpAddr, err := net.ResolveTCPAddr("tcp", uri) tcpAddr, err := net.ResolveTCPAddr("tcp", uri)
if err != nil { if err != nil {
return fmt.Errorf("OpenTSDB: TCP address cannot be resolved") return fmt.Errorf("OpenTSDB: TCP address cannot be resolved")
@ -81,18 +84,26 @@ func (o *OpenTSDB) Write(metrics []telegraf.Metric) error {
return nil return nil
} }
if o.UseHttp { u, err := url.Parse(o.Host)
return o.WriteHttp(metrics) if err != nil {
return fmt.Errorf("Error in parsing host url: %s", err.Error())
}
if u.Scheme == "" || u.Scheme == "tcp" {
return o.WriteTelnet(metrics, u)
} else if u.Scheme == "http" {
return o.WriteHttp(metrics, u)
} else { } else {
return o.WriteTelnet(metrics) return fmt.Errorf("Unknown scheme in host parameter.")
} }
} }
func (o *OpenTSDB) WriteHttp(metrics []telegraf.Metric) error { func (o *OpenTSDB) WriteHttp(metrics []telegraf.Metric, u *url.URL) error {
http := openTSDBHttp{ http := openTSDBHttp{
Host: o.Host, Host: u.Host,
Port: o.Port, Port: o.Port,
BatchSize: o.BatchSize, BatchSize: o.HttpBatchSize,
Debug: o.Debug, Debug: o.Debug,
} }
@ -128,9 +139,9 @@ func (o *OpenTSDB) WriteHttp(metrics []telegraf.Metric) error {
return nil return nil
} }
func (o *OpenTSDB) WriteTelnet(metrics []telegraf.Metric) error { func (o *OpenTSDB) WriteTelnet(metrics []telegraf.Metric, u *url.URL) error {
// Send Data with telnet / socket communication // Send Data with telnet / socket communication
uri := fmt.Sprintf("%s:%d", o.Host, o.Port) uri := fmt.Sprintf("%s:%d", u.Host, o.Port)
tcpAddr, _ := net.ResolveTCPAddr("tcp", uri) tcpAddr, _ := net.ResolveTCPAddr("tcp", uri)
connection, err := net.DialTCP("tcp", nil, tcpAddr) connection, err := net.DialTCP("tcp", nil, tcpAddr)
if err != nil { if err != nil {

View File

@ -94,7 +94,7 @@ func BenchmarkHttpSend(b *testing.B) {
panic(err) panic(err)
} }
host, p, _ := net.SplitHostPort(u.Host) _, p, _ := net.SplitHostPort(u.Host)
port, err := strconv.Atoi(p) port, err := strconv.Atoi(p)
if err != nil { if err != nil {
@ -102,11 +102,10 @@ func BenchmarkHttpSend(b *testing.B) {
} }
o := &OpenTSDB{ o := &OpenTSDB{
Host: host, Host: ts.URL,
Port: port, Port: port,
Prefix: "", Prefix: "",
UseHttp: true, HttpBatchSize: BatchSize,
BatchSize: BatchSize,
} }
b.ResetTimer() b.ResetTimer()