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

Set JournalTarget Priority value to keyword #2087

Merged
Merged
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
2 changes: 2 additions & 0 deletions docs/clients/promtail/configuration.md
Original file line number Diff line number Diff line change
Expand Up @@ -661,6 +661,8 @@ labels:
[path: <string>]
```

**Note**: priority label is available as both value and keyword. For example, if `priority` is `3` then the labels will be `__journal_priority` with a value `3` and `__journal_priority_keyword` with a corresponding keyword `err`.

### syslog_config

The `syslog_config` block configures a syslog listener allowing users to push
Expand Down
25 changes: 25 additions & 0 deletions pkg/promtail/targets/journaltarget.go
Original file line number Diff line number Diff line change
Expand Up @@ -322,7 +322,32 @@ func (t *JournalTarget) Stop() error {
func makeJournalFields(fields map[string]string) map[string]string {
result := make(map[string]string, len(fields))
for k, v := range fields {
if k == "PRIORITY" {
result[fmt.Sprintf("__journal_%s_%s", strings.ToLower(k), "keyword")] = makeJournalPriority(v)
}
result[fmt.Sprintf("__journal_%s", strings.ToLower(k))] = v
}
return result
}

func makeJournalPriority(priority string) string {
switch priority {
case "0":
return "emerg"
case "1":
return "alert"
case "2":
return "crit"
case "3":
return "error"
case "4":
return "warning"
case "5":
return "notice"
case "6":
return "info"
case "7":
return "debug"
}
return priority
}
19 changes: 18 additions & 1 deletion pkg/promtail/targets/journaltarget_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
package targets

import (
"fmt"
"io"
"os"
"testing"
Expand Down Expand Up @@ -113,7 +114,7 @@ func TestJournalTarget(t *testing.T) {
})
assert.NoError(t, err)
}

fmt.Println(client.messages)
assert.Len(t, client.messages, 10)
require.NoError(t, jt.Stop())
}
Expand Down Expand Up @@ -299,3 +300,19 @@ func TestJournalTarget_Cursor_NotTooOld(t *testing.T) {
require.Equal(t, r.config.Since, time.Duration(0))
require.Equal(t, r.config.Cursor, "foobar")
}

func Test_MakeJournalFields(t *testing.T) {
entryFields := map[string]string{
"CODE_FILE": "journaltarget_test.go",
"OTHER_FIELD": "foobar",
"PRIORITY": "6",
}
receivedFields := makeJournalFields(entryFields)
expectedFields := map[string]string{
"__journal_code_file": "journaltarget_test.go",
"__journal_other_field": "foobar",
"__journal_priority": "6",
"__journal_priority_keyword": "info",
}
assert.Equal(t, expectedFields, receivedFields)
}