Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Dropwizard 0.8.x NewRelic Integration inspired by http://kyleboon.org/blog/2013/09/23/newrelic-for-dropwizard/
@Singleton
@javax.ws.rs.ext.Provider
public class NewRelicTimedApplicationListener implements ApplicationEventListener {
private Map<Method,String> methodMap = new HashMap<>();
@Override
public void onEvent(ApplicationEvent event) {
if (event.getType() == ApplicationEvent.Type.INITIALIZATION_APP_FINISHED) {
for (Resource resource : event.getResourceModel().getResources()) {
resource.getAllMethods().forEach(this::registerUnitOfWorkAnnotations);
for (Resource childResource : resource.getChildResources()) {
childResource.getAllMethods().forEach(this::registerUnitOfWorkAnnotations);
}
}
}
}
@Override
public RequestEventListener onRequest(RequestEvent requestEvent) {
return new NewRelicTimedEventListener(methodMap);
}
private void registerUnitOfWorkAnnotations (ResourceMethod resourceMethod) {
final Method method = resourceMethod.getInvocable().getDefinitionMethod();
Timed annotation = method.getAnnotation(Timed.class);
if (annotation != null) {
final String resourceName = method.getDeclaringClass().getSimpleName();
final String methodName = method.getName();
this.methodMap.put(method, resourceName + "/" + methodName);
}
}
}
class NewRelicTimedEventListener implements RequestEventListener {
private final Map<Method, String> methodMap;
public NewRelicTimedEventListener(Map<Method, String> methodMap) {
this.methodMap = methodMap;
}
@Override
public void onEvent(RequestEvent event) {
if (event.getType() == RequestEvent.Type.RESOURCE_METHOD_START) {
Method method = event.getUriInfo().getMatchedResourceMethod().getInvocable().getDefinitionMethod();
NewRelic.setTransactionName("dropwizard", methodMap.get(method));
}
}
public Map<Method, String> getMethodMap() {
return methodMap;
}
}
@ajmath

This comment has been minimized.

Copy link

commented Oct 28, 2015

How do you register the listener with jersey/dropwizard?

@bluesliverx

This comment has been minimized.

Copy link

commented Sep 6, 2017

I used
env.jersey().register(new NewRelicTimedApplicationListener());

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.