Added support for sampling factor on timing events

This commit is contained in:
Andreas Andersen 2017-05-15 15:57:31 +02:00
parent 89e5e36b51
commit b5dfc9c9ce
2 changed files with 34 additions and 9 deletions

View file

@ -198,6 +198,21 @@ func TestHandlePacket(t *testing.T) {
labels: map[string]string{}, labels: map[string]string{},
}, },
}, },
}, {
name: "timings with sampling factor",
in: "foo.timing:0.5|ms|@0.1",
out: Events{
&TimerEvent{metricName: "foo.timing", value: 0.5, labels: map[string]string{}},
&TimerEvent{metricName: "foo.timing", value: 0.5, labels: map[string]string{}},
&TimerEvent{metricName: "foo.timing", value: 0.5, labels: map[string]string{}},
&TimerEvent{metricName: "foo.timing", value: 0.5, labels: map[string]string{}},
&TimerEvent{metricName: "foo.timing", value: 0.5, labels: map[string]string{}},
&TimerEvent{metricName: "foo.timing", value: 0.5, labels: map[string]string{}},
&TimerEvent{metricName: "foo.timing", value: 0.5, labels: map[string]string{}},
&TimerEvent{metricName: "foo.timing", value: 0.5, labels: map[string]string{}},
&TimerEvent{metricName: "foo.timing", value: 0.5, labels: map[string]string{}},
&TimerEvent{metricName: "foo.timing", value: 0.5, labels: map[string]string{}},
},
}, { }, {
name: "bad line", name: "bad line",
in: "foo", in: "foo",

View file

@ -373,7 +373,8 @@ func (l *StatsDListener) handlePacket(packet []byte, e chan<- Events) {
} else { } else {
samples = strings.Split(elements[1], ":") samples = strings.Split(elements[1], ":")
} }
samples: for _, sample := range samples { samples:
for _, sample := range samples {
components := strings.Split(sample, "|") components := strings.Split(sample, "|")
samplingFactor := 1.0 samplingFactor := 1.0
if len(components) < 2 || len(components) > 4 { if len(components) < 2 || len(components) > 4 {
@ -395,6 +396,7 @@ func (l *StatsDListener) handlePacket(packet []byte, e chan<- Events) {
continue continue
} }
multiplyEvents := 1
labels := map[string]string{} labels := map[string]string{}
if len(components) >= 3 { if len(components) >= 3 {
for _, component := range components[2:] { for _, component := range components[2:] {
@ -408,9 +410,10 @@ func (l *StatsDListener) handlePacket(packet []byte, e chan<- Events) {
for _, component := range components[2:] { for _, component := range components[2:] {
switch component[0] { switch component[0] {
case '@': case '@':
if statType != "c" { if statType != "c" && statType != "ms" {
log.Errorln("Illegal sampling factor for non-counter metric on line", line) log.Errorln("Illegal sampling factor for non-counter metric on line", line)
networkStats.WithLabelValues("illegal_sample_factor").Inc() networkStats.WithLabelValues("illegal_sample_factor").Inc()
continue
} }
samplingFactor, err = strconv.ParseFloat(component[1:], 64) samplingFactor, err = strconv.ParseFloat(component[1:], 64)
if err != nil { if err != nil {
@ -420,7 +423,12 @@ func (l *StatsDListener) handlePacket(packet []byte, e chan<- Events) {
if samplingFactor == 0 { if samplingFactor == 0 {
samplingFactor = 1 samplingFactor = 1
} }
if statType == "c" {
value /= samplingFactor value /= samplingFactor
} else if statType == "ms" {
multiplyEvents = int(1 / samplingFactor)
}
case '#': case '#':
labels = parseDogStatsDTagsToLabels(component) labels = parseDogStatsDTagsToLabels(component)
default: default:
@ -431,6 +439,7 @@ func (l *StatsDListener) handlePacket(packet []byte, e chan<- Events) {
} }
} }
for i := 0; i < multiplyEvents; i++ {
event, err := buildEvent(statType, metric, value, relative, labels) event, err := buildEvent(statType, metric, value, relative, 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)
@ -438,6 +447,7 @@ func (l *StatsDListener) handlePacket(packet []byte, e chan<- Events) {
continue continue
} }
events = append(events, event) events = append(events, event)
}
networkStats.WithLabelValues("legal").Inc() networkStats.WithLabelValues("legal").Inc()
} }
} }