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: Add support for using syslog message timestamp #2914

Merged
merged 1 commit into from
Nov 11, 2020
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
5 changes: 5 additions & 0 deletions docs/sources/clients/promtail/configuration.md
Original file line number Diff line number Diff line change
Expand Up @@ -765,6 +765,11 @@ label_structured_data: <bool>
# Label map to add to every log message.
labels:
[ <labelname>: <labelvalue> ... ]

# Whether promtail should pass on the timestamp from the incoming syslog message.
# When false, or if no timestamp is present on the syslog message, Promtail will assign the current timestamp to the log when it was processed.
# Default is false
use_incoming_timestamp: <bool>
```

#### Available Labels
Expand Down
4 changes: 4 additions & 0 deletions pkg/promtail/scrapeconfig/scrapeconfig.go
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,10 @@ type SyslogTargetConfig struct {

// Labels optionally holds labels to associate with each record read from syslog.
Labels model.LabelSet `yaml:"labels"`

// UseIncomingTimestamp sets the timestamp to the incoming syslog mesages
// timestamp if it's set.
UseIncomingTimestamp bool `yaml:"use_incoming_timestamp"`
}

// PushTargetConfig describes a scrape config that listens for Loki push messages.
Expand Down
15 changes: 11 additions & 4 deletions pkg/promtail/targets/syslog/syslogtarget.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,8 +64,9 @@ type SyslogTarget struct {
}

type message struct {
labels model.LabelSet
message string
labels model.LabelSet
message string
timestamp time.Time
}

// NewSyslogTarget configures a new SyslogTarget.
Expand Down Expand Up @@ -231,12 +232,18 @@ func (t *SyslogTarget) handleMessage(connLabels labels.Labels, msg syslog.Messag
filtered[model.LabelName(lbl.Name)] = model.LabelValue(lbl.Value)
}

t.messages <- message{filtered, *rfc5424Msg.Message}
var timestamp time.Time
if t.config.UseIncomingTimestamp && rfc5424Msg.Timestamp != nil {
timestamp = *rfc5424Msg.Timestamp
} else {
timestamp = time.Now()
}
t.messages <- message{filtered, *rfc5424Msg.Message, timestamp}
}

func (t *SyslogTarget) messageSender() {
for msg := range t.messages {
if err := t.handler.Handle(msg.labels, time.Now(), msg.message); err != nil {
if err := t.handler.Handle(msg.labels, msg.timestamp, msg.message); err != nil {
level.Error(t.logger).Log("msg", "error handling line", "error", err)
}
syslogEntries.Inc()
Expand Down