Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixes docker driver that would panic when closed. #3709

Merged
merged 1 commit into from
May 17, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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))
}