Search in sources :

Example 1 with MockHttpRequest

use of com.newrelic.agent.tracers.servlet.MockHttpRequest in project newrelic-java-agent by newrelic.

the class TransactionTest method createDispatcherTracer.

// Create a Tracer for tests that require one.
private Tracer createDispatcherTracer(boolean createRequest) {
    Transaction tx = Transaction.getTransaction();
    ClassMethodSignature sig = new ClassMethodSignature(getClass().getName(), "dude", "()V");
    if (createRequest) {
        MockHttpRequest httpRequest = new MockHttpRequest();
        MockHttpResponse httpResponse = new MockHttpResponse();
        return new BasicRequestRootTracer(tx, sig, this, httpRequest, httpResponse);
    } else {
        return new OtherRootTracer(tx, sig, this, new SimpleMetricNameFormat("thisismyname"));
    }
}
Also used : MockHttpRequest(com.newrelic.agent.tracers.servlet.MockHttpRequest) ClassMethodSignature(com.newrelic.agent.tracers.ClassMethodSignature) BasicRequestRootTracer(com.newrelic.agent.tracers.servlet.BasicRequestRootTracer) SimpleMetricNameFormat(com.newrelic.agent.tracers.metricname.SimpleMetricNameFormat) MockHttpResponse(com.newrelic.agent.tracers.servlet.MockHttpResponse) OtherRootTracer(com.newrelic.agent.tracers.OtherRootTracer)

Example 2 with MockHttpRequest

use of com.newrelic.agent.tracers.servlet.MockHttpRequest 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 3 with MockHttpRequest

use of com.newrelic.agent.tracers.servlet.MockHttpRequest in project newrelic-java-agent by newrelic.

the class BrowserConfigTest method createDispatcherTracer.

private BasicRequestRootTracer createDispatcherTracer() {
    Transaction tx = Transaction.getTransaction();
    MockHttpRequest httpRequest = new MockHttpRequest();
    MockHttpResponse httpResponse = new MockHttpResponse();
    ClassMethodSignature sig = new ClassMethodSignature(getClass().getName(), "dude", "()V");
    return new BasicRequestRootTracer(tx, sig, this, httpRequest, httpResponse);
}
Also used : MockHttpRequest(com.newrelic.agent.tracers.servlet.MockHttpRequest) Transaction(com.newrelic.agent.Transaction) ClassMethodSignature(com.newrelic.agent.tracers.ClassMethodSignature) BasicRequestRootTracer(com.newrelic.agent.tracers.servlet.BasicRequestRootTracer) MockHttpResponse(com.newrelic.agent.tracers.servlet.MockHttpResponse)

Example 4 with MockHttpRequest

use of com.newrelic.agent.tracers.servlet.MockHttpRequest in project newrelic-java-agent by newrelic.

the class SqlTraceServiceTest method startDispatcherTracer.

private Tracer startDispatcherTracer(String appName) throws Exception {
    Transaction tx = Transaction.getTransaction();
    MockHttpRequest httpRequest = new MockHttpRequest();
    MockHttpResponse httpResponse = new MockHttpResponse();
    ClassMethodSignature sig = new ClassMethodSignature(getClass().getName(), "dude", "()V");
    Tracer requestDispatcherTracer = new BasicRequestRootTracer(tx, sig, this, httpRequest, httpResponse);
    tx.getTransactionActivity().tracerStarted(requestDispatcherTracer);
    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) SqlTracer(com.newrelic.agent.tracers.SqlTracer) OtherRootSqlTracer(com.newrelic.agent.tracers.OtherRootSqlTracer) BasicRequestRootTracer(com.newrelic.agent.tracers.servlet.BasicRequestRootTracer) Tracer(com.newrelic.agent.tracers.Tracer) BasicRequestRootTracer(com.newrelic.agent.tracers.servlet.BasicRequestRootTracer) MockHttpResponse(com.newrelic.agent.tracers.servlet.MockHttpResponse)

Example 5 with MockHttpRequest

use of com.newrelic.agent.tracers.servlet.MockHttpRequest in project newrelic-java-agent by newrelic.

the class W3CTraceContextCrossAgentTest method testTraceContext.

@Test
public void testTraceContext() throws Exception {
    String testName = (String) testData.get("test_name");
    String accountKey = (String) testData.get("trusted_account_key");
    String accountId = (String) testData.get("account_id");
    String transportType = (String) testData.get("transport_type");
    Boolean webTransaction = (Boolean) testData.get("web_transaction");
    Boolean raisesException = (Boolean) testData.get("raises_exception");
    Boolean forceSampledTrue = (Boolean) testData.get("force_sampled_true");
    Boolean spanEventsEnabled = (Boolean) testData.get("span_events_enabled");
    replaceConfig(spanEventsEnabled);
    System.out.println("Running test: " + testName);
    JSONArray outbound_payloads = (JSONArray) testData.get("outbound_payloads");
    JSONArray inbound_payloads = (JSONArray) testData.get("inbound_headers");
    List expectedMetrics = (List) testData.get("expected_metrics");
    Map<String, Object> intrinsics = (Map<String, Object>) testData.get("intrinsics");
    Map<String, Object> commonAssertions = intrinsics == null ? Collections.<String, Object>emptyMap() : (Map<String, Object>) intrinsics.get("common");
    List targetEvents = intrinsics == null ? Collections.emptyList() : (ArrayList) intrinsics.get("target_events");
    Map<String, Object> transactionAssertions = intrinsics == null ? Collections.<String, Object>emptyMap() : (Map<String, Object>) intrinsics.get("Transaction");
    Map<String, Object> spanAssertions = intrinsics == null ? Collections.<String, Object>emptyMap() : (Map<String, Object>) intrinsics.get("Span");
    Map<String, Object> connectInfo = Maps.newHashMap();
    connectInfo.put(DistributedTracingConfig.ACCOUNT_ID, accountId);
    connectInfo.put(DistributedTracingConfig.TRUSTED_ACCOUNT_KEY, accountKey);
    connectInfo.put(DistributedTracingConfig.PRIMARY_APPLICATION_ID, "2827902");
    AgentConfig agentConfig = AgentHelper.createAgentConfig(true, Collections.<String, Object>emptyMap(), connectInfo);
    distributedTraceService.connected(null, agentConfig);
    Transaction.clearTransaction();
    TransactionActivity.clear();
    spanEventService.clearReservoir();
    Transaction tx = Transaction.getTransaction();
    TransactionData transactionData = new TransactionData(tx, 0);
    TransactionStats transactionStats = transactionData.getTransaction().getTransactionActivity().getTransactionStats();
    eventPool = spanEventService.getOrCreateDistributedSamplingReservoir(APP_NAME);
    List<String> parents = Lists.newArrayList();
    List<String> states = Lists.newArrayList();
    List<String> newrelic = Lists.newArrayList();
    if (inbound_payloads != null) {
        for (Object payload : inbound_payloads) {
            JSONObject j = (JSONObject) payload;
            if (j.get("traceparent") != null) {
                parents.addAll(Lists.newArrayList(String.valueOf(j.get("traceparent"))));
            }
            if (j.get("tracestate") != null) {
                states.addAll(Lists.newArrayList(String.valueOf(j.get("tracestate"))));
            }
            if (j.get("newrelic") != null) {
                newrelic.addAll(Lists.newArrayList(String.valueOf(j.get("newrelic"))));
            }
        }
    }
    MockHttpRequest httpRequest = new MockHttpRequest();
    for (String parent : parents) {
        httpRequest.setHeader("traceparent", parent);
    }
    for (String state : states) {
        httpRequest.setHeader("tracestate", state);
    }
    for (String header : newrelic) {
        httpRequest.setHeader("newrelic", header);
    }
    Tracer rootTracer;
    if (webTransaction) {
        rootTracer = TransactionAsyncUtility.createAndStartDispatcherTracer(this, "WebTransaction", httpRequest);
    } else {
        rootTracer = TransactionAsyncUtility.createOtherTracer("OtherTransaction");
        tx.getTransactionActivity().tracerStarted(rootTracer);
        tx.provideRawHeaders(httpRequest);
    }
    if (raisesException) {
        tx.setThrowable(new Throwable(), TransactionErrorPriority.API, false);
    }
    setTransportType(tx, transportType);
    if (forceSampledTrue && tx.getPriority() < 1) {
        tx.setPriorityIfNotNull(new Random().nextFloat() + 1.0f);
    }
    if (outbound_payloads != null) {
        for (Object assertion : outbound_payloads) {
            MockHttpResponse mockHttpResponse = new MockHttpResponse();
            tx.getCrossProcessState().processOutboundRequestHeaders(mockHttpResponse);
            JSONObject payloadAssertions = (JSONObject) assertion;
            String traceparent = mockHttpResponse.getHeader("traceparent");
            String tracestate = mockHttpResponse.getHeader("tracestate");
            if (traceparent != null) {
                W3CTraceParent traceParent = W3CTraceParentParser.parseHeaders(Collections.singletonList(traceparent));
                assertOutboundTraceParentPayload(payloadAssertions, traceParent);
            } else {
                assertOutboundTraceParentPayload(payloadAssertions, null);
            }
            if (tracestate != null) {
                assertOutboundTraceStatePayload(payloadAssertions, W3CTraceStateSupport.parseHeaders(Collections.singletonList(mockHttpResponse.getHeader("tracestate"))));
            } else {
                assertOutboundTraceStatePayload(payloadAssertions, null);
            }
        }
    }
    rootTracer.finish(Opcodes.RETURN, 0);
    List<SpanEvent> spans = eventPool.asList();
    TransactionEvent transactionEvent = serviceManager.getTransactionEventsService().createEvent(transactionData, transactionStats, "wat");
    JSONObject txnEvents = serializeAndParseEvents(transactionEvent);
    StatsEngine statsEngine = statsService.getStatsEngineForHarvest(APP_NAME);
    assertExpectedMetrics(expectedMetrics, statsEngine);
    for (Object event : targetEvents) {
        if (event.toString().startsWith("Transaction") && transactionAssertions != null) {
            assertTransactionEvents(transactionAssertions, txnEvents);
            assertTransactionEvents(commonAssertions, txnEvents);
        } else if (event.toString().startsWith("Span") && spanAssertions != null) {
            assertSpanEvents(spanAssertions, spans);
            assertSpanEvents(commonAssertions, spans);
        }
    }
}
Also used : MockHttpRequest(com.newrelic.agent.tracers.servlet.MockHttpRequest) Tracer(com.newrelic.agent.tracers.Tracer) JSONArray(org.json.simple.JSONArray) TransactionEvent(com.newrelic.agent.service.analytics.TransactionEvent) JSONObject(org.json.simple.JSONObject) SpanEvent(com.newrelic.agent.model.SpanEvent) JSONObject(org.json.simple.JSONObject) MockHttpResponse(com.newrelic.agent.tracers.servlet.MockHttpResponse) Test(org.junit.Test)

Aggregations

MockHttpRequest (com.newrelic.agent.tracers.servlet.MockHttpRequest)22 MockHttpResponse (com.newrelic.agent.tracers.servlet.MockHttpResponse)19 Transaction (com.newrelic.agent.Transaction)16 ClassMethodSignature (com.newrelic.agent.tracers.ClassMethodSignature)15 BasicRequestRootTracer (com.newrelic.agent.tracers.servlet.BasicRequestRootTracer)15 Test (org.junit.Test)5 Tracer (com.newrelic.agent.tracers.Tracer)4 OtherRootTracer (com.newrelic.agent.tracers.OtherRootTracer)3 Dispatcher (com.newrelic.agent.dispatchers.Dispatcher)2 WebRequestDispatcher (com.newrelic.agent.dispatchers.WebRequestDispatcher)1 SpanEvent (com.newrelic.agent.model.SpanEvent)1 TransactionEvent (com.newrelic.agent.service.analytics.TransactionEvent)1 DefaultTracer (com.newrelic.agent.tracers.DefaultTracer)1 OtherRootSqlTracer (com.newrelic.agent.tracers.OtherRootSqlTracer)1 SqlTracer (com.newrelic.agent.tracers.SqlTracer)1 SimpleMetricNameFormat (com.newrelic.agent.tracers.metricname.SimpleMetricNameFormat)1 HeaderType (com.newrelic.api.agent.HeaderType)1 Response (com.newrelic.api.agent.Response)1 Trace (com.newrelic.api.agent.Trace)1 JSONArray (org.json.simple.JSONArray)1