Move event-listener to sub package (#29)
This commit is contained in:
parent
a392076722
commit
32fc7a1d5d
9 changed files with 10 additions and 13 deletions
|
@ -19,9 +19,6 @@ import org.testcontainers.containers.GenericContainer;
|
|||
import org.testcontainers.containers.wait.strategy.Wait;
|
||||
import org.testcontainers.utility.MountableFile;
|
||||
|
||||
import io.kokuwa.keycloak.metrics.prometheus.Prometheus;
|
||||
import io.kokuwa.keycloak.metrics.prometheus.PrometheusClient;
|
||||
|
||||
/**
|
||||
* JUnit extension to start keycloak.
|
||||
*
|
||||
|
|
|
@ -0,0 +1,62 @@
|
|||
package io.kokuwa.keycloak.metrics.junit;
|
||||
|
||||
import java.util.HashSet;
|
||||
import java.util.Map;
|
||||
import java.util.Objects;
|
||||
import java.util.Set;
|
||||
import java.util.stream.Collectors;
|
||||
import java.util.stream.Stream;
|
||||
|
||||
import org.keycloak.events.EventType;
|
||||
|
||||
/**
|
||||
* Client to access Prometheus metric values:
|
||||
*
|
||||
* @author Stephan Schnabel
|
||||
*/
|
||||
public class Prometheus {
|
||||
|
||||
private final Set<PrometheusMetric> state = new HashSet<>();
|
||||
private final PrometheusClient client;
|
||||
|
||||
public Prometheus(PrometheusClient client) {
|
||||
this.client = client;
|
||||
}
|
||||
|
||||
public int userEvent(EventType type) {
|
||||
return state.stream()
|
||||
.filter(metric -> Objects.equals(metric.name(), "keycloak_event_user_total"))
|
||||
.filter(metric -> Objects.equals(metric.tags().get("type"), type.toString()))
|
||||
.mapToInt(metric -> metric.value().intValue())
|
||||
.sum();
|
||||
}
|
||||
|
||||
public int userEvent(EventType type, String realmName, String clientId) {
|
||||
return state.stream()
|
||||
.filter(metric -> Objects.equals(metric.name(), "keycloak_event_user_total"))
|
||||
.filter(metric -> Objects.equals(metric.tags().get("type"), type.toString()))
|
||||
.filter(metric -> Objects.equals(metric.tags().get("realm"), realmName))
|
||||
.filter(metric -> Objects.equals(metric.tags().get("client"), clientId))
|
||||
.mapToInt(metric -> metric.value().intValue())
|
||||
.sum();
|
||||
}
|
||||
|
||||
public void scrap() {
|
||||
state.clear();
|
||||
Stream.of(client.scrap().split("[\\r\\n]+"))
|
||||
.filter(line -> !line.startsWith("#"))
|
||||
.filter(line -> line.startsWith("keycloak"))
|
||||
.map(line -> {
|
||||
var name = line.substring(0, line.contains("{") ? line.indexOf("{") : line.lastIndexOf(" "));
|
||||
var tags = line.contains("{")
|
||||
? Stream.of(line.substring(line.indexOf("{") + 1, line.indexOf("}")).split(","))
|
||||
.map(tag -> tag.split("="))
|
||||
.filter(tag -> tag.length >= 2)
|
||||
.collect(Collectors.toMap(tag -> tag[0], tag -> tag[1].replace("\"", "")))
|
||||
: Map.<String, String>of();
|
||||
var value = Double.parseDouble(line.substring(line.lastIndexOf(" ")));
|
||||
return new PrometheusMetric(name, tags, value);
|
||||
})
|
||||
.forEach(state::add);
|
||||
}
|
||||
}
|
|
@ -0,0 +1,19 @@
|
|||
package io.kokuwa.keycloak.metrics.junit;
|
||||
|
||||
import javax.ws.rs.Consumes;
|
||||
import javax.ws.rs.GET;
|
||||
import javax.ws.rs.Path;
|
||||
import javax.ws.rs.core.MediaType;
|
||||
|
||||
/**
|
||||
* JAX-RS client for prometheus endpoint.
|
||||
*
|
||||
* @author Stephan Schnabel
|
||||
*/
|
||||
public interface PrometheusClient {
|
||||
|
||||
@GET
|
||||
@Path("/metrics")
|
||||
@Consumes(MediaType.TEXT_PLAIN)
|
||||
String scrap();
|
||||
}
|
|
@ -0,0 +1,13 @@
|
|||
package io.kokuwa.keycloak.metrics.junit;
|
||||
|
||||
import java.util.Map;
|
||||
|
||||
/**
|
||||
* Represents a parsed Prometheus line.
|
||||
*
|
||||
* @author Stephan Schnabel
|
||||
* @param name Metric name
|
||||
* @param tags Tags for this metriv value
|
||||
* @param value Metric value
|
||||
*/
|
||||
public record PrometheusMetric(String name, Map<String, String> tags, Double value) {}
|
Loading…
Add table
Add a link
Reference in a new issue