mirror of
https://github.com/prometheus/statsd_exporter.git
synced 2024-11-14 11:21:17 +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{
|
||||
metricName: "foo",
|
||||
value: 2,
|
||||
valueStr: "2",
|
||||
labels: map[string]string{},
|
||||
},
|
||||
},
|
||||
|
@ -44,18 +43,6 @@ func TestHandlePacket(t *testing.T) {
|
|||
&GaugeEvent{
|
||||
metricName: "foo",
|
||||
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{},
|
||||
},
|
||||
},
|
||||
|
@ -66,7 +53,7 @@ func TestHandlePacket(t *testing.T) {
|
|||
&GaugeEvent{
|
||||
metricName: "foo",
|
||||
value: -10,
|
||||
valueStr: "-10",
|
||||
relative: true,
|
||||
labels: map[string]string{},
|
||||
},
|
||||
},
|
||||
|
@ -77,7 +64,6 @@ func TestHandlePacket(t *testing.T) {
|
|||
&TimerEvent{
|
||||
metricName: "foo",
|
||||
value: 200,
|
||||
valueStr: "200",
|
||||
labels: map[string]string{},
|
||||
},
|
||||
},
|
||||
|
@ -88,7 +74,6 @@ func TestHandlePacket(t *testing.T) {
|
|||
&CounterEvent{
|
||||
metricName: "foo",
|
||||
value: 100,
|
||||
valueStr: "100",
|
||||
labels: map[string]string{"tag1": "bar", "tag2": "baz"},
|
||||
},
|
||||
},
|
||||
|
@ -99,7 +84,6 @@ func TestHandlePacket(t *testing.T) {
|
|||
&CounterEvent{
|
||||
metricName: "foo",
|
||||
value: 100,
|
||||
valueStr: "100",
|
||||
labels: map[string]string{"tag1": "bar", "tag2": "baz"},
|
||||
},
|
||||
},
|
||||
|
@ -110,7 +94,6 @@ func TestHandlePacket(t *testing.T) {
|
|||
&CounterEvent{
|
||||
metricName: "foo",
|
||||
value: 100,
|
||||
valueStr: "100",
|
||||
labels: map[string]string{"_09digits": "0", "tag_with_dots": "1"},
|
||||
},
|
||||
},
|
||||
|
@ -121,7 +104,6 @@ func TestHandlePacket(t *testing.T) {
|
|||
&CounterEvent{
|
||||
metricName: "foo",
|
||||
value: 100,
|
||||
valueStr: "100",
|
||||
labels: map[string]string{},
|
||||
},
|
||||
},
|
||||
|
@ -132,7 +114,6 @@ func TestHandlePacket(t *testing.T) {
|
|||
&CounterEvent{
|
||||
metricName: "foo",
|
||||
value: 100,
|
||||
valueStr: "100",
|
||||
labels: map[string]string{"tag": "value"},
|
||||
},
|
||||
},
|
||||
|
@ -143,7 +124,6 @@ func TestHandlePacket(t *testing.T) {
|
|||
&CounterEvent{
|
||||
metricName: "foo",
|
||||
value: 100,
|
||||
valueStr: "100",
|
||||
labels: map[string]string{"tag": "value"},
|
||||
},
|
||||
},
|
||||
|
@ -154,7 +134,6 @@ func TestHandlePacket(t *testing.T) {
|
|||
&CounterEvent{
|
||||
metricName: "foo",
|
||||
value: 1000,
|
||||
valueStr: "1000",
|
||||
labels: map[string]string{"tag1": "bar", "tag2": "baz"},
|
||||
},
|
||||
},
|
||||
|
@ -165,7 +144,6 @@ func TestHandlePacket(t *testing.T) {
|
|||
&CounterEvent{
|
||||
metricName: "foo",
|
||||
value: 1000,
|
||||
valueStr: "1000",
|
||||
labels: map[string]string{"tag1": "foo:bar"},
|
||||
},
|
||||
},
|
||||
|
@ -182,7 +160,6 @@ func TestHandlePacket(t *testing.T) {
|
|||
&CounterEvent{
|
||||
metricName: "foo",
|
||||
value: 200,
|
||||
valueStr: "200",
|
||||
labels: map[string]string{"tag": "value"},
|
||||
},
|
||||
},
|
||||
|
@ -193,37 +170,31 @@ func TestHandlePacket(t *testing.T) {
|
|||
&TimerEvent{
|
||||
metricName: "foo",
|
||||
value: 200,
|
||||
valueStr: "200",
|
||||
labels: map[string]string{},
|
||||
},
|
||||
&TimerEvent{
|
||||
metricName: "foo",
|
||||
value: 300,
|
||||
valueStr: "300",
|
||||
labels: map[string]string{},
|
||||
},
|
||||
&CounterEvent{
|
||||
metricName: "foo",
|
||||
value: 50,
|
||||
valueStr: "50",
|
||||
labels: map[string]string{},
|
||||
},
|
||||
&GaugeEvent{
|
||||
metricName: "foo",
|
||||
value: 6,
|
||||
valueStr: "6",
|
||||
labels: map[string]string{},
|
||||
},
|
||||
&CounterEvent{
|
||||
metricName: "bar",
|
||||
value: 1,
|
||||
valueStr: "1",
|
||||
labels: map[string]string{},
|
||||
},
|
||||
&TimerEvent{
|
||||
metricName: "bar",
|
||||
value: 5,
|
||||
valueStr: "5",
|
||||
labels: map[string]string{},
|
||||
},
|
||||
},
|
||||
|
@ -243,7 +214,6 @@ func TestHandlePacket(t *testing.T) {
|
|||
&CounterEvent{
|
||||
metricName: "foo",
|
||||
value: 1,
|
||||
valueStr: "1",
|
||||
labels: map[string]string{},
|
||||
},
|
||||
},
|
||||
|
@ -254,7 +224,6 @@ func TestHandlePacket(t *testing.T) {
|
|||
&CounterEvent{
|
||||
metricName: "foo",
|
||||
value: 2,
|
||||
valueStr: "2",
|
||||
labels: map[string]string{},
|
||||
},
|
||||
},
|
||||
|
@ -281,7 +250,6 @@ func TestHandlePacket(t *testing.T) {
|
|||
&CounterEvent{
|
||||
metricName: "valid_utf8",
|
||||
value: 1,
|
||||
valueStr: "1",
|
||||
labels: map[string]string{},
|
||||
},
|
||||
},
|
||||
|
|
32
exporter.go
32
exporter.go
|
@ -23,7 +23,6 @@ import (
|
|||
"strconv"
|
||||
"strings"
|
||||
"unicode/utf8"
|
||||
"math"
|
||||
|
||||
"github.com/prometheus/client_golang/prometheus"
|
||||
"github.com/prometheus/common/log"
|
||||
|
@ -145,44 +144,38 @@ func (c *SummaryContainer) Get(metricName string, labels prometheus.Labels) prom
|
|||
type Event interface {
|
||||
MetricName() string
|
||||
Value() float64
|
||||
ValueStr() string
|
||||
Labels() map[string]string
|
||||
}
|
||||
|
||||
type CounterEvent struct {
|
||||
metricName string
|
||||
value float64
|
||||
valueStr string
|
||||
labels map[string]string
|
||||
}
|
||||
|
||||
func (c *CounterEvent) MetricName() string { return c.metricName }
|
||||
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 }
|
||||
|
||||
type GaugeEvent struct {
|
||||
metricName string
|
||||
value float64
|
||||
valueStr string
|
||||
relative bool
|
||||
labels map[string]string
|
||||
}
|
||||
|
||||
func (g *GaugeEvent) MetricName() string { return g.metricName }
|
||||
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 }
|
||||
|
||||
type TimerEvent struct {
|
||||
metricName string
|
||||
value float64
|
||||
valueStr string
|
||||
labels map[string]string
|
||||
}
|
||||
|
||||
func (t *TimerEvent) MetricName() string { return t.metricName }
|
||||
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 }
|
||||
|
||||
type Events []Event
|
||||
|
@ -238,7 +231,7 @@ func (b *Exporter) Listen(e <-chan Events) {
|
|||
metricName = escapeMetricName(event.MetricName())
|
||||
}
|
||||
|
||||
switch event.(type) {
|
||||
switch ev := event.(type) {
|
||||
case *CounterEvent:
|
||||
counter := b.Counters.Get(
|
||||
b.suffix(metricName, "counter"),
|
||||
|
@ -261,11 +254,8 @@ func (b *Exporter) Listen(e <-chan Events) {
|
|||
prometheusLabels,
|
||||
)
|
||||
|
||||
var value = event.ValueStr()
|
||||
if strings.Index(value, "+") == 0 {
|
||||
if ev.relative {
|
||||
gauge.Add(event.Value())
|
||||
} else if strings.Index(value, "-") == 0 {
|
||||
gauge.Sub(math.Abs(event.Value()))
|
||||
} else {
|
||||
gauge.Set(event.Value())
|
||||
}
|
||||
|
@ -303,27 +293,25 @@ type StatsDListener struct {
|
|||
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 {
|
||||
case "c":
|
||||
return &CounterEvent{
|
||||
metricName: metric,
|
||||
value: float64(value),
|
||||
valueStr: valueStr,
|
||||
labels: labels,
|
||||
}, nil
|
||||
case "g":
|
||||
return &GaugeEvent{
|
||||
metricName: metric,
|
||||
value: float64(value),
|
||||
valueStr: valueStr,
|
||||
relative: relative,
|
||||
labels: labels,
|
||||
}, nil
|
||||
case "ms", "h":
|
||||
return &TimerEvent{
|
||||
metricName: metric,
|
||||
value: float64(value),
|
||||
valueStr: valueStr,
|
||||
labels: labels,
|
||||
}, nil
|
||||
case "s":
|
||||
|
@ -395,11 +383,11 @@ func (l *StatsDListener) handlePacket(packet []byte, e chan<- Events) {
|
|||
}
|
||||
valueStr, statType := components[0], components[1]
|
||||
|
||||
var prefix = ""
|
||||
var relative = false
|
||||
if strings.Index(valueStr, "+") == 0 {
|
||||
prefix = "+"
|
||||
relative = true
|
||||
} else if strings.Index(valueStr, "-") == 0 {
|
||||
prefix = "-"
|
||||
relative = true
|
||||
}
|
||||
|
||||
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
|
||||
valueStr = prefix + strconv.FormatFloat(math.Abs(value), 'f', -1, 64)
|
||||
event, err := buildEvent(statType, metric, value, valueStr, labels)
|
||||
event, err := buildEvent(statType, metric, value, relative, labels)
|
||||
if err != nil {
|
||||
log.Errorf("Error building event on line %s: %s", line, err)
|
||||
networkStats.WithLabelValues("illegal_event").Inc()
|
||||
|
|
Loading…
Reference in a new issue