Fixed tag cleaning in http output and refactored telnet output.
This commit is contained in:
parent
93d44e1a6a
commit
60004f586d
|
@ -3,10 +3,8 @@ package opentsdb
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"net"
|
"net"
|
||||||
"sort"
|
|
||||||
"strconv"
|
"strconv"
|
||||||
"strings"
|
"strings"
|
||||||
"time"
|
|
||||||
|
|
||||||
"github.com/influxdata/telegraf"
|
"github.com/influxdata/telegraf"
|
||||||
"github.com/influxdata/telegraf/plugins/outputs"
|
"github.com/influxdata/telegraf/plugins/outputs"
|
||||||
|
@ -48,12 +46,22 @@ var sampleConfig = `
|
||||||
## Debug true - Prints OpenTSDB communication
|
## Debug true - Prints OpenTSDB communication
|
||||||
debug = false
|
debug = false
|
||||||
`
|
`
|
||||||
|
type TagSet map[string]string
|
||||||
|
|
||||||
|
func (t TagSet) ToLineFormat() string {
|
||||||
|
var line string
|
||||||
|
for k, v := range t {
|
||||||
|
line += fmt.Sprintf(" %s=%s", k, v)
|
||||||
|
}
|
||||||
|
|
||||||
|
return strings.TrimLeft(line, " ")
|
||||||
|
}
|
||||||
|
|
||||||
type MetricLine struct {
|
type MetricLine struct {
|
||||||
Metric string
|
Metric string
|
||||||
Timestamp int64
|
Timestamp int64
|
||||||
Value string
|
Value string
|
||||||
Tags string
|
Tags TagSet
|
||||||
}
|
}
|
||||||
|
|
||||||
func (o *OpenTSDB) Connect() error {
|
func (o *OpenTSDB) Connect() error {
|
||||||
|
@ -93,6 +101,8 @@ func (o *OpenTSDB) WriteHttp(metrics []telegraf.Metric) error {
|
||||||
|
|
||||||
for _, m := range metrics {
|
for _, m := range metrics {
|
||||||
now := m.UnixNano() / 1000000000
|
now := m.UnixNano() / 1000000000
|
||||||
|
tags := cleanTags(m.Tags())
|
||||||
|
|
||||||
for fieldName, value := range m.Fields() {
|
for fieldName, value := range m.Fields() {
|
||||||
metricValue, buildError := buildValue(value)
|
metricValue, buildError := buildValue(value)
|
||||||
if buildError != nil {
|
if buildError != nil {
|
||||||
|
@ -103,7 +113,7 @@ func (o *OpenTSDB) WriteHttp(metrics []telegraf.Metric) error {
|
||||||
metric := &HttpMetric{
|
metric := &HttpMetric{
|
||||||
Metric: sanitizedChars.Replace(fmt.Sprintf("%s%s_%s",
|
Metric: sanitizedChars.Replace(fmt.Sprintf("%s%s_%s",
|
||||||
o.Prefix, m.Name(), fieldName)),
|
o.Prefix, m.Name(), fieldName)),
|
||||||
Tags: m.Tags(),
|
Tags: tags,
|
||||||
Timestamp: now,
|
Timestamp: now,
|
||||||
Value: metricValue,
|
Value: metricValue,
|
||||||
}
|
}
|
||||||
|
@ -122,8 +132,6 @@ func (o *OpenTSDB) WriteHttp(metrics []telegraf.Metric) error {
|
||||||
}
|
}
|
||||||
|
|
||||||
func (o *OpenTSDB) WriteTelnet(metrics []telegraf.Metric) error {
|
func (o *OpenTSDB) WriteTelnet(metrics []telegraf.Metric) error {
|
||||||
now := time.Now()
|
|
||||||
|
|
||||||
// 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", o.Host, o.Port)
|
||||||
tcpAddr, _ := net.ResolveTCPAddr("tcp", uri)
|
tcpAddr, _ := net.ResolveTCPAddr("tcp", uri)
|
||||||
|
@ -134,9 +142,20 @@ func (o *OpenTSDB) WriteTelnet(metrics []telegraf.Metric) error {
|
||||||
defer connection.Close()
|
defer connection.Close()
|
||||||
|
|
||||||
for _, m := range metrics {
|
for _, m := range metrics {
|
||||||
for _, metric := range buildMetrics(m, now, o.Prefix) {
|
now := m.UnixNano() / 1000000000
|
||||||
|
tags := cleanTags(m.Tags()).ToLineFormat()
|
||||||
|
|
||||||
|
for fieldName, value := range m.Fields() {
|
||||||
|
metricValue, buildError := buildValue(value)
|
||||||
|
if buildError != nil {
|
||||||
|
fmt.Printf("OpenTSDB: %s\n", buildError.Error())
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
|
||||||
messageLine := fmt.Sprintf("put %s %v %s %s\n",
|
messageLine := fmt.Sprintf("put %s %v %s %s\n",
|
||||||
metric.Metric, metric.Timestamp, metric.Value, metric.Tags)
|
sanitizedChars.Replace(fmt.Sprintf("%s%s_%s",o.Prefix, m.Name(), fieldName)),
|
||||||
|
now, metricValue, tags)
|
||||||
|
|
||||||
if o.Debug {
|
if o.Debug {
|
||||||
fmt.Print(messageLine)
|
fmt.Print(messageLine)
|
||||||
}
|
}
|
||||||
|
@ -150,37 +169,12 @@ func (o *OpenTSDB) WriteTelnet(metrics []telegraf.Metric) error {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func buildTags(mTags map[string]string) []string {
|
func cleanTags(tags map[string]string) TagSet {
|
||||||
tags := make([]string, len(mTags))
|
tagSet := make(map[string]string, len(tags))
|
||||||
index := 0
|
for k, v := range tags {
|
||||||
for k, v := range mTags {
|
tagSet[sanitizedChars.Replace(k)] = sanitizedChars.Replace(v)
|
||||||
tags[index] = sanitizedChars.Replace(fmt.Sprintf("%s=%s", k, v))
|
|
||||||
index++
|
|
||||||
}
|
}
|
||||||
sort.Strings(tags)
|
return tagSet
|
||||||
return tags
|
|
||||||
}
|
|
||||||
|
|
||||||
func buildMetrics(m telegraf.Metric, now time.Time, prefix string) []*MetricLine {
|
|
||||||
ret := []*MetricLine{}
|
|
||||||
for fieldName, value := range m.Fields() {
|
|
||||||
metric := &MetricLine{
|
|
||||||
Metric: sanitizedChars.Replace(fmt.Sprintf("%s%s_%s",
|
|
||||||
prefix, m.Name(), fieldName)),
|
|
||||||
Timestamp: now.Unix(),
|
|
||||||
}
|
|
||||||
|
|
||||||
metricValue, buildError := buildValue(value)
|
|
||||||
if buildError != nil {
|
|
||||||
fmt.Printf("OpenTSDB: %s\n", buildError.Error())
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
metric.Value = metricValue
|
|
||||||
tagsSlice := buildTags(m.Tags())
|
|
||||||
metric.Tags = fmt.Sprint(strings.Join(tagsSlice, " "))
|
|
||||||
ret = append(ret, metric)
|
|
||||||
}
|
|
||||||
return ret
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func buildValue(v interface{}) (string, error) {
|
func buildValue(v interface{}) (string, error) {
|
||||||
|
|
Loading…
Reference in New Issue