Skip to content

Commit

Permalink
Apply PR suggested fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
federicotdn committed Jul 25, 2023
1 parent 2ceea68 commit 05fea35
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 1 deletion.
2 changes: 1 addition & 1 deletion log/loki.go
Original file line number Diff line number Diff line change
Expand Up @@ -343,7 +343,7 @@ func (h *lokiHook) push(b bytes.Buffer) error {
req.Header.Set("Content-Type", "application/json")

for _, header := range h.headers {
req.Header.Set(header[0], header[1])
req.Header.Add(header[0], header[1])
}

res, err := h.client.Do(req)
Expand Down
35 changes: 35 additions & 0 deletions log/loki_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -191,3 +191,38 @@ func TestLokiFlushingOnStop(t *testing.T) {
t.Fatal("No logs were received from loki before hook has finished")
}
}

func TestLokiHeaders(t *testing.T) {
t.Parallel()
receivedHeaders := make(chan http.Header, 1)
srv := httptest.NewServer(
http.HandlerFunc(func(rw http.ResponseWriter, req *http.Request) {
receivedHeaders <- req.Header
close(receivedHeaders) // see comment in TestLokiFlushingOnStop
}),
)
configLine := fmt.Sprintf("loki=%s,pushPeriod=1h,header.X-Foo=bar,header.Test=hello world,header.X-Foo=baz", srv.URL)
h, err := LokiFromConfigLine(nil, configLine)
require.NoError(t, err)

ctx, cancel := context.WithCancel(context.Background())
wg := new(sync.WaitGroup)
now := time.Now()
wg.Add(1)
go func() {
defer wg.Done()
err = h.Fire(&logrus.Entry{Time: now, Level: logrus.InfoLevel, Message: "test message"})
time.Sleep(time.Millisecond * 10)
cancel()
}()
h.Listen(ctx)
wg.Wait()

select {
case headers := <-receivedHeaders:
require.Equal(t, []string{"bar", "baz"}, headers["X-Foo"])
require.Equal(t, []string{"hello world"}, headers["Test"])
default:
t.Fatal("No logs were received from loki before hook has finished")
}
}

0 comments on commit 05fea35

Please sign in to comment.