Document and add support to input plugins for logging alias (#6357)

This commit is contained in:
Greg
2019-09-23 16:39:50 -06:00
committed by Daniel Nelson
parent e42d2e39c6
commit 817c9a69a9
111 changed files with 961 additions and 659 deletions

View File

@@ -5,7 +5,6 @@ import (
"bytes"
"errors"
"fmt"
"log"
"net"
"sort"
"strconv"
@@ -34,13 +33,6 @@ const (
MaxTCPConnections = 250
)
var dropwarn = "E! [inputs.statsd] Error: statsd message queue full. " +
"We have dropped %d messages so far. " +
"You may want to increase allowed_pending_messages in the config\n"
var malformedwarn = "E! [inputs.statsd] Statsd over TCP has received %d malformed packets" +
" thus far."
// Statsd allows the importing of statsd and dogstatsd data.
type Statsd struct {
// Protocol used on listener - udp or tcp
@@ -133,6 +125,8 @@ type Statsd struct {
PacketsRecv selfstat.Stat
BytesRecv selfstat.Stat
Log telegraf.Logger
// A pool of byte slices to handle parsing
bufPool sync.Pool
}
@@ -312,7 +306,7 @@ func (s *Statsd) Gather(acc telegraf.Accumulator) error {
func (s *Statsd) Start(ac telegraf.Accumulator) error {
if s.ParseDataDogTags {
s.DataDogExtensions = true
log.Printf("W! [inputs.statsd] The parse_data_dog_tags option is deprecated, use datadog_extensions instead.")
s.Log.Warn("'parse_data_dog_tags' config option is deprecated, please use 'datadog_extensions' instead")
}
s.acc = ac
@@ -350,8 +344,7 @@ func (s *Statsd) Start(ac telegraf.Accumulator) error {
}
if s.ConvertNames {
log.Printf("W! [inputs.statsd] statsd: convert_names config option is deprecated," +
" please use metric_separator instead")
s.Log.Warn("'convert_names' config option is deprecated, please use 'metric_separator' instead")
}
if s.MetricSeparator == "" {
@@ -369,7 +362,7 @@ func (s *Statsd) Start(ac telegraf.Accumulator) error {
return err
}
log.Println("I! [inputs.statsd] Statsd UDP listener listening on: ", conn.LocalAddr().String())
s.Log.Infof("UDP listening on %q", conn.LocalAddr().String())
s.UDPlistener = conn
s.wg.Add(1)
@@ -387,7 +380,7 @@ func (s *Statsd) Start(ac telegraf.Accumulator) error {
return err
}
log.Println("I! [inputs.statsd] TCP Statsd listening on: ", listener.Addr().String())
s.Log.Infof("TCP listening on %q", listener.Addr().String())
s.TCPlistener = listener
s.wg.Add(1)
@@ -403,7 +396,7 @@ func (s *Statsd) Start(ac telegraf.Accumulator) error {
defer s.wg.Done()
s.parser()
}()
log.Printf("I! [inputs.statsd] Started the statsd service on %s\n", s.ServiceAddress)
s.Log.Infof("Started the statsd service on %q", s.ServiceAddress)
return nil
}
@@ -463,7 +456,7 @@ func (s *Statsd) udpListen(conn *net.UDPConn) error {
n, addr, err := conn.ReadFromUDP(buf)
if err != nil {
if !strings.Contains(err.Error(), "closed network") {
log.Printf("E! [inputs.statsd] Error READ: %s\n", err.Error())
s.Log.Errorf("Error reading: %s", err.Error())
continue
}
return err
@@ -479,7 +472,9 @@ func (s *Statsd) udpListen(conn *net.UDPConn) error {
default:
s.drops++
if s.drops == 1 || s.AllowedPendingMessages == 0 || s.drops%s.AllowedPendingMessages == 0 {
log.Printf(dropwarn, s.drops)
s.Log.Errorf("Statsd message queue full. "+
"We have dropped %d messages so far. "+
"You may want to increase allowed_pending_messages in the config", s.drops)
}
}
}
@@ -540,8 +535,8 @@ func (s *Statsd) parseStatsdLine(line string) error {
// Validate splitting the line on ":"
bits := strings.Split(line, ":")
if len(bits) < 2 {
log.Printf("E! [inputs.statsd] Error: splitting ':', Unable to parse metric: %s\n", line)
return errors.New("Error Parsing statsd line")
s.Log.Errorf("Splitting ':', unable to parse metric: %s", line)
return errors.New("error Parsing statsd line")
}
// Extract bucket name from individual metric bits
@@ -556,22 +551,22 @@ func (s *Statsd) parseStatsdLine(line string) error {
// Validate splitting the bit on "|"
pipesplit := strings.Split(bit, "|")
if len(pipesplit) < 2 {
log.Printf("E! [inputs.statsd] Error: splitting '|', Unable to parse metric: %s\n", line)
return errors.New("Error Parsing statsd line")
s.Log.Errorf("Splitting '|', unable to parse metric: %s", line)
return errors.New("error parsing statsd line")
} else if len(pipesplit) > 2 {
sr := pipesplit[2]
errmsg := "E! [inputs.statsd] parsing sample rate, %s, it must be in format like: " +
"@0.1, @0.5, etc. Ignoring sample rate for line: %s\n"
if strings.Contains(sr, "@") && len(sr) > 1 {
samplerate, err := strconv.ParseFloat(sr[1:], 64)
if err != nil {
log.Printf(errmsg, err.Error(), line)
s.Log.Errorf("Parsing sample rate: %s", err.Error())
} else {
// sample rate successfully parsed
m.samplerate = samplerate
}
} else {
log.Printf(errmsg, "", line)
s.Log.Debugf("Sample rate must be in format like: "+
"@0.1, @0.5, etc. Ignoring sample rate for line: %s", line)
}
}
@@ -580,15 +575,15 @@ func (s *Statsd) parseStatsdLine(line string) error {
case "g", "c", "s", "ms", "h":
m.mtype = pipesplit[1]
default:
log.Printf("E! [inputs.statsd] Error: Statsd Metric type %s unsupported", pipesplit[1])
return errors.New("Error Parsing statsd line")
s.Log.Errorf("Metric type %q unsupported", pipesplit[1])
return errors.New("error parsing statsd line")
}
// Parse the value
if strings.HasPrefix(pipesplit[0], "-") || strings.HasPrefix(pipesplit[0], "+") {
if m.mtype != "g" && m.mtype != "c" {
log.Printf("E! [inputs.statsd] Error: +- values are only supported for gauges & counters: %s\n", line)
return errors.New("Error Parsing statsd line")
s.Log.Errorf("+- values are only supported for gauges & counters, unable to parse metric: %s", line)
return errors.New("error parsing statsd line")
}
m.additive = true
}
@@ -597,8 +592,8 @@ func (s *Statsd) parseStatsdLine(line string) error {
case "g", "ms", "h":
v, err := strconv.ParseFloat(pipesplit[0], 64)
if err != nil {
log.Printf("E! [inputs.statsd] Error: parsing value to float64: %s\n", line)
return errors.New("Error Parsing statsd line")
s.Log.Errorf("Parsing value to float64, unable to parse metric: %s", line)
return errors.New("error parsing statsd line")
}
m.floatvalue = v
case "c":
@@ -607,8 +602,8 @@ func (s *Statsd) parseStatsdLine(line string) error {
if err != nil {
v2, err2 := strconv.ParseFloat(pipesplit[0], 64)
if err2 != nil {
log.Printf("E! [inputs.statsd] Error: parsing value to int64: %s\n", line)
return errors.New("Error Parsing statsd line")
s.Log.Errorf("Parsing value to int64, unable to parse metric: %s", line)
return errors.New("error parsing statsd line")
}
v = int64(v2)
}
@@ -852,7 +847,9 @@ func (s *Statsd) handler(conn *net.TCPConn, id string) {
default:
s.drops++
if s.drops == 1 || s.drops%s.AllowedPendingMessages == 0 {
log.Printf(dropwarn, s.drops)
s.Log.Errorf("Statsd message queue full. "+
"We have dropped %d messages so far. "+
"You may want to increase allowed_pending_messages in the config", s.drops)
}
}
}
@@ -862,9 +859,8 @@ func (s *Statsd) handler(conn *net.TCPConn, id string) {
// refuser refuses a TCP connection
func (s *Statsd) refuser(conn *net.TCPConn) {
conn.Close()
log.Printf("I! [inputs.statsd] Refused TCP Connection from %s", conn.RemoteAddr())
log.Printf("I! [inputs.statsd] WARNING: Maximum TCP Connections reached, you may want to" +
" adjust max_tcp_connections")
s.Log.Infof("Refused TCP Connection from %s", conn.RemoteAddr())
s.Log.Warn("Maximum TCP Connections reached, you may want to adjust max_tcp_connections")
}
// forget a TCP connection
@@ -883,7 +879,7 @@ func (s *Statsd) remember(id string, conn *net.TCPConn) {
func (s *Statsd) Stop() {
s.Lock()
log.Println("I! [inputs.statsd] Stopping the statsd service")
s.Log.Infof("Stopping the statsd service")
close(s.done)
if s.isUDP() {
s.UDPlistener.Close()
@@ -909,7 +905,7 @@ func (s *Statsd) Stop() {
s.Lock()
close(s.in)
log.Println("I! Stopped Statsd listener service on ", s.ServiceAddress)
s.Log.Infof("Stopped listener service on %q", s.ServiceAddress)
s.Unlock()
}

View File

@@ -18,7 +18,7 @@ const (
)
func NewTestStatsd() *Statsd {
s := Statsd{}
s := Statsd{Log: testutil.Logger{}}
// Make data structures
s.done = make(chan struct{})
@@ -36,6 +36,7 @@ func NewTestStatsd() *Statsd {
// Test that MaxTCPConections is respected
func TestConcurrentConns(t *testing.T) {
listener := Statsd{
Log: testutil.Logger{},
Protocol: "tcp",
ServiceAddress: "localhost:8125",
AllowedPendingMessages: 10000,
@@ -66,6 +67,7 @@ func TestConcurrentConns(t *testing.T) {
// Test that MaxTCPConections is respected when max==1
func TestConcurrentConns1(t *testing.T) {
listener := Statsd{
Log: testutil.Logger{},
Protocol: "tcp",
ServiceAddress: "localhost:8125",
AllowedPendingMessages: 10000,
@@ -94,6 +96,7 @@ func TestConcurrentConns1(t *testing.T) {
// Test that MaxTCPConections is respected
func TestCloseConcurrentConns(t *testing.T) {
listener := Statsd{
Log: testutil.Logger{},
Protocol: "tcp",
ServiceAddress: "localhost:8125",
AllowedPendingMessages: 10000,
@@ -115,6 +118,7 @@ func TestCloseConcurrentConns(t *testing.T) {
// benchmark how long it takes to accept & process 100,000 metrics:
func BenchmarkUDP(b *testing.B) {
listener := Statsd{
Log: testutil.Logger{},
Protocol: "udp",
ServiceAddress: "localhost:8125",
AllowedPendingMessages: 250000,
@@ -145,6 +149,7 @@ func BenchmarkUDP(b *testing.B) {
// benchmark how long it takes to accept & process 100,000 metrics:
func BenchmarkTCP(b *testing.B) {
listener := Statsd{
Log: testutil.Logger{},
Protocol: "tcp",
ServiceAddress: "localhost:8125",
AllowedPendingMessages: 250000,
@@ -1625,6 +1630,7 @@ func testValidateGauge(
func TestTCP(t *testing.T) {
statsd := Statsd{
Log: testutil.Logger{},
Protocol: "tcp",
ServiceAddress: "localhost:0",
AllowedPendingMessages: 10000,