Skip to content

Commit 906a30a

Browse files
authored
Merge pull request #205 from chaudhryfaisal/master
adding option to configure StoreExpireTickInterval and Fixed bug where `Store.Expire()` was only invoked once
2 parents b7ffb9e + 747cbad commit 906a30a

4 files changed

Lines changed: 32 additions & 13 deletions

File tree

cmd/mtail/main.go

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -53,8 +53,9 @@ var (
5353
emitProgLabel = flag.Bool("emit_prog_label", true, "Emit the 'prog' label in variable exports.")
5454

5555
// Ops flags
56-
pollInterval = flag.Duration("poll_interval", 0, "Set the interval to poll all log files for data; must be positive, or zero to disable polling. With polling mode, only the files found at mtail startup will be polled.")
57-
disableFsnotify = flag.Bool("disable_fsnotify", false, "When enabled no fsnotify watcher is created, and mtail falls back to polling mode only. Only the files known at program startup will be polled.")
56+
pollInterval = flag.Duration("poll_interval", 0, "Set the interval to poll all log files for data; must be positive, or zero to disable polling. With polling mode, only the files found at mtail startup will be polled.")
57+
disableFsnotify = flag.Bool("disable_fsnotify", false, "When enabled no fsnotify watcher is created, and mtail falls back to polling mode only. Only the files known at program startup will be polled.")
58+
storeExpireTickInterval = flag.Duration("store_expire_tick_interval", time.Hour, "interval to delete expired metrics from store")
5859

5960
// Debugging flags
6061
blockProfileRate = flag.Int("block_profile_rate", 0, "Nanoseconds of block time before goroutine blocking events reported. 0 turns off. See https://golang.org/pkg/runtime/#SetBlockProfileRate")
@@ -121,6 +122,7 @@ func main() {
121122
mtail.BindAddress(*address, *port),
122123
mtail.BuildInfo(buildInfo()),
123124
mtail.OverrideLocation(loc),
125+
mtail.StoreExpireTickInterval(*storeExpireTickInterval),
124126
}
125127
if *oneShot {
126128
opts = append(opts, mtail.OneShot)

internal/metrics/store.go

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,7 @@ func (s *Store) MarshalJSON() (b []byte, err error) {
102102
// Expire iterates through the Store looking for metrics that have been marked
103103
// for expiry, and removing them if their expiration time has passed.
104104
func (s *Store) Expire() error {
105+
glog.Info("Running Store.Expire()")
105106
s.Lock()
106107
defer s.Unlock()
107108
now := time.Now()
@@ -124,14 +125,21 @@ func (s *Store) Expire() error {
124125
}
125126

126127
// StartExpiryLoop runs a permanent goroutine to expire metrics every hour.
127-
func (s *Store) StartExpiryLoop() {
128+
func (s *Store) StartExpiryLoop(duration time.Duration) {
128129
go func() {
129-
ticker := time.NewTicker(time.Hour)
130-
select {
131-
case <-ticker.C:
132-
err := s.Expire()
133-
if err != nil {
134-
glog.Info(err)
130+
if duration <= 0 {
131+
glog.Infof("Metric store expiration disabled")
132+
return
133+
}
134+
glog.Infof("Configuring StartExpiryLoop with %s", duration.String())
135+
ticker := time.NewTicker(duration)
136+
for {
137+
select {
138+
case <-ticker.C:
139+
err := s.Expire()
140+
if err != nil {
141+
glog.Info(err)
142+
}
135143
}
136144
}
137145
}()

internal/mtail/mtail.go

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -58,9 +58,10 @@ type Server struct {
5858
dumpAstTypes bool // if set, mtail prints the program syntax tree after type checking
5959
dumpBytecode bool // if set, mtail prints the program bytecode after code generation
6060

61-
syslogUseCurrentYear bool // if set, use the current year for timestamps that have no year information
62-
omitMetricSource bool // if set, do not link the source program to a metric
63-
omitProgLabel bool // if set, do not put the program name in the metric labels
61+
syslogUseCurrentYear bool // if set, use the current year for timestamps that have no year information
62+
omitMetricSource bool // if set, do not link the source program to a metric
63+
omitProgLabel bool // if set, do not put the program name in the metric labels
64+
storeExpireTickInterval time.Duration // interval to expire/delete metrics from store
6465
}
6566

6667
// StartTailing adds each log path pattern to the tailer.
@@ -344,7 +345,7 @@ func (m *Server) Run() error {
344345
return err
345346
}
346347
} else {
347-
m.store.StartExpiryLoop()
348+
m.store.StartExpiryLoop(m.storeExpireTickInterval)
348349
if err := m.Serve(); err != nil {
349350
return err
350351
}

internal/mtail/options.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,14 @@ func OverrideLocation(loc *time.Location) func(*Server) error {
5050
}
5151
}
5252

53+
// StoreExpireTickInterval sets the interval to run ticker to delete expired metrics from store.
54+
func StoreExpireTickInterval(interval time.Duration) func(*Server) error {
55+
return func(m *Server) error {
56+
m.storeExpireTickInterval = interval
57+
return nil
58+
}
59+
}
60+
5361
// OneShot sets one-shot mode in the MtailServer.
5462
func OneShot(m *Server) error {
5563
m.oneShot = true

0 commit comments

Comments
 (0)