-
Notifications
You must be signed in to change notification settings - Fork 79
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add request level metrics for issue #395
- Loading branch information
1 parent
81b90fe
commit 5c058ad
Showing
9 changed files
with
192 additions
and
2 deletions.
There are no files selected for viewing
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
Binary file not shown.
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
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
77 changes: 77 additions & 0 deletions
77
orienteer-metrics/src/main/java/org/orienteer/metrics/OMetricSessionListener.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,77 @@ | ||
package org.orienteer.metrics; | ||
|
||
import java.util.HashMap; | ||
import java.util.Map; | ||
|
||
import org.apache.wicket.ISessionListener; | ||
import org.apache.wicket.Session; | ||
import org.apache.wicket.request.http.WebRequest; | ||
import org.orienteer.core.OrienteerWebApplication; | ||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
|
||
import io.prometheus.client.CollectorRegistry; | ||
import io.prometheus.client.Counter; | ||
import io.prometheus.client.Gauge; | ||
import io.prometheus.client.Histogram; | ||
|
||
/** | ||
* {@link ISessionListener} to monitor creation and unbounding of sessions | ||
*/ | ||
public class OMetricSessionListener implements ISessionListener { | ||
|
||
private static final Logger LOG = LoggerFactory.getLogger(OMetricSessionListener.class); | ||
|
||
private static OMetricSessionListener listener; | ||
|
||
|
||
private static final Counter GAUGE_CREATED_SESSIONS = Counter.build() | ||
.namespace("wicket") | ||
.subsystem("session") | ||
.name("created") | ||
.help("Sessions created") | ||
.create(); | ||
|
||
private static final Counter GAUGE_UNBOUND_SESSIONS = Counter.build() | ||
.namespace("wicket") | ||
.subsystem("session") | ||
.name("unbound") | ||
.help("Sessions unbound") | ||
.create(); | ||
|
||
|
||
private OMetricSessionListener() { | ||
CollectorRegistry.defaultRegistry.register(GAUGE_CREATED_SESSIONS); | ||
CollectorRegistry.defaultRegistry.register(GAUGE_UNBOUND_SESSIONS); | ||
} | ||
|
||
@Override | ||
public void onCreated(Session session) { | ||
GAUGE_CREATED_SESSIONS.inc(); | ||
} | ||
|
||
@Override | ||
public void onUnbound(String sessionId) { | ||
GAUGE_UNBOUND_SESSIONS.inc(); | ||
} | ||
|
||
protected void onDestroy() { | ||
CollectorRegistry.defaultRegistry.unregister(GAUGE_CREATED_SESSIONS); | ||
CollectorRegistry.defaultRegistry.unregister(GAUGE_UNBOUND_SESSIONS); | ||
} | ||
|
||
public static synchronized void install(OrienteerWebApplication app) { | ||
if(listener!=null) deinstall(app); | ||
listener = new OMetricSessionListener(); | ||
app.getSessionListeners().add(listener); | ||
} | ||
|
||
public static synchronized void deinstall(OrienteerWebApplication app) { | ||
if(listener!=null) { | ||
listener.onDestroy(); | ||
app.getSessionListeners().remove(listener); | ||
listener = null; | ||
} | ||
} | ||
|
||
} |
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
106 changes: 106 additions & 0 deletions
106
orienteer-metrics/src/main/java/org/orienteer/metrics/OMetricsRequestCycleListener.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,106 @@ | ||
package org.orienteer.metrics; | ||
|
||
|
||
import org.apache.wicket.MetaDataKey; | ||
import org.apache.wicket.request.IRequestHandler; | ||
import org.apache.wicket.request.cycle.IRequestCycleListener; | ||
import org.apache.wicket.request.cycle.RequestCycle; | ||
import org.apache.wicket.request.http.WebRequest; | ||
import org.apache.wicket.request.http.WebResponse; | ||
import org.orienteer.core.OrienteerWebApplication; | ||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
import io.prometheus.client.CollectorRegistry; | ||
import io.prometheus.client.Counter; | ||
import io.prometheus.client.Histogram; | ||
|
||
/** | ||
* {@link IRequestCycleListener} to monitor requests | ||
*/ | ||
public class OMetricsRequestCycleListener implements IRequestCycleListener { | ||
|
||
private static final Logger LOG = LoggerFactory.getLogger(OMetricsRequestCycleListener.class); | ||
|
||
private static OMetricsRequestCycleListener listener; | ||
|
||
|
||
private static final MetaDataKey<Histogram.Timer> REQUESTS_HISTOGRAM_KEY = new MetaDataKey<Histogram.Timer>() {}; | ||
|
||
private static final Counter COUNTER_EXCEPTIONS = Counter.build() | ||
.namespace("wicket") | ||
.name("exceptions_count") | ||
.help("Total number of exceptions") | ||
.labelNames("ajax") | ||
.create(); | ||
|
||
private static final Counter COUNTER_EXECUTIONS = Counter.build() | ||
.namespace("wicket") | ||
.name("executions") | ||
.help("Total number request handlers executions") | ||
.labelNames("ajax", "handler") | ||
.create(); | ||
|
||
private static final Histogram HISTOGRAM_REQUESTS = Histogram.build() | ||
.namespace("wicket") | ||
.name("requests") | ||
.help("Request times and counts histogram") | ||
.labelNames("ajax") | ||
.create(); | ||
|
||
private OMetricsRequestCycleListener() { | ||
CollectorRegistry.defaultRegistry.register(COUNTER_EXCEPTIONS); | ||
//Just to init by 0 sub counters | ||
COUNTER_EXCEPTIONS.labels(Boolean.TRUE.toString()).inc(0); | ||
COUNTER_EXCEPTIONS.labels(Boolean.FALSE.toString()).inc(0); | ||
CollectorRegistry.defaultRegistry.register(COUNTER_EXECUTIONS); | ||
CollectorRegistry.defaultRegistry.register(HISTOGRAM_REQUESTS); | ||
} | ||
|
||
@Override | ||
public void onBeginRequest(RequestCycle cycle) { | ||
Histogram.Timer requestTimer = HISTOGRAM_REQUESTS | ||
.labels(Boolean.toString(((WebRequest)cycle.getRequest()).isAjax())) | ||
.startTimer(); | ||
cycle.setMetaData(REQUESTS_HISTOGRAM_KEY, requestTimer); | ||
} | ||
|
||
@Override | ||
public void onEndRequest(RequestCycle cycle) { | ||
Histogram.Timer requestTimer = cycle.getMetaData(REQUESTS_HISTOGRAM_KEY); | ||
requestTimer.observeDuration(); | ||
} | ||
|
||
@Override | ||
public IRequestHandler onException(RequestCycle cycle, Exception ex) { | ||
COUNTER_EXCEPTIONS.labels(Boolean.toString(((WebRequest)cycle.getRequest()).isAjax())).inc(); | ||
return null; | ||
} | ||
|
||
@Override | ||
public void onRequestHandlerExecuted(RequestCycle cycle, IRequestHandler handler) { | ||
COUNTER_EXECUTIONS.labels(Boolean.toString(((WebRequest)cycle.getRequest()).isAjax()), | ||
handler.getClass().getSimpleName()).inc(); | ||
} | ||
|
||
protected void onDestroy() { | ||
CollectorRegistry.defaultRegistry.unregister(COUNTER_EXECUTIONS); | ||
CollectorRegistry.defaultRegistry.unregister(COUNTER_EXCEPTIONS); | ||
CollectorRegistry.defaultRegistry.unregister(HISTOGRAM_REQUESTS); | ||
} | ||
|
||
|
||
public static synchronized void install(OrienteerWebApplication app) { | ||
if(listener!=null) deinstall(app); | ||
listener = new OMetricsRequestCycleListener(); | ||
app.getRequestCycleListeners().add(listener); | ||
} | ||
|
||
public static synchronized void deinstall(OrienteerWebApplication app) { | ||
if(listener!=null) { | ||
listener.onDestroy(); | ||
app.getRequestCycleListeners().remove(listener); | ||
listener = null; | ||
} | ||
} | ||
|
||
} |
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