Manage number of goroutines

This commit is contained in:
Jack Zampolin 2016-09-02 13:09:54 -07:00
parent 522c45eb8a
commit 2287edce0a
1 changed files with 12 additions and 12 deletions

View File

@ -36,13 +36,13 @@ type RabbitMQParser struct {
Prefetch int Prefetch int
DroppedLog string DroppedLog string
conn *amqp.Connection conn *amqp.Connection
ch *amqp.Channel ch *amqp.Channel
q amqp.Queue q amqp.Queue
drops int drops int
acks int parsed int
log *os.File log *os.File
cl *ConcurrencyLimiter cl *ConcurrencyLimiter
sync.Mutex sync.Mutex
} }
@ -66,11 +66,11 @@ func (rmq *RabbitMQParser) SampleConfig() string {
// Gather satisfies the telegraf.ServiceInput interface // Gather satisfies the telegraf.ServiceInput interface
// All gathering is done in the Start function // All gathering is done in the Start function
func (rmq *RabbitMQParser) Gather(_ telegraf.Accumulator) error { func (rmq *RabbitMQParser) Gather(_ telegraf.Accumulator) error {
numMessages := rmq.drops + rmq.acks numMessages := rmq.drops + rmq.parsed
percentDrops := (float64(rmq.drops) / float64(numMessages)) * 100.0 percentDrops := (float64(rmq.drops) / float64(numMessages)) * 100.0
log.Printf("Dropped %.2f%% of %d metrics in the last interval", percentDrops, numMessages) log.Printf("Dropped %.2f%% of %d metrics in the last interval", percentDrops, numMessages)
rmq.drops = 0 rmq.drops = 0
rmq.acks = 0 rmq.parsed = 0
return nil return nil
} }
@ -85,7 +85,7 @@ func (rmq *RabbitMQParser) Start(acc telegraf.Accumulator) error {
rmq.log = f rmq.log = f
// Limit number of workers to the number of CPU on system // Limit number of workers to the number of CPU on system
rmq.cl = NewConcurrencyLimiter(runtime.NumCPU() * 2) rmq.cl = NewConcurrencyLimiter(runtime.NumCPU())
// Create queue connection and assign it to RabbitMQParser // Create queue connection and assign it to RabbitMQParser
conn, err := amqp.Dial(rmq.RabbitmqAddress) conn, err := amqp.Dial(rmq.RabbitmqAddress)
@ -147,7 +147,7 @@ func (rmq *RabbitMQParser) registerConsumer() <-chan amqp.Delivery {
func (rmq *RabbitMQParser) listen(msgs <-chan amqp.Delivery, acc telegraf.Accumulator) { func (rmq *RabbitMQParser) listen(msgs <-chan amqp.Delivery, acc telegraf.Accumulator) {
for d := range msgs { for d := range msgs {
rmq.cl.Increment() rmq.cl.Increment()
rmq.handleMessage(d, acc) go rmq.handleMessage(d, acc)
} }
} }
@ -168,7 +168,7 @@ func (rmq *RabbitMQParser) handleMessage(d amqp.Delivery, acc telegraf.Accumulat
} }
d.Ack(false) d.Ack(false)
acc.AddFields(msg.Name(), msg.Fields(), msg.Tags(), msg.Time()) acc.AddFields(msg.Name(), msg.Fields(), msg.Tags(), msg.Time())
rmq.acks++ rmq.parsed++
rmq.cl.Decrement() rmq.cl.Decrement()
} }