2015-09-24 18:06:11 +00:00
|
|
|
package statsd
|
|
|
|
|
|
|
|
import (
|
2015-10-05 22:19:08 +00:00
|
|
|
"errors"
|
2015-10-06 21:38:16 +00:00
|
|
|
"fmt"
|
2015-09-24 18:06:11 +00:00
|
|
|
"log"
|
|
|
|
"net"
|
2015-10-06 21:38:16 +00:00
|
|
|
"sort"
|
2015-09-24 18:06:11 +00:00
|
|
|
"strconv"
|
|
|
|
"strings"
|
|
|
|
"sync"
|
2016-01-28 23:09:41 +00:00
|
|
|
"time"
|
2015-09-24 18:06:11 +00:00
|
|
|
|
2016-02-06 00:36:35 +00:00
|
|
|
"github.com/influxdata/telegraf/plugins/parsers/graphite"
|
2015-10-06 21:38:16 +00:00
|
|
|
|
2016-01-27 21:21:36 +00:00
|
|
|
"github.com/influxdata/telegraf"
|
2016-01-20 18:57:35 +00:00
|
|
|
"github.com/influxdata/telegraf/plugins/inputs"
|
2015-09-24 18:06:11 +00:00
|
|
|
)
|
|
|
|
|
2016-01-19 18:01:53 +00:00
|
|
|
const UDP_PACKET_SIZE int = 1500
|
|
|
|
|
2015-09-24 18:06:11 +00:00
|
|
|
var dropwarn = "ERROR: Message queue full. Discarding line [%s] " +
|
|
|
|
"You may want to increase allowed_pending_messages in the config\n"
|
|
|
|
|
|
|
|
type Statsd struct {
|
|
|
|
// Address & Port to serve from
|
|
|
|
ServiceAddress string
|
|
|
|
|
|
|
|
// Number of messages allowed to queue up in between calls to Gather. If this
|
|
|
|
// fills up, packets will get dropped until the next Gather interval is ran.
|
|
|
|
AllowedPendingMessages int
|
|
|
|
|
2015-10-07 22:11:52 +00:00
|
|
|
// Percentiles specifies the percentiles that will be calculated for timing
|
|
|
|
// and histogram stats.
|
|
|
|
Percentiles []int
|
|
|
|
PercentileLimit int
|
|
|
|
|
2015-09-24 18:06:11 +00:00
|
|
|
DeleteGauges bool
|
|
|
|
DeleteCounters bool
|
|
|
|
DeleteSets bool
|
2015-10-07 22:11:52 +00:00
|
|
|
DeleteTimings bool
|
2016-01-15 22:38:32 +00:00
|
|
|
ConvertNames bool
|
2015-09-24 18:06:11 +00:00
|
|
|
|
2016-01-19 18:01:53 +00:00
|
|
|
// UDPPacketSize is the size of the read packets for the server listening
|
|
|
|
// for statsd UDP packets. This will default to 1500 bytes.
|
|
|
|
UDPPacketSize int `toml:"udp_packet_size"`
|
|
|
|
|
2015-09-24 18:06:11 +00:00
|
|
|
sync.Mutex
|
|
|
|
|
2016-01-19 18:01:53 +00:00
|
|
|
// Channel for all incoming statsd packets
|
|
|
|
in chan []byte
|
2015-10-07 22:11:52 +00:00
|
|
|
done chan struct{}
|
2015-09-24 18:06:11 +00:00
|
|
|
|
|
|
|
// Cache gauges, counters & sets so they can be aggregated as they arrive
|
2016-01-28 23:09:41 +00:00
|
|
|
// gauges and counters map measurement/tags hash -> field name -> metrics
|
|
|
|
// sets and timings map measurement/tags hash -> metrics
|
2015-10-06 19:33:35 +00:00
|
|
|
gauges map[string]cachedgauge
|
|
|
|
counters map[string]cachedcounter
|
|
|
|
sets map[string]cachedset
|
2015-10-07 22:11:52 +00:00
|
|
|
timings map[string]cachedtimings
|
2015-09-24 18:06:11 +00:00
|
|
|
|
2015-10-06 21:38:16 +00:00
|
|
|
// bucket -> influx templates
|
|
|
|
Templates []string
|
2015-09-24 18:06:11 +00:00
|
|
|
}
|
|
|
|
|
2015-10-05 22:19:08 +00:00
|
|
|
func NewStatsd() *Statsd {
|
|
|
|
s := Statsd{}
|
|
|
|
|
|
|
|
// Make data structures
|
|
|
|
s.done = make(chan struct{})
|
2016-01-19 18:01:53 +00:00
|
|
|
s.in = make(chan []byte, s.AllowedPendingMessages)
|
2015-10-06 19:33:35 +00:00
|
|
|
s.gauges = make(map[string]cachedgauge)
|
|
|
|
s.counters = make(map[string]cachedcounter)
|
|
|
|
s.sets = make(map[string]cachedset)
|
2015-10-07 22:11:52 +00:00
|
|
|
s.timings = make(map[string]cachedtimings)
|
2015-10-05 22:19:08 +00:00
|
|
|
|
2016-01-15 22:38:32 +00:00
|
|
|
s.ConvertNames = true
|
2016-01-19 18:01:53 +00:00
|
|
|
s.UDPPacketSize = UDP_PACKET_SIZE
|
2016-01-15 22:38:32 +00:00
|
|
|
|
2015-10-05 22:19:08 +00:00
|
|
|
return &s
|
|
|
|
}
|
|
|
|
|
2015-09-24 18:06:11 +00:00
|
|
|
// One statsd metric, form is <bucket>:<value>|<mtype>|@<samplerate>
|
|
|
|
type metric struct {
|
|
|
|
name string
|
2016-01-28 23:09:41 +00:00
|
|
|
field string
|
2015-09-24 18:06:11 +00:00
|
|
|
bucket string
|
2015-10-06 21:38:16 +00:00
|
|
|
hash string
|
2015-10-06 19:33:35 +00:00
|
|
|
intvalue int64
|
|
|
|
floatvalue float64
|
2015-09-24 18:06:11 +00:00
|
|
|
mtype string
|
|
|
|
additive bool
|
|
|
|
samplerate float64
|
|
|
|
tags map[string]string
|
|
|
|
}
|
|
|
|
|
2015-10-06 19:33:35 +00:00
|
|
|
type cachedset struct {
|
2016-01-28 23:09:41 +00:00
|
|
|
name string
|
|
|
|
fields map[string]map[int64]bool
|
|
|
|
tags map[string]string
|
2015-10-06 19:33:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type cachedgauge struct {
|
2016-01-28 23:09:41 +00:00
|
|
|
name string
|
|
|
|
fields map[string]interface{}
|
|
|
|
tags map[string]string
|
2015-10-06 19:33:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type cachedcounter struct {
|
2016-01-28 23:09:41 +00:00
|
|
|
name string
|
|
|
|
fields map[string]interface{}
|
|
|
|
tags map[string]string
|
2015-10-06 19:33:35 +00:00
|
|
|
}
|
|
|
|
|
2015-10-07 22:11:52 +00:00
|
|
|
type cachedtimings struct {
|
|
|
|
name string
|
|
|
|
stats RunningStats
|
|
|
|
tags map[string]string
|
2015-09-24 18:06:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (_ *Statsd) Description() string {
|
2015-10-15 21:53:29 +00:00
|
|
|
return "Statsd Server"
|
2015-09-24 18:06:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
const sampleConfig = `
|
2016-02-06 00:36:35 +00:00
|
|
|
### Address and port to host UDP listener on
|
2015-10-15 21:53:29 +00:00
|
|
|
service_address = ":8125"
|
2016-02-06 00:36:35 +00:00
|
|
|
### Delete gauges every interval (default=false)
|
2015-10-15 21:53:29 +00:00
|
|
|
delete_gauges = false
|
2016-02-06 00:36:35 +00:00
|
|
|
### Delete counters every interval (default=false)
|
2015-10-15 21:53:29 +00:00
|
|
|
delete_counters = false
|
2016-02-06 00:36:35 +00:00
|
|
|
### Delete sets every interval (default=false)
|
2015-10-15 21:53:29 +00:00
|
|
|
delete_sets = false
|
2016-02-06 00:36:35 +00:00
|
|
|
### Delete timings & histograms every interval (default=true)
|
2015-10-15 21:53:29 +00:00
|
|
|
delete_timings = true
|
2016-02-06 00:36:35 +00:00
|
|
|
### Percentiles to calculate for timing & histogram stats
|
2015-10-15 21:53:29 +00:00
|
|
|
percentiles = [90]
|
|
|
|
|
2016-02-06 00:36:35 +00:00
|
|
|
### convert measurement names, "." to "_" and "-" to "__"
|
2016-01-15 22:38:32 +00:00
|
|
|
convert_names = true
|
|
|
|
|
2016-02-06 00:36:35 +00:00
|
|
|
### Statsd data translation templates, more info can be read here:
|
2016-02-09 04:36:53 +00:00
|
|
|
### https://github.com/influxdata/telegraf/blob/master/DATA_FORMATS_INPUT.md#graphite
|
2015-10-15 21:53:29 +00:00
|
|
|
# templates = [
|
|
|
|
# "cpu.* measurement*"
|
|
|
|
# ]
|
|
|
|
|
2016-02-06 00:36:35 +00:00
|
|
|
### Number of UDP messages allowed to queue up, once filled,
|
|
|
|
### the statsd server will start dropping packets
|
2015-10-15 21:53:29 +00:00
|
|
|
allowed_pending_messages = 10000
|
|
|
|
|
2016-02-06 00:36:35 +00:00
|
|
|
### Number of timing/histogram values to track per-measurement in the
|
|
|
|
### calculation of percentiles. Raising this limit increases the accuracy
|
|
|
|
### of percentiles but also increases the memory usage and cpu time.
|
2015-10-15 21:53:29 +00:00
|
|
|
percentile_limit = 1000
|
2016-01-19 18:01:53 +00:00
|
|
|
|
2016-02-06 00:36:35 +00:00
|
|
|
### UDP packet size for the server to listen for. This will depend on the size
|
|
|
|
### of the packets that the client is sending, which is usually 1500 bytes.
|
2016-01-19 18:01:53 +00:00
|
|
|
udp_packet_size = 1500
|
2015-09-24 18:06:11 +00:00
|
|
|
`
|
|
|
|
|
|
|
|
func (_ *Statsd) SampleConfig() string {
|
|
|
|
return sampleConfig
|
|
|
|
}
|
|
|
|
|
2016-01-27 21:21:36 +00:00
|
|
|
func (s *Statsd) Gather(acc telegraf.Accumulator) error {
|
2015-09-24 18:06:11 +00:00
|
|
|
s.Lock()
|
|
|
|
defer s.Unlock()
|
2016-01-28 23:09:41 +00:00
|
|
|
now := time.Now()
|
2015-09-24 18:06:11 +00:00
|
|
|
|
2015-10-07 22:11:52 +00:00
|
|
|
for _, metric := range s.timings {
|
2016-01-28 13:49:28 +00:00
|
|
|
fields := make(map[string]interface{})
|
|
|
|
fields["mean"] = metric.stats.Mean()
|
|
|
|
fields["stddev"] = metric.stats.Stddev()
|
|
|
|
fields["upper"] = metric.stats.Upper()
|
|
|
|
fields["lower"] = metric.stats.Lower()
|
|
|
|
fields["count"] = metric.stats.Count()
|
2015-10-07 22:11:52 +00:00
|
|
|
for _, percentile := range s.Percentiles {
|
2016-01-28 13:49:28 +00:00
|
|
|
name := fmt.Sprintf("%v_percentile", percentile)
|
|
|
|
fields[name] = metric.stats.Percentile(percentile)
|
2015-09-24 18:06:11 +00:00
|
|
|
}
|
2016-01-28 23:09:41 +00:00
|
|
|
acc.AddFields(metric.name, fields, metric.tags, now)
|
2015-09-24 18:06:11 +00:00
|
|
|
}
|
2015-10-07 22:11:52 +00:00
|
|
|
if s.DeleteTimings {
|
|
|
|
s.timings = make(map[string]cachedtimings)
|
|
|
|
}
|
2015-09-24 18:06:11 +00:00
|
|
|
|
2015-10-07 22:11:52 +00:00
|
|
|
for _, metric := range s.gauges {
|
2016-01-28 23:09:41 +00:00
|
|
|
acc.AddFields(metric.name, metric.fields, metric.tags, now)
|
2015-09-24 18:06:11 +00:00
|
|
|
}
|
|
|
|
if s.DeleteGauges {
|
2015-10-06 19:33:35 +00:00
|
|
|
s.gauges = make(map[string]cachedgauge)
|
2015-09-24 18:06:11 +00:00
|
|
|
}
|
|
|
|
|
2015-10-07 22:11:52 +00:00
|
|
|
for _, metric := range s.counters {
|
2016-01-28 23:09:41 +00:00
|
|
|
acc.AddFields(metric.name, metric.fields, metric.tags, now)
|
2015-09-24 18:06:11 +00:00
|
|
|
}
|
|
|
|
if s.DeleteCounters {
|
2015-10-06 19:33:35 +00:00
|
|
|
s.counters = make(map[string]cachedcounter)
|
2015-09-24 18:06:11 +00:00
|
|
|
}
|
|
|
|
|
2015-10-07 22:11:52 +00:00
|
|
|
for _, metric := range s.sets {
|
2016-01-28 23:09:41 +00:00
|
|
|
fields := make(map[string]interface{})
|
|
|
|
for field, set := range metric.fields {
|
|
|
|
fields[field] = int64(len(set))
|
|
|
|
}
|
|
|
|
acc.AddFields(metric.name, fields, metric.tags, now)
|
2015-09-24 18:06:11 +00:00
|
|
|
}
|
|
|
|
if s.DeleteSets {
|
2015-10-06 19:33:35 +00:00
|
|
|
s.sets = make(map[string]cachedset)
|
2015-09-24 18:06:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *Statsd) Start() error {
|
|
|
|
// Make data structures
|
|
|
|
s.done = make(chan struct{})
|
2016-01-19 18:01:53 +00:00
|
|
|
s.in = make(chan []byte, s.AllowedPendingMessages)
|
2015-10-06 19:33:35 +00:00
|
|
|
s.gauges = make(map[string]cachedgauge)
|
|
|
|
s.counters = make(map[string]cachedcounter)
|
|
|
|
s.sets = make(map[string]cachedset)
|
2015-10-07 22:11:52 +00:00
|
|
|
s.timings = make(map[string]cachedtimings)
|
2015-09-24 18:06:11 +00:00
|
|
|
|
|
|
|
// Start the UDP listener
|
|
|
|
go s.udpListen()
|
|
|
|
// Start the line parser
|
|
|
|
go s.parser()
|
2015-11-16 20:12:45 +00:00
|
|
|
log.Printf("Started the statsd service on %s\n", s.ServiceAddress)
|
2015-09-24 18:06:11 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// udpListen starts listening for udp packets on the configured port.
|
|
|
|
func (s *Statsd) udpListen() error {
|
|
|
|
address, _ := net.ResolveUDPAddr("udp", s.ServiceAddress)
|
|
|
|
listener, err := net.ListenUDP("udp", address)
|
|
|
|
if err != nil {
|
|
|
|
log.Fatalf("ERROR: ListenUDP - %s", err)
|
|
|
|
}
|
|
|
|
defer listener.Close()
|
|
|
|
log.Println("Statsd listener listening on: ", listener.LocalAddr().String())
|
|
|
|
|
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case <-s.done:
|
|
|
|
return nil
|
|
|
|
default:
|
2016-01-19 18:01:53 +00:00
|
|
|
buf := make([]byte, s.UDPPacketSize)
|
2015-09-24 18:06:11 +00:00
|
|
|
n, _, err := listener.ReadFromUDP(buf)
|
|
|
|
if err != nil {
|
|
|
|
log.Printf("ERROR: %s\n", err.Error())
|
|
|
|
}
|
|
|
|
|
2016-01-19 18:01:53 +00:00
|
|
|
select {
|
|
|
|
case s.in <- buf[:n]:
|
|
|
|
default:
|
|
|
|
log.Printf(dropwarn, string(buf[:n]))
|
2015-09-24 18:06:11 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-01-19 18:01:53 +00:00
|
|
|
// parser monitors the s.in channel, if there is a packet ready, it parses the
|
|
|
|
// packet into statsd strings and then calls parseStatsdLine, which parses a
|
|
|
|
// single statsd metric into a struct.
|
2015-09-24 18:06:11 +00:00
|
|
|
func (s *Statsd) parser() error {
|
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case <-s.done:
|
|
|
|
return nil
|
2016-01-19 18:01:53 +00:00
|
|
|
case packet := <-s.in:
|
|
|
|
lines := strings.Split(string(packet), "\n")
|
|
|
|
for _, line := range lines {
|
|
|
|
line = strings.TrimSpace(line)
|
|
|
|
if line != "" {
|
|
|
|
s.parseStatsdLine(line)
|
|
|
|
}
|
|
|
|
}
|
2015-09-24 18:06:11 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// parseStatsdLine will parse the given statsd line, validating it as it goes.
|
|
|
|
// If the line is valid, it will be cached for the next call to Gather()
|
2015-10-05 22:19:08 +00:00
|
|
|
func (s *Statsd) parseStatsdLine(line string) error {
|
2015-09-24 18:06:11 +00:00
|
|
|
s.Lock()
|
|
|
|
defer s.Unlock()
|
|
|
|
|
2015-11-08 10:19:00 +00:00
|
|
|
// Validate splitting the line on ":"
|
|
|
|
bits := strings.Split(line, ":")
|
|
|
|
if len(bits) < 2 {
|
|
|
|
log.Printf("Error: splitting ':', Unable to parse metric: %s\n", line)
|
2015-10-05 22:19:08 +00:00
|
|
|
return errors.New("Error Parsing statsd line")
|
2015-09-24 18:06:11 +00:00
|
|
|
}
|
|
|
|
|
2015-11-08 10:19:00 +00:00
|
|
|
// Extract bucket name from individual metric bits
|
|
|
|
bucketName, bits := bits[0], bits[1:]
|
2015-09-24 18:06:11 +00:00
|
|
|
|
2015-11-08 10:19:00 +00:00
|
|
|
// Add a metric for each bit available
|
|
|
|
for _, bit := range bits {
|
|
|
|
m := metric{}
|
|
|
|
|
|
|
|
m.bucket = bucketName
|
2015-09-24 18:06:11 +00:00
|
|
|
|
2015-11-08 10:19:00 +00:00
|
|
|
// Validate splitting the bit on "|"
|
|
|
|
pipesplit := strings.Split(bit, "|")
|
|
|
|
if len(pipesplit) < 2 {
|
|
|
|
log.Printf("Error: splitting '|', Unable to parse metric: %s\n", line)
|
2015-10-05 22:19:08 +00:00
|
|
|
return errors.New("Error Parsing statsd line")
|
2015-11-08 10:19:00 +00:00
|
|
|
} else if len(pipesplit) > 2 {
|
|
|
|
sr := pipesplit[2]
|
|
|
|
errmsg := "Error: 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)
|
|
|
|
} else {
|
|
|
|
// sample rate successfully parsed
|
|
|
|
m.samplerate = samplerate
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
log.Printf(errmsg, "", line)
|
|
|
|
}
|
2015-09-24 18:06:11 +00:00
|
|
|
}
|
2015-10-06 19:33:35 +00:00
|
|
|
|
2015-11-08 10:19:00 +00:00
|
|
|
// Validate metric type
|
|
|
|
switch pipesplit[1] {
|
|
|
|
case "g", "c", "s", "ms", "h":
|
|
|
|
m.mtype = pipesplit[1]
|
|
|
|
default:
|
|
|
|
log.Printf("Error: Statsd Metric type %s unsupported", pipesplit[1])
|
2015-10-06 19:33:35 +00:00
|
|
|
return errors.New("Error Parsing statsd line")
|
|
|
|
}
|
2015-11-08 10:19:00 +00:00
|
|
|
|
|
|
|
// Parse the value
|
|
|
|
if strings.ContainsAny(pipesplit[0], "-+") {
|
|
|
|
if m.mtype != "g" {
|
|
|
|
log.Printf("Error: +- values are only supported for gauges: %s\n", line)
|
|
|
|
return errors.New("Error Parsing statsd line")
|
|
|
|
}
|
|
|
|
m.additive = true
|
2015-10-06 19:33:35 +00:00
|
|
|
}
|
2015-11-08 10:19:00 +00:00
|
|
|
|
|
|
|
switch m.mtype {
|
|
|
|
case "g", "ms", "h":
|
|
|
|
v, err := strconv.ParseFloat(pipesplit[0], 64)
|
|
|
|
if err != nil {
|
|
|
|
log.Printf("Error: parsing value to float64: %s\n", line)
|
|
|
|
return errors.New("Error Parsing statsd line")
|
|
|
|
}
|
|
|
|
m.floatvalue = v
|
|
|
|
case "c", "s":
|
2016-01-20 19:18:10 +00:00
|
|
|
var v int64
|
2015-11-08 10:19:00 +00:00
|
|
|
v, err := strconv.ParseInt(pipesplit[0], 10, 64)
|
|
|
|
if err != nil {
|
2016-01-20 19:18:10 +00:00
|
|
|
v2, err2 := strconv.ParseFloat(pipesplit[0], 64)
|
|
|
|
if err2 != nil {
|
|
|
|
log.Printf("Error: parsing value to int64: %s\n", line)
|
|
|
|
return errors.New("Error Parsing statsd line")
|
|
|
|
}
|
|
|
|
v = int64(v2)
|
2015-11-08 10:19:00 +00:00
|
|
|
}
|
|
|
|
// If a sample rate is given with a counter, divide value by the rate
|
|
|
|
if m.samplerate != 0 && m.mtype == "c" {
|
|
|
|
v = int64(float64(v) / m.samplerate)
|
|
|
|
}
|
|
|
|
m.intvalue = v
|
2015-10-06 19:33:35 +00:00
|
|
|
}
|
2015-09-24 18:06:11 +00:00
|
|
|
|
2015-11-08 10:19:00 +00:00
|
|
|
// Parse the name & tags from bucket
|
2016-01-28 23:09:41 +00:00
|
|
|
m.name, m.field, m.tags = s.parseName(m.bucket)
|
|
|
|
// fields are not supported for timings, so if specified combine into
|
|
|
|
// the name
|
|
|
|
if (m.mtype == "ms" || m.mtype == "h") && m.field != "value" {
|
|
|
|
m.name += "_" + m.field
|
|
|
|
}
|
2015-11-08 10:19:00 +00:00
|
|
|
switch m.mtype {
|
|
|
|
case "c":
|
|
|
|
m.tags["metric_type"] = "counter"
|
|
|
|
case "g":
|
|
|
|
m.tags["metric_type"] = "gauge"
|
|
|
|
case "s":
|
|
|
|
m.tags["metric_type"] = "set"
|
|
|
|
case "ms":
|
|
|
|
m.tags["metric_type"] = "timing"
|
|
|
|
case "h":
|
|
|
|
m.tags["metric_type"] = "histogram"
|
|
|
|
}
|
|
|
|
|
|
|
|
// Make a unique key for the measurement name/tags
|
|
|
|
var tg []string
|
|
|
|
for k, v := range m.tags {
|
|
|
|
tg = append(tg, fmt.Sprintf("%s=%s", k, v))
|
|
|
|
}
|
|
|
|
sort.Strings(tg)
|
|
|
|
m.hash = fmt.Sprintf("%s%s", strings.Join(tg, ""), m.name)
|
2015-09-24 18:06:11 +00:00
|
|
|
|
2015-11-08 10:19:00 +00:00
|
|
|
s.aggregate(m)
|
2015-10-06 21:38:16 +00:00
|
|
|
}
|
|
|
|
|
2015-10-05 22:19:08 +00:00
|
|
|
return nil
|
2015-09-24 18:06:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// parseName parses the given bucket name with the list of bucket maps in the
|
|
|
|
// config file. If there is a match, it will parse the name of the metric and
|
|
|
|
// map of tags.
|
2016-01-28 23:09:41 +00:00
|
|
|
// Return values are (<name>, <field>, <tags>)
|
|
|
|
func (s *Statsd) parseName(bucket string) (string, string, map[string]string) {
|
2015-10-06 19:33:35 +00:00
|
|
|
tags := make(map[string]string)
|
2015-09-24 18:06:11 +00:00
|
|
|
|
2015-10-07 22:11:52 +00:00
|
|
|
bucketparts := strings.Split(bucket, ",")
|
|
|
|
// Parse out any tags in the bucket
|
|
|
|
if len(bucketparts) > 1 {
|
|
|
|
for _, btag := range bucketparts[1:] {
|
|
|
|
k, v := parseKeyValue(btag)
|
|
|
|
if k != "" {
|
|
|
|
tags[k] = v
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-01-28 23:09:41 +00:00
|
|
|
var field string
|
2015-10-07 22:11:52 +00:00
|
|
|
name := bucketparts[0]
|
2016-02-06 00:36:35 +00:00
|
|
|
p, err := graphite.NewGraphiteParser(".", s.Templates, nil)
|
2015-10-06 21:38:16 +00:00
|
|
|
if err == nil {
|
2016-02-06 00:36:35 +00:00
|
|
|
p.DefaultTags = tags
|
2016-01-28 23:09:41 +00:00
|
|
|
name, tags, field, _ = p.ApplyTemplate(name)
|
2015-10-06 21:38:16 +00:00
|
|
|
}
|
2016-02-06 00:36:35 +00:00
|
|
|
|
2016-01-15 22:38:32 +00:00
|
|
|
if s.ConvertNames {
|
|
|
|
name = strings.Replace(name, ".", "_", -1)
|
|
|
|
name = strings.Replace(name, "-", "__", -1)
|
|
|
|
}
|
2016-01-28 23:09:41 +00:00
|
|
|
if field == "" {
|
|
|
|
field = "value"
|
|
|
|
}
|
2015-10-06 21:38:16 +00:00
|
|
|
|
2016-01-28 23:09:41 +00:00
|
|
|
return name, field, tags
|
2015-10-07 22:11:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Parse the key,value out of a string that looks like "key=value"
|
|
|
|
func parseKeyValue(keyvalue string) (string, string) {
|
|
|
|
var key, val string
|
|
|
|
|
|
|
|
split := strings.Split(keyvalue, "=")
|
|
|
|
// Must be exactly 2 to get anything meaningful out of them
|
|
|
|
if len(split) == 2 {
|
|
|
|
key = split[0]
|
|
|
|
val = split[1]
|
|
|
|
} else if len(split) == 1 {
|
|
|
|
val = split[0]
|
2015-09-24 18:06:11 +00:00
|
|
|
}
|
|
|
|
|
2015-10-07 22:11:52 +00:00
|
|
|
return key, val
|
2015-09-24 18:06:11 +00:00
|
|
|
}
|
|
|
|
|
2015-10-07 22:11:52 +00:00
|
|
|
// aggregate takes in a metric. It then
|
|
|
|
// aggregates and caches the current value(s). It does not deal with the
|
|
|
|
// Delete* options, because those are dealt with in the Gather function.
|
2015-09-24 18:06:11 +00:00
|
|
|
func (s *Statsd) aggregate(m metric) {
|
|
|
|
switch m.mtype {
|
2015-10-07 22:11:52 +00:00
|
|
|
case "ms", "h":
|
|
|
|
cached, ok := s.timings[m.hash]
|
|
|
|
if !ok {
|
|
|
|
cached = cachedtimings{
|
|
|
|
name: m.name,
|
|
|
|
tags: m.tags,
|
|
|
|
stats: RunningStats{
|
|
|
|
PercLimit: s.PercentileLimit,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if m.samplerate > 0 {
|
|
|
|
for i := 0; i < int(1.0/m.samplerate); i++ {
|
|
|
|
cached.stats.AddValue(m.floatvalue)
|
|
|
|
}
|
|
|
|
s.timings[m.hash] = cached
|
|
|
|
} else {
|
|
|
|
cached.stats.AddValue(m.floatvalue)
|
|
|
|
s.timings[m.hash] = cached
|
|
|
|
}
|
2015-09-24 18:06:11 +00:00
|
|
|
case "c":
|
2016-01-28 23:09:41 +00:00
|
|
|
// check if the measurement exists
|
|
|
|
_, ok := s.counters[m.hash]
|
2015-09-24 18:06:11 +00:00
|
|
|
if !ok {
|
2015-10-06 21:38:16 +00:00
|
|
|
s.counters[m.hash] = cachedcounter{
|
2016-01-28 23:09:41 +00:00
|
|
|
name: m.name,
|
|
|
|
fields: make(map[string]interface{}),
|
|
|
|
tags: m.tags,
|
2015-09-24 18:06:11 +00:00
|
|
|
}
|
|
|
|
}
|
2016-01-28 23:09:41 +00:00
|
|
|
// check if the field exists
|
|
|
|
_, ok = s.counters[m.hash].fields[m.field]
|
|
|
|
if !ok {
|
|
|
|
s.counters[m.hash].fields[m.field] = int64(0)
|
|
|
|
}
|
|
|
|
s.counters[m.hash].fields[m.field] =
|
|
|
|
s.counters[m.hash].fields[m.field].(int64) + m.intvalue
|
2015-09-24 18:06:11 +00:00
|
|
|
case "g":
|
2016-01-28 23:09:41 +00:00
|
|
|
// check if the measurement exists
|
|
|
|
_, ok := s.gauges[m.hash]
|
2015-09-24 18:06:11 +00:00
|
|
|
if !ok {
|
2015-10-06 21:38:16 +00:00
|
|
|
s.gauges[m.hash] = cachedgauge{
|
2016-01-28 23:09:41 +00:00
|
|
|
name: m.name,
|
|
|
|
fields: make(map[string]interface{}),
|
|
|
|
tags: m.tags,
|
2015-09-24 18:06:11 +00:00
|
|
|
}
|
2016-01-28 23:09:41 +00:00
|
|
|
}
|
|
|
|
// check if the field exists
|
|
|
|
_, ok = s.gauges[m.hash].fields[m.field]
|
|
|
|
if !ok {
|
|
|
|
s.gauges[m.hash].fields[m.field] = float64(0)
|
|
|
|
}
|
|
|
|
if m.additive {
|
|
|
|
s.gauges[m.hash].fields[m.field] =
|
|
|
|
s.gauges[m.hash].fields[m.field].(float64) + m.floatvalue
|
2015-09-24 18:06:11 +00:00
|
|
|
} else {
|
2016-01-28 23:09:41 +00:00
|
|
|
s.gauges[m.hash].fields[m.field] = m.floatvalue
|
2015-09-24 18:06:11 +00:00
|
|
|
}
|
|
|
|
case "s":
|
2016-01-28 23:09:41 +00:00
|
|
|
// check if the measurement exists
|
|
|
|
_, ok := s.sets[m.hash]
|
2015-09-24 18:06:11 +00:00
|
|
|
if !ok {
|
2015-10-06 21:38:16 +00:00
|
|
|
s.sets[m.hash] = cachedset{
|
2016-01-28 23:09:41 +00:00
|
|
|
name: m.name,
|
|
|
|
fields: make(map[string]map[int64]bool),
|
|
|
|
tags: m.tags,
|
2015-09-24 18:06:11 +00:00
|
|
|
}
|
|
|
|
}
|
2016-01-28 23:09:41 +00:00
|
|
|
// check if the field exists
|
|
|
|
_, ok = s.sets[m.hash].fields[m.field]
|
|
|
|
if !ok {
|
|
|
|
s.sets[m.hash].fields[m.field] = make(map[int64]bool)
|
|
|
|
}
|
|
|
|
s.sets[m.hash].fields[m.field][m.intvalue] = true
|
2015-09-24 18:06:11 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *Statsd) Stop() {
|
|
|
|
s.Lock()
|
|
|
|
defer s.Unlock()
|
|
|
|
log.Println("Stopping the statsd service")
|
|
|
|
close(s.done)
|
|
|
|
close(s.in)
|
|
|
|
}
|
|
|
|
|
|
|
|
func init() {
|
2016-01-27 21:21:36 +00:00
|
|
|
inputs.Add("statsd", func() telegraf.Input {
|
2016-01-19 18:01:53 +00:00
|
|
|
return &Statsd{
|
|
|
|
ConvertNames: true,
|
|
|
|
UDPPacketSize: UDP_PACKET_SIZE,
|
|
|
|
}
|
2015-09-24 18:06:11 +00:00
|
|
|
})
|
|
|
|
}
|