Search in sources :

Example 6 with TracedError

use of com.newrelic.agent.errors.TracedError in project newrelic-java-agent by newrelic.

the class ApiTest method testNoticeErrorOutsideTransaction.

@Test
public void testNoticeErrorOutsideTransaction() throws Exception {
    ApiTestHelper.mockOutServiceManager();
    ErrorService errorService = ServiceFactory.getRPMService().getErrorService();
    NewRelic.noticeError(new RuntimeException("boom"));
    try {
        // ensure that the ThrowableErrors have different timestamps
        Thread.sleep(5);
    } catch (InterruptedException e) {
    }
    NewRelic.noticeError(new RuntimeException("boom2"));
    Collection<TracedError> tracedErrors = errorService.getAndClearTracedErrors();
    Assert.assertEquals("incorrect traced errors count: " + tracedErrors.toString(), 2, tracedErrors.size());
    TracedError tracedError = (TracedError) tracedErrors.toArray()[0];
    Assert.assertEquals("exception class incorrect", "java.lang.RuntimeException", tracedError.getExceptionClass());
    Assert.assertEquals("error message incorrect", "boom", tracedError.getMessage());
}
Also used : ErrorService(com.newrelic.agent.errors.ErrorService) TracedError(com.newrelic.agent.errors.TracedError) BrowserConfigTest(com.newrelic.agent.browser.BrowserConfigTest) Test(org.junit.Test)

Example 7 with TracedError

use of com.newrelic.agent.errors.TracedError in project newrelic-java-agent by newrelic.

the class SpanParentTest method testErrorAttributes.

@Test
public void testErrorAttributes() throws Exception {
    EnvironmentHolder holder = setupEnvironemntHolder("all_enabled_test");
    Header actualHeader = txnStarter(true);
    try {
        TransactionDataList transactionList = holder.getTransactionList();
        ServiceFactory.getHarvestService().harvestNow();
        assertEquals(1, transactionList.size());
        Collection<Tracer> tracers = transactionList.get(0).getTracers();
        Tracer tracer = ((TracerList) tracers).get(0);
        String expectedGuid = tracer.getGuid();
        String actualGuid = findGuid(actualHeader.getValue());
        Assert.assertEquals(expectedGuid, actualGuid);
        ErrorServiceImpl errorService = (ErrorServiceImpl) ServiceFactory.getRPMService().getErrorService();
        List<TracedError> tracedErrors = errorService.getAndClearTracedErrors();
        assertEquals(1, tracedErrors.size());
        TracedError errorTrace = tracedErrors.get(0);
        Map<String, ?> errorAtts = errorTrace.getIntrinsicAtts();
        Assert.assertNotNull(errorAtts.get("traceId"));
        Assert.assertNotNull(errorAtts.get("guid"));
        Assert.assertNotNull(errorAtts.get("priority"));
        Assert.assertNotNull(errorAtts.get("sampled"));
    } finally {
        holder.close();
    }
}
Also used : TransactionDataList(com.newrelic.agent.TransactionDataList) Header(org.apache.http.Header) ErrorServiceImpl(com.newrelic.agent.errors.ErrorServiceImpl) TracedError(com.newrelic.agent.errors.TracedError) EnvironmentHolder(test.newrelic.test.agent.EnvironmentHolder) Tracer(com.newrelic.agent.tracers.Tracer) TracerList(com.newrelic.agent.TracerList) Test(org.junit.Test)

Example 8 with TracedError

use of com.newrelic.agent.errors.TracedError in project newrelic-java-agent by newrelic.

the class DeadLockDetector method getTracedErrors.

/**
 * Returns a list of traced errors for the given deadlocked threads. This list should be smaller than the original
 * list of threads - one error is created for each pair of deadlocked threads, and the error contains a stack trace
 * for each thread.
 */
TracedError[] getTracedErrors(List<ThreadInfo> threadInfos) {
    Map<Long, ThreadInfo> idToThreads = new HashMap<>();
    for (ThreadInfo thread : threadInfos) {
        idToThreads.put(thread.getThreadId(), thread);
    }
    List<TracedError> errors = new ArrayList<>();
    Set<Long> skipIds = new HashSet<>();
    StringBuffer deadlockMsg = new StringBuffer();
    for (ThreadInfo thread : threadInfos) {
        if (!skipIds.contains(thread.getThreadId())) {
            long otherId = thread.getLockOwnerId();
            skipIds.add(otherId);
            ThreadInfo otherThread = idToThreads.get(otherId);
            // four to preserve memory and not cause a new map to be created
            Map<String, String> parameters = Maps.newHashMapWithExpectedSize(4);
            parameters.put(AttributeNames.THREAD_NAME, thread.getThreadName());
            Map<String, StackTraceElement[]> stackTraces = new HashMap<>();
            stackTraces.put(thread.getThreadName(), thread.getStackTrace());
            if (otherThread != null) {
                parameters.put(AttributeNames.LOCK_THREAD_NAME, otherThread.getThreadName());
                stackTraces.put(otherThread.getThreadName(), otherThread.getStackTrace());
            }
            if (!errors.isEmpty()) {
                deadlockMsg.append(", ");
            }
            deadlockMsg.append(thread.toString());
            errors.add(DeadlockTraceError.builder(errorCollectorConfig, null, System.currentTimeMillis()).threadInfoAndStackTrace(thread, stackTraces).errorAttributes(parameters).build());
        }
    }
    Agent.LOG.log(Level.FINER, "There are {0} deadlocked thread(s): [{1}]", errors.size(), deadlockMsg);
    return errors.toArray(new TracedError[0]);
}
Also used : HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) ThreadInfo(java.lang.management.ThreadInfo) TracedError(com.newrelic.agent.errors.TracedError) HashSet(java.util.HashSet)

Aggregations

TracedError (com.newrelic.agent.errors.TracedError)8 Test (org.junit.Test)6 BrowserConfigTest (com.newrelic.agent.browser.BrowserConfigTest)4 ErrorService (com.newrelic.agent.errors.ErrorService)4 HashMap (java.util.HashMap)3 Tracer (com.newrelic.agent.tracers.Tracer)2 ThreadInfo (java.lang.management.ThreadInfo)2 MockRPMService (com.newrelic.agent.MockRPMService)1 TracerList (com.newrelic.agent.TracerList)1 TransactionDataList (com.newrelic.agent.TransactionDataList)1 ErrorCollectorConfig (com.newrelic.agent.config.ErrorCollectorConfig)1 ErrorServiceImpl (com.newrelic.agent.errors.ErrorServiceImpl)1 DefaultTracer (com.newrelic.agent.tracers.DefaultTracer)1 OtherRootSqlTracer (com.newrelic.agent.tracers.OtherRootSqlTracer)1 OtherRootTracer (com.newrelic.agent.tracers.OtherRootTracer)1 SqlTracer (com.newrelic.agent.tracers.SqlTracer)1 BasicRequestRootTracer (com.newrelic.agent.tracers.servlet.BasicRequestRootTracer)1 TransactionThrowable (com.newrelic.agent.transaction.TransactionThrowable)1 StringWriter (java.io.StringWriter)1 Writer (java.io.Writer)1