Created
January 29, 2015 10:15
-
-
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/
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@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); | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I used
env.jersey().register(new NewRelicTimedApplicationListener());