@@ -22,6 +22,7 @@ import (
22
22
"fmt"
23
23
"os"
24
24
"strings"
25
+ "syscall"
25
26
"time"
26
27
27
28
"github.com/coreos/go-systemd/sdjournal"
@@ -131,7 +132,7 @@ func getJournal(cfg types.WatcherConfig) (*sdjournal.Journal, error) {
131
132
path = cfg .LogPath
132
133
}
133
134
// Get lookback duration.
134
- since , err := time .ParseDuration (cfg .Lookback )
135
+ lookback , err := time .ParseDuration (cfg .Lookback )
135
136
if err != nil {
136
137
return nil , fmt .Errorf ("failed to parse lookback duration %q: %v" , cfg .Lookback , err )
137
138
}
@@ -145,11 +146,24 @@ func getJournal(cfg types.WatcherConfig) (*sdjournal.Journal, error) {
145
146
if err != nil {
146
147
return nil , fmt .Errorf ("failed to create journal client from path %q: %v" , path , err )
147
148
}
149
+ // Use system uptime if lookback duration is longer than it.
150
+ // Ideally, we should use monotonic timestamp + boot id in journald. However, it doesn't seem
151
+ // to work with go-system/journal package.
152
+ // TODO(random-liu): Use monotonic timestamp + boot id.
153
+ var info syscall.Sysinfo_t
154
+ if err := syscall .Sysinfo (& info ); err != nil {
155
+ return nil , fmt .Errorf ("failed to get system info: %v" , err )
156
+ }
157
+ uptime := time .Duration (info .Uptime ) * time .Second
158
+ if lookback > uptime {
159
+ lookback = uptime
160
+ glog .Infof ("Lookback changed to system uptime: %v" , lookback )
161
+ }
148
162
// Seek journal client based on the lookback duration.
149
- start := time .Now ().Add (- since )
163
+ start := time .Now ().Add (- lookback )
150
164
err = journal .SeekRealtimeUsec (uint64 (start .UnixNano () / 1000 ))
151
165
if err != nil {
152
- return nil , fmt .Errorf ("failed to lookback %q: %v" , since , err )
166
+ return nil , fmt .Errorf ("failed to lookback %q: %v" , lookback , err )
153
167
}
154
168
// Empty source is not allowed and treated as an error.
155
169
source := cfg .PluginConfig [configSourceKey ]
0 commit comments