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"
|
2018-11-27 12:53:45 +00:00
|
|
|
"bytes"
|
|
|
|
"encoding/binary"
|
2013-07-05 22:12:43 +00:00
|
|
|
"fmt"
|
2018-11-27 12:53:45 +00:00
|
|
|
"hash/fnv"
|
2017-08-01 10:21:00 +00:00
|
|
|
"io"
|
2013-07-05 22:12:43 +00:00
|
|
|
"net"
|
2018-11-27 12:41:04 +00:00
|
|
|
"sort"
|
2013-07-05 22:12:43 +00:00
|
|
|
"strconv"
|
|
|
|
"strings"
|
2018-11-27 12:53:45 +00:00
|
|
|
"time"
|
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"
|
2018-11-27 12:53:45 +00:00
|
|
|
"github.com/prometheus/common/model"
|
2018-08-10 12:28:38 +00:00
|
|
|
|
2018-12-19 05:21:43 +00:00
|
|
|
"github.com/prometheus/statsd_exporter/pkg/clock"
|
2018-08-14 09:20:00 +00:00
|
|
|
"github.com/prometheus/statsd_exporter/pkg/mapper"
|
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."
|
2019-03-28 21:51:12 +00:00
|
|
|
regErrF = "Failed to update metric %q. Error: %s"
|
2014-06-26 13:56:21 +00:00
|
|
|
)
|
|
|
|
|
2013-07-12 12:27:51 +00:00
|
|
|
var (
|
2018-11-27 12:53:45 +00:00
|
|
|
hash = fnv.New64a()
|
|
|
|
strBuf bytes.Buffer // Used for hashing.
|
|
|
|
intBuf = make([]byte, 8)
|
2013-07-12 12:27:51 +00:00
|
|
|
)
|
|
|
|
|
2019-03-28 21:51:12 +00:00
|
|
|
// uncheckedCollector wraps a Collector but its Describe method yields no Desc.
|
|
|
|
// This allows incoming metrics to have inconsistent label sets
|
|
|
|
type uncheckedCollector struct {
|
|
|
|
c prometheus.Collector
|
|
|
|
}
|
|
|
|
|
|
|
|
func (u uncheckedCollector) Describe(_ chan<- *prometheus.Desc) {}
|
|
|
|
func (u uncheckedCollector) Collect(c chan<- prometheus.Metric) {
|
|
|
|
u.c.Collect(c)
|
|
|
|
}
|
|
|
|
|
2019-05-14 20:04:07 +00:00
|
|
|
type metricType int
|
|
|
|
|
|
|
|
const (
|
|
|
|
CounterMetricType metricType = iota
|
|
|
|
GaugeMetricType
|
|
|
|
SummaryMetricType
|
|
|
|
HistogramMetricType
|
|
|
|
)
|
|
|
|
|
|
|
|
type metricChecker interface {
|
|
|
|
metricConflicts(string, metricType) bool
|
|
|
|
}
|
|
|
|
|
2019-03-28 21:51:12 +00:00
|
|
|
func getLabelNames(labels prometheus.Labels) []string {
|
2018-11-27 12:41:04 +00:00
|
|
|
names := make([]string, 0, len(labels))
|
|
|
|
for labelName := range labels {
|
|
|
|
names = append(names, labelName)
|
|
|
|
}
|
|
|
|
sort.Strings(names)
|
|
|
|
return names
|
2014-06-26 13:56:21 +00:00
|
|
|
}
|
|
|
|
|
2019-03-29 20:25:25 +00:00
|
|
|
func getContainerMapKey(metricName string, labelNames []string) string {
|
|
|
|
return metricName + "," + strings.Join(labelNames, ",")
|
|
|
|
}
|
|
|
|
|
2018-11-27 12:53:45 +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 {
|
2018-11-27 12:41:04 +00:00
|
|
|
// metric name
|
|
|
|
Elements map[string]*prometheus.CounterVec
|
2013-07-05 22:12:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func NewCounterContainer() *CounterContainer {
|
|
|
|
return &CounterContainer{
|
2018-11-27 12:41:04 +00:00
|
|
|
Elements: make(map[string]*prometheus.CounterVec),
|
2013-07-05 22:12:43 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-05-14 20:04:07 +00:00
|
|
|
func (c *CounterContainer) Get(metricName string, labels prometheus.Labels, mc metricChecker, help string) (prometheus.Counter, error) {
|
2019-03-28 21:51:12 +00:00
|
|
|
labelNames := getLabelNames(labels)
|
2019-03-29 20:25:25 +00:00
|
|
|
mapKey := getContainerMapKey(metricName, labelNames)
|
2019-03-28 21:51:12 +00:00
|
|
|
|
|
|
|
counterVec, ok := c.Elements[mapKey]
|
2013-07-05 22:12:43 +00:00
|
|
|
if !ok {
|
2019-05-14 20:04:07 +00:00
|
|
|
if mc.metricConflicts(metricName, CounterMetricType) {
|
|
|
|
return nil, fmt.Errorf("metric with name %s is already registered", metricName)
|
|
|
|
}
|
2019-04-12 17:29:25 +00:00
|
|
|
metricsCount.WithLabelValues("counter").Inc()
|
2018-11-27 12:41:04 +00:00
|
|
|
counterVec = prometheus.NewCounterVec(prometheus.CounterOpts{
|
|
|
|
Name: metricName,
|
|
|
|
Help: help,
|
2019-03-28 21:51:12 +00:00
|
|
|
}, labelNames)
|
|
|
|
if err := prometheus.Register(uncheckedCollector{counterVec}); err != nil {
|
2017-07-18 15:42:12 +00:00
|
|
|
return nil, err
|
2014-06-26 13:56:21 +00:00
|
|
|
}
|
2019-03-28 21:51:12 +00:00
|
|
|
c.Elements[mapKey] = counterVec
|
2013-07-05 22:12:43 +00:00
|
|
|
}
|
2018-11-27 12:41:04 +00:00
|
|
|
return counterVec.GetMetricWith(labels)
|
2013-07-05 22:12:43 +00:00
|
|
|
}
|
|
|
|
|
2018-11-27 12:53:45 +00:00
|
|
|
func (c *CounterContainer) Delete(metricName string, labels prometheus.Labels) {
|
2019-03-28 21:51:12 +00:00
|
|
|
labelNames := getLabelNames(labels)
|
2019-03-29 20:25:25 +00:00
|
|
|
mapKey := getContainerMapKey(metricName, labelNames)
|
2019-03-28 21:51:12 +00:00
|
|
|
if _, ok := c.Elements[mapKey]; ok {
|
|
|
|
c.Elements[mapKey].Delete(labels)
|
2019-04-12 17:29:25 +00:00
|
|
|
metricsCount.WithLabelValues("counter").Dec()
|
2018-11-27 12:53:45 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-07-05 22:12:43 +00:00
|
|
|
type GaugeContainer struct {
|
2018-11-27 12:41:04 +00:00
|
|
|
Elements map[string]*prometheus.GaugeVec
|
2013-07-05 22:12:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func NewGaugeContainer() *GaugeContainer {
|
|
|
|
return &GaugeContainer{
|
2018-11-27 12:41:04 +00:00
|
|
|
Elements: make(map[string]*prometheus.GaugeVec),
|
2013-07-05 22:12:43 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-05-14 20:04:07 +00:00
|
|
|
func (c *GaugeContainer) Get(metricName string, labels prometheus.Labels, mc metricChecker, help string) (prometheus.Gauge, error) {
|
2019-03-28 21:51:12 +00:00
|
|
|
labelNames := getLabelNames(labels)
|
2019-03-29 20:25:25 +00:00
|
|
|
mapKey := getContainerMapKey(metricName, labelNames)
|
2019-03-28 21:51:12 +00:00
|
|
|
|
|
|
|
gaugeVec, ok := c.Elements[mapKey]
|
2013-07-05 22:12:43 +00:00
|
|
|
if !ok {
|
2019-05-14 20:04:07 +00:00
|
|
|
if mc.metricConflicts(metricName, GaugeMetricType) {
|
|
|
|
return nil, fmt.Errorf("metric with name %s is already registered", metricName)
|
|
|
|
}
|
2019-04-12 17:29:25 +00:00
|
|
|
metricsCount.WithLabelValues("gauge").Inc()
|
2018-11-27 12:41:04 +00:00
|
|
|
gaugeVec = prometheus.NewGaugeVec(prometheus.GaugeOpts{
|
|
|
|
Name: metricName,
|
|
|
|
Help: help,
|
2019-03-28 21:51:12 +00:00
|
|
|
}, labelNames)
|
|
|
|
if err := prometheus.Register(uncheckedCollector{gaugeVec}); err != nil {
|
2017-07-18 15:42:12 +00:00
|
|
|
return nil, err
|
2014-06-26 13:56:21 +00:00
|
|
|
}
|
2019-03-28 21:51:12 +00:00
|
|
|
c.Elements[mapKey] = gaugeVec
|
2013-07-05 22:12:43 +00:00
|
|
|
}
|
2018-11-27 12:41:04 +00:00
|
|
|
return gaugeVec.GetMetricWith(labels)
|
2013-07-05 22:12:43 +00:00
|
|
|
}
|
|
|
|
|
2018-11-27 12:53:45 +00:00
|
|
|
func (c *GaugeContainer) Delete(metricName string, labels prometheus.Labels) {
|
2019-03-28 21:51:12 +00:00
|
|
|
labelNames := getLabelNames(labels)
|
2019-03-29 20:25:25 +00:00
|
|
|
mapKey := getContainerMapKey(metricName, labelNames)
|
2019-03-28 21:51:12 +00:00
|
|
|
if _, ok := c.Elements[mapKey]; ok {
|
|
|
|
c.Elements[mapKey].Delete(labels)
|
2019-04-12 17:29:25 +00:00
|
|
|
metricsCount.WithLabelValues("gauge").Dec()
|
2018-11-27 12:53:45 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-07-05 22:12:43 +00:00
|
|
|
type SummaryContainer struct {
|
2018-11-27 12:41:04 +00:00
|
|
|
Elements map[string]*prometheus.SummaryVec
|
2018-08-10 12:28:38 +00:00
|
|
|
mapper *mapper.MetricMapper
|
2013-07-05 22:12:43 +00:00
|
|
|
}
|
|
|
|
|
2018-08-10 12:28:38 +00:00
|
|
|
func NewSummaryContainer(mapper *mapper.MetricMapper) *SummaryContainer {
|
2013-07-05 22:12:43 +00:00
|
|
|
return &SummaryContainer{
|
2018-11-27 12:41:04 +00:00
|
|
|
Elements: make(map[string]*prometheus.SummaryVec),
|
2018-08-09 02:41:41 +00:00
|
|
|
mapper: mapper,
|
2013-07-05 22:12:43 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-05-14 20:04:07 +00:00
|
|
|
func (c *SummaryContainer) Get(metricName string, labels prometheus.Labels, mc metricChecker, help string, mapping *mapper.MetricMapping) (prometheus.Observer, error) {
|
2019-03-28 21:51:12 +00:00
|
|
|
labelNames := getLabelNames(labels)
|
2019-03-29 20:25:25 +00:00
|
|
|
mapKey := getContainerMapKey(metricName, labelNames)
|
2019-03-28 21:51:12 +00:00
|
|
|
|
|
|
|
summaryVec, ok := c.Elements[mapKey]
|
2013-07-05 22:12:43 +00:00
|
|
|
if !ok {
|
2019-05-14 20:04:07 +00:00
|
|
|
if mc.metricConflicts(metricName, SummaryMetricType) {
|
|
|
|
return nil, fmt.Errorf("metric with name %s is already registered", metricName)
|
|
|
|
}
|
|
|
|
if mc.metricConflicts(metricName+"_sum", SummaryMetricType) {
|
|
|
|
return nil, fmt.Errorf("metric with name %s is already registered", metricName)
|
|
|
|
}
|
|
|
|
if mc.metricConflicts(metricName+"_count", SummaryMetricType) {
|
|
|
|
return nil, fmt.Errorf("metric with name %s is already registered", metricName)
|
|
|
|
}
|
2019-04-12 17:29:25 +00:00
|
|
|
metricsCount.WithLabelValues("summary").Inc()
|
2018-08-09 02:41:41 +00:00
|
|
|
quantiles := c.mapper.Defaults.Quantiles
|
|
|
|
if mapping != nil && mapping.Quantiles != nil && len(mapping.Quantiles) > 0 {
|
|
|
|
quantiles = mapping.Quantiles
|
|
|
|
}
|
|
|
|
objectives := make(map[float64]float64)
|
|
|
|
for _, q := range quantiles {
|
|
|
|
objectives[q.Quantile] = q.Error
|
|
|
|
}
|
2019-05-14 00:14:42 +00:00
|
|
|
// In the case of no mapping file, explicitly define the default quantiles
|
|
|
|
if len(objectives) == 0 {
|
|
|
|
objectives = map[float64]float64{0.5: 0.05, 0.9: 0.01, 0.99: 0.001}
|
|
|
|
}
|
2018-11-27 12:41:04 +00:00
|
|
|
summaryVec = prometheus.NewSummaryVec(
|
2014-06-26 13:56:21 +00:00
|
|
|
prometheus.SummaryOpts{
|
2018-11-27 12:41:04 +00:00
|
|
|
Name: metricName,
|
|
|
|
Help: help,
|
|
|
|
Objectives: objectives,
|
2019-03-28 21:51:12 +00:00
|
|
|
}, getLabelNames(labels))
|
|
|
|
if err := prometheus.Register(uncheckedCollector{summaryVec}); err != nil {
|
2017-07-18 15:42:12 +00:00
|
|
|
return nil, err
|
2014-06-26 13:56:21 +00:00
|
|
|
}
|
2019-03-28 21:51:12 +00:00
|
|
|
c.Elements[mapKey] = summaryVec
|
2013-07-05 22:12:43 +00:00
|
|
|
}
|
2018-11-27 12:41:04 +00:00
|
|
|
return summaryVec.GetMetricWith(labels)
|
2013-07-05 22:12:43 +00:00
|
|
|
}
|
|
|
|
|
2018-11-27 12:53:45 +00:00
|
|
|
func (c *SummaryContainer) Delete(metricName string, labels prometheus.Labels) {
|
2019-03-28 21:51:12 +00:00
|
|
|
labelNames := getLabelNames(labels)
|
2019-03-29 20:25:25 +00:00
|
|
|
mapKey := getContainerMapKey(metricName, labelNames)
|
2019-03-28 21:51:12 +00:00
|
|
|
if _, ok := c.Elements[mapKey]; ok {
|
|
|
|
c.Elements[mapKey].Delete(labels)
|
2019-04-12 17:29:25 +00:00
|
|
|
metricsCount.WithLabelValues("summary").Dec()
|
2018-11-27 12:53:45 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-03-13 01:11:05 +00:00
|
|
|
type HistogramContainer struct {
|
2018-11-27 12:41:04 +00:00
|
|
|
Elements map[string]*prometheus.HistogramVec
|
2018-08-10 12:28:38 +00:00
|
|
|
mapper *mapper.MetricMapper
|
2017-03-13 01:11:05 +00:00
|
|
|
}
|
|
|
|
|
2018-08-10 12:28:38 +00:00
|
|
|
func NewHistogramContainer(mapper *mapper.MetricMapper) *HistogramContainer {
|
2017-03-13 01:11:05 +00:00
|
|
|
return &HistogramContainer{
|
2018-11-27 12:41:04 +00:00
|
|
|
Elements: make(map[string]*prometheus.HistogramVec),
|
2017-03-13 10:32:58 +00:00
|
|
|
mapper: mapper,
|
2017-03-13 01:11:05 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-05-14 20:04:07 +00:00
|
|
|
func (c *HistogramContainer) Get(metricName string, labels prometheus.Labels, mc metricChecker, help string, mapping *mapper.MetricMapping) (prometheus.Observer, error) {
|
2019-03-28 21:51:12 +00:00
|
|
|
labelNames := getLabelNames(labels)
|
2019-03-29 20:25:25 +00:00
|
|
|
mapKey := getContainerMapKey(metricName, labelNames)
|
2019-03-28 21:51:12 +00:00
|
|
|
|
|
|
|
histogramVec, ok := c.Elements[mapKey]
|
2017-03-13 01:11:05 +00:00
|
|
|
if !ok {
|
2019-05-15 13:01:35 +00:00
|
|
|
if mc.metricConflicts(metricName, HistogramMetricType) {
|
|
|
|
return nil, fmt.Errorf("metric with name %s is already registered", metricName)
|
|
|
|
}
|
2019-05-14 20:04:07 +00:00
|
|
|
if mc.metricConflicts(metricName+"_sum", HistogramMetricType) {
|
|
|
|
return nil, fmt.Errorf("metric with name %s is already registered", metricName)
|
|
|
|
}
|
|
|
|
if mc.metricConflicts(metricName+"_count", HistogramMetricType) {
|
|
|
|
return nil, fmt.Errorf("metric with name %s is already registered", metricName)
|
|
|
|
}
|
|
|
|
if mc.metricConflicts(metricName+"_bucket", HistogramMetricType) {
|
|
|
|
return nil, fmt.Errorf("metric with name %s is already registered", metricName)
|
|
|
|
}
|
2019-04-12 17:29:25 +00:00
|
|
|
metricsCount.WithLabelValues("histogram").Inc()
|
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
|
|
|
|
}
|
2018-11-27 12:59:01 +00:00
|
|
|
histogramVec = prometheus.NewHistogramVec(
|
2017-03-13 01:11:05 +00:00
|
|
|
prometheus.HistogramOpts{
|
2018-11-27 12:41:04 +00:00
|
|
|
Name: metricName,
|
|
|
|
Help: help,
|
|
|
|
Buckets: buckets,
|
2019-03-28 21:51:12 +00:00
|
|
|
}, labelNames)
|
|
|
|
if err := prometheus.Register(uncheckedCollector{histogramVec}); err != nil {
|
2017-07-26 18:03:08 +00:00
|
|
|
return nil, err
|
2017-03-13 01:11:05 +00:00
|
|
|
}
|
2019-03-28 21:51:12 +00:00
|
|
|
c.Elements[mapKey] = histogramVec
|
2017-03-13 01:11:05 +00:00
|
|
|
}
|
2018-11-27 12:41:04 +00:00
|
|
|
return histogramVec.GetMetricWith(labels)
|
2017-03-13 01:11:05 +00:00
|
|
|
}
|
|
|
|
|
2018-11-27 12:53:45 +00:00
|
|
|
func (c *HistogramContainer) Delete(metricName string, labels prometheus.Labels) {
|
2019-03-28 21:51:12 +00:00
|
|
|
labelNames := getLabelNames(labels)
|
2019-03-29 20:25:25 +00:00
|
|
|
mapKey := getContainerMapKey(metricName, labelNames)
|
2019-03-28 21:51:12 +00:00
|
|
|
if _, ok := c.Elements[mapKey]; ok {
|
|
|
|
c.Elements[mapKey].Delete(labels)
|
2019-04-12 17:29:25 +00:00
|
|
|
metricsCount.WithLabelValues("histogram").Dec()
|
2018-11-27 12:53:45 +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
|
2018-08-10 12:28:38 +00:00
|
|
|
MetricType() mapper.MetricType
|
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
|
|
|
}
|
|
|
|
|
2018-08-10 12:28:38 +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 }
|
|
|
|
func (c *CounterEvent) MetricType() mapper.MetricType { return mapper.MetricTypeCounter }
|
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
|
|
|
}
|
|
|
|
|
2018-08-10 12:28:38 +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 }
|
|
|
|
func (c *GaugeEvent) MetricType() mapper.MetricType { return mapper.MetricTypeGauge }
|
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
|
|
|
}
|
|
|
|
|
2018-08-10 12:28:38 +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 }
|
|
|
|
func (c *TimerEvent) MetricType() mapper.MetricType { return mapper.MetricTypeTimer }
|
2013-07-05 22:12:43 +00:00
|
|
|
|
|
|
|
type Events []Event
|
|
|
|
|
2018-11-27 12:53:45 +00:00
|
|
|
type LabelValues struct {
|
|
|
|
lastRegisteredAt time.Time
|
|
|
|
labels prometheus.Labels
|
2018-12-13 13:53:40 +00:00
|
|
|
ttl time.Duration
|
2019-05-14 20:04:07 +00:00
|
|
|
metricType metricType
|
2018-11-27 12:53:45 +00:00
|
|
|
}
|
|
|
|
|
2015-10-10 00:34:28 +00:00
|
|
|
type Exporter struct {
|
2018-11-27 12:53:45 +00:00
|
|
|
Counters *CounterContainer
|
|
|
|
Gauges *GaugeContainer
|
|
|
|
Summaries *SummaryContainer
|
|
|
|
Histograms *HistogramContainer
|
|
|
|
mapper *mapper.MetricMapper
|
|
|
|
labelValues map[string]map[uint64]*LabelValues
|
2013-07-05 22:12:43 +00:00
|
|
|
}
|
|
|
|
|
2019-04-09 15:52:31 +00:00
|
|
|
// Replace invalid characters in the metric name with "_"
|
|
|
|
// Valid characters are a-z, A-Z, 0-9, and _
|
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.
|
2019-03-23 18:14:08 +00:00
|
|
|
if len(metricName) > 0 && metricName[0] >= '0' && metricName[0] <= '9' {
|
2013-07-12 12:27:51 +00:00
|
|
|
metricName = "_" + metricName
|
|
|
|
}
|
|
|
|
|
2019-04-09 15:52:31 +00:00
|
|
|
// this is an character replacement method optimized for this limited
|
|
|
|
// use case. It is much faster than using a regex.
|
2019-04-02 21:42:39 +00:00
|
|
|
out := make([]byte, len(metricName))
|
|
|
|
j := 0
|
|
|
|
for _, c := range metricName {
|
2019-04-09 18:10:26 +00:00
|
|
|
// check if the rune is valid for a metric name
|
|
|
|
// and replace it if it is not.
|
|
|
|
// As only certain ASCII characters are valid in metric names,
|
|
|
|
// we can use a byte.
|
2019-04-02 21:42:39 +00:00
|
|
|
if (c >= 'a' && c <= 'z') ||
|
|
|
|
(c >= 'A' && c <= 'Z') ||
|
|
|
|
(c >= '0' && c <= '9') {
|
|
|
|
out[j] = byte(c)
|
|
|
|
} else {
|
|
|
|
out[j] = byte('_')
|
|
|
|
}
|
|
|
|
j++
|
|
|
|
}
|
|
|
|
|
|
|
|
return string(out[:j])
|
|
|
|
|
2013-07-05 22:12:43 +00:00
|
|
|
}
|
|
|
|
|
2018-11-02 17:28:18 +00:00
|
|
|
// Listen handles all events sent to the given channel sequentially. It
|
|
|
|
// terminates when the channel is closed.
|
2015-10-10 00:34:28 +00:00
|
|
|
func (b *Exporter) Listen(e <-chan Events) {
|
2018-12-19 05:21:43 +00:00
|
|
|
removeStaleMetricsTicker := clock.NewTicker(time.Second)
|
2018-11-27 12:53:45 +00:00
|
|
|
|
2013-07-05 22:12:43 +00:00
|
|
|
for {
|
2018-11-27 12:53:45 +00:00
|
|
|
select {
|
|
|
|
case <-removeStaleMetricsTicker.C:
|
|
|
|
b.removeStaleMetrics()
|
|
|
|
case events, ok := <-e:
|
|
|
|
if !ok {
|
|
|
|
log.Debug("Channel is closed. Break out of Exporter.Listener.")
|
|
|
|
removeStaleMetricsTicker.Stop()
|
|
|
|
return
|
|
|
|
}
|
|
|
|
for _, event := range events {
|
|
|
|
b.handleEvent(event)
|
|
|
|
}
|
2018-11-02 17:28:18 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2013-07-05 22:12:43 +00:00
|
|
|
|
2018-11-02 17:28:18 +00:00
|
|
|
// handleEvent processes a single Event according to the configured mapping.
|
|
|
|
func (b *Exporter) handleEvent(event Event) {
|
|
|
|
mapping, labels, present := b.mapper.GetMapping(event.MetricName(), event.MetricType())
|
|
|
|
if mapping == nil {
|
|
|
|
mapping = &mapper.MetricMapping{}
|
2018-12-13 13:53:40 +00:00
|
|
|
if b.mapper.Defaults.Ttl != 0 {
|
|
|
|
mapping.Ttl = b.mapper.Defaults.Ttl
|
|
|
|
}
|
2018-11-02 17:28:18 +00:00
|
|
|
}
|
2018-01-02 22:21:50 +00:00
|
|
|
|
2018-11-02 17:28:18 +00:00
|
|
|
if mapping.Action == mapper.ActionTypeDrop {
|
2019-03-25 22:16:38 +00:00
|
|
|
eventsActions.WithLabelValues("drop").Inc()
|
2018-11-02 17:28:18 +00:00
|
|
|
return
|
|
|
|
}
|
2018-01-02 22:21:50 +00:00
|
|
|
|
2018-11-02 17:28:18 +00:00
|
|
|
help := defaultHelp
|
|
|
|
if mapping.HelpText != "" {
|
|
|
|
help = mapping.HelpText
|
|
|
|
}
|
|
|
|
|
|
|
|
metricName := ""
|
|
|
|
prometheusLabels := event.Labels()
|
|
|
|
if present {
|
2019-03-23 18:14:08 +00:00
|
|
|
if mapping.Name == "" {
|
2019-03-25 23:44:17 +00:00
|
|
|
log.Debugf("The mapping of '%s' for match '%s' generates an empty metric name", event.MetricName(), mapping.Match)
|
|
|
|
errorEventStats.WithLabelValues("empty_metric_name").Inc()
|
2019-03-23 18:14:08 +00:00
|
|
|
return
|
|
|
|
}
|
2018-11-02 17:28:18 +00:00
|
|
|
metricName = escapeMetricName(mapping.Name)
|
|
|
|
for label, value := range labels {
|
|
|
|
prometheusLabels[label] = value
|
|
|
|
}
|
2019-03-25 22:16:38 +00:00
|
|
|
eventsActions.WithLabelValues(string(mapping.Action)).Inc()
|
2018-11-02 17:28:18 +00:00
|
|
|
} else {
|
|
|
|
eventsUnmapped.Inc()
|
|
|
|
metricName = escapeMetricName(event.MetricName())
|
|
|
|
}
|
|
|
|
|
|
|
|
switch ev := event.(type) {
|
|
|
|
case *CounterEvent:
|
|
|
|
// 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 {
|
|
|
|
log.Debugf("Counter %q is: '%f' (counter must be non-negative value)", metricName, event.Value())
|
2019-03-25 23:44:17 +00:00
|
|
|
errorEventStats.WithLabelValues("illegal_negative_counter").Inc()
|
2018-11-02 17:28:18 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
counter, err := b.Counters.Get(
|
|
|
|
metricName,
|
|
|
|
prometheusLabels,
|
2019-05-14 20:04:07 +00:00
|
|
|
b,
|
2018-11-02 17:28:18 +00:00
|
|
|
help,
|
|
|
|
)
|
|
|
|
if err == nil {
|
|
|
|
counter.Add(event.Value())
|
2019-05-14 20:04:07 +00:00
|
|
|
b.saveLabelValues(metricName, CounterMetricType, prometheusLabels, mapping.Ttl)
|
2018-11-02 17:28:18 +00:00
|
|
|
eventStats.WithLabelValues("counter").Inc()
|
|
|
|
} else {
|
|
|
|
log.Debugf(regErrF, metricName, err)
|
|
|
|
conflictingEventStats.WithLabelValues("counter").Inc()
|
|
|
|
}
|
|
|
|
|
|
|
|
case *GaugeEvent:
|
|
|
|
gauge, err := b.Gauges.Get(
|
|
|
|
metricName,
|
|
|
|
prometheusLabels,
|
2019-05-14 20:04:07 +00:00
|
|
|
b,
|
2018-11-02 17:28:18 +00:00
|
|
|
help,
|
|
|
|
)
|
|
|
|
|
|
|
|
if err == nil {
|
|
|
|
if ev.relative {
|
|
|
|
gauge.Add(event.Value())
|
2017-10-04 16:11:58 +00:00
|
|
|
} else {
|
2018-11-02 17:28:18 +00:00
|
|
|
gauge.Set(event.Value())
|
2017-10-04 16:11:58 +00:00
|
|
|
}
|
2019-05-14 20:04:07 +00:00
|
|
|
b.saveLabelValues(metricName, GaugeMetricType, prometheusLabels, mapping.Ttl)
|
2018-11-02 17:28:18 +00:00
|
|
|
eventStats.WithLabelValues("gauge").Inc()
|
|
|
|
} else {
|
|
|
|
log.Debugf(regErrF, metricName, err)
|
|
|
|
conflictingEventStats.WithLabelValues("gauge").Inc()
|
|
|
|
}
|
|
|
|
|
|
|
|
case *TimerEvent:
|
|
|
|
t := mapper.TimerTypeDefault
|
|
|
|
if mapping != nil {
|
|
|
|
t = mapping.TimerType
|
|
|
|
}
|
|
|
|
if t == mapper.TimerTypeDefault {
|
|
|
|
t = b.mapper.Defaults.TimerType
|
|
|
|
}
|
|
|
|
|
|
|
|
switch t {
|
|
|
|
case mapper.TimerTypeHistogram:
|
|
|
|
histogram, err := b.Histograms.Get(
|
|
|
|
metricName,
|
|
|
|
prometheusLabels,
|
2019-05-14 20:04:07 +00:00
|
|
|
b,
|
2018-11-02 17:28:18 +00:00
|
|
|
help,
|
|
|
|
mapping,
|
|
|
|
)
|
|
|
|
if err == nil {
|
|
|
|
histogram.Observe(event.Value() / 1000) // prometheus presumes seconds, statsd millisecond
|
2019-05-14 20:04:07 +00:00
|
|
|
b.saveLabelValues(metricName, HistogramMetricType, prometheusLabels, mapping.Ttl)
|
2018-11-02 17:28:18 +00:00
|
|
|
eventStats.WithLabelValues("timer").Inc()
|
2013-07-05 22:12:43 +00:00
|
|
|
} else {
|
2018-11-02 17:28:18 +00:00
|
|
|
log.Debugf(regErrF, metricName, err)
|
|
|
|
conflictingEventStats.WithLabelValues("timer").Inc()
|
2013-07-05 22:12:43 +00:00
|
|
|
}
|
|
|
|
|
2018-11-02 17:28:18 +00:00
|
|
|
case mapper.TimerTypeDefault, mapper.TimerTypeSummary:
|
|
|
|
summary, err := b.Summaries.Get(
|
|
|
|
metricName,
|
|
|
|
prometheusLabels,
|
2019-05-14 20:04:07 +00:00
|
|
|
b,
|
2018-11-02 17:28:18 +00:00
|
|
|
help,
|
|
|
|
mapping,
|
|
|
|
)
|
|
|
|
if err == nil {
|
2019-01-09 15:31:05 +00:00
|
|
|
summary.Observe(event.Value() / 1000) // prometheus presumes seconds, statsd millisecond
|
2019-05-14 20:04:07 +00:00
|
|
|
b.saveLabelValues(metricName, SummaryMetricType, prometheusLabels, mapping.Ttl)
|
2018-11-02 17:28:18 +00:00
|
|
|
eventStats.WithLabelValues("timer").Inc()
|
|
|
|
} else {
|
|
|
|
log.Debugf(regErrF, metricName, err)
|
|
|
|
conflictingEventStats.WithLabelValues("timer").Inc()
|
2013-07-05 22:12:43 +00:00
|
|
|
}
|
2018-11-02 17:28:18 +00:00
|
|
|
|
|
|
|
default:
|
|
|
|
panic(fmt.Sprintf("unknown timer type '%s'", t))
|
2013-07-05 22:12:43 +00:00
|
|
|
}
|
2018-11-02 17:28:18 +00:00
|
|
|
|
|
|
|
default:
|
|
|
|
log.Debugln("Unsupported event type")
|
|
|
|
eventStats.WithLabelValues("illegal").Inc()
|
2013-07-05 22:12:43 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-11-27 12:53:45 +00:00
|
|
|
// removeStaleMetrics removes label values set from metric with stale values
|
|
|
|
func (b *Exporter) removeStaleMetrics() {
|
2018-12-19 05:21:43 +00:00
|
|
|
now := clock.Now()
|
2018-11-27 12:53:45 +00:00
|
|
|
// delete timeseries with expired ttl
|
|
|
|
for metricName := range b.labelValues {
|
|
|
|
for hash, lvs := range b.labelValues[metricName] {
|
2018-11-27 12:59:01 +00:00
|
|
|
if lvs.ttl == 0 {
|
|
|
|
continue
|
|
|
|
}
|
2018-12-13 13:53:40 +00:00
|
|
|
if lvs.lastRegisteredAt.Add(lvs.ttl).Before(now) {
|
2018-11-27 12:53:45 +00:00
|
|
|
b.Counters.Delete(metricName, lvs.labels)
|
|
|
|
b.Gauges.Delete(metricName, lvs.labels)
|
|
|
|
b.Summaries.Delete(metricName, lvs.labels)
|
|
|
|
b.Histograms.Delete(metricName, lvs.labels)
|
|
|
|
delete(b.labelValues[metricName], hash)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-12-13 16:36:19 +00:00
|
|
|
// saveLabelValues stores label values set to labelValues and update lastRegisteredAt time and ttl value
|
2019-05-14 20:04:07 +00:00
|
|
|
func (b *Exporter) saveLabelValues(metricName string, metricType metricType, labels prometheus.Labels, ttl time.Duration) {
|
2018-12-18 12:16:05 +00:00
|
|
|
metric, hasMetric := b.labelValues[metricName]
|
2018-11-27 12:53:45 +00:00
|
|
|
if !hasMetric {
|
2018-12-18 12:16:05 +00:00
|
|
|
metric = make(map[uint64]*LabelValues)
|
|
|
|
b.labelValues[metricName] = metric
|
2018-11-27 12:53:45 +00:00
|
|
|
}
|
|
|
|
hash := hashNameAndLabels(metricName, labels)
|
2018-12-18 12:16:05 +00:00
|
|
|
metricLabelValues, ok := metric[hash]
|
2018-11-27 12:53:45 +00:00
|
|
|
if !ok {
|
2018-12-18 12:16:05 +00:00
|
|
|
metricLabelValues = &LabelValues{
|
2019-05-14 20:04:07 +00:00
|
|
|
labels: labels,
|
|
|
|
ttl: ttl,
|
|
|
|
metricType: metricType,
|
2018-11-27 12:53:45 +00:00
|
|
|
}
|
2018-12-18 12:16:05 +00:00
|
|
|
b.labelValues[metricName][hash] = metricLabelValues
|
2018-11-27 12:53:45 +00:00
|
|
|
}
|
2018-12-19 05:21:43 +00:00
|
|
|
now := clock.Now()
|
2018-12-18 12:16:05 +00:00
|
|
|
metricLabelValues.lastRegisteredAt = now
|
2018-12-13 16:36:19 +00:00
|
|
|
// Update ttl from mapping
|
2018-12-18 12:16:05 +00:00
|
|
|
metricLabelValues.ttl = ttl
|
2018-11-27 12:53:45 +00:00
|
|
|
}
|
|
|
|
|
2019-05-14 20:04:07 +00:00
|
|
|
func (b *Exporter) metricConflicts(metricName string, metricType metricType) bool {
|
|
|
|
metric, hasMetric := b.labelValues[metricName]
|
|
|
|
if !hasMetric {
|
|
|
|
// No metric with this name exists
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
// The metric does exist. All metrics in the hash should be of the same
|
|
|
|
// type, so we pick check the first one we find in the hash to check the
|
|
|
|
// type.
|
|
|
|
for _, lvs := range metric {
|
|
|
|
if lvs.metricType == metricType {
|
|
|
|
// We've found a copy of this metric with this type, but different
|
|
|
|
// labels, so it's safe to create a new one.
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// The metric exists, but it's of a different type than we're trying to
|
|
|
|
// create.
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
2018-08-10 12:28:38 +00:00
|
|
|
func NewExporter(mapper *mapper.MetricMapper) *Exporter {
|
2015-10-10 00:34:28 +00:00
|
|
|
return &Exporter{
|
2018-11-27 12:53:45 +00:00
|
|
|
Counters: NewCounterContainer(),
|
|
|
|
Gauges: NewGaugeContainer(),
|
|
|
|
Summaries: NewSummaryContainer(mapper),
|
|
|
|
Histograms: NewHistogramContainer(mapper),
|
|
|
|
mapper: mapper,
|
2019-01-02 08:54:28 +00:00
|
|
|
labelValues: make(map[string]map[uint64]*LabelValues),
|
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
|
2019-05-13 18:30:38 +00:00
|
|
|
case "ms", "h", "d":
|
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":
|
2019-01-02 08:54:28 +00:00
|
|
|
return nil, fmt.Errorf("no support for StatsD sets")
|
2013-07-05 22:12:43 +00:00
|
|
|
default:
|
2019-01-02 08:54:28 +00:00
|
|
|
return nil, fmt.Errorf("bad stat type %s", statType)
|
2013-07-05 22:12:43 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-05-13 16:10:17 +00:00
|
|
|
func handleDogStatsDTagToKeyValue(labels map[string]string, component, tag string) {
|
2019-05-13 15:42:33 +00:00
|
|
|
// Bail early if the tag is empty
|
|
|
|
if len(tag) == 0 {
|
2019-05-13 16:10:17 +00:00
|
|
|
tagErrors.Inc()
|
|
|
|
log.Debugf("Malformed or empty DogStatsD tag %s in component %s", tag, component)
|
2019-05-13 15:42:33 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
// Skip hash if found.
|
|
|
|
if tag[0] == '#' {
|
|
|
|
tag = tag[1:]
|
|
|
|
}
|
|
|
|
|
|
|
|
// find the first comma and split the tag into key and value.
|
2019-05-13 16:10:17 +00:00
|
|
|
var k, v string
|
2019-05-13 15:42:33 +00:00
|
|
|
for i, c := range tag {
|
|
|
|
if c == ':' {
|
|
|
|
k = tag[0:i]
|
|
|
|
v = tag[(i + 1):]
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
2019-05-13 16:10:17 +00:00
|
|
|
// If either of them is empty, then either the k or v is empty, or we
|
|
|
|
// didn't find a colon, either way, throw an error and skip ahead.
|
|
|
|
if len(k) == 0 || len(v) == 0 {
|
|
|
|
tagErrors.Inc()
|
|
|
|
log.Debugf("Malformed or empty DogStatsD tag %s in component %s", tag, component)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
labels[escapeMetricName(k)] = v
|
2019-05-13 15:42:33 +00:00
|
|
|
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
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()
|
2019-05-13 16:10:17 +00:00
|
|
|
|
|
|
|
lastTagEndIndex := 0
|
|
|
|
for i, c := range component {
|
|
|
|
if c == ',' {
|
|
|
|
tag := component[lastTagEndIndex:i]
|
|
|
|
lastTagEndIndex = i + 1
|
|
|
|
handleDogStatsDTagToKeyValue(labels, component, tag)
|
2016-04-23 21:50:41 +00:00
|
|
|
}
|
2019-05-13 16:10:17 +00:00
|
|
|
}
|
2016-04-23 21:50:41 +00:00
|
|
|
|
2019-05-13 16:10:17 +00:00
|
|
|
// If we're not off the end of the string, add the last tag
|
|
|
|
if lastTagEndIndex < len(component) {
|
|
|
|
tag := component[lastTagEndIndex:]
|
|
|
|
handleDogStatsDTagToKeyValue(labels, component, tag)
|
2016-04-23 21:50:41 +00:00
|
|
|
}
|
2019-05-13 16:10:17 +00:00
|
|
|
|
2016-04-23 21:50:41 +00:00
|
|
|
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 '#':
|
2019-05-13 16:10:17 +00:00
|
|
|
labels = parseDogStatsDTagsToLabels(component[1:])
|
2017-08-01 10:21:00 +00:00
|
|
|
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 {
|
2019-04-22 23:31:24 +00:00
|
|
|
// https://github.com/golang/go/issues/4373
|
|
|
|
// ignore net: errClosing error as it will occur during shutdown
|
|
|
|
if strings.HasSuffix(err.Error(), "use of closed network connection") {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
log.Error(err)
|
|
|
|
return
|
2017-08-01 10:21:00 +00:00
|
|
|
}
|
|
|
|
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 {
|
2019-04-22 23:31:24 +00:00
|
|
|
// https://github.com/golang/go/issues/4373
|
|
|
|
// ignore net: errClosing error as it will occur during shutdown
|
|
|
|
if strings.HasSuffix(err.Error(), "use of closed network connection") {
|
|
|
|
return
|
|
|
|
}
|
2017-08-01 10:21:00 +00:00
|
|
|
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))
|
|
|
|
}
|
|
|
|
}
|
2019-04-05 23:37:23 +00:00
|
|
|
|
|
|
|
type StatsDUnixgramListener struct {
|
|
|
|
conn *net.UnixConn
|
|
|
|
}
|
|
|
|
|
|
|
|
func (l *StatsDUnixgramListener) Listen(e chan<- Events) {
|
|
|
|
buf := make([]byte, 65535)
|
|
|
|
for {
|
|
|
|
n, _, err := l.conn.ReadFromUnix(buf)
|
|
|
|
if err != nil {
|
2019-04-22 23:31:24 +00:00
|
|
|
// https://github.com/golang/go/issues/4373
|
|
|
|
// ignore net: errClosing error as it will occur during shutdown
|
|
|
|
if strings.HasSuffix(err.Error(), "use of closed network connection") {
|
|
|
|
return
|
|
|
|
}
|
2019-04-05 23:37:23 +00:00
|
|
|
log.Fatal(err)
|
|
|
|
}
|
|
|
|
l.handlePacket(buf[:n], e)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (l *StatsDUnixgramListener) handlePacket(packet []byte, e chan<- Events) {
|
|
|
|
unixgramPackets.Inc()
|
|
|
|
lines := strings.Split(string(packet), "\n")
|
|
|
|
events := Events{}
|
|
|
|
for _, line := range lines {
|
|
|
|
linesReceived.Inc()
|
|
|
|
events = append(events, lineToEvents(line)...)
|
|
|
|
}
|
|
|
|
e <- events
|
|
|
|
}
|