Skip to content

Commit

Permalink
Allow specifying custom headers for Loki log output (#3227)
Browse files Browse the repository at this point in the history
  • Loading branch information
federicotdn authored Jul 27, 2023
1 parent 6c31075 commit 3c006a5
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 1 deletion.
10 changes: 10 additions & 0 deletions log/loki.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import (
type lokiHook struct {
fallbackLogger logrus.FieldLogger
addr string
headers [][2]string
labels [][2]string
ch chan *logrus.Entry
limit int
Expand Down Expand Up @@ -124,6 +125,11 @@ func (h *lokiHook) parseArgs(line string) error {
labelKey := strings.TrimPrefix(key, "label.")
h.labels = append(h.labels, [2]string{labelKey, value})

continue
} else if strings.HasPrefix(key, "header.") {
headerKey := strings.TrimPrefix(key, "header.")
h.headers = append(h.headers, [2]string{headerKey, value})

continue
}

Expand Down Expand Up @@ -336,6 +342,10 @@ func (h *lokiHook) push(b bytes.Buffer) error {

req.Header.Set("Content-Type", "application/json")

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

res, err := h.client.Do(req)

if res != nil {
Expand Down
38 changes: 37 additions & 1 deletion log/loki_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,10 @@ func TestSyslogFromConfigLine(t *testing.T) {
},
},
{
line: "loki=somewhere:1233,label.something=else,label.foo=bar,limit=32,level=info,allowedLabels=[something],pushPeriod=5m32s,msgMaxSize=1231",
line: "loki=somewhere:1233,label.something=else,label.foo=bar,limit=32,level=info,allowedLabels=[something],pushPeriod=5m32s,msgMaxSize=1231,header.x-test=123,header.authorization=token foobar",
res: lokiHook{
addr: "somewhere:1233",
headers: [][2]string{{"x-test", "123"}, {"authorization", "token foobar"}},
limit: 32,
pushPeriod: time.Minute*5 + time.Second*32,
levels: logrus.AllLevels[:5],
Expand Down Expand Up @@ -190,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 3c006a5

Please sign in to comment.