-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1 from rtufisi/test-default-events-store
Test default events metrics handling
- Loading branch information
Showing
14 changed files
with
233 additions
and
492 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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
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,20 @@ | ||
version: '3' | ||
|
||
services: | ||
keycloak: | ||
image: quay.io/keycloak/keycloak:25.0.0 | ||
environment: | ||
KEYCLOAK_ADMIN: admin | ||
KEYCLOAK_ADMIN_PASSWORD: admin | ||
KC_HTTP_RELATIVE_PATH: /auth | ||
KC_HEALTH_ENABLED: 'true' | ||
KC_METRICS_ENABLED: 'true' | ||
KC_COMMUNITY_EVENTS_METRICS_ENABLED: 'true' | ||
JAVA_OPTS: '-agentlib:jdwp=transport=dt_socket,address=*:8787,server=y,suspend=n' | ||
ports: | ||
- 8080:8080 | ||
- 8787:8787 | ||
- 9000:9000 | ||
volumes: | ||
- ./target/keycloak-event-metrics-1.0.1-SNAPSHOT.jar:/opt/keycloak/providers/keycloak-event-metrics.jar | ||
command: [ "start-dev" ] |
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
21 changes: 21 additions & 0 deletions
21
src/main/java/io/kokuwa/keycloak/metrics/CommunityProfiles.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,21 @@ | ||
package io.kokuwa.keycloak.metrics; | ||
|
||
public class CommunityProfiles { | ||
private static final String ENV_EVENTS_METRICS_ENABLED = "KC_COMMUNITY_EVENTS_METRICS_ENABLED"; | ||
private static final String PROP_EVENTS_METRICS_ENABLED = "kc.community.events.metrics.enabled"; | ||
|
||
private static final boolean isEventsMetricsEnabled; | ||
|
||
static { | ||
isEventsMetricsEnabled = | ||
Boolean.parseBoolean(System.getenv(ENV_EVENTS_METRICS_ENABLED)) | ||
|| Boolean.parseBoolean(System.getProperty(PROP_EVENTS_METRICS_ENABLED)); | ||
} | ||
|
||
private CommunityProfiles() { | ||
} | ||
|
||
public static boolean isEventsMetricsEnabled() { | ||
return isEventsMetricsEnabled; | ||
} | ||
} |
71 changes: 0 additions & 71 deletions
71
src/main/java/io/kokuwa/keycloak/metrics/event/MetricsEventListener.java
This file was deleted.
Oops, something went wrong.
41 changes: 0 additions & 41 deletions
41
src/main/java/io/kokuwa/keycloak/metrics/event/MetricsEventListenerFactory.java
This file was deleted.
Oops, something went wrong.
122 changes: 122 additions & 0 deletions
122
src/main/java/io/kokuwa/keycloak/metrics/store/MicrometerEventStoreProvider.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,122 @@ | ||
package io.kokuwa.keycloak.metrics.store; | ||
|
||
import io.micrometer.core.instrument.Metrics; | ||
import jakarta.persistence.EntityManager; | ||
import org.jboss.logging.Logger; | ||
import org.keycloak.events.Event; | ||
import org.keycloak.events.admin.AdminEvent; | ||
import org.keycloak.events.jpa.JpaEventStoreProvider; | ||
import org.keycloak.models.AbstractKeycloakTransaction; | ||
import org.keycloak.models.KeycloakContext; | ||
import org.keycloak.models.KeycloakSession; | ||
import org.keycloak.models.RealmModel; | ||
import org.keycloak.models.utils.KeycloakModelUtils; | ||
|
||
import java.util.Optional; | ||
|
||
public class MicrometerEventStoreProvider extends JpaEventStoreProvider { | ||
private static final Logger log = Logger.getLogger(MicrometerEventStoreProvider.class); | ||
|
||
private final boolean replaceIds; | ||
private final KeycloakSession session; | ||
private final boolean isEventsMetricsEnabled; | ||
|
||
public MicrometerEventStoreProvider(KeycloakSession session, | ||
EntityManager em, | ||
boolean replaceIds, | ||
boolean isEventsMetricsEnabled) { | ||
super(session, em); | ||
this.replaceIds = replaceIds; | ||
this.session = session; | ||
this.isEventsMetricsEnabled = isEventsMetricsEnabled; | ||
} | ||
|
||
@Override | ||
public void onEvent(Event event) { | ||
super.onEvent(event); | ||
if (isEventsMetricsEnabled) { | ||
log.info("User Event registered "); | ||
countUserEvent(event); | ||
} | ||
} | ||
|
||
@Override | ||
public void onEvent(AdminEvent event, boolean includeRepresentation) { | ||
super.onEvent(event, includeRepresentation); | ||
if (isEventsMetricsEnabled) { | ||
countAdminEvent(event); | ||
} | ||
} | ||
|
||
@Override | ||
public void close() { | ||
} | ||
|
||
private String getRealmName(String id) { | ||
return Optional.ofNullable(session.getContext()).map(KeycloakContext::getRealm) | ||
.filter(realm -> id == null || id.equals(realm.getId())) | ||
.or(() -> { | ||
log.tracev("Context realm was empty with id {0}", id); | ||
return Optional.ofNullable(id).map(session.realms()::getRealm); | ||
}) | ||
.map(RealmModel::getName) | ||
.orElseGet(() -> { | ||
log.warnv("Failed to find realm with id {0}", id); | ||
return id; | ||
}); | ||
} | ||
|
||
private String toBlank(Object value) { | ||
return value == null ? "" : value.toString(); | ||
} | ||
|
||
void countUserEvent(Event event) { | ||
session | ||
.getTransactionManager() | ||
.enlistAfterCompletion( | ||
new AbstractKeycloakTransaction() { | ||
@Override | ||
protected void commitImpl() { | ||
KeycloakModelUtils.runJobInTransaction( | ||
session.getKeycloakSessionFactory(), | ||
(s) -> { | ||
Metrics.counter("keycloak_event_user", | ||
"realm", toBlank(replaceIds ? getRealmName(event.getRealmId()) : event.getRealmId()), | ||
"type", toBlank(event.getType()), | ||
"client", toBlank(event.getClientId()), | ||
"error", toBlank(event.getError())) | ||
.increment(); | ||
}); | ||
} | ||
|
||
@Override | ||
protected void rollbackImpl() { | ||
} | ||
}); | ||
} | ||
|
||
void countAdminEvent(AdminEvent event) { | ||
session | ||
.getTransactionManager() | ||
.enlistAfterCompletion( | ||
new AbstractKeycloakTransaction() { | ||
@Override | ||
protected void commitImpl() { | ||
KeycloakModelUtils.runJobInTransaction( | ||
session.getKeycloakSessionFactory(), | ||
(s) -> { | ||
Metrics.counter("keycloak_event_admin", | ||
"realm", toBlank(replaceIds ? getRealmName(event.getRealmId()) : event.getRealmId()), | ||
"resource", toBlank(event.getResourceType()), | ||
"operation", toBlank(event.getOperationType()), | ||
"error", toBlank(event.getError())) | ||
.increment(); | ||
}); | ||
} | ||
|
||
@Override | ||
protected void rollbackImpl() { | ||
} | ||
}); | ||
} | ||
} |
36 changes: 36 additions & 0 deletions
36
src/main/java/io/kokuwa/keycloak/metrics/store/MicrometerEventStoreProviderFactory.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 io.kokuwa.keycloak.metrics.store; | ||
|
||
import io.kokuwa.keycloak.metrics.CommunityProfiles; | ||
import org.jboss.logging.Logger; | ||
import org.keycloak.Config; | ||
import org.keycloak.connections.jpa.JpaConnectionProvider; | ||
import org.keycloak.events.EventStoreProvider; | ||
import org.keycloak.events.jpa.JpaEventStoreProviderFactory; | ||
import org.keycloak.models.KeycloakSession; | ||
|
||
public class MicrometerEventStoreProviderFactory extends JpaEventStoreProviderFactory { | ||
private static final Logger log = Logger.getLogger(MicrometerEventStoreProviderFactory.class); | ||
|
||
public static final String ID = "jpa"; // Override default event store provider | ||
private boolean replaceIds; | ||
private boolean isEventsMetricsEnabled; | ||
|
||
@Override | ||
public EventStoreProvider create(KeycloakSession session) { | ||
JpaConnectionProvider connection = session.getProvider(JpaConnectionProvider.class); | ||
return new MicrometerEventStoreProvider(session, connection.getEntityManager(), replaceIds, isEventsMetricsEnabled); | ||
} | ||
|
||
public void init(Config.Scope config) { | ||
replaceIds = "true".equals(System.getenv().getOrDefault("KC_METRICS_EVENT_REPLACE_IDS", "true")); | ||
log.info(replaceIds ? "Configured with model names." : "Configured with model ids."); | ||
|
||
isEventsMetricsEnabled = CommunityProfiles.isEventsMetricsEnabled(); | ||
log.info("Admin event metrics enabled: " + isEventsMetricsEnabled); | ||
} | ||
|
||
@Override | ||
public String getId() { | ||
return ID; | ||
} | ||
} |
1 change: 0 additions & 1 deletion
1
src/main/resources/META-INF/services/org.keycloak.events.EventListenerProviderFactory
This file was deleted.
Oops, something went wrong.
18 changes: 18 additions & 0 deletions
18
src/main/resources/META-INF/services/org.keycloak.events.EventStoreProviderFactory
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,18 @@ | ||
# | ||
# Copyright 2016 Red Hat, Inc. and/or its affiliates | ||
# and other contributors as indicated by the @author tags. | ||
# | ||
# Licensed under the Apache License, Version 2.0 (the "License"); | ||
# you may not use this file except in compliance with the License. | ||
# You may obtain a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
# | ||
|
||
io.kokuwa.keycloak.metrics.store.MicrometerEventStoreProviderFactory |
Oops, something went wrong.