Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion src/main/java/in/projecteka/monitor/Constants.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ public class Constants {
public static final String CURRENT_VERSION = "/" + API_VERSION;

public static final String PATH_HEARTBEAT = CURRENT_VERSION + "/heartbeat";

public static final String PATH_READINESS = CURRENT_VERSION + "/readiness";

private Constants() {}
}
30 changes: 16 additions & 14 deletions src/main/java/in/projecteka/monitor/Metric.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,15 +24,16 @@ public class Metric {
private final MetricServiceClient metricServiceClient;
private static final HashMap<String, Gauge> gaugeMap = new HashMap<>();

public void processRequests() {
public void processRequests(Boolean isReadiness) {
Service service = metricServiceClient.getService();
process(service.getBridgeProperties());
process(service.getConsentManagerProperties());
process(service.getBridgeProperties(), isReadiness);
process(service.getConsentManagerProperties(), isReadiness);
}

private void process(List<ServiceProperties> properties) {
private void process(List<ServiceProperties> properties, Boolean isReadiness) {
properties.forEach(property -> {
String path = String.format("%s%s", property.getUrl(), Constants.PATH_HEARTBEAT);
String path = isReadiness ? String.format("%s%s", property.getUrl(), Constants.PATH_READINESS)
: String.format("%s%s", property.getUrl(), Constants.PATH_HEARTBEAT);
HeartbeatResponse heartbeatResponse = metricServiceClient.getHeartbeat(path);
if (!gaugeMap.containsKey(PROJECT_EKA_METRICS + property.getType())) {
Gauge gaugeStatus = Gauge.build()
Expand All @@ -41,30 +42,31 @@ private void process(List<ServiceProperties> properties) {
.help("Heartbeat Status")
.register();
gaugeMap.put(PROJECT_EKA_METRICS + property.getType(), gaugeStatus);
appendToStatus(property, path, heartbeatResponse, gaugeStatus);
appendToStatus(property, path, heartbeatResponse, gaugeStatus, isReadiness);
} else {
appendToStatus(property, path, heartbeatResponse, gaugeMap.get(PROJECT_EKA_METRICS + property.getType()));
appendToStatus(property, path, heartbeatResponse, gaugeMap.get(PROJECT_EKA_METRICS + property.getType()), isReadiness);
}
});
}

private void appendToStatus(ServiceProperties property, String path,
HeartbeatResponse heartbeatResponse,
Gauge status) {
Gauge status,
Boolean isReadiness) {
if (heartbeatResponse.getStatus().equals(DOWN)) {
String lastUpTime = metricRepository.getIfPresent(path).block();
String lastUpTime = metricRepository.getIfPresent(path, isReadiness).block();
status.labels(property.getName(),
property.getId(),
path,
heartbeatResponse.getStatus().toString(),
lastUpTime).set(0);
} else {
LocalDateTime lastUpTime = LocalDateTime.now(UTC);
Boolean isPresent = isEntryPresent(path).block();
Boolean isPresent = isEntryPresent(path, isReadiness).block();
if (isPresent) {
metricRepository.update(lastUpTime, path).block();
metricRepository.update(lastUpTime, path, isReadiness).block();
} else {
metricRepository.insert(path, Status.UP.toString(), lastUpTime).block();
metricRepository.insert(path, Status.UP.toString(), lastUpTime, isReadiness).block();
}
status.labels(property.getName(),
property.getId(),
Expand All @@ -74,8 +76,8 @@ private void appendToStatus(ServiceProperties property, String path,
}
}

private Mono<Boolean> isEntryPresent(String path) {
return metricRepository.getIfPresent(path)
private Mono<Boolean> isEntryPresent(String path, Boolean isReadiness) {
return metricRepository.getIfPresent(path, isReadiness)
.map(entry -> !entry.equals(""))
.switchIfEmpty(defer(() -> just(false)));
}
Expand Down
9 changes: 8 additions & 1 deletion src/main/java/in/projecteka/monitor/MetricController.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,14 @@ public class MetricController {

@GetMapping(path = "/metrics")
public void metrics(Writer responseWriter) throws IOException {
metric.processRequests();
metric.processRequests(false);
TextFormat.write004(responseWriter, CollectorRegistry.defaultRegistry.metricFamilySamples());
responseWriter.close();
}

@GetMapping(path = "/readiness")
public void readiness(Writer responseWriter) throws IOException {
metric.processRequests(true);
TextFormat.write004(responseWriter, CollectorRegistry.defaultRegistry.metricFamilySamples());
responseWriter.close();
}
Expand Down
21 changes: 14 additions & 7 deletions src/main/java/in/projecteka/monitor/MetricRepository.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,16 +13,21 @@ public class MetricRepository {
private static final String INSERT_TO_METRICS = "INSERT INTO metrics " +
"(path, status, last_up_time) VALUES ($1, $2, $3)";
private static final String UPDATE_TO_METRICS = "UPDATE metrics SET last_up_time = $1 WHERE path = $2";
private static final String SELECT_LAST_UP_TIME = "SELECT last_up_time FROM metrics WHERE path=$1";
private static final String SELECT_LAST_UP_TIME_FOR_METRICS = "SELECT last_up_time FROM metrics WHERE path=$1";
private static final String INSERT_TO_READINESS = "INSERT INTO readiness " +
"(path, status, last_up_time) VALUES ($1, $2, $3)";
private static final String UPDATE_TO_READINESS = "UPDATE readiness SET last_up_time = $1 WHERE path = $2";
private static final String SELECT_LAST_UP_TIME_FOR_READINESS = "SELECT last_up_time FROM readiness WHERE path=$1";
private final PgPool dbClient;

public MetricRepository(PgPool dbClient) {
this.dbClient = dbClient;
}

public Mono<Void> insert(String path, String status, LocalDateTime lastUpTime) {
public Mono<Void> insert(String path, String status, LocalDateTime lastUpTime, Boolean isReadiness) {
var query = isReadiness ? INSERT_TO_READINESS : INSERT_TO_METRICS;
return Mono.create(monoSink ->
dbClient.preparedQuery(INSERT_TO_METRICS)
dbClient.preparedQuery(query)
.execute(Tuple.of(path, status, lastUpTime),
handler -> {
if (handler.failed()) {
Expand All @@ -34,9 +39,10 @@ public Mono<Void> insert(String path, String status, LocalDateTime lastUpTime) {
}));
}

public Mono<Void> update(LocalDateTime lastUpTime, String path) {
public Mono<Void> update(LocalDateTime lastUpTime, String path,Boolean isReadiness) {
var query = isReadiness ? UPDATE_TO_READINESS : UPDATE_TO_METRICS;
return Mono.create(monoSink ->
dbClient.preparedQuery(UPDATE_TO_METRICS)
dbClient.preparedQuery(query)
.execute(Tuple.of(lastUpTime, path),
handler -> {
if (handler.failed()) {
Expand All @@ -48,9 +54,10 @@ public Mono<Void> update(LocalDateTime lastUpTime, String path) {
}));
}

public Mono<String> getIfPresent(String path) {
public Mono<String> getIfPresent(String path, Boolean isReadiness) {
var query = isReadiness ? SELECT_LAST_UP_TIME_FOR_READINESS : SELECT_LAST_UP_TIME_FOR_METRICS;
return Mono.create(monoSink ->
dbClient.preparedQuery(SELECT_LAST_UP_TIME)
dbClient.preparedQuery(query)
.execute(Tuple.of(path),
handler -> {
if (handler.failed()) {
Expand Down