@Timed annotation is not working for filters in dropwizard.
hrishikesh-kumar opened this issue · 3 comments
@timed annotation is not working for filters( ContainerRequestFilter ) in dropwizard.
Example code:
import com.codahale.metrics.annotation.Timed;
import lombok.extern.slf4j.Slf4j;
import javax.ws.rs.container.ContainerRequestContext;
import javax.ws.rs.container.ContainerRequestFilter;
import javax.ws.rs.ext.Provider;
import java.io.IOException;
@Provider
@XFilter
@Slf4j
public class TestFilter implements ContainerRequestFilter {
@Timed
@Override
public void filter(ContainerRequestContext containerRequestContext) throws IOException {
//Do something
}
}
If Guice is not the one creating the Filter instance, it cannot use its AOP to do the necessary invocation wrapping.
It looks like you're trying to measure JAX-RS invocations. Try https://bitbucket.org/marshallpierce/jersey2-metrics for that.
Its a resource filter, in which I'm filtering out some requests before actual resource method invocation.
http://www.dropwizard.io/0.8.2/docs/manual/core.html#jersey-filters
For time being, I did this way.
import com.codahale.metrics.MetricRegistry;
import com.codahale.metrics.Timer;
import com.google.inject.Inject;
import lombok.extern.slf4j.Slf4j;
import javax.ws.rs.container.ContainerRequestContext;
import javax.ws.rs.container.ContainerRequestFilter;
import javax.ws.rs.ext.Provider;
import java.io.IOException;
@Provider
@XEvent
@Slf4j
public class TestFilter implements ContainerRequestFilter {
private final MetricRegistry metricRegistry;
private static final String METRICS_NAME = "testMetricsName";
@Inject
public TestFilter(MetricRegistry metricRegistry) {
this.metricRegistry = metricRegistry;
}
@Override
public void filter(ContainerRequestContext containerRequestContext) throws IOException {
try(Timer.Context context = metricRegistry.timer(MetricRegistry.name(getClass(), METRICS_NAME)).time()) {
doSomething(containerRequestContext);
}
}
private void doSomething(ContainerRequestContext containerRequestContext) {
// Do some validation
//
}
}
See this part of the readme:
Since this uses Guice AOP, instances must be created by Guice; see the Guice wiki. This means that using a Provider where you create the instance won't work, or binding a singleton to an instance, etc.