Skip to content

Commit

Permalink
Fixes docker driver that would panic when closed. (#3709)
Browse files Browse the repository at this point in the history
This is happening because it can still received log, but the client might be already closed.

Fixes #3705

Signed-off-by: Cyril Tovena <cyril.tovena@gmail.com>
  • Loading branch information
cyriltovena authored May 17, 2021
1 parent 74ee19c commit 7a5fe3d
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 0 deletions.
15 changes: 15 additions & 0 deletions clients/cmd/docker-driver/loki.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,12 @@ package main

import (
"bytes"
"sync"

"github.com/docker/docker/daemon/logger"
"github.com/go-kit/kit/log"
"github.com/go-kit/kit/log/level"
"github.com/pkg/errors"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/common/model"

Expand All @@ -24,6 +26,9 @@ type loki struct {
labels model.LabelSet
logger log.Logger

closed bool
mutex sync.RWMutex

stop func()
}

Expand Down Expand Up @@ -59,6 +64,13 @@ func New(logCtx logger.Info, logger log.Logger) (logger.Logger, error) {

// Log implements `logger.Logger`
func (l *loki) Log(m *logger.Message) error {
l.mutex.RLock()
defer l.mutex.RUnlock()

if l.closed {
return errors.New("client closed")
}

if len(bytes.Fields(m.Line)) == 0 {
level.Debug(l.logger).Log("msg", "ignoring empty line", "line", string(m.Line))
return nil
Expand All @@ -84,7 +96,10 @@ func (l *loki) Name() string {

// Log implements `logger.Logger`
func (l *loki) Close() error {
l.mutex.Lock()
defer l.mutex.Unlock()
l.stop()
l.client.StopNow()
l.closed = true
return nil
}
25 changes: 25 additions & 0 deletions clients/cmd/docker-driver/loki_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package main

import (
"testing"
"time"

util_log "github.com/cortexproject/cortex/pkg/util/log"
"github.com/docker/docker/daemon/logger"
"github.com/stretchr/testify/require"
)

func Test_loki_LogWhenClosed(t *testing.T) {
l, err := New(logger.Info{
Config: map[string]string{
"loki-url": "http://localhost:3000",
},
}, util_log.Logger)
require.Nil(t, err)
msg := logger.NewMessage()
msg.Line = []byte(`foo`)
msg.Timestamp = time.Now()
require.Nil(t, l.Log(msg))
require.Nil(t, l.Close())
require.NotNil(t, l.Log(msg))
}

0 comments on commit 7a5fe3d

Please sign in to comment.