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{
&event.TimerEvent{
TMetricName: "foo",
TValue: 200,
TValue: 0.2, // convert statsd ms to seconds in parsing
TLabels: map[string]string{},
},
},
@ -323,12 +323,12 @@ func TestHandlePacket(t *testing.T) {
out: event.Events{
&event.TimerEvent{
TMetricName: "foo",
TValue: 200,
TValue: .200,
TLabels: map[string]string{},
},
&event.TimerEvent{
TMetricName: "foo",
TValue: 300,
TValue: .300,
TLabels: map[string]string{},
},
&event.CounterEvent{
@ -348,7 +348,7 @@ func TestHandlePacket(t *testing.T) {
},
&event.TimerEvent{
TMetricName: "bar",
TValue: 5,
TValue: .005,
TLabels: map[string]string{},
},
},
@ -356,16 +356,16 @@ func TestHandlePacket(t *testing.T) {
name: "timings with sampling factor",
in: "foo.timing:0.5|ms|@0.1",
out: event.Events{
&event.TimerEvent{TMetricName: "foo.timing", TValue: 0.5, TLabels: map[string]string{}},
&event.TimerEvent{TMetricName: "foo.timing", TValue: 0.5, TLabels: map[string]string{}},
&event.TimerEvent{TMetricName: "foo.timing", TValue: 0.5, TLabels: map[string]string{}},
&event.TimerEvent{TMetricName: "foo.timing", TValue: 0.5, TLabels: map[string]string{}},
&event.TimerEvent{TMetricName: "foo.timing", TValue: 0.5, TLabels: map[string]string{}},
&event.TimerEvent{TMetricName: "foo.timing", TValue: 0.5, TLabels: map[string]string{}},
&event.TimerEvent{TMetricName: "foo.timing", TValue: 0.5, TLabels: map[string]string{}},
&event.TimerEvent{TMetricName: "foo.timing", TValue: 0.5, TLabels: map[string]string{}},
&event.TimerEvent{TMetricName: "foo.timing", TValue: 0.5, 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.0005, TLabels: map[string]string{}},
&event.TimerEvent{TMetricName: "foo.timing", TValue: 0.0005, TLabels: map[string]string{}},
&event.TimerEvent{TMetricName: "foo.timing", TValue: 0.0005, TLabels: map[string]string{}},
&event.TimerEvent{TMetricName: "foo.timing", TValue: 0.0005, TLabels: map[string]string{}},
&event.TimerEvent{TMetricName: "foo.timing", TValue: 0.0005, TLabels: map[string]string{}},
&event.TimerEvent{TMetricName: "foo.timing", TValue: 0.0005, TLabels: map[string]string{}},
&event.TimerEvent{TMetricName: "foo.timing", TValue: 0.0005, TLabels: map[string]string{}},
&event.TimerEvent{TMetricName: "foo.timing", TValue: 0.0005, TLabels: map[string]string{}},
&event.TimerEvent{TMetricName: "foo.timing", TValue: 0.0005, TLabels: map[string]string{}},
},
}, {
name: "bad line",
@ -422,6 +422,36 @@ func TestHandlePacket(t *testing.T) {
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.TimerEvent{
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) MetricType() mapper.MetricType { return mapper.MetricTypeGauge }
// TimerEvent now defaults to seconds units
type TimerEvent struct {
TMetricName string
TValue float64

View file

@ -153,7 +153,7 @@ func (b *Exporter) handleEvent(thisEvent event.Event) {
case mapper.TimerTypeHistogram:
histogram, err := b.Registry.GetHistogram(metricName, prometheusLabels, help, mapping, b.MetricsCount)
if err == nil {
histogram.Observe(thisEvent.Value() / 1000) // prometheus presumes seconds, statsd millisecond
histogram.Observe(thisEvent.Value())
b.EventStats.WithLabelValues("timer").Inc()
} else {
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:
summary, err := b.Registry.GetSummary(metricName, prometheusLabels, help, mapping, b.MetricsCount)
if err == nil {
summary.Observe(thisEvent.Value() / 1000) // prometheus presumes seconds, statsd millisecond
summary.Observe(thisEvent.Value())
b.EventStats.WithLabelValues("timer").Inc()
} else {
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{
&event.TimerEvent{
TMetricName: name,
TValue: 300,
TValue: .300,
},
}
events <- c
@ -737,9 +737,7 @@ func TestHistogramUnits(t *testing.T) {
if value == nil {
t.Fatal("Histogram value should not be nil")
}
if *value == 300 {
t.Fatalf("Histogram observations not scaled into Seconds")
} else if *value != .300 {
if *value != .300 {
t.Fatalf("Received unexpected value for histogram observation %f != .300", *value)
}
}
@ -871,7 +869,7 @@ mappings:
// event with ttl = 2s from a mapping
&event.TimerEvent{
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,
GLabels: labels,
}, 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{
TMetricName: metric,
TValue: float64(value),