From 5b97b5c96d031cf85cb33e8c5c2206a1702f9548 Mon Sep 17 00:00:00 2001 From: Ilia Naryzhny Date: Sun, 15 Mar 2020 20:01:16 -0700 Subject: [PATCH] Add counter for session bind for issue #395 --- .../metrics/OMetricSessionListener.java | 37 ++++++++++++++----- 1 file changed, 28 insertions(+), 9 deletions(-) diff --git a/orienteer-metrics/src/main/java/org/orienteer/metrics/OMetricSessionListener.java b/orienteer-metrics/src/main/java/org/orienteer/metrics/OMetricSessionListener.java index 9adc640ad..a727c3f3e 100644 --- a/orienteer-metrics/src/main/java/org/orienteer/metrics/OMetricSessionListener.java +++ b/orienteer-metrics/src/main/java/org/orienteer/metrics/OMetricSessionListener.java @@ -5,7 +5,9 @@ import org.apache.wicket.ISessionListener; import org.apache.wicket.Session; +import org.apache.wicket.request.Request; import org.apache.wicket.request.http.WebRequest; +import org.apache.wicket.session.ISessionStore; import org.orienteer.core.OrienteerWebApplication; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -18,21 +20,28 @@ /** * {@link ISessionListener} to monitor creation and unbounding of sessions */ -public class OMetricSessionListener implements ISessionListener { +public class OMetricSessionListener implements ISessionListener, ISessionStore.BindListener { private static final Logger LOG = LoggerFactory.getLogger(OMetricSessionListener.class); private static OMetricSessionListener listener; - private static final Counter GAUGE_CREATED_SESSIONS = Counter.build() + private static final Counter COUNTER_CREATED_SESSIONS = Counter.build() .namespace("wicket") .subsystem("session") .name("created") .help("Sessions created") .create(); - private static final Counter GAUGE_UNBOUND_SESSIONS = Counter.build() + private static final Counter COUNTER_BOUND_SESSIONS = Counter.build() + .namespace("wicket") + .subsystem("session") + .name("bound") + .help("Sessions bound") + .create(); + + private static final Counter COUNTER_UNBOUND_SESSIONS = Counter.build() .namespace("wicket") .subsystem("session") .name("unbound") @@ -41,37 +50,47 @@ public class OMetricSessionListener implements ISessionListener { private OMetricSessionListener() { - CollectorRegistry.defaultRegistry.register(GAUGE_CREATED_SESSIONS); - CollectorRegistry.defaultRegistry.register(GAUGE_UNBOUND_SESSIONS); + CollectorRegistry.defaultRegistry.register(COUNTER_CREATED_SESSIONS); + CollectorRegistry.defaultRegistry.register(COUNTER_BOUND_SESSIONS); + CollectorRegistry.defaultRegistry.register(COUNTER_UNBOUND_SESSIONS); } @Override public void onCreated(Session session) { - GAUGE_CREATED_SESSIONS.inc(); + COUNTER_CREATED_SESSIONS.inc(); + } + + @Override + public void bindingSession(Request request, Session newSession) { + COUNTER_BOUND_SESSIONS.inc(); } @Override public void onUnbound(String sessionId) { - GAUGE_UNBOUND_SESSIONS.inc(); + COUNTER_UNBOUND_SESSIONS.inc(); } protected void onDestroy() { - CollectorRegistry.defaultRegistry.unregister(GAUGE_CREATED_SESSIONS); - CollectorRegistry.defaultRegistry.unregister(GAUGE_UNBOUND_SESSIONS); + CollectorRegistry.defaultRegistry.unregister(COUNTER_CREATED_SESSIONS); + CollectorRegistry.defaultRegistry.unregister(COUNTER_BOUND_SESSIONS); + CollectorRegistry.defaultRegistry.unregister(COUNTER_UNBOUND_SESSIONS); } public static synchronized void install(OrienteerWebApplication app) { if(listener!=null) deinstall(app); listener = new OMetricSessionListener(); app.getSessionListeners().add(listener); + app.getSessionStore().registerBindListener(listener); } public static synchronized void deinstall(OrienteerWebApplication app) { if(listener!=null) { listener.onDestroy(); + app.getSessionStore().unregisterBindListener(listener); app.getSessionListeners().remove(listener); listener = null; } } + }