add support for histograms and distributions without unit conversion

Signed-off-by: glightfoot <glightfoot@rsglab.com>
This commit is contained in:
glightfoot 2020-06-11 10:17:09 -04:00
parent 2fc2dcdff6
commit 5c0b206f6e
5 changed files with 58 additions and 23 deletions

View file

@ -85,7 +85,7 @@ func TestHandlePacket(t *testing.T) {
out: event.Events{ out: event.Events{
&event.TimerEvent{ &event.TimerEvent{
TMetricName: "foo", TMetricName: "foo",
TValue: 200, TValue: 0.2, // convert statsd ms to seconds in parsing
TLabels: map[string]string{}, TLabels: map[string]string{},
}, },
}, },
@ -323,12 +323,12 @@ func TestHandlePacket(t *testing.T) {
out: event.Events{ out: event.Events{
&event.TimerEvent{ &event.TimerEvent{
TMetricName: "foo", TMetricName: "foo",
TValue: 200, TValue: .200,
TLabels: map[string]string{}, TLabels: map[string]string{},
}, },
&event.TimerEvent{ &event.TimerEvent{
TMetricName: "foo", TMetricName: "foo",
TValue: 300, TValue: .300,
TLabels: map[string]string{}, TLabels: map[string]string{},
}, },
&event.CounterEvent{ &event.CounterEvent{
@ -348,7 +348,7 @@ func TestHandlePacket(t *testing.T) {
}, },
&event.TimerEvent{ &event.TimerEvent{
TMetricName: "bar", TMetricName: "bar",
TValue: 5, TValue: .005,
TLabels: map[string]string{}, TLabels: map[string]string{},
}, },
}, },
@ -356,16 +356,16 @@ func TestHandlePacket(t *testing.T) {
name: "timings with sampling factor", name: "timings with sampling factor",
in: "foo.timing:0.5|ms|@0.1", in: "foo.timing:0.5|ms|@0.1",
out: event.Events{ out: event.Events{
&event.TimerEvent{TMetricName: "foo.timing", TValue: 0.5, TLabels: map[string]string{}}, &event.TimerEvent{TMetricName: "foo.timing", TValue: 0.0005, TLabels: map[string]string{}},
&event.TimerEvent{TMetricName: "foo.timing", TValue: 0.5, TLabels: map[string]string{}}, &event.TimerEvent{TMetricName: "foo.timing", TValue: 0.0005, TLabels: map[string]string{}},
&event.TimerEvent{TMetricName: "foo.timing", TValue: 0.5, TLabels: map[string]string{}}, &event.TimerEvent{TMetricName: "foo.timing", TValue: 0.0005, TLabels: map[string]string{}},
&event.TimerEvent{TMetricName: "foo.timing", TValue: 0.5, TLabels: map[string]string{}}, &event.TimerEvent{TMetricName: "foo.timing", TValue: 0.0005, TLabels: map[string]string{}},
&event.TimerEvent{TMetricName: "foo.timing", TValue: 0.5, TLabels: map[string]string{}}, &event.TimerEvent{TMetricName: "foo.timing", TValue: 0.0005, TLabels: map[string]string{}},
&event.TimerEvent{TMetricName: "foo.timing", TValue: 0.5, TLabels: map[string]string{}}, &event.TimerEvent{TMetricName: "foo.timing", TValue: 0.0005, TLabels: map[string]string{}},
&event.TimerEvent{TMetricName: "foo.timing", TValue: 0.5, TLabels: map[string]string{}}, &event.TimerEvent{TMetricName: "foo.timing", TValue: 0.0005, TLabels: map[string]string{}},
&event.TimerEvent{TMetricName: "foo.timing", TValue: 0.5, TLabels: map[string]string{}}, &event.TimerEvent{TMetricName: "foo.timing", TValue: 0.0005, TLabels: map[string]string{}},
&event.TimerEvent{TMetricName: "foo.timing", TValue: 0.5, TLabels: map[string]string{}}, &event.TimerEvent{TMetricName: "foo.timing", TValue: 0.0005, TLabels: map[string]string{}},
&event.TimerEvent{TMetricName: "foo.timing", TValue: 0.5, TLabels: map[string]string{}}, &event.TimerEvent{TMetricName: "foo.timing", TValue: 0.0005, TLabels: map[string]string{}},
}, },
}, { }, {
name: "bad line", name: "bad line",
@ -422,6 +422,36 @@ func TestHandlePacket(t *testing.T) {
CLabels: map[string]string{}, CLabels: map[string]string{},
}, },
}, },
}, {
name: "ms timer with conversion to seconds",
in: "foo:200|ms",
out: event.Events{
&event.TimerEvent{
TMetricName: "foo",
TValue: 0.2,
TLabels: map[string]string{},
},
},
}, {
name: "histogram with no unit conversion",
in: "foo:200|h",
out: event.Events{
&event.TimerEvent{
TMetricName: "foo",
TValue: 200,
TLabels: map[string]string{},
},
},
}, {
name: "distribution with no unit conversion",
in: "foo:200|d",
out: event.Events{
&event.TimerEvent{
TMetricName: "foo",
TValue: 200, // convert statsd ms to seconds in parsing
TLabels: map[string]string{},
},
},
}, },
} }
@ -556,7 +586,7 @@ mappings:
// event with ttl = 2s from a mapping // event with ttl = 2s from a mapping
&event.TimerEvent{ &event.TimerEvent{
TMetricName: "bazqux.main", TMetricName: "bazqux.main",
TValue: 42000, TValue: 42,
}, },
} }

View file

@ -52,6 +52,7 @@ func (g *GaugeEvent) Value() float64 { return g.GValue }
func (c *GaugeEvent) Labels() map[string]string { return c.GLabels } func (c *GaugeEvent) Labels() map[string]string { return c.GLabels }
func (c *GaugeEvent) MetricType() mapper.MetricType { return mapper.MetricTypeGauge } func (c *GaugeEvent) MetricType() mapper.MetricType { return mapper.MetricTypeGauge }
// TimerEvent now defaults to seconds units
type TimerEvent struct { type TimerEvent struct {
TMetricName string TMetricName string
TValue float64 TValue float64

View file

@ -153,7 +153,7 @@ func (b *Exporter) handleEvent(thisEvent event.Event) {
case mapper.TimerTypeHistogram: case mapper.TimerTypeHistogram:
histogram, err := b.Registry.GetHistogram(metricName, prometheusLabels, help, mapping, b.MetricsCount) histogram, err := b.Registry.GetHistogram(metricName, prometheusLabels, help, mapping, b.MetricsCount)
if err == nil { if err == nil {
histogram.Observe(thisEvent.Value() / 1000) // prometheus presumes seconds, statsd millisecond histogram.Observe(thisEvent.Value())
b.EventStats.WithLabelValues("timer").Inc() b.EventStats.WithLabelValues("timer").Inc()
} else { } else {
level.Debug(b.Logger).Log("msg", regErrF, "metric", metricName, "error", err) level.Debug(b.Logger).Log("msg", regErrF, "metric", metricName, "error", err)
@ -163,7 +163,7 @@ func (b *Exporter) handleEvent(thisEvent event.Event) {
case mapper.TimerTypeDefault, mapper.TimerTypeSummary: case mapper.TimerTypeDefault, mapper.TimerTypeSummary:
summary, err := b.Registry.GetSummary(metricName, prometheusLabels, help, mapping, b.MetricsCount) summary, err := b.Registry.GetSummary(metricName, prometheusLabels, help, mapping, b.MetricsCount)
if err == nil { if err == nil {
summary.Observe(thisEvent.Value() / 1000) // prometheus presumes seconds, statsd millisecond summary.Observe(thisEvent.Value())
b.EventStats.WithLabelValues("timer").Inc() b.EventStats.WithLabelValues("timer").Inc()
} else { } else {
level.Debug(b.Logger).Log("msg", regErrF, "metric", metricName, "error", err) level.Debug(b.Logger).Log("msg", regErrF, "metric", metricName, "error", err)

View file

@ -721,7 +721,7 @@ func TestHistogramUnits(t *testing.T) {
c := event.Events{ c := event.Events{
&event.TimerEvent{ &event.TimerEvent{
TMetricName: name, TMetricName: name,
TValue: 300, TValue: .300,
}, },
} }
events <- c events <- c
@ -737,9 +737,7 @@ func TestHistogramUnits(t *testing.T) {
if value == nil { if value == nil {
t.Fatal("Histogram value should not be nil") t.Fatal("Histogram value should not be nil")
} }
if *value == 300 { if *value != .300 {
t.Fatalf("Histogram observations not scaled into Seconds")
} else if *value != .300 {
t.Fatalf("Received unexpected value for histogram observation %f != .300", *value) t.Fatalf("Received unexpected value for histogram observation %f != .300", *value)
} }
} }
@ -871,7 +869,7 @@ mappings:
// event with ttl = 2s from a mapping // event with ttl = 2s from a mapping
&event.TimerEvent{ &event.TimerEvent{
TMetricName: "bazqux.main", TMetricName: "bazqux.main",
TValue: 42000, TValue: 42,
}, },
} }

View file

@ -42,7 +42,13 @@ func buildEvent(statType, metric string, value float64, relative bool, labels ma
GRelative: relative, GRelative: relative,
GLabels: labels, GLabels: labels,
}, nil }, nil
case "ms", "h", "d": case "ms":
return &event.TimerEvent{
TMetricName: metric,
TValue: float64(value) / 1000, // prometheus presumes seconds, statsd millisecond
TLabels: labels,
}, nil
case "h", "d":
return &event.TimerEvent{ return &event.TimerEvent{
TMetricName: metric, TMetricName: metric,
TValue: float64(value), TValue: float64(value),