Skip to content

Commit

Permalink
Set JournalTarget Priority value to keyword (#2087)
Browse files Browse the repository at this point in the history
  • Loading branch information
adityacs authored May 26, 2020
1 parent 8cbeb53 commit ff0a4c3
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 1 deletion.
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 @@ -331,7 +331,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)
}

0 comments on commit ff0a4c3

Please sign in to comment.