mirror of
https://github.com/prometheus/statsd_exporter.git
synced 2024-11-29 02:31:00 +00:00
Simplified inc/dec logic based on PR feedback.
There is no need to add the string to all event types as only Gauge is compatible with inc/dec functionality. Removed valueStr from all events and added a simple boolean to the Gauge event instead
This commit is contained in:
parent
7fc8727306
commit
3efcef6229
2 changed files with 10 additions and 56 deletions
|
@ -33,7 +33,6 @@ func TestHandlePacket(t *testing.T) {
|
||||||
&CounterEvent{
|
&CounterEvent{
|
||||||
metricName: "foo",
|
metricName: "foo",
|
||||||
value: 2,
|
value: 2,
|
||||||
valueStr: "2",
|
|
||||||
labels: map[string]string{},
|
labels: map[string]string{},
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
|
@ -44,18 +43,6 @@ func TestHandlePacket(t *testing.T) {
|
||||||
&GaugeEvent{
|
&GaugeEvent{
|
||||||
metricName: "foo",
|
metricName: "foo",
|
||||||
value: 3,
|
value: 3,
|
||||||
valueStr: "3",
|
|
||||||
labels: map[string]string{},
|
|
||||||
},
|
|
||||||
},
|
|
||||||
}, {
|
|
||||||
name: "simple gauge with sampling",
|
|
||||||
in: "foo:100|g|@0.1",
|
|
||||||
out: Events{
|
|
||||||
&GaugeEvent{
|
|
||||||
metricName: "foo",
|
|
||||||
value: 1000,
|
|
||||||
valueStr: "1000",
|
|
||||||
labels: map[string]string{},
|
labels: map[string]string{},
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
|
@ -66,7 +53,7 @@ func TestHandlePacket(t *testing.T) {
|
||||||
&GaugeEvent{
|
&GaugeEvent{
|
||||||
metricName: "foo",
|
metricName: "foo",
|
||||||
value: -10,
|
value: -10,
|
||||||
valueStr: "-10",
|
relative: true,
|
||||||
labels: map[string]string{},
|
labels: map[string]string{},
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
|
@ -77,7 +64,6 @@ func TestHandlePacket(t *testing.T) {
|
||||||
&TimerEvent{
|
&TimerEvent{
|
||||||
metricName: "foo",
|
metricName: "foo",
|
||||||
value: 200,
|
value: 200,
|
||||||
valueStr: "200",
|
|
||||||
labels: map[string]string{},
|
labels: map[string]string{},
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
|
@ -88,7 +74,6 @@ func TestHandlePacket(t *testing.T) {
|
||||||
&CounterEvent{
|
&CounterEvent{
|
||||||
metricName: "foo",
|
metricName: "foo",
|
||||||
value: 100,
|
value: 100,
|
||||||
valueStr: "100",
|
|
||||||
labels: map[string]string{"tag1": "bar", "tag2": "baz"},
|
labels: map[string]string{"tag1": "bar", "tag2": "baz"},
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
|
@ -99,7 +84,6 @@ func TestHandlePacket(t *testing.T) {
|
||||||
&CounterEvent{
|
&CounterEvent{
|
||||||
metricName: "foo",
|
metricName: "foo",
|
||||||
value: 100,
|
value: 100,
|
||||||
valueStr: "100",
|
|
||||||
labels: map[string]string{"tag1": "bar", "tag2": "baz"},
|
labels: map[string]string{"tag1": "bar", "tag2": "baz"},
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
|
@ -110,7 +94,6 @@ func TestHandlePacket(t *testing.T) {
|
||||||
&CounterEvent{
|
&CounterEvent{
|
||||||
metricName: "foo",
|
metricName: "foo",
|
||||||
value: 100,
|
value: 100,
|
||||||
valueStr: "100",
|
|
||||||
labels: map[string]string{"_09digits": "0", "tag_with_dots": "1"},
|
labels: map[string]string{"_09digits": "0", "tag_with_dots": "1"},
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
|
@ -121,7 +104,6 @@ func TestHandlePacket(t *testing.T) {
|
||||||
&CounterEvent{
|
&CounterEvent{
|
||||||
metricName: "foo",
|
metricName: "foo",
|
||||||
value: 100,
|
value: 100,
|
||||||
valueStr: "100",
|
|
||||||
labels: map[string]string{},
|
labels: map[string]string{},
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
|
@ -132,7 +114,6 @@ func TestHandlePacket(t *testing.T) {
|
||||||
&CounterEvent{
|
&CounterEvent{
|
||||||
metricName: "foo",
|
metricName: "foo",
|
||||||
value: 100,
|
value: 100,
|
||||||
valueStr: "100",
|
|
||||||
labels: map[string]string{"tag": "value"},
|
labels: map[string]string{"tag": "value"},
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
|
@ -143,7 +124,6 @@ func TestHandlePacket(t *testing.T) {
|
||||||
&CounterEvent{
|
&CounterEvent{
|
||||||
metricName: "foo",
|
metricName: "foo",
|
||||||
value: 100,
|
value: 100,
|
||||||
valueStr: "100",
|
|
||||||
labels: map[string]string{"tag": "value"},
|
labels: map[string]string{"tag": "value"},
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
|
@ -154,7 +134,6 @@ func TestHandlePacket(t *testing.T) {
|
||||||
&CounterEvent{
|
&CounterEvent{
|
||||||
metricName: "foo",
|
metricName: "foo",
|
||||||
value: 1000,
|
value: 1000,
|
||||||
valueStr: "1000",
|
|
||||||
labels: map[string]string{"tag1": "bar", "tag2": "baz"},
|
labels: map[string]string{"tag1": "bar", "tag2": "baz"},
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
|
@ -165,7 +144,6 @@ func TestHandlePacket(t *testing.T) {
|
||||||
&CounterEvent{
|
&CounterEvent{
|
||||||
metricName: "foo",
|
metricName: "foo",
|
||||||
value: 1000,
|
value: 1000,
|
||||||
valueStr: "1000",
|
|
||||||
labels: map[string]string{"tag1": "foo:bar"},
|
labels: map[string]string{"tag1": "foo:bar"},
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
|
@ -182,7 +160,6 @@ func TestHandlePacket(t *testing.T) {
|
||||||
&CounterEvent{
|
&CounterEvent{
|
||||||
metricName: "foo",
|
metricName: "foo",
|
||||||
value: 200,
|
value: 200,
|
||||||
valueStr: "200",
|
|
||||||
labels: map[string]string{"tag": "value"},
|
labels: map[string]string{"tag": "value"},
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
|
@ -193,37 +170,31 @@ func TestHandlePacket(t *testing.T) {
|
||||||
&TimerEvent{
|
&TimerEvent{
|
||||||
metricName: "foo",
|
metricName: "foo",
|
||||||
value: 200,
|
value: 200,
|
||||||
valueStr: "200",
|
|
||||||
labels: map[string]string{},
|
labels: map[string]string{},
|
||||||
},
|
},
|
||||||
&TimerEvent{
|
&TimerEvent{
|
||||||
metricName: "foo",
|
metricName: "foo",
|
||||||
value: 300,
|
value: 300,
|
||||||
valueStr: "300",
|
|
||||||
labels: map[string]string{},
|
labels: map[string]string{},
|
||||||
},
|
},
|
||||||
&CounterEvent{
|
&CounterEvent{
|
||||||
metricName: "foo",
|
metricName: "foo",
|
||||||
value: 50,
|
value: 50,
|
||||||
valueStr: "50",
|
|
||||||
labels: map[string]string{},
|
labels: map[string]string{},
|
||||||
},
|
},
|
||||||
&GaugeEvent{
|
&GaugeEvent{
|
||||||
metricName: "foo",
|
metricName: "foo",
|
||||||
value: 6,
|
value: 6,
|
||||||
valueStr: "6",
|
|
||||||
labels: map[string]string{},
|
labels: map[string]string{},
|
||||||
},
|
},
|
||||||
&CounterEvent{
|
&CounterEvent{
|
||||||
metricName: "bar",
|
metricName: "bar",
|
||||||
value: 1,
|
value: 1,
|
||||||
valueStr: "1",
|
|
||||||
labels: map[string]string{},
|
labels: map[string]string{},
|
||||||
},
|
},
|
||||||
&TimerEvent{
|
&TimerEvent{
|
||||||
metricName: "bar",
|
metricName: "bar",
|
||||||
value: 5,
|
value: 5,
|
||||||
valueStr: "5",
|
|
||||||
labels: map[string]string{},
|
labels: map[string]string{},
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
|
@ -243,7 +214,6 @@ func TestHandlePacket(t *testing.T) {
|
||||||
&CounterEvent{
|
&CounterEvent{
|
||||||
metricName: "foo",
|
metricName: "foo",
|
||||||
value: 1,
|
value: 1,
|
||||||
valueStr: "1",
|
|
||||||
labels: map[string]string{},
|
labels: map[string]string{},
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
|
@ -254,7 +224,6 @@ func TestHandlePacket(t *testing.T) {
|
||||||
&CounterEvent{
|
&CounterEvent{
|
||||||
metricName: "foo",
|
metricName: "foo",
|
||||||
value: 2,
|
value: 2,
|
||||||
valueStr: "2",
|
|
||||||
labels: map[string]string{},
|
labels: map[string]string{},
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
|
@ -281,7 +250,6 @@ func TestHandlePacket(t *testing.T) {
|
||||||
&CounterEvent{
|
&CounterEvent{
|
||||||
metricName: "valid_utf8",
|
metricName: "valid_utf8",
|
||||||
value: 1,
|
value: 1,
|
||||||
valueStr: "1",
|
|
||||||
labels: map[string]string{},
|
labels: map[string]string{},
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
|
|
32
exporter.go
32
exporter.go
|
@ -23,7 +23,6 @@ import (
|
||||||
"strconv"
|
"strconv"
|
||||||
"strings"
|
"strings"
|
||||||
"unicode/utf8"
|
"unicode/utf8"
|
||||||
"math"
|
|
||||||
|
|
||||||
"github.com/prometheus/client_golang/prometheus"
|
"github.com/prometheus/client_golang/prometheus"
|
||||||
"github.com/prometheus/common/log"
|
"github.com/prometheus/common/log"
|
||||||
|
@ -145,44 +144,38 @@ func (c *SummaryContainer) Get(metricName string, labels prometheus.Labels) prom
|
||||||
type Event interface {
|
type Event interface {
|
||||||
MetricName() string
|
MetricName() string
|
||||||
Value() float64
|
Value() float64
|
||||||
ValueStr() string
|
|
||||||
Labels() map[string]string
|
Labels() map[string]string
|
||||||
}
|
}
|
||||||
|
|
||||||
type CounterEvent struct {
|
type CounterEvent struct {
|
||||||
metricName string
|
metricName string
|
||||||
value float64
|
value float64
|
||||||
valueStr string
|
|
||||||
labels map[string]string
|
labels map[string]string
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *CounterEvent) MetricName() string { return c.metricName }
|
func (c *CounterEvent) MetricName() string { return c.metricName }
|
||||||
func (c *CounterEvent) Value() float64 { return c.value }
|
func (c *CounterEvent) Value() float64 { return c.value }
|
||||||
func (c *CounterEvent) ValueStr() string { return c.valueStr }
|
|
||||||
func (c *CounterEvent) Labels() map[string]string { return c.labels }
|
func (c *CounterEvent) Labels() map[string]string { return c.labels }
|
||||||
|
|
||||||
type GaugeEvent struct {
|
type GaugeEvent struct {
|
||||||
metricName string
|
metricName string
|
||||||
value float64
|
value float64
|
||||||
valueStr string
|
relative bool
|
||||||
labels map[string]string
|
labels map[string]string
|
||||||
}
|
}
|
||||||
|
|
||||||
func (g *GaugeEvent) MetricName() string { return g.metricName }
|
func (g *GaugeEvent) MetricName() string { return g.metricName }
|
||||||
func (g *GaugeEvent) Value() float64 { return g.value }
|
func (g *GaugeEvent) Value() float64 { return g.value }
|
||||||
func (g *GaugeEvent) ValueStr() string { return g.valueStr }
|
|
||||||
func (c *GaugeEvent) Labels() map[string]string { return c.labels }
|
func (c *GaugeEvent) Labels() map[string]string { return c.labels }
|
||||||
|
|
||||||
type TimerEvent struct {
|
type TimerEvent struct {
|
||||||
metricName string
|
metricName string
|
||||||
value float64
|
value float64
|
||||||
valueStr string
|
|
||||||
labels map[string]string
|
labels map[string]string
|
||||||
}
|
}
|
||||||
|
|
||||||
func (t *TimerEvent) MetricName() string { return t.metricName }
|
func (t *TimerEvent) MetricName() string { return t.metricName }
|
||||||
func (t *TimerEvent) Value() float64 { return t.value }
|
func (t *TimerEvent) Value() float64 { return t.value }
|
||||||
func (t *TimerEvent) ValueStr() string { return t.valueStr }
|
|
||||||
func (c *TimerEvent) Labels() map[string]string { return c.labels }
|
func (c *TimerEvent) Labels() map[string]string { return c.labels }
|
||||||
|
|
||||||
type Events []Event
|
type Events []Event
|
||||||
|
@ -238,7 +231,7 @@ func (b *Exporter) Listen(e <-chan Events) {
|
||||||
metricName = escapeMetricName(event.MetricName())
|
metricName = escapeMetricName(event.MetricName())
|
||||||
}
|
}
|
||||||
|
|
||||||
switch event.(type) {
|
switch ev := event.(type) {
|
||||||
case *CounterEvent:
|
case *CounterEvent:
|
||||||
counter := b.Counters.Get(
|
counter := b.Counters.Get(
|
||||||
b.suffix(metricName, "counter"),
|
b.suffix(metricName, "counter"),
|
||||||
|
@ -261,11 +254,8 @@ func (b *Exporter) Listen(e <-chan Events) {
|
||||||
prometheusLabels,
|
prometheusLabels,
|
||||||
)
|
)
|
||||||
|
|
||||||
var value = event.ValueStr()
|
if ev.relative {
|
||||||
if strings.Index(value, "+") == 0 {
|
|
||||||
gauge.Add(event.Value())
|
gauge.Add(event.Value())
|
||||||
} else if strings.Index(value, "-") == 0 {
|
|
||||||
gauge.Sub(math.Abs(event.Value()))
|
|
||||||
} else {
|
} else {
|
||||||
gauge.Set(event.Value())
|
gauge.Set(event.Value())
|
||||||
}
|
}
|
||||||
|
@ -303,27 +293,25 @@ type StatsDListener struct {
|
||||||
conn *net.UDPConn
|
conn *net.UDPConn
|
||||||
}
|
}
|
||||||
|
|
||||||
func buildEvent(statType, metric string, value float64, valueStr string, labels map[string]string) (Event, error) {
|
func buildEvent(statType, metric string, value float64, relative bool, labels map[string]string) (Event, error) {
|
||||||
switch statType {
|
switch statType {
|
||||||
case "c":
|
case "c":
|
||||||
return &CounterEvent{
|
return &CounterEvent{
|
||||||
metricName: metric,
|
metricName: metric,
|
||||||
value: float64(value),
|
value: float64(value),
|
||||||
valueStr: valueStr,
|
|
||||||
labels: labels,
|
labels: labels,
|
||||||
}, nil
|
}, nil
|
||||||
case "g":
|
case "g":
|
||||||
return &GaugeEvent{
|
return &GaugeEvent{
|
||||||
metricName: metric,
|
metricName: metric,
|
||||||
value: float64(value),
|
value: float64(value),
|
||||||
valueStr: valueStr,
|
relative: relative,
|
||||||
labels: labels,
|
labels: labels,
|
||||||
}, nil
|
}, nil
|
||||||
case "ms", "h":
|
case "ms", "h":
|
||||||
return &TimerEvent{
|
return &TimerEvent{
|
||||||
metricName: metric,
|
metricName: metric,
|
||||||
value: float64(value),
|
value: float64(value),
|
||||||
valueStr: valueStr,
|
|
||||||
labels: labels,
|
labels: labels,
|
||||||
}, nil
|
}, nil
|
||||||
case "s":
|
case "s":
|
||||||
|
@ -395,11 +383,11 @@ func (l *StatsDListener) handlePacket(packet []byte, e chan<- Events) {
|
||||||
}
|
}
|
||||||
valueStr, statType := components[0], components[1]
|
valueStr, statType := components[0], components[1]
|
||||||
|
|
||||||
var prefix = ""
|
var relative = false
|
||||||
if strings.Index(valueStr, "+") == 0 {
|
if strings.Index(valueStr, "+") == 0 {
|
||||||
prefix = "+"
|
relative = true
|
||||||
} else if strings.Index(valueStr, "-") == 0 {
|
} else if strings.Index(valueStr, "-") == 0 {
|
||||||
prefix = "-"
|
relative = true
|
||||||
}
|
}
|
||||||
|
|
||||||
value, err := strconv.ParseFloat(valueStr, 64)
|
value, err := strconv.ParseFloat(valueStr, 64)
|
||||||
|
@ -445,9 +433,7 @@ func (l *StatsDListener) handlePacket(packet []byte, e chan<- Events) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// convert the (possibly) sampled value to a string and add the operation prefix back on
|
event, err := buildEvent(statType, metric, value, relative, labels)
|
||||||
valueStr = prefix + strconv.FormatFloat(math.Abs(value), 'f', -1, 64)
|
|
||||||
event, err := buildEvent(statType, metric, value, valueStr, labels)
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Errorf("Error building event on line %s: %s", line, err)
|
log.Errorf("Error building event on line %s: %s", line, err)
|
||||||
networkStats.WithLabelValues("illegal_event").Inc()
|
networkStats.WithLabelValues("illegal_event").Inc()
|
||||||
|
|
Loading…
Reference in a new issue