Skip to content

Commit

Permalink
Add counter for session bind for issue #395
Browse files Browse the repository at this point in the history
  • Loading branch information
PhantomYdn committed Mar 16, 2020
1 parent 5c058ad commit 5b97b5c
Showing 1 changed file with 28 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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")
Expand All @@ -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;
}
}


}

0 comments on commit 5b97b5c

Please sign in to comment.