Skip to content

Commit

Permalink
feat(loki): add support for custom headers (#142)
Browse files Browse the repository at this point in the history
add custom headers support in Loki sink, and documentation in README

fix: #141
  • Loading branch information
btoic committed Nov 17, 2023
1 parent f48d5bf commit ff7498c
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 0 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -515,6 +515,8 @@ receivers:
receivers:
- name: "loki"
loki:
headers: # optional
X-Scope-OrgID: tennantID
streamLabels:
foo: bar
url: http://127.0.0.1:3100/loki/api/v1/push
Expand Down
13 changes: 13 additions & 0 deletions pkg/sinks/loki.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
"net/http"
"strconv"
"time"
"github.com/rs/zerolog/log"
)

type promtailStream struct {
Expand All @@ -27,6 +28,7 @@ type LokiConfig struct {
StreamLabels map[string]string `yaml:"streamLabels"`
TLS TLS `yaml:"tls"`
URL string `yaml:"url"`
Headers map[string]string `yaml:"headers"`
}

type Loki struct {
Expand Down Expand Up @@ -72,6 +74,17 @@ func (l *Loki) Send(ctx context.Context, ev *kube.EnhancedEvent) error {

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

for k, v := range l.cfg.Headers {
realValue, err := GetString(ev, v)
if err != nil {
log.Debug().Err(err).Msgf("parse template failed: %s", v)
req.Header.Add(k, v)
} else {
log.Debug().Msgf("request header: {%s: %s}", k, realValue)
req.Header.Add(k, realValue)
}
}

client := http.DefaultClient
resp, err := client.Do(req)
if err != nil {
Expand Down

0 comments on commit ff7498c

Please sign in to comment.