-
Notifications
You must be signed in to change notification settings - Fork 233
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add internal event queuing and flushing
At high traffic levels, the locking around sending on channels can cause a large amount of blocking and CPU usage. These adds an event queue mechanism so that events are queued for short period of time, and flushed in batches to the main exporter goroutine periodically. The default is is to flush every 1000 events, or every 200ms, whichever happens first. Signed-off-by: Clayton O'Neill <claytono@github.com>
- Loading branch information
Showing
9 changed files
with
296 additions
and
88 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,132 @@ | ||
// Copyright 2013 The Prometheus Authors | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
package main | ||
|
||
import ( | ||
"sync" | ||
"time" | ||
|
||
"github.com/prometheus/statsd_exporter/pkg/clock" | ||
"github.com/prometheus/statsd_exporter/pkg/mapper" | ||
) | ||
|
||
type Event interface { | ||
MetricName() string | ||
Value() float64 | ||
Labels() map[string]string | ||
MetricType() mapper.MetricType | ||
} | ||
|
||
type CounterEvent struct { | ||
metricName string | ||
value float64 | ||
labels map[string]string | ||
} | ||
|
||
func (c *CounterEvent) MetricName() string { return c.metricName } | ||
func (c *CounterEvent) Value() float64 { return c.value } | ||
func (c *CounterEvent) Labels() map[string]string { return c.labels } | ||
func (c *CounterEvent) MetricType() mapper.MetricType { return mapper.MetricTypeCounter } | ||
|
||
type GaugeEvent struct { | ||
metricName string | ||
value float64 | ||
relative bool | ||
labels map[string]string | ||
} | ||
|
||
func (g *GaugeEvent) MetricName() string { return g.metricName } | ||
func (g *GaugeEvent) Value() float64 { return g.value } | ||
func (c *GaugeEvent) Labels() map[string]string { return c.labels } | ||
func (c *GaugeEvent) MetricType() mapper.MetricType { return mapper.MetricTypeGauge } | ||
|
||
type TimerEvent struct { | ||
metricName string | ||
value float64 | ||
labels map[string]string | ||
} | ||
|
||
func (t *TimerEvent) MetricName() string { return t.metricName } | ||
func (t *TimerEvent) Value() float64 { return t.value } | ||
func (c *TimerEvent) Labels() map[string]string { return c.labels } | ||
func (c *TimerEvent) MetricType() mapper.MetricType { return mapper.MetricTypeTimer } | ||
|
||
type Events []Event | ||
|
||
type eventQueue struct { | ||
c chan Events | ||
q Events | ||
m sync.Mutex | ||
flushThreshold int | ||
flushTicker *time.Ticker | ||
} | ||
|
||
type eventHandler interface { | ||
queue(event Events) | ||
} | ||
|
||
func newEventQueue(c chan Events, flushThreshold int, flushInterval time.Duration) *eventQueue { | ||
ticker := clock.NewTicker(flushInterval) | ||
eq := &eventQueue{ | ||
c: c, | ||
flushThreshold: flushThreshold, | ||
flushTicker: ticker, | ||
} | ||
go func() { | ||
for { | ||
<-ticker.C | ||
eq.flush() | ||
} | ||
}() | ||
return eq | ||
} | ||
|
||
func (eq *eventQueue) queue(events Events) { | ||
eq.m.Lock() | ||
defer eq.m.Unlock() | ||
|
||
for _, e := range events { | ||
eq.q = append(eq.q, e) | ||
if len(eq.q) >= eq.flushThreshold { | ||
eq.flushUnlocked() | ||
} | ||
} | ||
} | ||
|
||
func (eq *eventQueue) flush() { | ||
eq.m.Lock() | ||
defer eq.m.Unlock() | ||
eq.flushUnlocked() | ||
} | ||
|
||
func (eq *eventQueue) flushUnlocked() { | ||
eq.c <- eq.q | ||
eq.q = eq.q[:0] | ||
eventsFlushed.Inc() | ||
} | ||
|
||
func (eq *eventQueue) len() int { | ||
eq.m.Lock() | ||
defer eq.m.Unlock() | ||
|
||
return len(eq.q) | ||
} | ||
|
||
type unbufferedEventHandler struct { | ||
c chan Events | ||
} | ||
|
||
func (ueh *unbufferedEventHandler) queue(events Events) { | ||
ueh.c <- events | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
// Copyright 2013 The Prometheus Authors | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
package main | ||
|
||
import ( | ||
"testing" | ||
"time" | ||
|
||
"github.com/prometheus/statsd_exporter/pkg/clock" | ||
) | ||
|
||
func TestEventThresholdFlush(t *testing.T) { | ||
c := make(chan Events, 100) | ||
// We're not going to flush during this test, so the duration doesn't matter. | ||
eq := newEventQueue(c, 5, time.Second) | ||
e := make(Events, 13) | ||
go func() { | ||
eq.queue(e) | ||
}() | ||
|
||
batch := <-c | ||
if len(batch) != 5 { | ||
t.Fatalf("Expected event batch to be 5 elements, but got %v", len(batch)) | ||
} | ||
batch = <-c | ||
if len(batch) != 5 { | ||
t.Fatalf("Expected event batch to be 5 elements, but got %v", len(batch)) | ||
} | ||
batch = <-c | ||
if len(batch) != 3 { | ||
t.Fatalf("Expected event batch to be 3 elements, but got %v", len(batch)) | ||
} | ||
} | ||
|
||
func TestEventIntervalFlush(t *testing.T) { | ||
// Mock a time.NewTicker | ||
tickerCh := make(chan time.Time) | ||
clock.ClockInstance = &clock.Clock{ | ||
TickerCh: tickerCh, | ||
} | ||
clock.ClockInstance.Instant = time.Unix(0, 0) | ||
|
||
c := make(chan Events, 100) | ||
eq := newEventQueue(c, 1000, time.Second*1000) | ||
e := make(Events, 10) | ||
eq.queue(e) | ||
|
||
if eq.len() != 10 { | ||
t.Fatal("Expected 10 events to be queued, but got", eq.len()) | ||
} | ||
|
||
if len(eq.c) != 0 { | ||
t.Fatal("Expected 0 events in the event channel, but got", len(eq.c)) | ||
} | ||
|
||
// Tick time forward to trigger a flush | ||
clock.ClockInstance.Instant = time.Unix(10000, 0) | ||
clock.ClockInstance.TickerCh <- time.Unix(10000, 0) | ||
|
||
events := <-eq.c | ||
if eq.len() != 0 { | ||
t.Fatal("Expected 0 events to be queued, but got", eq.len()) | ||
} | ||
|
||
if len(events) != 10 { | ||
t.Fatal("Expected 10 events in the event channel, but got", len(events)) | ||
} | ||
|
||
} |
Oops, something went wrong.