Skip to content

Commit

Permalink
edeleon/nr 80764 (#1329)
Browse files Browse the repository at this point in the history
* added instrumentation for the HttpServlet class in servlet-2.4, servlet-5.0 and servlet-6.0

* added instrumentation for the HttpServlet class in servlet-5.0

* added instrumentation for the HttpServlet class in servlet-6.0

* updated build.gradle
  • Loading branch information
deleonenriqueta committed Jul 31, 2023
1 parent b028be1 commit 3bbdccc
Show file tree
Hide file tree
Showing 14 changed files with 1,572 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package javax.servlet.http;


import com.newrelic.api.agent.Trace;
import com.newrelic.api.agent.weaver.MatchType;
import com.newrelic.api.agent.weaver.Weave;

import javax.servlet.ServletException;
import java.io.IOException;


@Weave(type = MatchType.BaseClass, originalName = "javax.servlet.http.HttpServlet")
public abstract class HttpServlet_Instrumentation {


@Trace
protected abstract void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException;


@Trace
protected abstract void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException;


@Trace
protected abstract void doPut(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException;


@Trace
protected abstract void doDelete(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException;


}
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package jakarta.servlet.http;


import com.newrelic.api.agent.Trace;
import com.newrelic.api.agent.weaver.MatchType;
import com.newrelic.api.agent.weaver.Weave;
import jakarta.servlet.ServletException;


import java.io.IOException;


@Weave(type = MatchType.BaseClass, originalName = "jakarta.servlet.http.HttpServlet")
public abstract class HttpServlet_Instrumentation {


@Trace
protected abstract void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException;


@Trace
protected abstract void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException;


@Trace
protected abstract void doPut(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException;


@Trace
protected abstract void doDelete(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
package notjavax.servlet;

import com.newrelic.agent.introspec.*;
import org.junit.Test;
import org.junit.runner.RunWith;

import jakarta.servlet.ServletException;
import jakarta.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.lang.management.ManagementFactory;
import java.util.Collection;
import java.util.Map;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;

@RunWith(InstrumentationTestRunner.class)
@InstrumentationTestConfig(includePrefixes = "jakarta.servlet", configName = "debug.yml")
public class HttpServletInstrumentationTest {

@Test
public void doGetShouldAddMetricsForTransaction() throws InterruptedException, ServletException, IOException {

String currentMethod = "GET";
String transactionName = "WebTransaction/Uri/Unknown";

startTx(currentMethod);
Introspector introspector = InstrumentationTestRunner.getIntrospector();
Collection<TransactionTrace> traces = introspector.getTransactionTracesForTransaction(transactionName);
Map<String, TracedMetricData> metricsForTransaction = introspector.getMetricsForTransaction(transactionName);
String methodInTrace = traces.iterator().next().getInitialTraceSegment().getChildren().get(0).getMethodName();

assertEquals(1, traces.size());
assertEquals(2, metricsForTransaction.size());
assertEquals("doGet", methodInTrace);
assertTrue(metricsForTransaction.containsKey("Java/notjavax.servlet.MyServlet/doGet"));
}

@Test
public void doPostShouldAddMetricsForTransaction() throws InterruptedException, ServletException, IOException {

String currentMethod = "POST";
String transactionName = "WebTransaction/Uri/Unknown";

startTx(currentMethod);
Introspector introspector = InstrumentationTestRunner.getIntrospector();
Collection<TransactionTrace> traces = introspector.getTransactionTracesForTransaction(transactionName);
Map<String, TracedMetricData> metricsForTransaction = introspector.getMetricsForTransaction(transactionName);
String methodInTrace = traces.iterator().next().getInitialTraceSegment().getChildren().get(0).getMethodName();

assertEquals(1, traces.size());
assertEquals(2, metricsForTransaction.size());
assertEquals("doPost", methodInTrace);
assertTrue(metricsForTransaction.containsKey("Java/notjavax.servlet.MyServlet/doPost"));
}

@Test
public void doPutShouldAddMetricsForTransaction() throws InterruptedException, ServletException, IOException {

String currentMethod = "PUT";
String transactionName = "WebTransaction/Uri/Unknown";

startTx(currentMethod);
Introspector introspector = InstrumentationTestRunner.getIntrospector();
Collection<TransactionTrace> traces = introspector.getTransactionTracesForTransaction(transactionName);
Map<String, TracedMetricData> metricsForTransaction = introspector.getMetricsForTransaction(transactionName);
String methodInTrace = traces.iterator().next().getInitialTraceSegment().getChildren().get(0).getMethodName();

assertEquals(1, traces.size());
assertEquals(2, metricsForTransaction.size());
assertEquals("doPut", methodInTrace);
assertTrue(metricsForTransaction.containsKey("Java/notjavax.servlet.MyServlet/doPut"));
}

@Test
public void doDeleteShouldAddMetricsForTransaction() throws InterruptedException, ServletException, IOException {

String currentMethod = "DELETE";
String transactionName = "WebTransaction/Uri/Unknown";

startTx(currentMethod);
Introspector introspector = InstrumentationTestRunner.getIntrospector();
Collection<TransactionTrace> traces = introspector.getTransactionTracesForTransaction(transactionName);
Map<String, TracedMetricData> metricsForTransaction = introspector.getMetricsForTransaction(transactionName);
String methodInTrace = traces.iterator().next().getInitialTraceSegment().getChildren().get(0).getMethodName();

assertEquals(1, traces.size());
assertEquals(2, metricsForTransaction.size());
assertEquals("doDelete", methodInTrace);
assertTrue(metricsForTransaction.containsKey("Java/notjavax.servlet.MyServlet/doDelete"));
}

private void startTx(String currentMethod) throws ServletException, IOException {

MyServlet myServlet = new MyServlet();
HttpServletTestRequest request = new HttpServletTestRequest();
HttpServletTestResponse response = new HttpServletTestResponse();

request.setCurrentMethod(currentMethod);
myServlet.service(request, response);
}
}
Loading

0 comments on commit 3bbdccc

Please sign in to comment.