2015-02-18 21:40:55 +00:00
|
|
|
// Copyright 2013 The Prometheus Authors
|
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
|
// You may obtain a copy of the License at
|
2013-07-05 22:12:43 +00:00
|
|
|
//
|
2015-02-18 21:40:55 +00:00
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
//
|
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
// limitations under the License.
|
2013-07-05 22:12:43 +00:00
|
|
|
|
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2017-08-01 10:21:00 +00:00
|
|
|
"bufio"
|
2014-06-26 13:56:21 +00:00
|
|
|
"bytes"
|
|
|
|
"encoding/binary"
|
2013-07-05 22:12:43 +00:00
|
|
|
"fmt"
|
2014-06-26 13:56:21 +00:00
|
|
|
"hash/fnv"
|
2017-08-01 10:21:00 +00:00
|
|
|
"io"
|
2013-07-05 22:12:43 +00:00
|
|
|
"net"
|
2013-07-12 12:27:51 +00:00
|
|
|
"regexp"
|
2013-07-05 22:12:43 +00:00
|
|
|
"strconv"
|
|
|
|
"strings"
|
2016-07-15 14:05:47 +00:00
|
|
|
"unicode/utf8"
|
2013-07-05 22:12:43 +00:00
|
|
|
|
|
|
|
"github.com/prometheus/client_golang/prometheus"
|
2016-05-04 19:16:17 +00:00
|
|
|
"github.com/prometheus/common/log"
|
2015-09-17 11:14:18 +00:00
|
|
|
"github.com/prometheus/common/model"
|
2013-07-05 22:12:43 +00:00
|
|
|
)
|
|
|
|
|
2014-06-26 13:56:21 +00:00
|
|
|
const (
|
2015-10-10 00:34:28 +00:00
|
|
|
defaultHelp = "Metric autogenerated by statsd_exporter."
|
2014-06-26 13:56:21 +00:00
|
|
|
regErrF = "A change of configuration created inconsistent metrics for " +
|
2015-10-10 00:34:28 +00:00
|
|
|
"%q. You have to restart the statsd_exporter, and you should " +
|
2014-06-26 13:56:21 +00:00
|
|
|
"consider the effects on your monitoring setup. Error: %s"
|
|
|
|
)
|
|
|
|
|
2013-07-12 12:27:51 +00:00
|
|
|
var (
|
|
|
|
illegalCharsRE = regexp.MustCompile(`[^a-zA-Z0-9_]`)
|
2014-06-26 13:56:21 +00:00
|
|
|
|
|
|
|
hash = fnv.New64a()
|
|
|
|
strBuf bytes.Buffer // Used for hashing.
|
|
|
|
intBuf = make([]byte, 8)
|
2013-07-12 12:27:51 +00:00
|
|
|
)
|
|
|
|
|
2014-06-26 13:56:21 +00:00
|
|
|
// hashNameAndLabels returns a hash value of the provided name string and all
|
|
|
|
// the label names and values in the provided labels map.
|
|
|
|
//
|
|
|
|
// Not safe for concurrent use! (Uses a shared buffer and hasher to save on
|
|
|
|
// allocations.)
|
|
|
|
func hashNameAndLabels(name string, labels prometheus.Labels) uint64 {
|
|
|
|
hash.Reset()
|
|
|
|
strBuf.Reset()
|
|
|
|
strBuf.WriteString(name)
|
|
|
|
hash.Write(strBuf.Bytes())
|
|
|
|
binary.BigEndian.PutUint64(intBuf, model.LabelsToSignature(labels))
|
|
|
|
hash.Write(intBuf)
|
|
|
|
return hash.Sum64()
|
|
|
|
}
|
|
|
|
|
2013-07-05 22:12:43 +00:00
|
|
|
type CounterContainer struct {
|
2014-06-26 13:56:21 +00:00
|
|
|
Elements map[uint64]prometheus.Counter
|
2013-07-05 22:12:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func NewCounterContainer() *CounterContainer {
|
|
|
|
return &CounterContainer{
|
2014-06-26 13:56:21 +00:00
|
|
|
Elements: make(map[uint64]prometheus.Counter),
|
2013-07-05 22:12:43 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-10-04 16:11:58 +00:00
|
|
|
func (c *CounterContainer) Get(metricName string, labels prometheus.Labels, help string) (prometheus.Counter, error) {
|
2014-06-26 13:56:21 +00:00
|
|
|
hash := hashNameAndLabels(metricName, labels)
|
|
|
|
counter, ok := c.Elements[hash]
|
2013-07-05 22:12:43 +00:00
|
|
|
if !ok {
|
2014-06-26 13:56:21 +00:00
|
|
|
counter = prometheus.NewCounter(prometheus.CounterOpts{
|
|
|
|
Name: metricName,
|
2017-10-04 16:11:58 +00:00
|
|
|
Help: help,
|
2014-06-26 13:56:21 +00:00
|
|
|
ConstLabels: labels,
|
|
|
|
})
|
2015-01-12 18:14:29 +00:00
|
|
|
if err := prometheus.Register(counter); err != nil {
|
2017-07-18 15:42:12 +00:00
|
|
|
return nil, err
|
2014-06-26 13:56:21 +00:00
|
|
|
}
|
2017-07-18 15:48:51 +00:00
|
|
|
c.Elements[hash] = counter
|
2013-07-05 22:12:43 +00:00
|
|
|
}
|
2017-07-18 15:42:12 +00:00
|
|
|
return counter, nil
|
2013-07-05 22:12:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type GaugeContainer struct {
|
2014-06-26 13:56:21 +00:00
|
|
|
Elements map[uint64]prometheus.Gauge
|
2013-07-05 22:12:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func NewGaugeContainer() *GaugeContainer {
|
|
|
|
return &GaugeContainer{
|
2014-06-26 13:56:21 +00:00
|
|
|
Elements: make(map[uint64]prometheus.Gauge),
|
2013-07-05 22:12:43 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-10-04 16:11:58 +00:00
|
|
|
func (c *GaugeContainer) Get(metricName string, labels prometheus.Labels, help string) (prometheus.Gauge, error) {
|
2014-06-26 13:56:21 +00:00
|
|
|
hash := hashNameAndLabels(metricName, labels)
|
|
|
|
gauge, ok := c.Elements[hash]
|
2013-07-05 22:12:43 +00:00
|
|
|
if !ok {
|
2014-06-26 13:56:21 +00:00
|
|
|
gauge = prometheus.NewGauge(prometheus.GaugeOpts{
|
|
|
|
Name: metricName,
|
2017-10-04 16:11:58 +00:00
|
|
|
Help: help,
|
2014-06-26 13:56:21 +00:00
|
|
|
ConstLabels: labels,
|
|
|
|
})
|
2015-01-12 18:14:29 +00:00
|
|
|
if err := prometheus.Register(gauge); err != nil {
|
2017-07-18 15:42:12 +00:00
|
|
|
return nil, err
|
2014-06-26 13:56:21 +00:00
|
|
|
}
|
2017-07-18 15:48:51 +00:00
|
|
|
c.Elements[hash] = gauge
|
2013-07-05 22:12:43 +00:00
|
|
|
}
|
2017-07-18 15:42:12 +00:00
|
|
|
return gauge, nil
|
2013-07-05 22:12:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type SummaryContainer struct {
|
2014-06-26 13:56:21 +00:00
|
|
|
Elements map[uint64]prometheus.Summary
|
2013-07-05 22:12:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func NewSummaryContainer() *SummaryContainer {
|
|
|
|
return &SummaryContainer{
|
2014-06-26 13:56:21 +00:00
|
|
|
Elements: make(map[uint64]prometheus.Summary),
|
2013-07-05 22:12:43 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-10-04 16:11:58 +00:00
|
|
|
func (c *SummaryContainer) Get(metricName string, labels prometheus.Labels, help string) (prometheus.Summary, error) {
|
2014-06-26 13:56:21 +00:00
|
|
|
hash := hashNameAndLabels(metricName, labels)
|
|
|
|
summary, ok := c.Elements[hash]
|
2013-07-05 22:12:43 +00:00
|
|
|
if !ok {
|
2014-06-26 13:56:21 +00:00
|
|
|
summary = prometheus.NewSummary(
|
|
|
|
prometheus.SummaryOpts{
|
|
|
|
Name: metricName,
|
2017-10-04 16:11:58 +00:00
|
|
|
Help: help,
|
2014-06-26 13:56:21 +00:00
|
|
|
ConstLabels: labels,
|
|
|
|
})
|
2015-01-12 18:14:29 +00:00
|
|
|
if err := prometheus.Register(summary); err != nil {
|
2017-07-18 15:42:12 +00:00
|
|
|
return nil, err
|
2014-06-26 13:56:21 +00:00
|
|
|
}
|
2017-07-18 15:48:51 +00:00
|
|
|
c.Elements[hash] = summary
|
2013-07-05 22:12:43 +00:00
|
|
|
}
|
2017-07-18 15:42:12 +00:00
|
|
|
return summary, nil
|
2013-07-05 22:12:43 +00:00
|
|
|
}
|
|
|
|
|
2017-03-13 01:11:05 +00:00
|
|
|
type HistogramContainer struct {
|
|
|
|
Elements map[uint64]prometheus.Histogram
|
2017-03-13 10:32:58 +00:00
|
|
|
mapper *metricMapper
|
2017-03-13 01:11:05 +00:00
|
|
|
}
|
|
|
|
|
2017-03-13 10:32:58 +00:00
|
|
|
func NewHistogramContainer(mapper *metricMapper) *HistogramContainer {
|
2017-03-13 01:11:05 +00:00
|
|
|
return &HistogramContainer{
|
|
|
|
Elements: make(map[uint64]prometheus.Histogram),
|
2017-03-13 10:32:58 +00:00
|
|
|
mapper: mapper,
|
2017-03-13 01:11:05 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-10-04 16:11:58 +00:00
|
|
|
func (c *HistogramContainer) Get(metricName string, labels prometheus.Labels, help string, mapping *metricMapping) (prometheus.Histogram, error) {
|
2017-03-13 01:11:05 +00:00
|
|
|
hash := hashNameAndLabels(metricName, labels)
|
|
|
|
histogram, ok := c.Elements[hash]
|
|
|
|
if !ok {
|
2017-03-13 10:32:58 +00:00
|
|
|
buckets := c.mapper.Defaults.Buckets
|
|
|
|
if mapping != nil && mapping.Buckets != nil && len(mapping.Buckets) > 0 {
|
|
|
|
buckets = mapping.Buckets
|
|
|
|
}
|
2017-03-13 01:11:05 +00:00
|
|
|
histogram = prometheus.NewHistogram(
|
|
|
|
prometheus.HistogramOpts{
|
|
|
|
Name: metricName,
|
2017-10-04 16:11:58 +00:00
|
|
|
Help: help,
|
2017-03-13 01:11:05 +00:00
|
|
|
ConstLabels: labels,
|
2017-03-13 10:32:58 +00:00
|
|
|
Buckets: buckets,
|
2017-03-13 01:11:05 +00:00
|
|
|
})
|
|
|
|
c.Elements[hash] = histogram
|
|
|
|
if err := prometheus.Register(histogram); err != nil {
|
2017-07-26 18:03:08 +00:00
|
|
|
return nil, err
|
2017-03-13 01:11:05 +00:00
|
|
|
}
|
|
|
|
}
|
2017-07-26 18:03:08 +00:00
|
|
|
return histogram, nil
|
2017-03-13 01:11:05 +00:00
|
|
|
}
|
|
|
|
|
2013-07-05 22:12:43 +00:00
|
|
|
type Event interface {
|
|
|
|
MetricName() string
|
|
|
|
Value() float64
|
2014-11-04 11:44:59 +00:00
|
|
|
Labels() map[string]string
|
2013-07-05 22:12:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type CounterEvent struct {
|
|
|
|
metricName string
|
|
|
|
value float64
|
2014-11-04 11:44:59 +00:00
|
|
|
labels map[string]string
|
2013-07-05 22:12:43 +00:00
|
|
|
}
|
|
|
|
|
2014-11-04 11:44:59 +00:00
|
|
|
func (c *CounterEvent) MetricName() string { return c.metricName }
|
|
|
|
func (c *CounterEvent) Value() float64 { return c.value }
|
|
|
|
func (c *CounterEvent) Labels() map[string]string { return c.labels }
|
2013-07-05 22:12:43 +00:00
|
|
|
|
|
|
|
type GaugeEvent struct {
|
|
|
|
metricName string
|
|
|
|
value float64
|
2017-03-10 00:35:29 +00:00
|
|
|
relative bool
|
2014-11-04 11:44:59 +00:00
|
|
|
labels map[string]string
|
2013-07-05 22:12:43 +00:00
|
|
|
}
|
|
|
|
|
2014-11-04 11:44:59 +00:00
|
|
|
func (g *GaugeEvent) MetricName() string { return g.metricName }
|
|
|
|
func (g *GaugeEvent) Value() float64 { return g.value }
|
|
|
|
func (c *GaugeEvent) Labels() map[string]string { return c.labels }
|
2013-07-05 22:12:43 +00:00
|
|
|
|
|
|
|
type TimerEvent struct {
|
|
|
|
metricName string
|
|
|
|
value float64
|
2014-11-04 11:44:59 +00:00
|
|
|
labels map[string]string
|
2013-07-05 22:12:43 +00:00
|
|
|
}
|
|
|
|
|
2014-11-04 11:44:59 +00:00
|
|
|
func (t *TimerEvent) MetricName() string { return t.metricName }
|
|
|
|
func (t *TimerEvent) Value() float64 { return t.value }
|
|
|
|
func (c *TimerEvent) Labels() map[string]string { return c.labels }
|
2013-07-05 22:12:43 +00:00
|
|
|
|
|
|
|
type Events []Event
|
|
|
|
|
2015-10-10 00:34:28 +00:00
|
|
|
type Exporter struct {
|
2017-03-13 01:11:05 +00:00
|
|
|
Counters *CounterContainer
|
|
|
|
Gauges *GaugeContainer
|
|
|
|
Summaries *SummaryContainer
|
|
|
|
Histograms *HistogramContainer
|
|
|
|
mapper *metricMapper
|
2013-07-05 22:12:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func escapeMetricName(metricName string) string {
|
2013-07-12 12:27:51 +00:00
|
|
|
// If a metric starts with a digit, prepend an underscore.
|
|
|
|
if metricName[0] >= '0' && metricName[0] <= '9' {
|
|
|
|
metricName = "_" + metricName
|
|
|
|
}
|
|
|
|
|
|
|
|
// Replace all illegal metric chars with underscores.
|
|
|
|
metricName = illegalCharsRE.ReplaceAllString(metricName, "_")
|
2013-07-05 22:12:43 +00:00
|
|
|
return metricName
|
|
|
|
}
|
|
|
|
|
2015-10-10 00:34:28 +00:00
|
|
|
func (b *Exporter) Listen(e <-chan Events) {
|
2013-07-05 22:12:43 +00:00
|
|
|
for {
|
2016-03-30 01:55:14 +00:00
|
|
|
events, ok := <-e
|
|
|
|
if !ok {
|
|
|
|
log.Debug("Channel is closed. Break out of Exporter.Listener.")
|
|
|
|
return
|
|
|
|
}
|
2013-07-05 22:12:43 +00:00
|
|
|
for _, event := range events {
|
2017-10-04 16:11:58 +00:00
|
|
|
var help string
|
2013-07-05 22:12:43 +00:00
|
|
|
metricName := ""
|
2014-11-04 11:44:59 +00:00
|
|
|
prometheusLabels := event.Labels()
|
2013-07-05 22:12:43 +00:00
|
|
|
|
2017-03-13 01:11:05 +00:00
|
|
|
mapping, labels, present := b.mapper.getMapping(event.MetricName())
|
2017-10-04 16:11:58 +00:00
|
|
|
if mapping == nil {
|
|
|
|
mapping = &metricMapping{}
|
|
|
|
}
|
2018-01-02 22:21:50 +00:00
|
|
|
|
|
|
|
if mapping.Action == actionTypeDrop {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2017-10-04 16:11:58 +00:00
|
|
|
if mapping.HelpText == "" {
|
|
|
|
help = defaultHelp
|
|
|
|
} else {
|
|
|
|
help = mapping.HelpText
|
|
|
|
}
|
2013-07-05 22:12:43 +00:00
|
|
|
if present {
|
2018-01-16 13:16:15 +00:00
|
|
|
metricName = escapeMetricName(mapping.Name)
|
2013-07-05 22:12:43 +00:00
|
|
|
for label, value := range labels {
|
2017-11-13 12:21:29 +00:00
|
|
|
prometheusLabels[label] = value
|
2013-07-05 22:12:43 +00:00
|
|
|
}
|
|
|
|
} else {
|
2016-05-02 19:19:46 +00:00
|
|
|
eventsUnmapped.Inc()
|
2013-07-05 22:12:43 +00:00
|
|
|
metricName = escapeMetricName(event.MetricName())
|
|
|
|
}
|
|
|
|
|
2017-03-10 00:35:29 +00:00
|
|
|
switch ev := event.(type) {
|
2013-07-05 22:12:43 +00:00
|
|
|
case *CounterEvent:
|
2016-03-30 01:55:14 +00:00
|
|
|
// We don't accept negative values for counters. Incrementing the counter with a negative number
|
|
|
|
// will cause the exporter to panic. Instead we will warn and continue to the next event.
|
|
|
|
if event.Value() < 0.0 {
|
2017-08-14 23:04:57 +00:00
|
|
|
log.Debugf("Counter %q is: '%f' (counter must be non-negative value)", metricName, event.Value())
|
2017-08-15 16:03:13 +00:00
|
|
|
eventStats.WithLabelValues("illegal_negative_counter").Inc()
|
2016-03-30 01:55:14 +00:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2017-07-18 15:42:12 +00:00
|
|
|
counter, err := b.Counters.Get(
|
2017-09-28 18:30:17 +00:00
|
|
|
metricName,
|
2017-07-18 15:37:03 +00:00
|
|
|
prometheusLabels,
|
2017-10-04 16:11:58 +00:00
|
|
|
help,
|
2017-07-18 15:37:03 +00:00
|
|
|
)
|
2017-07-18 15:42:12 +00:00
|
|
|
if err == nil {
|
|
|
|
counter.Add(event.Value())
|
2017-07-18 15:37:03 +00:00
|
|
|
|
2017-07-18 15:42:12 +00:00
|
|
|
eventStats.WithLabelValues("counter").Inc()
|
|
|
|
} else {
|
2017-08-14 23:04:57 +00:00
|
|
|
log.Debugf(regErrF, metricName, err)
|
2017-07-18 15:56:27 +00:00
|
|
|
conflictingEventStats.WithLabelValues("counter").Inc()
|
2017-07-18 15:42:12 +00:00
|
|
|
}
|
2013-07-05 22:12:43 +00:00
|
|
|
|
|
|
|
case *GaugeEvent:
|
2017-07-18 15:42:12 +00:00
|
|
|
gauge, err := b.Gauges.Get(
|
2017-09-28 18:30:17 +00:00
|
|
|
metricName,
|
2014-06-26 13:56:21 +00:00
|
|
|
prometheusLabels,
|
2017-10-04 16:11:58 +00:00
|
|
|
help,
|
2014-06-26 13:56:21 +00:00
|
|
|
)
|
2017-03-09 22:50:06 +00:00
|
|
|
|
2017-07-18 15:42:12 +00:00
|
|
|
if err == nil {
|
|
|
|
if ev.relative {
|
|
|
|
gauge.Add(event.Value())
|
|
|
|
} else {
|
|
|
|
gauge.Set(event.Value())
|
|
|
|
}
|
|
|
|
|
|
|
|
eventStats.WithLabelValues("gauge").Inc()
|
2017-03-09 22:50:06 +00:00
|
|
|
} else {
|
2017-08-14 23:04:57 +00:00
|
|
|
log.Debugf(regErrF, metricName, err)
|
2017-07-18 15:56:27 +00:00
|
|
|
conflictingEventStats.WithLabelValues("gauge").Inc()
|
2017-03-09 22:50:06 +00:00
|
|
|
}
|
2013-07-05 22:12:43 +00:00
|
|
|
|
|
|
|
case *TimerEvent:
|
2017-07-26 18:50:29 +00:00
|
|
|
t := timerTypeDefault
|
2017-03-13 10:32:58 +00:00
|
|
|
if mapping != nil {
|
2017-07-26 18:50:29 +00:00
|
|
|
t = mapping.TimerType
|
2017-03-13 10:32:58 +00:00
|
|
|
}
|
2017-07-26 18:50:29 +00:00
|
|
|
if t == timerTypeDefault {
|
|
|
|
t = b.mapper.Defaults.TimerType
|
2017-03-13 10:32:58 +00:00
|
|
|
}
|
2013-07-05 22:12:43 +00:00
|
|
|
|
2017-07-26 18:50:29 +00:00
|
|
|
switch t {
|
2017-07-26 22:40:56 +00:00
|
|
|
case timerTypeHistogram:
|
2017-07-26 18:03:08 +00:00
|
|
|
histogram, err := b.Histograms.Get(
|
2017-09-28 18:30:17 +00:00
|
|
|
metricName,
|
2017-03-13 01:11:05 +00:00
|
|
|
prometheusLabels,
|
2017-10-04 16:11:58 +00:00
|
|
|
help,
|
2017-03-13 01:11:05 +00:00
|
|
|
mapping,
|
|
|
|
)
|
2017-07-26 18:03:08 +00:00
|
|
|
if err == nil {
|
2017-08-14 18:20:47 +00:00
|
|
|
histogram.Observe(event.Value() / 1000) // prometheus presumes seconds, statsd millisecond
|
2017-07-26 18:03:08 +00:00
|
|
|
eventStats.WithLabelValues("timer").Inc()
|
|
|
|
} else {
|
2017-08-14 23:04:57 +00:00
|
|
|
log.Debugf(regErrF, metricName, err)
|
2017-07-26 18:03:08 +00:00
|
|
|
conflictingEventStats.WithLabelValues("timer").Inc()
|
|
|
|
}
|
2017-07-26 18:50:29 +00:00
|
|
|
|
|
|
|
case timerTypeDefault, timerTypeSummary:
|
2017-07-26 18:03:08 +00:00
|
|
|
summary, err := b.Summaries.Get(
|
2017-09-28 18:30:17 +00:00
|
|
|
metricName,
|
2017-03-13 01:11:05 +00:00
|
|
|
prometheusLabels,
|
2017-10-04 16:11:58 +00:00
|
|
|
help,
|
2017-03-13 01:11:05 +00:00
|
|
|
)
|
2017-07-26 18:03:08 +00:00
|
|
|
if err == nil {
|
|
|
|
summary.Observe(event.Value())
|
|
|
|
eventStats.WithLabelValues("timer").Inc()
|
|
|
|
} else {
|
2017-08-14 23:04:57 +00:00
|
|
|
log.Debugf(regErrF, metricName, err)
|
2017-07-26 18:03:08 +00:00
|
|
|
conflictingEventStats.WithLabelValues("timer").Inc()
|
|
|
|
}
|
2017-07-26 18:50:29 +00:00
|
|
|
|
|
|
|
default:
|
|
|
|
panic(fmt.Sprintf("unknown timer type '%s'", t))
|
2017-07-18 15:42:12 +00:00
|
|
|
}
|
2013-07-05 22:12:43 +00:00
|
|
|
|
|
|
|
default:
|
2017-08-14 23:04:57 +00:00
|
|
|
log.Debugln("Unsupported event type")
|
2014-06-26 13:56:21 +00:00
|
|
|
eventStats.WithLabelValues("illegal").Inc()
|
2013-07-05 22:12:43 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-09-28 18:30:17 +00:00
|
|
|
func NewExporter(mapper *metricMapper) *Exporter {
|
2015-10-10 00:34:28 +00:00
|
|
|
return &Exporter{
|
2017-03-13 01:11:05 +00:00
|
|
|
Counters: NewCounterContainer(),
|
|
|
|
Gauges: NewGaugeContainer(),
|
|
|
|
Summaries: NewSummaryContainer(),
|
2017-03-13 10:32:58 +00:00
|
|
|
Histograms: NewHistogramContainer(mapper),
|
2017-03-13 01:11:05 +00:00
|
|
|
mapper: mapper,
|
2013-07-05 22:12:43 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-03-10 00:35:29 +00:00
|
|
|
func buildEvent(statType, metric string, value float64, relative bool, labels map[string]string) (Event, error) {
|
2013-07-05 22:12:43 +00:00
|
|
|
switch statType {
|
|
|
|
case "c":
|
|
|
|
return &CounterEvent{
|
|
|
|
metricName: metric,
|
|
|
|
value: float64(value),
|
2014-11-04 11:44:59 +00:00
|
|
|
labels: labels,
|
2013-07-05 22:12:43 +00:00
|
|
|
}, nil
|
|
|
|
case "g":
|
|
|
|
return &GaugeEvent{
|
|
|
|
metricName: metric,
|
|
|
|
value: float64(value),
|
2017-03-10 00:35:29 +00:00
|
|
|
relative: relative,
|
2014-11-04 11:44:59 +00:00
|
|
|
labels: labels,
|
2013-07-05 22:12:43 +00:00
|
|
|
}, nil
|
2014-11-04 11:44:59 +00:00
|
|
|
case "ms", "h":
|
2013-07-05 22:12:43 +00:00
|
|
|
return &TimerEvent{
|
|
|
|
metricName: metric,
|
|
|
|
value: float64(value),
|
2014-11-04 11:44:59 +00:00
|
|
|
labels: labels,
|
2013-07-05 22:12:43 +00:00
|
|
|
}, nil
|
|
|
|
case "s":
|
|
|
|
return nil, fmt.Errorf("No support for StatsD sets")
|
|
|
|
default:
|
|
|
|
return nil, fmt.Errorf("Bad stat type %s", statType)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-04-23 21:50:41 +00:00
|
|
|
func parseDogStatsDTagsToLabels(component string) map[string]string {
|
|
|
|
labels := map[string]string{}
|
2017-11-10 19:23:54 +00:00
|
|
|
tagsReceived.Inc()
|
2016-04-23 21:50:41 +00:00
|
|
|
tags := strings.Split(component, ",")
|
|
|
|
for _, t := range tags {
|
|
|
|
t = strings.TrimPrefix(t, "#")
|
|
|
|
kv := strings.SplitN(t, ":", 2)
|
|
|
|
|
|
|
|
if len(kv) < 2 || len(kv[1]) == 0 {
|
2017-11-10 19:23:54 +00:00
|
|
|
tagErrors.Inc()
|
2017-08-14 23:04:57 +00:00
|
|
|
log.Debugf("Malformed or empty DogStatsD tag %s in component %s", t, component)
|
2016-04-23 21:50:41 +00:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
labels[escapeMetricName(kv[0])] = kv[1]
|
|
|
|
}
|
|
|
|
return labels
|
|
|
|
}
|
|
|
|
|
2017-08-01 10:21:00 +00:00
|
|
|
func lineToEvents(line string) Events {
|
2013-07-05 22:12:43 +00:00
|
|
|
events := Events{}
|
2017-08-01 10:21:00 +00:00
|
|
|
if line == "" {
|
|
|
|
return events
|
|
|
|
}
|
2016-07-21 15:55:47 +00:00
|
|
|
|
2017-08-01 10:21:00 +00:00
|
|
|
elements := strings.SplitN(line, ":", 2)
|
|
|
|
if len(elements) < 2 || len(elements[0]) == 0 || !utf8.ValidString(line) {
|
2017-11-10 19:23:54 +00:00
|
|
|
sampleErrors.WithLabelValues("malformed_line").Inc()
|
2017-08-14 23:04:57 +00:00
|
|
|
log.Debugln("Bad line from StatsD:", line)
|
2017-08-01 10:21:00 +00:00
|
|
|
return events
|
|
|
|
}
|
|
|
|
metric := elements[0]
|
|
|
|
var samples []string
|
|
|
|
if strings.Contains(elements[1], "|#") {
|
|
|
|
// using datadog extensions, disable multi-metrics
|
|
|
|
samples = elements[1:]
|
|
|
|
} else {
|
|
|
|
samples = strings.Split(elements[1], ":")
|
|
|
|
}
|
|
|
|
samples:
|
|
|
|
for _, sample := range samples {
|
2017-11-10 19:23:54 +00:00
|
|
|
samplesReceived.Inc()
|
2017-08-01 10:21:00 +00:00
|
|
|
components := strings.Split(sample, "|")
|
|
|
|
samplingFactor := 1.0
|
|
|
|
if len(components) < 2 || len(components) > 4 {
|
2017-11-10 19:23:54 +00:00
|
|
|
sampleErrors.WithLabelValues("malformed_component").Inc()
|
2017-08-14 23:04:57 +00:00
|
|
|
log.Debugln("Bad component on line:", line)
|
2013-07-05 22:12:43 +00:00
|
|
|
continue
|
|
|
|
}
|
2017-08-01 10:21:00 +00:00
|
|
|
valueStr, statType := components[0], components[1]
|
|
|
|
|
|
|
|
var relative = false
|
|
|
|
if strings.Index(valueStr, "+") == 0 || strings.Index(valueStr, "-") == 0 {
|
|
|
|
relative = true
|
2014-11-04 11:44:59 +00:00
|
|
|
}
|
2017-03-09 22:50:06 +00:00
|
|
|
|
2017-08-01 10:21:00 +00:00
|
|
|
value, err := strconv.ParseFloat(valueStr, 64)
|
|
|
|
if err != nil {
|
2017-08-14 23:04:57 +00:00
|
|
|
log.Debugf("Bad value %s on line: %s", valueStr, line)
|
2017-11-10 19:23:54 +00:00
|
|
|
sampleErrors.WithLabelValues("malformed_value").Inc()
|
2017-08-01 10:21:00 +00:00
|
|
|
continue
|
|
|
|
}
|
2017-03-09 22:50:06 +00:00
|
|
|
|
2017-08-01 10:21:00 +00:00
|
|
|
multiplyEvents := 1
|
|
|
|
labels := map[string]string{}
|
|
|
|
if len(components) >= 3 {
|
|
|
|
for _, component := range components[2:] {
|
|
|
|
if len(component) == 0 {
|
2017-08-14 23:04:57 +00:00
|
|
|
log.Debugln("Empty component on line: ", line)
|
2017-11-10 19:23:54 +00:00
|
|
|
sampleErrors.WithLabelValues("malformed_component").Inc()
|
2017-08-01 10:21:00 +00:00
|
|
|
continue samples
|
|
|
|
}
|
2013-07-05 22:12:43 +00:00
|
|
|
}
|
|
|
|
|
2017-08-01 10:21:00 +00:00
|
|
|
for _, component := range components[2:] {
|
|
|
|
switch component[0] {
|
|
|
|
case '@':
|
|
|
|
if statType != "c" && statType != "ms" {
|
2017-08-14 23:04:57 +00:00
|
|
|
log.Debugln("Illegal sampling factor for non-counter metric on line", line)
|
2017-11-10 19:23:54 +00:00
|
|
|
sampleErrors.WithLabelValues("illegal_sample_factor").Inc()
|
2017-08-01 10:21:00 +00:00
|
|
|
continue
|
2016-05-19 13:17:36 +00:00
|
|
|
}
|
2017-08-01 10:21:00 +00:00
|
|
|
samplingFactor, err = strconv.ParseFloat(component[1:], 64)
|
|
|
|
if err != nil {
|
2017-08-14 23:04:57 +00:00
|
|
|
log.Debugf("Invalid sampling factor %s on line %s", component[1:], line)
|
2017-11-10 19:23:54 +00:00
|
|
|
sampleErrors.WithLabelValues("invalid_sample_factor").Inc()
|
2014-11-04 11:44:59 +00:00
|
|
|
}
|
2017-08-01 10:21:00 +00:00
|
|
|
if samplingFactor == 0 {
|
|
|
|
samplingFactor = 1
|
|
|
|
}
|
2013-07-05 22:12:43 +00:00
|
|
|
|
2017-08-01 10:21:00 +00:00
|
|
|
if statType == "c" {
|
|
|
|
value /= samplingFactor
|
|
|
|
} else if statType == "ms" {
|
|
|
|
multiplyEvents = int(1 / samplingFactor)
|
|
|
|
}
|
|
|
|
case '#':
|
|
|
|
labels = parseDogStatsDTagsToLabels(component)
|
|
|
|
default:
|
2017-08-14 23:04:57 +00:00
|
|
|
log.Debugf("Invalid sampling factor or tag section %s on line %s", components[2], line)
|
2017-11-10 19:23:54 +00:00
|
|
|
sampleErrors.WithLabelValues("invalid_sample_factor").Inc()
|
2017-05-15 13:57:31 +00:00
|
|
|
continue
|
|
|
|
}
|
2013-07-05 22:12:43 +00:00
|
|
|
}
|
|
|
|
}
|
2017-08-01 10:21:00 +00:00
|
|
|
|
|
|
|
for i := 0; i < multiplyEvents; i++ {
|
|
|
|
event, err := buildEvent(statType, metric, value, relative, labels)
|
|
|
|
if err != nil {
|
2017-08-14 23:04:57 +00:00
|
|
|
log.Debugf("Error building event on line %s: %s", line, err)
|
2017-11-10 19:23:54 +00:00
|
|
|
sampleErrors.WithLabelValues("illegal_event").Inc()
|
2017-08-01 10:21:00 +00:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
events = append(events, event)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return events
|
|
|
|
}
|
|
|
|
|
|
|
|
type StatsDUDPListener struct {
|
|
|
|
conn *net.UDPConn
|
|
|
|
}
|
|
|
|
|
|
|
|
func (l *StatsDUDPListener) Listen(e chan<- Events) {
|
|
|
|
buf := make([]byte, 65535)
|
|
|
|
for {
|
|
|
|
n, _, err := l.conn.ReadFromUDP(buf)
|
|
|
|
if err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
|
|
|
l.handlePacket(buf[0:n], e)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (l *StatsDUDPListener) handlePacket(packet []byte, e chan<- Events) {
|
2017-11-10 19:23:54 +00:00
|
|
|
udpPackets.Inc()
|
2017-08-01 10:21:00 +00:00
|
|
|
lines := strings.Split(string(packet), "\n")
|
|
|
|
events := Events{}
|
|
|
|
for _, line := range lines {
|
2017-11-10 19:23:54 +00:00
|
|
|
linesReceived.Inc()
|
2017-08-01 10:21:00 +00:00
|
|
|
events = append(events, lineToEvents(line)...)
|
2013-07-05 22:12:43 +00:00
|
|
|
}
|
|
|
|
e <- events
|
|
|
|
}
|
2017-08-01 10:21:00 +00:00
|
|
|
|
|
|
|
type StatsDTCPListener struct {
|
|
|
|
conn *net.TCPListener
|
|
|
|
}
|
|
|
|
|
|
|
|
func (l *StatsDTCPListener) Listen(e chan<- Events) {
|
|
|
|
for {
|
|
|
|
c, err := l.conn.AcceptTCP()
|
|
|
|
if err != nil {
|
|
|
|
log.Fatalf("AcceptTCP failed: %v", err)
|
|
|
|
}
|
|
|
|
go l.handleConn(c, e)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (l *StatsDTCPListener) handleConn(c *net.TCPConn, e chan<- Events) {
|
|
|
|
defer c.Close()
|
|
|
|
|
2017-11-10 19:23:54 +00:00
|
|
|
tcpConnections.Inc()
|
|
|
|
|
2017-08-01 10:21:00 +00:00
|
|
|
r := bufio.NewReader(c)
|
|
|
|
for {
|
|
|
|
line, isPrefix, err := r.ReadLine()
|
|
|
|
if err != nil {
|
|
|
|
if err != io.EOF {
|
2017-11-10 19:23:54 +00:00
|
|
|
tcpErrors.Inc()
|
2017-08-14 23:04:57 +00:00
|
|
|
log.Debugf("Read %s failed: %v", c.RemoteAddr(), err)
|
2017-08-01 10:21:00 +00:00
|
|
|
}
|
|
|
|
break
|
|
|
|
}
|
|
|
|
if isPrefix {
|
2017-11-10 19:23:54 +00:00
|
|
|
tcpLineTooLong.Inc()
|
2017-08-14 23:04:57 +00:00
|
|
|
log.Debugf("Read %s failed: line too long", c.RemoteAddr())
|
2017-08-01 10:21:00 +00:00
|
|
|
break
|
|
|
|
}
|
2017-11-10 19:23:54 +00:00
|
|
|
linesReceived.Inc()
|
2017-08-01 10:21:00 +00:00
|
|
|
e <- lineToEvents(string(line))
|
|
|
|
}
|
|
|
|
}
|