Skip to content

Commit 46254ec

Browse files
authored
Added new metric: seconds since the last event has been synced (#28)
1 parent d2a9b70 commit 46254ec

File tree

2 files changed

+36
-8
lines changed

2 files changed

+36
-8
lines changed

internal/bridge/replicator.go

+24-7
Original file line numberDiff line numberDiff line change
@@ -33,8 +33,9 @@ type Bridge struct {
3333
cancel context.CancelFunc
3434
logger zerolog.Logger
3535

36-
dumping *atomic.Bool
37-
running *atomic.Bool
36+
dumping *atomic.Bool
37+
running *atomic.Bool
38+
syncedAt *atomic.Int64
3839

3940
syncCh chan interface{}
4041
closeOnce *sync.Once
@@ -45,6 +46,7 @@ func New(cfg *config.Config, logger zerolog.Logger) (*Bridge, error) {
4546
logger: logger,
4647
dumping: atomic.NewBool(false),
4748
running: atomic.NewBool(false),
49+
syncedAt: atomic.NewInt64(0),
4850
syncCh: make(chan interface{}, eventsBufSize),
4951
closeOnce: &sync.Once{},
5052
}
@@ -258,11 +260,7 @@ func (b *Bridge) newTarantoolClient(cfg *config.Config) {
258260
func (b *Bridge) Run() <-chan error {
259261
defer b.setRunning(false)
260262

261-
go func() {
262-
for range time.Tick(1 * time.Second) {
263-
metrics.SetSecondsBehindMaster(b.Delay())
264-
}
265-
}()
263+
go b.runBackgroundJobs()
266264

267265
maxErrs := 3
268266
errCh := make(chan error, maxErrs)
@@ -328,6 +326,7 @@ func (b *Bridge) syncLoop() error {
328326
return err
329327
}
330328
}
329+
b.syncedAt.Store(time.Now().Unix())
331330
case <-b.ctx.Done():
332331
return nil
333332
}
@@ -400,3 +399,21 @@ func (b *Bridge) setDumping(v bool) {
400399
func (b *Bridge) Dumping() bool {
401400
return b.dumping.Load()
402401
}
402+
403+
func (b *Bridge) runBackgroundJobs() {
404+
go func() {
405+
for range time.Tick(1 * time.Second) {
406+
metrics.SetSecondsBehindMaster(b.Delay())
407+
}
408+
}()
409+
410+
go func() {
411+
for range time.Tick(1 * time.Second) {
412+
syncedAt := b.syncedAt.Load()
413+
if syncedAt > 0 {
414+
now := time.Now().Unix()
415+
metrics.SetSyncedSecondsAgo(now - syncedAt)
416+
}
417+
}
418+
}()
419+
}

internal/metrics/metrics.go

+12-1
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,13 @@ var (
1414
secondsBehindMaster = prometheus.NewGauge(prometheus.GaugeOpts{
1515
Namespace: "mysql2tarantool",
1616
Name: "seconds_behind",
17-
Help: "Current replication lag of the replicator",
17+
Help: "Current replication lag of the replicator. Calculates as diff between current timestamp and last event timestamp. The value updates only after receiving the event.",
18+
})
19+
20+
syncedSecondsAgo = prometheus.NewGauge(prometheus.GaugeOpts{
21+
Namespace: "mysql2tarantool",
22+
Name: "synced_seconds_ago",
23+
Help: "Seconds since the last event has been synced",
1824
})
1925

2026
replState = prometheus.NewGauge(prometheus.GaugeOpts{
@@ -27,12 +33,17 @@ var (
2733
func Init() {
2834
prometheus.MustRegister(secondsBehindMaster)
2935
prometheus.MustRegister(replState)
36+
prometheus.MustRegister(syncedSecondsAgo)
3037
}
3138

3239
func SetSecondsBehindMaster(value uint32) {
3340
secondsBehindMaster.Set(float64(value))
3441
}
3542

43+
func SetSyncedSecondsAgo(sec int64) {
44+
syncedSecondsAgo.Set(float64(sec))
45+
}
46+
3647
func SetReplicationState(state ReplState) {
3748
replState.Set(float64(state))
3849
}

0 commit comments

Comments
 (0)