Skip to content

Commit

Permalink
#168 Completed first new metrics
Browse files Browse the repository at this point in the history
  • Loading branch information
svenkubiak committed May 30, 2016
1 parent b5d9b7b commit a836b2e
Show file tree
Hide file tree
Showing 8 changed files with 79 additions and 31 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,8 @@ private Response metrics() {
.andContent(VERSION, BootstrapUtils.getVersion())
.andContent(METRICS, metrics.getMetrics())
.andContent("totalRequests", totalRequests)
.andContent("minRequestTime", metrics.getMinRequestTime())
.andContent("maxRequestTime", metrics.getMaxRequestTime())
.andContent("errorRate", errorRate)
.andTemplate(Template.DEFAULT.metricsPath());
}
Expand Down
33 changes: 32 additions & 1 deletion mangooio-core/src/main/java/io/mangoo/models/Metrics.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.atomic.AtomicIntegerFieldUpdater;
import java.util.concurrent.atomic.LongAdder;

import com.google.inject.Singleton;
Expand All @@ -14,13 +15,43 @@
*/
@Singleton
public class Metrics {
private final AtomicIntegerFieldUpdater<Metrics> maxRequestTimeUpdater = AtomicIntegerFieldUpdater.newUpdater(Metrics.class, "maxRequestTime");
private final AtomicIntegerFieldUpdater<Metrics> minRequestTimeUpdater = AtomicIntegerFieldUpdater.newUpdater(Metrics.class, "minRequestTime");
private final Map<Integer, LongAdder> metricsCount = new ConcurrentHashMap<>(16, 0.9f, 1);

private volatile int maxRequestTime;
private volatile int minRequestTime = -1;

public void inc(int responseCode) {
this.metricsCount.computeIfAbsent(responseCode, t -> new LongAdder()).increment();
}

public void update(final int requestTime) {
int maxRequestTime;
do {
maxRequestTime = this.maxRequestTime;
if (requestTime < maxRequestTime) {
break;
}
} while (!this.maxRequestTimeUpdater.compareAndSet(this, maxRequestTime, requestTime));

int minRequestTime;
do {
minRequestTime = this.minRequestTime;
if (requestTime > minRequestTime && minRequestTime != -1) {
break;
}
} while (!this.minRequestTimeUpdater.compareAndSet(this, minRequestTime, requestTime));
}

public Map<Integer, LongAdder> getMetrics() {
return this.metricsCount;
}

public int getMaxRequestTime() {
return maxRequestTime;
}

public int getMinRequestTime() {
return minRequestTime;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -43,13 +43,11 @@ public class DispatcherHandler implements HttpHandler {
private final TemplateEngine templateEngine;
private final Messages messages;
private final Crypto crypto;
private final MetricsListener metricsListener;
private final Map<String, Class<?>> methodParameters;
private final Class<?> controllerClass;
private final String controllerClassName;
private final String controllerMethodName;
private final int methodParametersCount;
private final boolean metrics;
private final boolean async;
private final boolean hasRequestFilter;

Expand All @@ -59,7 +57,6 @@ public DispatcherHandler(Class<?> controllerClass, String controllerMethod, bool

this.templateEngine = internal ? Application.getInternalTemplateEngine() : Application.getInstance(TemplateEngine.class);
this.messages = Application.getInstance(Messages.class);
this.metricsListener = Application.getInstance(MetricsListener.class);
this.crypto = Application.getInstance(Crypto.class);
this.controllerClass = controllerClass;
this.controllerMethodName = controllerMethod;
Expand All @@ -68,7 +65,6 @@ public DispatcherHandler(Class<?> controllerClass, String controllerMethod, bool
this.methodParametersCount = this.methodParameters.size();
this.async = async;
this.hasRequestFilter = Application.getInjector().getAllBindings().containsKey(com.google.inject.Key.get(MangooRequestFilter.class));
this.metrics = CONFIG.isAdminEnabled();

try {
this.method = Application.getInstance(this.controllerClass)
Expand All @@ -86,8 +82,8 @@ public void handleRequest(HttpServerExchange exchange) throws Exception {
return;
}

if (this.metrics) {
exchange.addResponseCommitListener(this.metricsListener);
if (CONFIG.isAdminEnabled()) {
exchange.addExchangeCompleteListener(new MetricsListener(System.currentTimeMillis()));
}

final Attachment attachment = Attachment.build()
Expand Down
Original file line number Diff line number Diff line change
@@ -1,35 +1,34 @@
package io.mangoo.routing.listeners;

import java.util.Objects;
import java.util.Optional;

import com.google.inject.Inject;
import com.google.inject.Singleton;

import io.mangoo.core.Application;
import io.mangoo.models.Metrics;
import io.undertow.server.ExchangeCompletionListener;
import io.undertow.server.HttpServerExchange;
import io.undertow.server.ResponseCommitListener;

/**
* Listener that is invoked for counting metrics of HTTP response codes
*
* @author Kubiak
*
* @author svenkubiak
*
*/
@Singleton
public class MetricsListener implements ResponseCommitListener {
private final Metrics metrics;
public class MetricsListener implements ExchangeCompletionListener {
private long start;

@Inject
public MetricsListener(Metrics metrics) {
this.metrics = Objects.requireNonNull(metrics, "metrics can not be null");
public MetricsListener(long start) {
this.start = start;
}

@Override
public void beforeCommit(HttpServerExchange exchange) {
public void exchangeEvent(HttpServerExchange exchange, NextListener nextListener) {
Metrics metrics = Application.getInstance(Metrics.class);
metrics.update((int) (System.currentTimeMillis() - this.start));

String uri = Optional.ofNullable(exchange.getRequestURI()).orElse("").replace("/", "").toLowerCase();
if (!uri.contains("@admin")) {
this.metrics.inc(exchange.getStatusCode());
metrics.inc(exchange.getStatusCode());
}

nextListener.proceed();
}
}
2 changes: 1 addition & 1 deletion mangooio-core/src/main/resources/templates/admin/index.ftl
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
<table class="table table-hover">
<thead>
<tr>
<th data-sort="string"><b>Key</b></th>
<th data-sort="string"><b>Property</b></th>
<th data-sort="string"><b>Value</b></th>
</tr>
</thead>
Expand Down
24 changes: 21 additions & 3 deletions mangooio-core/src/main/resources/templates/admin/metrics.ftl
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,25 @@
<div class="small-box bg-red">
<div class="inner">
<h3>${errorRate} %</h3>
<p>Error rates</p>
<p>Error rate</p>
</div>
</div>
</div>
</div>
<div class="row">
<div class="col-lg-3 col-xs-6">
<div class="small-box bg-aqua">
<div class="inner">
<h3>${minRequestTime} ms</h3>
<p>Min request time</p>
</div>
</div>
</div>
<div class="col-lg-3 col-xs-6">
<div class="small-box bg-aqua">
<div class="inner">
<h3>${maxRequestTime} ms</h3>
<p>Max request time</p>
</div>
</div>
</div>
Expand All @@ -38,8 +56,8 @@
<table class="table table-hover">
<thead>
<tr>
<th data-sort="string"><b>Key</b></th>
<th data-sort="string"><b>Value</b></th>
<th data-sort="string"><b>HTTP status</b></th>
<th data-sort="string"><b>Count</b></th>
</tr>
</thead>
<tbody class="searchable">
Expand Down
7 changes: 5 additions & 2 deletions mangooio-core/src/main/resources/templates/admin/routes.ftl
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,11 @@
<table class="table table-hover">
<thead>
<tr>
<th data-sort="string"><b>Key</b></th>
<th data-sort="string"><b>Value</b></th>
<th data-sort="string"><b>Method</b></th>
<th data-sort="string"><b>URL</b></th>
<th data-sort="string"><b>Controller class</b></th>
<th data-sort="string"><b>Controller method</b></th>
<th data-sort="string"><b>Type</b></th>
</tr>
</thead>
<tbody class="searchable">
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@
&nbsp;&nbsp;
<a href="/@admin/scheduler/execute/${job.name}" class="simpletooltip">
<span class="simpletooltiptext">Execute job</span>
<i class="fa fa-random" aria-hidden="true"></i>
<i class="fa fa-fire" aria-hidden="true"></i>
</a>
</td>
</tr>
Expand All @@ -60,5 +60,4 @@
</div>
</div>
</div>

<#include "footer.ftl">

0 comments on commit a836b2e

Please sign in to comment.