Skip to content

Commit

Permalink
Merge pull request #28 from Kiliaro/master
Browse files Browse the repository at this point in the history
Added support for sampling factor on timing events
  • Loading branch information
grobie authored May 25, 2017
2 parents 20becbc + b5dfc9c commit 663b6a1
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 9 deletions.
15 changes: 15 additions & 0 deletions bridge_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -198,6 +198,21 @@ func TestHandlePacket(t *testing.T) {
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",
in: "foo",
Expand Down
28 changes: 19 additions & 9 deletions exporter.go
Original file line number Diff line number Diff line change
Expand Up @@ -373,7 +373,8 @@ func (l *StatsDListener) handlePacket(packet []byte, e chan<- Events) {
} else {
samples = strings.Split(elements[1], ":")
}
samples: for _, sample := range samples {
samples:
for _, sample := range samples {
components := strings.Split(sample, "|")
samplingFactor := 1.0
if len(components) < 2 || len(components) > 4 {
Expand All @@ -395,6 +396,7 @@ func (l *StatsDListener) handlePacket(packet []byte, e chan<- Events) {
continue
}

multiplyEvents := 1
labels := map[string]string{}
if len(components) >= 3 {
for _, component := range components[2:] {
Expand All @@ -408,9 +410,10 @@ func (l *StatsDListener) handlePacket(packet []byte, e chan<- Events) {
for _, component := range components[2:] {
switch component[0] {
case '@':
if statType != "c" {
if statType != "c" && statType != "ms" {
log.Errorln("Illegal sampling factor for non-counter metric on line", line)
networkStats.WithLabelValues("illegal_sample_factor").Inc()
continue
}
samplingFactor, err = strconv.ParseFloat(component[1:], 64)
if err != nil {
Expand All @@ -420,7 +423,12 @@ func (l *StatsDListener) handlePacket(packet []byte, e chan<- Events) {
if samplingFactor == 0 {
samplingFactor = 1
}
value /= samplingFactor

if statType == "c" {
value /= samplingFactor
} else if statType == "ms" {
multiplyEvents = int(1 / samplingFactor)
}
case '#':
labels = parseDogStatsDTagsToLabels(component)
default:
Expand All @@ -431,13 +439,15 @@ func (l *StatsDListener) handlePacket(packet []byte, e chan<- Events) {
}
}

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()
continue
for i := 0; i < multiplyEvents; i++ {
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()
continue
}
events = append(events, event)
}
events = append(events, event)
networkStats.WithLabelValues("legal").Inc()
}
}
Expand Down

0 comments on commit 663b6a1

Please sign in to comment.