Search in sources :

Example 1 with HeaderType

use of com.newrelic.api.agent.HeaderType in project newrelic-java-agent by newrelic.

the class SegmentTest method testSpanParenting.

@Test
public void testSpanParenting() {
    SpanEventsService spanEventService = ServiceFactory.getSpanEventService();
    SamplingPriorityQueue<SpanEvent> eventPool = spanEventService.getOrCreateDistributedSamplingReservoir(APP_NAME);
    Transaction.clearTransaction();
    Tracer rootTracer = makeTransaction();
    rootTracer.getTransactionActivity().getTransaction().setPriorityIfNotNull(1.0f);
    rootTracer.setMetricName("RootTracer");
    Transaction tx = rootTracer.getTransactionActivity().getTransaction();
    Segment segment = tx.startSegment("custom", "segment");
    final HashMap<String, String> headers = new HashMap<>();
    segment.addOutboundRequestHeaders(new OutboundHeaders() {

        @Override
        public HeaderType getHeaderType() {
            return HeaderType.HTTP;
        }

        @Override
        public void setHeader(String name, String value) {
            headers.put(name, value);
        }
    });
    String payload = headers.get("newrelic");
    segment.end();
    rootTracer.finish(Opcodes.ARETURN, null);
    Transaction.clearTransaction();
    Tracer secondRootTracer = makeTransaction();
    secondRootTracer.setMetricName("SecondRootTracer");
    Transaction secondTxn = secondRootTracer.getTransactionActivity().getTransaction();
    secondTxn.acceptDistributedTracePayload(payload);
    secondRootTracer.finish(Opcodes.ARETURN, null);
    List<SpanEvent> spanEvents = eventPool.asList();
    Assert.assertNotNull(spanEvents);
    assertEquals(3, spanEvents.size());
    SpanEvent rootTracerSpanEvent = findSpanByName(spanEvents, "RootTracer");
    SpanEvent segmentSpanEvent = findSpanByName(spanEvents, "custom/segment");
    SpanEvent secondRootTracerSpanEvent = findSpanByName(spanEvents, "SecondRootTracer");
    assertEquals(rootTracerSpanEvent.getTraceId(), segmentSpanEvent.getTraceId());
    assertEquals(segmentSpanEvent.getTraceId(), secondRootTracerSpanEvent.getTraceId());
    assertEquals(rootTracerSpanEvent.getGuid(), segmentSpanEvent.getParentId());
    assertEquals(segmentSpanEvent.getGuid(), secondRootTracerSpanEvent.getParentId());
}
Also used : HashMap(java.util.HashMap) ExitTracer(com.newrelic.agent.bridge.ExitTracer) DefaultTracer(com.newrelic.agent.tracers.DefaultTracer) Tracer(com.newrelic.agent.tracers.Tracer) OtherRootTracer(com.newrelic.agent.tracers.OtherRootTracer) SpanEventsService(com.newrelic.agent.service.analytics.SpanEventsService) HeaderType(com.newrelic.api.agent.HeaderType) SpanEvent(com.newrelic.agent.model.SpanEvent) OutboundHeaders(com.newrelic.api.agent.OutboundHeaders) Test(org.junit.Test)

Example 2 with HeaderType

use of com.newrelic.api.agent.HeaderType in project newrelic-java-agent by newrelic.

the class OtherTransactionErrorsTest method testException.

@Test
public void testException() throws Exception {
    MockHttpRequest mockHttpRequest = new MockHttpRequest();
    mockHttpRequest.setMethod("GET");
    mockHttpRequest.setRequestURI("requesturi");
    mockHttpRequest.setHeader(AttributeNames.REQUEST_ACCEPT_PARAMETER_NAME, "accept");
    mockHttpRequest.setHeader(AttributeNames.REQUEST_HOST_PARAMETER_NAME, "host");
    mockHttpRequest.setHeader(AttributeNames.REQUEST_USER_AGENT_PARAMETER_NAME, "useragent");
    mockHttpRequest.setHeader(AttributeNames.REQUEST_CONTENT_LENGTH_PARAMETER_NAME, "content-length");
    NewRelic.setRequestAndResponse(mockHttpRequest, new Response() {

        @Override
        public int getStatus() throws Exception {
            return 200;
        }

        @Override
        public String getStatusMessage() throws Exception {
            return "Status Message";
        }

        @Override
        public String getContentType() {
            return "text/content-type";
        }

        @Override
        public HeaderType getHeaderType() {
            return HeaderType.HTTP;
        }

        @Override
        public void setHeader(String name, String value) {
        // 
        }
    });
    try {
        new Runnable() {

            @Trace(dispatcher = true)
            @Override
            public void run() {
                throw new RuntimeException();
            }
        }.run();
    } catch (RuntimeException ex) {
    }
    Assert.assertEquals(1, transactions.size());
    Assert.assertNotNull(transactions.get(0).getThrowable());
}
Also used : Response(com.newrelic.api.agent.Response) Trace(com.newrelic.api.agent.Trace) MockHttpRequest(com.newrelic.agent.tracers.servlet.MockHttpRequest) HeaderType(com.newrelic.api.agent.HeaderType) Test(org.junit.Test)

Aggregations

HeaderType (com.newrelic.api.agent.HeaderType)2 Test (org.junit.Test)2 ExitTracer (com.newrelic.agent.bridge.ExitTracer)1 SpanEvent (com.newrelic.agent.model.SpanEvent)1 SpanEventsService (com.newrelic.agent.service.analytics.SpanEventsService)1 DefaultTracer (com.newrelic.agent.tracers.DefaultTracer)1 OtherRootTracer (com.newrelic.agent.tracers.OtherRootTracer)1 Tracer (com.newrelic.agent.tracers.Tracer)1 MockHttpRequest (com.newrelic.agent.tracers.servlet.MockHttpRequest)1 OutboundHeaders (com.newrelic.api.agent.OutboundHeaders)1 Response (com.newrelic.api.agent.Response)1 Trace (com.newrelic.api.agent.Trace)1 HashMap (java.util.HashMap)1