Search in sources :

Example 6 with Dispatcher

use of com.newrelic.agent.dispatchers.Dispatcher in project newrelic-java-agent by newrelic.

the class TransactionInboundHeadersTest method TestNullRequestRequestHeaders.

@Test
public void TestNullRequestRequestHeaders() {
    Transaction tx = Transaction.getTransaction();
    Dispatcher dispatcher = Mockito.mock(Dispatcher.class);
    Mockito.when(dispatcher.getRequest()).thenReturn(null);
    tx.setDispatcher(dispatcher);
    InboundHeaderState inboundHeaderState = tx.getInboundHeaderState();
    Assert.assertTrue(inboundHeaderState != null);
    Assert.assertNull(inboundHeaderState.getClientCrossProcessId());
    Assert.assertNull(inboundHeaderState.getUnparsedSyntheticsHeader());
    Assert.assertNull(inboundHeaderState.getReferrerGuid());
    InboundHeaders requestHeaders = Transaction.getRequestHeaders(tx);
    Assert.assertNull(requestHeaders);
}
Also used : InboundHeaders(com.newrelic.api.agent.InboundHeaders) Dispatcher(com.newrelic.agent.dispatchers.Dispatcher) Test(org.junit.Test)

Example 7 with Dispatcher

use of com.newrelic.agent.dispatchers.Dispatcher in project newrelic-java-agent by newrelic.

the class TransactionDataTestBuilder method build.

public TransactionData build() {
    when(tx.getRootTracer()).thenReturn(tracer);
    if (synJobId == null || synMonitorId == null || synResourceId == null) {
        when(tx.isSynthetic()).thenReturn(false);
    } else {
        when(tx.isSynthetic()).thenReturn(true);
    }
    when(tx.getGuid()).thenReturn("guid");
    Dispatcher mockDispatcher = mock(Dispatcher.class);
    when(mockDispatcher.getUri()).thenReturn(requestUri);
    when(mockDispatcher.isWebTransaction()).thenReturn(dispatcher == null ? true : dispatcher.isWebTransaction());
    when(tx.getDispatcher()).thenReturn(mockDispatcher);
    if (throwable != null) {
        when(tx.getThrowable()).thenReturn(new TransactionThrowable(throwable, expectedError, null));
    }
    PriorityTransactionName priorityTransactionName = mock(PriorityTransactionName.class);
    when(priorityTransactionName.getName()).thenReturn(frontendMetricName);
    when(tx.getPriorityTransactionName()).thenReturn(priorityTransactionName);
    Map<String, Map<String, String>> prefixed = new HashMap<>();
    prefixed.put("request.parameters.", requestParams);
    when(tx.getPrefixedAgentAttributes()).thenReturn(prefixed);
    when(tx.getUserAttributes()).thenReturn(userParams);
    when(tx.getAgentAttributes()).thenReturn(agentParams);
    when(tx.getErrorAttributes()).thenReturn(errorParams);
    when(tx.getIntrinsicAttributes()).thenReturn(intrinsics);
    when(tx.isIgnore()).thenReturn(false);
    when(tx.getStatus()).thenReturn(responseStatus);
    when(tx.getStatusMessage()).thenReturn(statusMessage);
    when(tx.isErrorReportableAndNotIgnored()).thenReturn(true);
    when(tx.getSpanProxy()).thenReturn(new SpanProxy());
    ErrorServiceImpl errorService = mock(ErrorServiceImpl.class);
    IRPMService rpmService = mock(IRPMService.class);
    when(rpmService.getApplicationName()).thenReturn(appName);
    when(rpmService.getErrorService()).thenReturn(errorService);
    when(tx.getRPMService()).thenReturn(rpmService);
    when(tx.getAgentConfig()).thenReturn(agentConfig);
    when(tx.getWallClockStartTimeMs()).thenReturn(startTime);
    if (slowQueryListener != null) {
        when(tx.getSlowQueryListener(anyBoolean())).thenReturn(slowQueryListener);
    }
    when(tx.getTracers()).thenReturn(tracers);
    CrossProcessTransactionState crossProcessTransactionState = mock(CrossProcessTransactionState.class);
    when(tx.getCrossProcessTransactionState()).thenReturn(crossProcessTransactionState);
    when(crossProcessTransactionState.getTripId()).thenReturn("tripId");
    InboundHeaderState ihs = mock(InboundHeaderState.class);
    when(ihs.getSyntheticsJobId()).thenReturn(synJobId);
    when(ihs.getSyntheticsMonitorId()).thenReturn(synMonitorId);
    when(ihs.getSyntheticsResourceId()).thenReturn(synResourceId);
    when(ihs.getSyntheticsVersion()).thenReturn(HeadersUtil.SYNTHETICS_MIN_VERSION);
    when(tx.getInboundHeaderState()).thenReturn(ihs);
    TransactionTimer timer = new TransactionTimer(tracer.getStartTime());
    timer.markTransactionActivityAsDone(tracer.getEndTime(), tracer.getDuration());
    timer.markTransactionAsDone();
    when(tx.getTransactionTimer()).thenReturn(timer);
    // when(tx.getApplicationName()).thenReturn(appName);
    Set<TransactionActivity> activities = new HashSet<>();
    for (Map.Entry<Long, Collection<Duration>> entry : threadIdToDuration.asMap().entrySet()) {
        for (Duration duration : entry.getValue()) {
            TransactionActivity activity = mock(TransactionActivity.class);
            when(activity.getThreadId()).thenReturn(entry.getKey());
            Tracer rootTracer = mock(Tracer.class);
            when(rootTracer.getStartTime()).thenReturn(duration.startTime);
            when(rootTracer.getEndTime()).thenReturn(duration.endTime);
            when(activity.getRootTracer()).thenReturn(rootTracer);
            activities.add(activity);
        }
    }
    when(tx.getFinishedChildren()).thenReturn(activities);
    if (includeDistributedTracePayload) {
        SpanProxy spanProxy = mock(SpanProxy.class);
        DistributedTracePayloadImpl payload = DistributedTracePayloadImpl.createDistributedTracePayload("abc", "def", "def", new Random().nextFloat());
        when(spanProxy.getInboundDistributedTracePayload()).thenReturn(payload);
        when(tx.getSpanProxy()).thenReturn(spanProxy);
    }
    return new TransactionData(tx, 0);
}
Also used : ErrorServiceImpl(com.newrelic.agent.errors.ErrorServiceImpl) HashMap(java.util.HashMap) SpanProxy(com.newrelic.agent.tracing.SpanProxy) Tracer(com.newrelic.agent.tracers.Tracer) PriorityTransactionName(com.newrelic.agent.transaction.PriorityTransactionName) Dispatcher(com.newrelic.agent.dispatchers.Dispatcher) DistributedTracePayloadImpl(com.newrelic.agent.tracing.DistributedTracePayloadImpl) Random(java.util.Random) TransactionThrowable(com.newrelic.agent.transaction.TransactionThrowable) Collection(java.util.Collection) TransactionTimer(com.newrelic.agent.transaction.TransactionTimer) HashMap(java.util.HashMap) Map(java.util.Map) HashSet(java.util.HashSet)

Example 8 with Dispatcher

use of com.newrelic.agent.dispatchers.Dispatcher in project newrelic-java-agent by newrelic.

the class RequestUriConfigTests method startDispatcherTracer.

private Tracer startDispatcherTracer(String appName, String uri) throws Exception {
    Transaction tx = Transaction.getTransaction();
    MockHttpRequest httpRequest = new MockHttpRequest();
    httpRequest.setRequestURI(uri);
    MockHttpResponse httpResponse = new MockHttpResponse();
    ClassMethodSignature sig = new ClassMethodSignature(getClass().getName(), "dude", "()V");
    BasicRequestRootTracer requestDispatcherTracer = new BasicRequestRootTracer(tx, sig, this, httpRequest, httpResponse);
    Dispatcher dispatcher = requestDispatcherTracer.createDispatcher();
    tx.getTransactionActivity().tracerStarted(requestDispatcherTracer);
    tx.setDispatcher(dispatcher);
    tx.setApplicationName(ApplicationNamePriority.REQUEST_ATTRIBUTE, appName);
    return requestDispatcherTracer;
}
Also used : MockHttpRequest(com.newrelic.agent.tracers.servlet.MockHttpRequest) Transaction(com.newrelic.agent.Transaction) ClassMethodSignature(com.newrelic.agent.tracers.ClassMethodSignature) Dispatcher(com.newrelic.agent.dispatchers.Dispatcher) BasicRequestRootTracer(com.newrelic.agent.tracers.servlet.BasicRequestRootTracer) MockHttpResponse(com.newrelic.agent.tracers.servlet.MockHttpResponse)

Example 9 with Dispatcher

use of com.newrelic.agent.dispatchers.Dispatcher in project newrelic-java-agent by newrelic.

the class AgentTest method testMultipleRequestInitializedAndDestroyedCalls.

@Test
public void testMultipleRequestInitializedAndDestroyedCalls() {
    AgentConfig agentConfig = AgentHelper.mockAgentConfig();
    Assert.assertNotNull(agentConfig);
    final com.newrelic.api.agent.Request request = Mockito.mock(com.newrelic.api.agent.Request.class);
    Mockito.doReturn(HeaderType.HTTP).when(request).getHeaderType();
    // final HttpServletResponse httpResponse = Mockito.mock(HttpServletResponse.class);
    final com.newrelic.api.agent.Response response = Mockito.mock(com.newrelic.api.agent.Response.class);
    // Assert that the dispatcher starts null, gets initialized, and then doesn't change.
    Transaction tx = Transaction.getTransaction();
    Assert.assertNull(tx.getDispatcher());
    tx.requestInitialized(request, response);
    Dispatcher disp = tx.getDispatcher();
    Assert.assertNotNull(disp);
    tx.requestInitialized(request, response);
    Assert.assertEquals(disp, tx.getDispatcher());
    tx.requestDestroyed();
    // There's no obvious way to tell that the transaction complete, so the test just
    // checks that we don't throw an exception here:
    tx.requestDestroyed();
}
Also used : AgentConfig(com.newrelic.agent.config.AgentConfig) NewRelicIgnoreTransaction(test.newrelic.test.agent.TraceAnnotationTest.NewRelicIgnoreTransaction) Transaction(com.newrelic.agent.Transaction) Dispatcher(com.newrelic.agent.dispatchers.Dispatcher) WebRequestDispatcher(com.newrelic.agent.dispatchers.WebRequestDispatcher) Test(org.junit.Test)

Example 10 with Dispatcher

use of com.newrelic.agent.dispatchers.Dispatcher in project newrelic-java-agent by newrelic.

the class AgentTest method ignoreApdex.

@Test
public void ignoreApdex() throws Exception {
    final List<Dispatcher> txData = new ArrayList<>();
    Servlet servlet = new DummyServlet() {

        private static final long serialVersionUID = 1L;

        @NewRelicIgnoreApdex
        @Override
        public void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
            Tracer lastTracer = Transaction.getTransaction().getTransactionActivity().getLastTracer();
            txData.add(Transaction.getTransaction().getDispatcher());
            Dude dude = new Dude();
            dude.go().go();
            Assert.assertEquals(2, AgentHelper.getChildren(lastTracer).size());
        }
    };
    AgentHelper.invokeServlet(servlet, "ignore", "Test", "/foo/bar");
    Assert.assertEquals(1, txData.size());
    WebRequestDispatcher dispatcher = (WebRequestDispatcher) txData.get(0);
    Assert.assertTrue(dispatcher.isIgnoreApdex());
}
Also used : MockHttpServletRequest(org.apache.struts.mock.MockHttpServletRequest) HttpServletRequest(javax.servlet.http.HttpServletRequest) Tracer(com.newrelic.agent.tracers.Tracer) ArrayList(java.util.ArrayList) HttpServlet(javax.servlet.http.HttpServlet) Servlet(javax.servlet.Servlet) GenericServlet(javax.servlet.GenericServlet) WebServlet(javax.servlet.annotation.WebServlet) MockHttpServletResponse(org.apache.struts.mock.MockHttpServletResponse) HttpServletResponse(javax.servlet.http.HttpServletResponse) WebRequestDispatcher(com.newrelic.agent.dispatchers.WebRequestDispatcher) Dispatcher(com.newrelic.agent.dispatchers.Dispatcher) WebRequestDispatcher(com.newrelic.agent.dispatchers.WebRequestDispatcher) Test(org.junit.Test)

Aggregations

Dispatcher (com.newrelic.agent.dispatchers.Dispatcher)20 Transaction (com.newrelic.agent.Transaction)11 Test (org.junit.Test)9 WebRequestDispatcher (com.newrelic.agent.dispatchers.WebRequestDispatcher)4 AgentConfig (com.newrelic.agent.config.AgentConfig)2 Tracer (com.newrelic.agent.tracers.Tracer)2 MockHttpRequest (com.newrelic.agent.tracers.servlet.MockHttpRequest)2 MockHttpResponse (com.newrelic.agent.tracers.servlet.MockHttpResponse)2 TransactionTimer (com.newrelic.agent.transaction.TransactionTimer)2 InboundHeaders (com.newrelic.api.agent.InboundHeaders)2 NewRelicIgnoreTransaction (test.newrelic.test.agent.TraceAnnotationTest.NewRelicIgnoreTransaction)2 CrossProcessTransactionState (com.newrelic.agent.CrossProcessTransactionState)1 TransactionData (com.newrelic.agent.TransactionData)1 ErrorServiceImpl (com.newrelic.agent.errors.ErrorServiceImpl)1 ClassMethodSignature (com.newrelic.agent.tracers.ClassMethodSignature)1 BasicRequestRootTracer (com.newrelic.agent.tracers.servlet.BasicRequestRootTracer)1 DistributedTracePayloadImpl (com.newrelic.agent.tracing.DistributedTracePayloadImpl)1 SpanProxy (com.newrelic.agent.tracing.SpanProxy)1 PriorityTransactionName (com.newrelic.agent.transaction.PriorityTransactionName)1 TransactionNamingPolicy (com.newrelic.agent.transaction.TransactionNamingPolicy)1