-
Notifications
You must be signed in to change notification settings - Fork 40
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Added replication lag metric publisher
- Loading branch information
Showing
1 changed file
with
36 additions
and
0 deletions.
There are no files selected for viewing
36 changes: 36 additions & 0 deletions
36
plugins/riot/src/main/java/com/redis/riot/ReplicateLagWriteListener.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
package com.redis.riot; | ||
|
||
import java.util.concurrent.TimeUnit; | ||
|
||
import org.springframework.batch.core.ItemWriteListener; | ||
import org.springframework.batch.item.Chunk; | ||
|
||
import com.redis.spring.batch.item.redis.common.KeyValue; | ||
|
||
import io.micrometer.core.instrument.MeterRegistry; | ||
import io.micrometer.core.instrument.Metrics; | ||
import io.micrometer.core.instrument.Tags; | ||
import io.micrometer.core.instrument.Timer; | ||
|
||
public class ReplicateLagWriteListener implements ItemWriteListener<KeyValue<byte[]>> { | ||
|
||
private static final String METRIC_NAME = "riot.replicate.lag"; | ||
|
||
private MeterRegistry meterRegistry = Metrics.globalRegistry; | ||
|
||
@Override | ||
public void afterWrite(Chunk<? extends KeyValue<byte[]>> items) { | ||
items.forEach(this::afterWrite); | ||
} | ||
|
||
private void afterWrite(KeyValue<byte[]> item) { | ||
long lagMillis = System.currentTimeMillis() - item.getTimestamp(); | ||
Timer timer = meterRegistry.timer(METRIC_NAME, Tags.of("event", item.getEvent(), "type", item.getType())); | ||
timer.record(lagMillis, TimeUnit.MILLISECONDS); | ||
} | ||
|
||
public void setMeterRegistry(MeterRegistry registry) { | ||
this.meterRegistry = registry; | ||
} | ||
|
||
} |