Skip to content

Commit

Permalink
promtail: change journal cutoff setting to max_age
Browse files Browse the repository at this point in the history
  • Loading branch information
rfratto committed Aug 20, 2019
1 parent e24ee8d commit 3f227f5
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 28 deletions.
8 changes: 4 additions & 4 deletions docs/promtail-examples.md
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ clients:
scrape_configs:
- job_name: journal
journal:
cutoff: 12h
max_age: 12h
path: /var/log/journal
labels:
job: systemd-journal
Expand All @@ -104,10 +104,10 @@ Just like the Docker example, the `scrape_configs` sections holds various
jobs for parsing logs. A job with a `journal` key configures it for systemd
journal reading.

`cutoff` is an optional string specifying the earliest entry that will be
read. If unspecified, `cutoff` defaults to `7h`. Even if the position in the
`max_age` is an optional string specifying the earliest entry that will be
read. If unspecified, `max_age` defaults to `7h`. Even if the position in the
journal is saved, if the entry corresponding to that position is older than
the cutoff, the position won't be used.
the max_age, the position won't be used.

`path` is an optional string specifying the path to read journal entries
from. If unspecified, defaults to the system default (`/var/log/journal`).
Expand Down
6 changes: 3 additions & 3 deletions pkg/promtail/scrape/scrape.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,13 +25,13 @@ type Config struct {

// JournalTargetConfig describes systemd journal records to scrape.
type JournalTargetConfig struct {
// Cutoff determines the oldest relative time from process start that will
// MaxAge determines the oldest relative time from process start that will
// be read and sent to Loki. Values like 14h means no entry older than
// 14h will be read. If unspecified, defaults to 7h.
//
// A relative time specified here takes precedence over the saved position;
// if the cursor is older than the Cutoff value, it will not be used.
Cutoff string `yaml:"cutoff"`
// if the cursor is older than the MaxAge value, it will not be used.
MaxAge string `yaml:"max_age"`

// Labels optionally holds labels to associate with each record coming out
// of the journal.
Expand Down
53 changes: 33 additions & 20 deletions pkg/promtail/targets/journaltarget.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,10 @@ const (
// with how that is handled in sdjournal.
journalEmptyStr = " "

// journalDefaultCutoffTime represents the default earliest entry that
// journalDefaultMaxAgeTime represents the default earliest entry that
// will be read by the journal reader if there is no saved position
// newer than the "cutoff" time.
journalDefaultCutoffTime = time.Hour * 7
// newer than the "max_age" time.
journalDefaultMaxAgeTime = time.Hour * 7
)

type journalReader interface {
Expand Down Expand Up @@ -143,15 +143,15 @@ func journalTargetWithReader(
until: until,
}

var fromTime time.Duration
var maxAge time.Duration
var err error
if targetConfig.Cutoff == "" {
fromTime = journalDefaultCutoffTime
if targetConfig.MaxAge == "" {
maxAge = journalDefaultMaxAgeTime
} else {
fromTime, err = time.ParseDuration(targetConfig.Cutoff)
maxAge, err = time.ParseDuration(targetConfig.MaxAge)
}
if err != nil {
return nil, errors.Wrap(err, "parsing journal reader 'from' config value")
return nil, errors.Wrap(err, "parsing journal reader 'max_age' config value")
}

// Default to system path if not defined. Passing an empty string to
Expand All @@ -164,7 +164,12 @@ func journalTargetWithReader(
journalPath = "/var/log/journal"
}

cfg := t.generateJournalConfig(journalPath, position, fromTime, entryFunc)
cfg := t.generateJournalConfig(journalConfigBuilder{
JournalPath: journalPath,
Position: position,
MaxAge: maxAge,
EntryFunc: entryFunc,
})
t.r, err = readerFunc(cfg)
if err != nil {
return nil, errors.Wrap(err, "creating journal reader")
Expand All @@ -180,39 +185,47 @@ func journalTargetWithReader(
return t, nil
}

type journalConfigBuilder struct {
JournalPath string
Position string
MaxAge time.Duration
EntryFunc journalEntryFunc
}

// generateJournalConfig generates a journal config by trying to intelligently
// determine if a time offset or the cursor should be used for the starting
// position in the reader.
func (t *JournalTarget) generateJournalConfig(journalPath string,
position string, fromTime time.Duration, entryFunc journalEntryFunc) sdjournal.JournalReaderConfig {
func (t *JournalTarget) generateJournalConfig(
cb journalConfigBuilder,
) sdjournal.JournalReaderConfig {

cfg := sdjournal.JournalReaderConfig{
Path: journalPath,
Path: cb.JournalPath,
Formatter: t.formatter,
}

if position == "" {
cfg.Since = -1 * fromTime
if cb.Position == "" {
cfg.Since = -1 * cb.MaxAge
return cfg
}

// We have a saved position and need to get that entry to see if it's
// older than fromTime. If it _is_ older, then we need to use cfg.Since
// older than cb.MaxAge. If it _is_ older, then we need to use cfg.Since
// rather than cfg.Cursor.
entry, err := entryFunc(cfg, position)
entry, err := cb.EntryFunc(cfg, cb.Position)
if err != nil {
level.Error(t.logger).Log("msg", "received error reading saved journal position", "err", err.Error())
cfg.Since = -1 * fromTime
cfg.Since = -1 * cb.MaxAge
return cfg
}

ts := time.Unix(0, int64(entry.RealtimeTimestamp)*int64(time.Microsecond))
if time.Since(ts) > fromTime {
cfg.Since = -1 * fromTime
if time.Since(ts) > cb.MaxAge {
cfg.Since = -1 * cb.MaxAge
return cfg
}

cfg.Cursor = position
cfg.Cursor = cb.Position
return cfg
}

Expand Down
2 changes: 1 addition & 1 deletion pkg/promtail/targets/journaltarget_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,7 @@ func TestJournalTarget_Since(t *testing.T) {
}

cfg := scrape.JournalTargetConfig{
Cutoff: "4h",
MaxAge: "4h",
}

jt, err := journalTargetWithReader(logger, client, ps, "test", nil,
Expand Down

0 comments on commit 3f227f5

Please sign in to comment.