The New Relic Java agent API allows you to set up custom error grouping using a custom callback implementation in your Java application. This document shows an example of using the custom error grouping with a callback implementation in sample application.
중요
For best results when using the API, ensure you have the latest Java agent release.
Example: Implementing a custom error grouping callback
Below is an example of a sample application using the Java agent API to implement custom error grouping with a callback and registering the callback.
팁
If you copy and paste example code, be sure to use appropriate spacing on your command lines.
package test;import java.io.IOException;import javax.servlet.ServletContextEvent;import javax.servlet.ServletContextListener;import javax.servlet.ServletException;import javax.servlet.http.HttpServlet;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;
import com.newrelic.api.agent.ErrorGroupCallback;import com.newrelic.api.agent.NewRelic;
public class MyServletContextListener implements ServletContextListener {
@Overridepublic void contextDestroyed(ServletContextEvent arg0) {// Do any cleanup if needed }
@Overridepublic void contextInitialized(ServletContextEvent arg0) {// Register the error grouping callback on application startupNewRelic.setErrorGroupCallback(new MyErrorGrouper()); }}
public class MyErrorGrouper implements ErrorGroupCallback {
public String generateGroupingString(ErrorData errorData) {String clazz = errorData.getErrorClass();String txnName = errorData.getTransactionName();
return (clazz.isEmpty() || txnName.isEmpty()) ? null : clazz + "_" + txnName; }}
public class TestServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
protected void processRequest(HttpServletRequest req, HttpServletResponse resp)throws ServletException, IOException {// Simulate an exception throw new ServletException("Simulated ServletException"); }
@Overrideprotected void doGet(HttpServletRequest req, HttpServletResponse resp)throws ServletException, IOException { processRequest(req, resp); }
@Overrideprotected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { processRequest(req, resp); }}```