Skip to content

Add counter for inbound request #1295

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: 2.4
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions modules/org.restlet/pom.xml
Original file line number Diff line number Diff line change
@@ -13,5 +13,11 @@
<description>RESTful web framework for Java (API and Engine).</description>

<dependencies>
<!-- https://mvnrepository.com/artifact/io.dropwizard.metrics/metrics-core -->
<dependency>
<groupId>io.dropwizard.metrics</groupId>
<artifactId>metrics-core</artifactId>
<version>3.2.5</version>
</dependency>
</dependencies>
</project>
22 changes: 18 additions & 4 deletions modules/org.restlet/src/main/java/org/restlet/Application.java
Original file line number Diff line number Diff line change
@@ -47,6 +47,10 @@
import org.restlet.service.TunnelService;
import org.restlet.util.ServiceList;

import com.codahale.metrics.Counter;
import com.codahale.metrics.MetricRegistry;
import com.codahale.metrics.MetricSet;

/**
* Restlet managing a coherent set of resources and services. Applications are
* guaranteed to receive calls with their base reference set relatively to the
@@ -123,7 +127,7 @@ public static void setCurrent(Application application) {

/** The list of services. */
private final ServiceList services;

/**
* Constructor. Note this constructor is convenient because you don't have
* to provide a context like for {@link #Application(Context)}. Therefore
@@ -174,7 +178,7 @@ public Application(Context context) {

// [ifndef gae]
this.services.add(new org.restlet.service.TaskService(false));
// [enddef]
// [enddef]
}

/**
@@ -271,14 +275,22 @@ public Restlet getInboundRoot() {
if (this.inboundRoot == null) {
synchronized (this) {
if (this.inboundRoot == null) {
this.inboundRoot = createInboundRoot();
Restlet tempInboundRoot = createInboundRoot();
RequestCounterFilter requestCounterFilter = createRequestCounterFilter();
requestCounterFilter.setNext(tempInboundRoot);
this.inboundRoot = requestCounterFilter;
}
}
}

return this.inboundRoot;
}

private RequestCounterFilter createRequestCounterFilter() {
RequestCounterFilter requestCounterFilter = new RequestCounterFilter(getName());
return requestCounterFilter;
}

/**
* Returns the metadata service. The service is enabled by default.
*
@@ -483,7 +495,9 @@ public synchronized void setInboundRoot(
* The inbound root Restlet.
*/
public synchronized void setInboundRoot(Restlet inboundRoot) {
this.inboundRoot = inboundRoot;
RequestCounterFilter requestCounterFilter = createRequestCounterFilter();
requestCounterFilter.setNext(inboundRoot);
this.inboundRoot = requestCounterFilter;

if ((inboundRoot != null) && (inboundRoot.getContext() == null)) {
inboundRoot.setContext(getContext());
14 changes: 14 additions & 0 deletions modules/org.restlet/src/main/java/org/restlet/Metrics.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package org.restlet;

import com.codahale.metrics.JmxReporter;
import com.codahale.metrics.MetricRegistry;

public final class Metrics {

public static final MetricRegistry REGISTRY = new MetricRegistry();

static {
JmxReporter.forRegistry(REGISTRY).build().start();
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package org.restlet;

import org.restlet.routing.Filter;

import com.codahale.metrics.Counter;

public class RequestCounterFilter extends Filter{

private final Counter counter;

private final String name;

public RequestCounterFilter(String name) {

this.name = name;
this.counter = new Counter();

Metrics.REGISTRY.remove(name);
Metrics.REGISTRY.register(name, counter);
}

@Override
protected int beforeHandle(Request request, Response response) {
counter.inc();
return super.beforeHandle(request, response);
}

}