feat: initial commit registering in the event-listener- and realm-resource-SPIs

This commit is contained in:
transcaffeine 2024-08-20 17:58:00 +02:00
commit 23bc2ec41d
Signed by: transcaffeine
GPG Key ID: 03624C433676E465
15 changed files with 507 additions and 0 deletions

38
.gitignore vendored Normal file
View File

@ -0,0 +1,38 @@
target/
!.mvn/wrapper/maven-wrapper.jar
!**/src/main/**/target/
!**/src/test/**/target/
### IntelliJ IDEA ###
.idea/modules.xml
.idea/jarRepositories.xml
.idea/compiler.xml
.idea/libraries/
*.iws
*.iml
*.ipr
### Eclipse ###
.apt_generated
.classpath
.factorypath
.project
.settings
.springBeans
.sts4-cache
### NetBeans ###
/nbproject/private/
/nbbuild/
/dist/
/nbdist/
/.nb-gradle/
build/
!**/src/main/**/build/
!**/src/test/**/build/
### VS Code ###
.vscode/
### Mac OS ###
.DS_Store

7
.idea/encodings.xml generated Normal file
View File

@ -0,0 +1,7 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="Encoding">
<file url="file://$PROJECT_DIR$/src/main/java" charset="UTF-8" />
<file url="file://$PROJECT_DIR$/src/main/resources" charset="UTF-8" />
</component>
</project>

13
.idea/misc.xml generated Normal file
View File

@ -0,0 +1,13 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="MavenProjectsManager">
<option name="originalFiles">
<list>
<option value="$PROJECT_DIR$/pom.xml" />
</list>
</option>
</component>
<component name="ProjectRootManager" version="2" languageLevel="JDK_21" default="true" project-jdk-name="21" project-jdk-type="JavaSDK">
<output url="file://$PROJECT_DIR$/out" />
</component>
</project>

View File

@ -0,0 +1,32 @@
<component name="ProjectRunConfigurationManager">
<configuration default="false" name="keycloak-metrics [package]" type="MavenRunConfiguration" factoryName="Maven" nameIsGenerated="true">
<MavenSettings>
<option name="myGeneralSettings" />
<option name="myRunnerSettings" />
<option name="myRunnerParameters">
<MavenRunnerParameters>
<option name="cmdOptions" />
<option name="profiles">
<set />
</option>
<option name="goals">
<list>
<option value="package" />
</list>
</option>
<option name="multimoduleDir" />
<option name="pomFileName" />
<option name="profilesMap">
<map />
</option>
<option name="projectsCmdOptionValues">
<list />
</option>
<option name="resolveToWorkspace" value="false" />
<option name="workingDirPath" value="$PROJECT_DIR$" />
</MavenRunnerParameters>
</option>
</MavenSettings>
<method v="2" />
</configuration>
</component>

6
.idea/vcs.xml generated Normal file
View File

@ -0,0 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="VcsDirectoryMappings">
<mapping directory="$PROJECT_DIR$" vcs="Git" />
</component>
</project>

87
.idea/workspace.xml generated Normal file
View File

@ -0,0 +1,87 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="AutoImportSettings">
<option name="autoReloadType" value="SELECTIVE" />
</component>
<component name="ChangeListManager">
<list default="true" id="0b19344d-a6be-4de4-b466-20f9855520b4" name="Changes" comment="">
<change afterPath="$PROJECT_DIR$/.gitignore" afterDir="false" />
<change afterPath="$PROJECT_DIR$/.idea/encodings.xml" afterDir="false" />
<change afterPath="$PROJECT_DIR$/.idea/misc.xml" afterDir="false" />
<change afterPath="$PROJECT_DIR$/.idea/runConfigurations/keycloak_metrics__package_.xml" afterDir="false" />
<change afterPath="$PROJECT_DIR$/.idea/vcs.xml" afterDir="false" />
<change afterPath="$PROJECT_DIR$/.idea/workspace.xml" afterDir="false" />
<change afterPath="$PROJECT_DIR$/pom.xml" afterDir="false" />
</list>
<option name="SHOW_DIALOG" value="false" />
<option name="HIGHLIGHT_CONFLICTS" value="true" />
<option name="HIGHLIGHT_NON_ACTIVE_CHANGELIST" value="false" />
<option name="LAST_RESOLUTION" value="IGNORE" />
</component>
<component name="FileTemplateManagerImpl">
<option name="RECENT_TEMPLATES">
<list>
<option value="Class" />
</list>
</option>
</component>
<component name="Git.Settings">
<option name="RECENT_GIT_ROOT_PATH" value="$PROJECT_DIR$" />
</component>
<component name="MavenImportPreferences">
<option name="importingSettings">
<MavenImportingSettings>
<option name="downloadAnnotationsAutomatically" value="true" />
<option name="downloadDocsAutomatically" value="true" />
<option name="downloadSourcesAutomatically" value="true" />
<option name="workspaceImportForciblyTurnedOn" value="true" />
</MavenImportingSettings>
</option>
</component>
<component name="ProjectColorInfo"><![CDATA[{
"associatedIndex": 6
}]]></component>
<component name="ProjectId" id="2kqCF33qcDFLtxiSNN2CesYrBhr" />
<component name="ProjectLevelVcsManager" settingsEditedManually="true">
<ConfirmationsSetting value="1" id="Add" />
</component>
<component name="ProjectViewState">
<option name="hideEmptyMiddlePackages" value="true" />
<option name="showLibraryContents" value="true" />
</component>
<component name="PropertiesComponent"><![CDATA[{
"keyToString": {
"Downloaded.Files.Path.Enabled": "false",
"Maven.keycloak-metrics [compile].executor": "Run",
"Maven.keycloak-metrics [package].executor": "Run",
"Maven.keycloak-metrics.executor": "Run",
"Repository.Attach.Annotations": "false",
"Repository.Attach.JavaDocs": "false",
"Repository.Attach.Sources": "false",
"RunOnceActivity.ShowReadmeOnStart": "true",
"SHARE_PROJECT_CONFIGURATION_FILES": "true",
"git-widget-placeholder": "master",
"kotlin-language-version-configured": "true",
"project.structure.last.edited": "Libraries",
"project.structure.proportion": "0.15",
"project.structure.side.proportion": "0.3908046",
"settings.editor.selected.configurable": "preferences.lookFeel"
}
}]]></component>
<component name="RecentsManager">
<key name="CreateClassDialog.RecentsKey">
<recent name="coffee._finally.keycloak_metrics" />
</key>
</component>
<component name="SpellCheckerSettings" RuntimeDictionaries="0" Folders="0" CustomDictionaries="0" DefaultDictionary="application-level" UseSingleDictionary="true" transferred="true" />
<component name="TaskManager">
<task active="true" id="Default" summary="Default task">
<changelist id="0b19344d-a6be-4de4-b466-20f9855520b4" name="Changes" comment="" />
<created>1724001613087</created>
<option name="number" value="Default" />
<option name="presentableId" value="Default" />
<updated>1724001613087</updated>
</task>
<servers />
</component>
</project>

66
pom.xml Normal file
View File

@ -0,0 +1,66 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.keycloak</groupId>
<artifactId>keycloak-parent</artifactId>
<version>24.0.0</version>
</parent>
<artifactId>keycloak-metrics</artifactId>
<packaging>jar</packaging>
<dependencies>
<dependency>
<groupId>org.keycloak</groupId>
<artifactId>keycloak-core</artifactId>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.keycloak</groupId>
<artifactId>keycloak-server-spi</artifactId>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.keycloak</groupId>
<artifactId>keycloak-server-spi-private</artifactId>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.keycloak</groupId>
<artifactId>keycloak-common</artifactId>
</dependency>
<dependency>
<groupId>org.keycloak</groupId>
<artifactId>keycloak-services</artifactId>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>io.prometheus</groupId>
<artifactId>simpleclient</artifactId>
<version>0.16.0</version>
</dependency>
<dependency>
<groupId>io.prometheus</groupId>
<artifactId>simpleclient_common</artifactId>
<version>0.16.0</version>
</dependency>
</dependencies>
<build>
<finalName>keycloak-metrics</finalName>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
</plugins>
</build>
</project>

View File

@ -0,0 +1,2 @@
public class KeycloakMetricsServer {
}

View File

@ -0,0 +1,108 @@
package coffee._finally.keycloak_metrics;
import io.prometheus.client.Collector;
import io.prometheus.client.CollectorRegistry;
import io.prometheus.client.Counter;
import io.prometheus.client.Gauge;
import io.prometheus.client.exporter.common.TextFormat;
import java.io.*;
import java.util.HashMap;
import java.util.Map;
public class Metrics {
private static Metrics METRICS_INSTANCE = null;
private static Map<String, Collector> metrics = new HashMap();
private static CollectorRegistry registry = new CollectorRegistry();
private Metrics() {
initializeMetrics();
}
private void initializeMetrics() {
metrics.put("realm", Gauge.build()
.namespace("keycloak")
.name("realm_count")
.labelNames("realm")
.help("Number of realms in keycloak")
.register(registry));
metrics.put("realm_role", Gauge.build()
.namespace("keycloak")
.name("role_count")
.labelNames("realm")
.help("Number of roles in a realm")
.register(registry));
metrics.put("admin_event", Counter.build()
.namespace("keycloak").name("admin_event_count")
.labelNames("realm", "operation", "resource")
.help("Number and type of admin events since startup")
.register(registry));
metrics.put("user_event", Counter.build()
.namespace("keycloak").name("user_event_count")
.labelNames("realm", "type")
.help("Number and type of user events since startup")
.register(registry));
}
public static synchronized Metrics getInstance() {
if (METRICS_INSTANCE == null) {
METRICS_INSTANCE = new Metrics();
}
return METRICS_INSTANCE;
}
public static void increment(String metric) {
Collector collector = metrics.get(metric);
if (collector == null) {
return;
}
if (collector instanceof Gauge) {
((Gauge) collector).inc();
return;
}
if (collector instanceof Counter) {
((Counter) collector).inc();
}
}
public static void incrementLabelled(String metric, String... labels) {
Collector collector = metrics.get(metric);
if (collector == null) {
return;
}
if (collector instanceof Gauge) {
((Gauge) collector).labels(labels).inc();
return;
}
if (collector instanceof Counter) {
((Counter) collector).labels(labels).inc();
}
}
public static void decrement(String metric) {
Collector collector = metrics.get(metric);
if (collector == null) {
return;
}
if (collector instanceof Gauge) {
((Gauge) collector).dec();
}
}
public static void decrementLabelled(String metric, String... labels) {
Collector collector = metrics.get(metric);
if (collector == null) {
return;
}
if (collector instanceof Gauge) {
((Gauge) collector).labels(labels).dec();
}
}
public void export(final OutputStream stream) throws IOException {
final Writer writer = new BufferedWriter(new OutputStreamWriter(stream));
TextFormat.writeOpenMetrics100(writer, registry.metricFamilySamples());
writer.flush();
}
}

View File

@ -0,0 +1,28 @@
package coffee._finally.keycloak_metrics;
import jakarta.ws.rs.GET;
import jakarta.ws.rs.Produces;
import jakarta.ws.rs.core.*;
import org.keycloak.services.resource.RealmResourceProvider;
public class MetricsEndpoint implements RealmResourceProvider {
public static final String ID = "metrics";
@Override
public Object getResource() {
return this;
}
@Override
public void close() {
}
@GET
@Produces(MediaType.TEXT_PLAIN)
public Response get(@Context HttpHeaders headers) {
final StreamingOutput stream = outputStream -> Metrics.getInstance().export(outputStream);
return Response.ok(stream).build();
}
}

View File

@ -0,0 +1,34 @@
package coffee._finally.keycloak_metrics;
import org.keycloak.Config;
import org.keycloak.models.KeycloakSession;
import org.keycloak.models.KeycloakSessionFactory;
import org.keycloak.services.resource.RealmResourceProvider;
import org.keycloak.services.resource.RealmResourceProviderFactory;
public class MetricsEndpointFactory implements RealmResourceProviderFactory {
@Override
public RealmResourceProvider create(KeycloakSession keycloakSession) {
return new MetricsEndpoint();
}
@Override
public void init(Config.Scope scope) {
// empty on purpose
}
@Override
public void postInit(KeycloakSessionFactory keycloakSessionFactory) {
// empty on purpose
}
@Override
public void close() {
// empty on purpose as nothing needs to be closed manually
}
@Override
public String getId() {
return MetricsEndpoint.ID;
}
}

View File

@ -0,0 +1,50 @@
package coffee._finally.keycloak_metrics;
import org.keycloak.events.Event;
import org.keycloak.events.EventListenerProvider;
import org.keycloak.events.admin.AdminEvent;
public class MetricsEventListener implements EventListenerProvider {
public static final String ID = "metrics-listener";
@Override
public void onEvent(Event event) {
Metrics.getInstance().incrementLabelled(
"user_event",
event.getRealmId(), event.getType().toString()
);
}
@Override
public void onEvent(AdminEvent adminEvent, boolean b) {
Metrics.getInstance().incrementLabelled(
"admin_event",
adminEvent.getRealmId(),
adminEvent.getOperationType().toString(),
adminEvent.getResourceTypeAsString()
);
switch (adminEvent.getOperationType()) {
case CREATE:
Metrics.getInstance().incrementLabelled(
adminEvent.getResourceTypeAsString().toLowerCase(),
adminEvent.getRealmId()
);
break;
case DELETE:
Metrics.getInstance().decrementLabelled(
adminEvent.getResourceTypeAsString().toLowerCase(),
adminEvent.getRealmId()
);
break;
case UPDATE:
case ACTION:
break;
}
}
@Override
public void close() {
}
}

View File

@ -0,0 +1,34 @@
package coffee._finally.keycloak_metrics;
import org.keycloak.Config;
import org.keycloak.events.EventListenerProvider;
import org.keycloak.events.EventListenerProviderFactory;
import org.keycloak.models.KeycloakSession;
import org.keycloak.models.KeycloakSessionFactory;
public class MetricsEventListenerFactory implements EventListenerProviderFactory {
@Override
public EventListenerProvider create(KeycloakSession keycloakSession) {
return new MetricsEventListener();
}
@Override
public void init(Config.Scope scope) {
}
@Override
public void postInit(KeycloakSessionFactory keycloakSessionFactory) {
}
@Override
public void close() {
}
@Override
public String getId() {
return MetricsEventListener.ID;
}
}

View File

@ -0,0 +1 @@
coffee._finally.keycloak_metrics.MetricsEventListenerFactory

View File

@ -0,0 +1 @@
coffee._finally.keycloak_metrics.MetricsEndpointFactory