Skip to content

Instantly share code, notes, and snippets.

@mrserverless
Created January 29, 2015 10:15
Show Gist options
  • Save mrserverless/8d65f88a1cb8fa2db46e to your computer and use it in GitHub Desktop.
Save mrserverless/8d65f88a1cb8fa2db46e to your computer and use it in GitHub Desktop.
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;
}
}
@bluesliverx
Copy link

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