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

Promtail: Autodetect log format #380

Closed
Closed
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
26 changes: 25 additions & 1 deletion pkg/promtail/api/entry_parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,15 +18,22 @@ const (
Docker EntryParser = iota
Raw
CRI
Auto
)

var (
criPattern = regexp.MustCompile(`^(?s)(?P<time>\S+?) (?P<stream>stdout|stderr) (?P<flags>\S+?) (?P<content>.*)$`)
criPattern = regexp.MustCompile(`^(?s)(?P<time>\S+?) (?P<stream>stdout|stderr) (?P<flags>\S+?) (?P<content>.*)$`)
autodetectOrder = [3]EntryParser{Docker, CRI, Raw}
dummyHandler = EntryHandlerFunc(func(ls model.LabelSet, t time.Time, s string) error {
return nil
})
)

// String returns a string representation of the EntryParser.
func (e EntryParser) String() string {
switch e {
case Auto:
return "auto"
case CRI:
return "cri"
case Docker:
Expand All @@ -41,6 +48,9 @@ func (e EntryParser) String() string {
// Set implements flag.Value.
func (e *EntryParser) Set(s string) error {
switch strings.ToLower(s) {
case "auto":
*e = Auto
return nil
case "cri":
*e = CRI
return nil
Expand All @@ -67,6 +77,20 @@ func (e *EntryParser) UnmarshalYAML(unmarshal func(interface{}) error) error {
// Wrap implements EntryMiddleware.
func (e EntryParser) Wrap(next EntryHandler) EntryHandler {
switch e {
case Auto:
autodetected := EntryHandler(nil)
return EntryHandlerFunc(func(labels model.LabelSet, timestamp time.Time, line string) error {
if autodetected == nil {
for _, current := range autodetectOrder {
if current.Wrap(dummyHandler).Handle(labels, timestamp, line) == nil {
autodetected = current.Wrap(next)
break
}
}
}

return autodetected.Handle(labels, timestamp, line)
})
case CRI:
return EntryHandlerFunc(func(labels model.LabelSet, _ time.Time, line string) error {
parts := criPattern.FindStringSubmatch(line)
Expand Down
21 changes: 20 additions & 1 deletion pkg/promtail/api/entry_parser_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (
var (
TestTimeStr = "2019-01-01T01:00:00.000000001Z"
TestTime, _ = time.Parse(time.RFC3339Nano, TestTimeStr)
CurrentTime = time.Now()
)

type Entry struct {
Expand Down Expand Up @@ -73,14 +74,32 @@ func TestDocker(t *testing.T) {
runTestCases(Docker, dockerTestCases, t)
}

var rawTestCases = []TestCase{
{"message", false, Entry{CurrentTime, "message", model.LabelSet{}}},
}

func TestRaw(t *testing.T) {
runTestCases(Raw, rawTestCases, t)
}

var autoTestCases = []TestCase{
{"{\"log\":\"message\",\"stream\":\"stdout\",\"time\":\"" + TestTimeStr + "\"}", false, NewEntry(TestTime, "message", "stdout")},
{TestTimeStr + " stdout F message", false, NewEntry(TestTime, "message", "stdout")},
{"message", false, Entry{CurrentTime, "message", model.LabelSet{}}},
}

func TestAuto(t *testing.T) {
runTestCases(Auto, autoTestCases, t)
}

func runTestCases(parser EntryParser, testCases []TestCase, t *testing.T) {
for i, tc := range testCases {
client := &TestClient{
Entries: make([]Entry, 0),
}

handler := parser.Wrap(client)
err := handler.Handle(model.LabelSet{}, time.Now(), tc.Line)
err := handler.Handle(model.LabelSet{}, CurrentTime, tc.Line)

if err != nil && tc.ExpectedError {
continue
Expand Down
2 changes: 1 addition & 1 deletion pkg/promtail/scrape/scrape.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ type Config struct {

// DefaultScrapeConfig is the default Config.
var DefaultScrapeConfig = Config{
EntryParser: api.Docker,
EntryParser: api.Auto,
}

// UnmarshalYAML implements the yaml.Unmarshaler interface.
Expand Down
6 changes: 4 additions & 2 deletions pkg/promtail/targets/filetargetmanager.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,8 @@ func NewFileTargetManager(
relabelConfig: cfg.RelabelConfigs,
targets: map[string]*FileTarget{},
hostname: hostname,
entryHandler: cfg.EntryParser.Wrap(client),
entryHandler: client,
entryParser: cfg.EntryParser,
targetConfig: targetConfig,
}
tm.syncers[cfg.JobName] = s
Expand Down Expand Up @@ -124,6 +125,7 @@ type syncer struct {
log log.Logger
positions *positions.Positions
entryHandler api.EntryHandler
entryParser api.EntryParser
hostname string

targets map[string]*FileTarget
Expand Down Expand Up @@ -201,7 +203,7 @@ func (s *syncer) sync(groups []*targetgroup.Group) {
}

func (s *syncer) newTarget(path string, labels model.LabelSet) (*FileTarget, error) {
return NewFileTarget(s.log, s.entryHandler, s.positions, path, labels, s.targetConfig)
return NewFileTarget(s.log, s.entryParser.Wrap(s.entryHandler), s.positions, path, labels, s.targetConfig)
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should I re-detect at a deeper level than FileTarget?

}

func (s *syncer) ready() bool {
Expand Down
2 changes: 1 addition & 1 deletion production/helm/promtail/values.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ annotations: {}

deploymentStrategy: RollingUpdate

entryParser: docker
entryParser: auto

image:
repository: grafana/promtail
Expand Down
2 changes: 1 addition & 1 deletion production/ksonnet/promtail/config.libsonnet
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
hostname: 'logs-us-west1.grafana.net',
container_root_path: '/var/lib/docker',
external_labels: {},
entry_parser: 'docker',
entry_parser: 'auto',
},

service_url:
Expand Down
2 changes: 1 addition & 1 deletion tools/promtail.sh
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ APIKEY="${2:-}"
INSTANCEURL="${3:-}"
NAMESPACE="${4:-default}"
CONTAINERROOT="${5:-/var/lib/docker}"
PARSER="${6:-docker}"
PARSER="${6:-auto}"

if [ -z "$INSTANCEID" -o -z "$APIKEY" -o -z "$INSTANCEURL" -o -z "$NAMESPACE" -o -z "$CONTAINERROOT" -o -z "$PARSER" ]; then
echo "usage: $0 <instanceId> <apiKey> <url> [<namespace>[<container_root_path>[<parser>]]]"
Expand Down