Skip to content

Commit

Permalink
fix panic loki source docker (#875)
Browse files Browse the repository at this point in the history
  • Loading branch information
wildum authored May 16, 2024
1 parent 7f540a2 commit 4fb1df9
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 4 deletions.
4 changes: 3 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,9 @@ Main (unreleased)

- Exit Alloy immediately if the port it runs on is not available.
This port can be configured with `--server.http.listen-addr` or using
the default listen address`127.0.0.1:12345`. (@mattdurham)
the default listen address`127.0.0.1:12345`. (@mattdurham)

- Fix a panic in `loki.source.docker` when trying to stop a target that was never started. (@wildum)

### Other changes

Expand Down
16 changes: 16 additions & 0 deletions internal/component/loki/source/docker/docker_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,22 @@ func TestRestart(t *testing.T) {
}, time.Second, 20*time.Millisecond, "Expected log lines were not found within the time limit after restart.")
}

func TestTargetNeverStarted(t *testing.T) {
runningState := false
client := clientMock{
logLine: "2024-05-02T13:11:55.879889Z caller=module_service.go:114 msg=\"module stopped\" module=distributor",
running: func() bool { return runningState },
}

tailer, _ := setupTailer(t, client)
ctx, cancel := context.WithCancel(context.Background())
go tailer.Run(ctx)

time.Sleep(20 * time.Millisecond)

require.NotPanics(t, func() { cancel() })
}

func setupTailer(t *testing.T, client clientMock) (tailer *tailer, entryHandler *fake.Client) {
w := log.NewSyncWriter(os.Stderr)
logger := log.NewLogfmtLogger(w)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -235,9 +235,11 @@ func (t *Target) StartIfNotRunning() {

// Stop shuts down the target.
func (t *Target) Stop() {
t.cancel()
t.wg.Wait()
level.Debug(t.logger).Log("msg", "stopped Docker target", "container", t.containerName)
if t.Ready() {
t.cancel()
t.wg.Wait()
level.Debug(t.logger).Log("msg", "stopped Docker target", "container", t.containerName)
}
}

// Ready reports whether the target is running.
Expand Down

0 comments on commit 4fb1df9

Please sign in to comment.