Skip to content

Commit

Permalink
Expose lag as prometheus metric (#379)
Browse files Browse the repository at this point in the history
  • Loading branch information
benbjohnson authored Aug 10, 2023
1 parent 46f7a0e commit 53cb40b
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 1 deletion.
4 changes: 4 additions & 0 deletions cmd/litefs/mount_linux.go
Original file line number Diff line number Diff line change
Expand Up @@ -359,6 +359,10 @@ func (c *MountCommand) initStore(ctx context.Context) error {
if err := c.initStoreBackupClient(ctx); err != nil {
return err
}

// Initialize as a singleton so we can automatically collect metrics.
litefs.GlobalStore.Store(c.Store)

return nil
}

Expand Down
2 changes: 1 addition & 1 deletion db.go
Original file line number Diff line number Diff line change
Expand Up @@ -3602,7 +3602,7 @@ var (
}, []string{"db"})

dbLatencySecondsMetricVec = promauto.NewGaugeVec(prometheus.GaugeOpts{
Name: "litefs_db_latency_seconds",
Name: "litefs_db_lag_seconds",
Help: "Latency between generating an LTX file and consuming it.",
}, []string{"db"})
)
29 changes: 29 additions & 0 deletions store.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,15 @@ const (
DefaultBackupFullSyncInterval = 10 * time.Second
)

const (
MetricsMonitorInterval = 1 * time.Second
)

var ErrStoreClosed = fmt.Errorf("store closed")

// GlobalStore represents a single store used for metrics collection.
var GlobalStore atomic.Value

// Store represents a collection of databases.
type Store struct {
mu sync.Mutex
Expand Down Expand Up @@ -1595,6 +1602,18 @@ func (s *Store) setPrimaryTimestamp(ts int64) {
}
}

// Lag returns the number of seconds that the local instance is lagging
// behind the primary node. Returns 0 if the node is the primary or if the
// node is not marked as ready yet.
func (s *Store) Lag() time.Duration {
switch ts := s.PrimaryTimestamp(); ts {
case 0, -1:
return 0 // primary or not ready
default:
return time.Duration(time.Now().UnixMilli()-ts) * time.Millisecond
}
}

// Expvar returns a variable for debugging output.
func (s *Store) Expvar() expvar.Var { return (*StoreVar)(s) }

Expand Down Expand Up @@ -1821,4 +1840,14 @@ var (
Name: "litefs_subscriber_count",
Help: "Number of connected subscribers",
})

_ = promauto.NewGaugeFunc(prometheus.GaugeOpts{
Name: "litefs_lag_seconds",
Help: "Lag behind the primary node, in seconds",
}, func() float64 {
if s := GlobalStore.Load(); s != nil {
return s.(*Store).Lag().Seconds()
}
return 0
})
)

0 comments on commit 53cb40b

Please sign in to comment.