Skip to content

Commit

Permalink
fix(kafkaclient): do not close logs channel (#180)
Browse files Browse the repository at this point in the history
  • Loading branch information
smoya authored Sep 24, 2020
1 parent 40ea6fa commit 101e3b3
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 25 deletions.
38 changes: 15 additions & 23 deletions kafka/config.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package kafka

import (
"context"
"fmt"
"log/syslog"
"strings"
Expand Down Expand Up @@ -153,7 +152,7 @@ func (c Config) configureAuth(configMap *kafkalib.ConfigMap) error {
type ConfigOpt func(c *kafkalib.ConfigMap)

// WithLogger adds a logger to a Kafka consumer or producer.
func WithLogger(ctx context.Context, log logrus.FieldLogger) ConfigOpt {
func WithLogger(log logrus.FieldLogger) ConfigOpt {
return func(c *kafkalib.ConfigMap) {

syslogToLogrusLevelMapping := map[syslog.Priority]logrus.Level{
Expand All @@ -175,27 +174,20 @@ func WithLogger(ctx context.Context, log logrus.FieldLogger) ConfigOpt {

// Read from channel and print logs using the provided logger.
go func() {
defer close(logsChan)
for {
select {
case <-ctx.Done():
return
case m, ok := <-logsChan:
if !ok {
return
}
l := log.WithFields(logrus.Fields{
"kafka_context": m.Tag,
"kafka_client": m.Name,
}).WithTime(m.Timestamp)

logrusLevel := syslogToLogrusLevelMapping[syslog.Priority(m.Level)]
switch logrusLevel {
case logrus.ErrorLevel:
l.WithError(errors.New(m.Message)).Error("Error in Kafka Consumer")
default:
l.Log(logrusLevel, m.Message)
}
// Do not close logsChan because confluent-kafka-go will send logs until we close the client.
// Otherwise it will panic trying to send messages to a closed channel.
for m := range logsChan {
l := log.WithFields(logrus.Fields{
"kafka_context": m.Tag,
"kafka_client": m.Name,
}).WithTime(m.Timestamp)

logrusLevel := syslogToLogrusLevelMapping[syslog.Priority(m.Level)]
switch logrusLevel {
case logrus.ErrorLevel:
l.WithError(errors.New(m.Message)).Error("Error in Kafka Consumer")
default:
l.Log(logrusLevel, m.Message)
}
}
}()
Expand Down
4 changes: 2 additions & 2 deletions kafka/integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ func TestIntegration(t *testing.T) {
}

// create the producer
p, err := NewProducer(conf, WithLogger(ctx, log), WithPartitionerAlgorithm(PartitionerMurMur2))
p, err := NewProducer(conf, WithLogger(log), WithPartitionerAlgorithm(PartitionerMurMur2))
assert.NoError(err)
assert.NotNil(p)

Expand Down Expand Up @@ -131,7 +131,7 @@ func TestIntegration(t *testing.T) {
val := "gotestval"

// create the producer
p, err := NewProducer(conf, WithLogger(ctx, log), WithPartitionerAlgorithm(PartitionerMurMur2))
p, err := NewProducer(conf, WithLogger(log), WithPartitionerAlgorithm(PartitionerMurMur2))
assert.NoError(err)
assert.NotNil(p)

Expand Down

0 comments on commit 101e3b3

Please sign in to comment.