Skip to content

Commit

Permalink
feat: Added replication lag metric publisher
Browse files Browse the repository at this point in the history
  • Loading branch information
jruaux committed Oct 11, 2024
1 parent df530d4 commit a3425be
Showing 1 changed file with 36 additions and 0 deletions.
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;
}

}

0 comments on commit a3425be

Please sign in to comment.