Search in sources :

Example 16 with Transaction

use of com.newrelic.agent.bridge.Transaction in project newrelic-java-agent by newrelic.

the class ExternalMetricsTest method testNullHost.

@Test
public void testNullHost() throws Exception {
    TracedMethod tracedMethod = Mockito.mock(TracedMethod.class);
    Transaction transaction = Mockito.mock(Transaction.class);
    String nullHost = null;
    ExternalMetrics.makeExternalComponentTrace(transaction, tracedMethod, nullHost, "library", true, "http://uri", "operation");
}
Also used : Transaction(com.newrelic.agent.bridge.Transaction) TracedMethod(com.newrelic.agent.bridge.TracedMethod) Test(org.junit.Test)

Example 17 with Transaction

use of com.newrelic.agent.bridge.Transaction in project newrelic-java-agent by newrelic.

the class Utils method getThreadTokenAndRefCount.

public static AgentBridge.TokenAndRefCount getThreadTokenAndRefCount() {
    AgentBridge.TokenAndRefCount tokenAndRefCount = AgentBridge.activeToken.get();
    if (tokenAndRefCount == null) {
        Transaction tx = AgentBridge.getAgent().getTransaction(false);
        if (tx != null) {
            tokenAndRefCount = new AgentBridge.TokenAndRefCount(tx.getToken(), AgentBridge.getAgent().getTracedMethod(), new AtomicInteger(0));
            AgentBridge.activeToken.set(tokenAndRefCount);
        }
    }
    return tokenAndRefCount;
}
Also used : Transaction(com.newrelic.agent.bridge.Transaction) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) AgentBridge(com.newrelic.agent.bridge.AgentBridge)

Example 18 with Transaction

use of com.newrelic.agent.bridge.Transaction in project newrelic-java-agent by newrelic.

the class JavaxWsRsApi_Subresource_Instrumentation method instrumentation.

@WeaveWithAnnotation(annotationClasses = { "javax.ws.rs.PUT", "javax.ws.rs.POST", "javax.ws.rs.GET", "javax.ws.rs.DELETE", "javax.ws.rs.HEAD", "javax.ws.rs.OPTIONS", "javax.ws.rs.Path", "javax.ws.rs.PATCH" })
@WeaveIntoAllMethods
@Trace(dispatcher = true)
private static void instrumentation() {
    Transaction transaction = AgentBridge.getAgent().getWeakRefTransaction(false);
    if (transaction != null) {
        JavaxWsRsApiHelper.ResourcePath resourcePath = JavaxWsRsApiHelper.subresourcePath.get();
        if (!transaction.equals(resourcePath.transaction)) {
            resourcePath.pathQueue.clear();
            resourcePath.transaction = transaction;
        }
        String rootPath = null;
        Path rootPathAnnotation = Weaver.getClassAnnotation(Path.class);
        if (rootPathAnnotation != null) {
            rootPath = rootPathAnnotation.value();
        }
        String methodPath = null;
        Path methodPathAnnotation = Weaver.getMethodAnnotation(Path.class);
        if (methodPathAnnotation != null) {
            methodPath = methodPathAnnotation.value();
        }
        String httpMethod = null;
        /* Unfortunately we have to do this in a big if/else block because we can't currently support variables passed
             into the Weaver.getMethodAnnotation method. We might be able to make an improvement to handle this in the future */
        if (Weaver.getMethodAnnotation(PUT.class) != null) {
            httpMethod = PUT.class.getSimpleName();
        } else if (Weaver.getMethodAnnotation(POST.class) != null) {
            httpMethod = POST.class.getSimpleName();
        } else if (Weaver.getMethodAnnotation(GET.class) != null) {
            httpMethod = GET.class.getSimpleName();
        } else if (Weaver.getMethodAnnotation(DELETE.class) != null) {
            httpMethod = DELETE.class.getSimpleName();
        } else if (Weaver.getMethodAnnotation(HEAD.class) != null) {
            httpMethod = HEAD.class.getSimpleName();
        } else if (Weaver.getMethodAnnotation(OPTIONS.class) != null) {
            httpMethod = OPTIONS.class.getSimpleName();
        } else {
            // PATCH annotation was added later so may not be available
            try {
                if (Weaver.getMethodAnnotation(PATCH.class) != null) {
                    httpMethod = PATCH.class.getSimpleName();
                }
            } catch (NoClassDefFoundError e) {
            }
        }
        if (httpMethod != null) {
            Queue<String> subresourcePath = resourcePath.pathQueue;
            String fullPath = JavaxWsRsApiHelper.getPath(rootPath, methodPath, httpMethod);
            subresourcePath.offer(fullPath);
            StringBuilder txNameBuilder = new StringBuilder();
            String pathPart;
            while ((pathPart = subresourcePath.poll()) != null) {
                txNameBuilder.append(pathPart);
            }
            transaction.setTransactionName(TransactionNamePriority.FRAMEWORK_HIGH, false, "RestWebService", txNameBuilder.toString());
        } else {
            String fullPath = JavaxWsRsApiHelper.getPath(rootPath, methodPath, null);
            if (!resourcePath.pathQueue.offer(fullPath)) {
                AgentBridge.getAgent().getLogger().log(Level.FINE, "JAX-RS Subresource naming queue is full.");
                resourcePath.pathQueue.clear();
            }
        }
    }
}
Also used : Path(javax.ws.rs.Path) HEAD(javax.ws.rs.HEAD) Transaction(com.newrelic.agent.bridge.Transaction) GET(javax.ws.rs.GET) PUT(javax.ws.rs.PUT) WeaveIntoAllMethods(com.newrelic.api.agent.weaver.WeaveIntoAllMethods) Trace(com.newrelic.api.agent.Trace) WeaveWithAnnotation(com.newrelic.api.agent.weaver.WeaveWithAnnotation)

Example 19 with Transaction

use of com.newrelic.agent.bridge.Transaction in project newrelic-java-agent by newrelic.

the class AgentPostprocessorsTest method testApiTestClass.

private void testApiTestClass(Class<?> clazz, WeavePackageType weavePackageType) throws Exception {
    ThreadLocal<WeavePackageType> weavePackageTypeSpy = new ThreadLocal<WeavePackageType>() {

        @Override
        protected WeavePackageType initialValue() {
            return null;
        }
    };
    AgentBridge.currentApiSource = spy(weavePackageTypeSpy);
    ApiTestClass testClass = (ApiTestClass) clazz.newInstance();
    // Token API
    assertBeforeApi();
    Token token = testClass.getToken();
    assertNotNull(token);
    assertAfterApi(1, weavePackageType);
    assertBeforeApi();
    com.newrelic.api.agent.Transaction transaction = testClass.getTransaction();
    assertNotNull(transaction);
    assertAfterApi(0, weavePackageType);
    assertBeforeApi();
    testClass.getTokenNoReturn();
    assertAfterApi(2, weavePackageType);
    assertBeforeApi();
    testClass.getTokenNoReturnException();
    assertAfterApi(2, weavePackageType);
    assertBeforeApi();
    testClass.getTokenSeparateLinkExpireNoReturn();
    assertAfterApi(3, weavePackageType);
    assertBeforeApi();
    testClass.getTokenSeparateLinkExpireNoReturnException();
    assertAfterApi(3, weavePackageType);
    // Token API (Bridge)
    assertBeforeApi();
    com.newrelic.agent.bridge.Token tokenBridge = testClass.getTokenBridge();
    assertNotNull(tokenBridge);
    assertAfterApi(1, weavePackageType);
    assertBeforeApi();
    Transaction transactionBridge = testClass.getTransactionBridge();
    assertNotNull(transactionBridge);
    assertAfterApi(0, weavePackageType);
    assertBeforeApi();
    testClass.getTokenNoReturnBridge();
    assertAfterApi(2, weavePackageType);
    assertBeforeApi();
    testClass.getTokenNoReturnExceptionBridge();
    assertAfterApi(2, weavePackageType);
    assertBeforeApi();
    testClass.getTokenSeparateLinkExpireNoReturnBridge();
    assertAfterApi(3, weavePackageType);
    assertBeforeApi();
    testClass.getTokenSeparateLinkExpireNoReturnExceptionBridge();
    assertAfterApi(3, weavePackageType);
    // Segment API
    assertBeforeApi();
    testClass.startSegmentNoArgsWithTx(NewRelic.getAgent().getTransaction());
    assertAfterApi(2, weavePackageType);
    assertBeforeApi();
    testClass.startSegmentNoArgs();
    assertAfterApi(1, weavePackageType);
    assertBeforeApi();
    testClass.startSegmentNoArgsNoReturn();
    assertAfterApi(3, weavePackageType);
    assertBeforeApi();
    testClass.startSegmentNoArgsNoReturnException();
    assertAfterApi(3, weavePackageType);
    assertBeforeApi();
    testClass.startSegmentArgs();
    assertAfterApi(1, weavePackageType);
    assertBeforeApi();
    testClass.startSegmentArgsNoReturn();
    assertAfterApi(2, weavePackageType);
    assertBeforeApi();
    testClass.startSegmentArgsNoReturnException();
    assertAfterApi(2, weavePackageType);
    assertBeforeApi();
    testClass.startSegmentNoArgsWithGetToken();
    assertAfterApi(2, weavePackageType);
    assertBeforeApi();
    testClass.startSegmentNoArgsNoReturnWithGetToken();
    assertAfterApi(3, weavePackageType);
    assertBeforeApi();
    testClass.startSegmentNoArgsNoReturnExceptionWithGetToken();
    assertAfterApi(3, weavePackageType);
    assertBeforeApi();
    testClass.startSegmentArgsWithGetToken();
    assertAfterApi(2, weavePackageType);
    assertBeforeApi();
    testClass.startSegmentArgsNoReturnWithGetToken();
    assertAfterApi(4, weavePackageType);
    assertBeforeApi();
    testClass.startSegmentArgsNoReturnExceptionWithGetToken();
    assertAfterApi(3, weavePackageType);
    // TracedActivity API (Bridge)
    assertBeforeApi();
    testClass.createAndStartTracedActivity();
    assertAfterApi(1, weavePackageType);
    assertBeforeApi();
    testClass.createAndStartTracedActivityNoReturn();
    assertAfterApi(1, weavePackageType);
    assertBeforeApi();
    testClass.createAndStartTracedActivityNoReturnException();
    assertAfterApi(1, weavePackageType);
    // NewRelic API
    assertBeforeApi();
    testClass.noticeErrorWithStringAndEmptyMap();
    assertAfterApi(1, weavePackageType);
    assertBeforeApi();
    testClass.noticeErrorWithStringAndMap();
    assertAfterApi(1, weavePackageType);
    assertBeforeApi();
    testClass.noticeErrorWithThrowableAndEmptyMap();
    assertAfterApi(1, weavePackageType);
    assertBeforeApi();
    testClass.noticeErrorWithThrowableAndMap();
    assertAfterApi(1, weavePackageType);
    assertBeforeApi();
    testClass.noticeErrorExpectedWithStringAndEmptyMap();
    assertAfterApi(1, weavePackageType);
    assertBeforeApi();
    testClass.noticeErrorExpectedWithStringAndMap();
    assertAfterApi(1, weavePackageType);
    assertBeforeApi();
    testClass.noticeErrorExpectedWithThrowableAndEmptyMap();
    assertAfterApi(1, weavePackageType);
    assertBeforeApi();
    testClass.noticeErrorExpectedWithThrowableAndMap();
    assertAfterApi(1, weavePackageType);
    assertBeforeApi();
    testClass.addCustomParameter();
    assertAfterApi(2, weavePackageType);
    assertBeforeApi();
    testClass.ignoreApdexNewRelic();
    assertAfterApi(1, weavePackageType);
    assertBeforeApi();
    testClass.ignoreTransaction();
    assertAfterApi(1, weavePackageType);
    assertBeforeApi();
    testClass.setAppServerPort();
    assertAfterApi(1, weavePackageType);
    assertBeforeApi();
    testClass.setInstanceName();
    assertAfterApi(1, weavePackageType);
    assertBeforeApi();
    testClass.setServerInfo();
    assertAfterApi(1, weavePackageType);
    assertBeforeApi();
    testClass.setTransactionNameNewRelic();
    assertAfterApi(1, weavePackageType);
    assertBeforeApi();
    testClass.setUserName();
    assertAfterApi(1, weavePackageType);
    assertBeforeApi();
    testClass.setProductName();
    assertAfterApi(1, weavePackageType);
    assertBeforeApi();
    testClass.setAccountName();
    assertAfterApi(1, weavePackageType);
    // Transaction API
    assertBeforeApi();
    testClass.ignore();
    assertAfterApi(1, weavePackageType);
    assertBeforeApi();
    testClass.ignoreApdexTransaction();
    assertAfterApi(1, weavePackageType);
    assertBeforeApi();
    testClass.processRequestMetadata();
    assertAfterApi(1, weavePackageType);
    assertBeforeApi();
    testClass.processResponseMetadataWithURI();
    assertAfterApi(1, weavePackageType);
    assertBeforeApi();
    testClass.processResponseMetadata();
    assertAfterApi(1, weavePackageType);
    assertBeforeApi();
    testClass.setTransactionName();
    assertAfterApi(1, weavePackageType);
    // Transaction API (Bridge)
    assertBeforeApi();
    testClass.setTransactionNameBridge();
    assertAfterApi(1, weavePackageType);
    // TracedMethod API
    assertBeforeApi();
    testClass.reportAsExternal();
    assertAfterApi(1, weavePackageType);
    // TracedMethod API (Bridge)
    assertBeforeApi();
    testClass.reportAsExternalBridge();
    assertAfterApi(1, weavePackageType);
    // Insights API
    assertBeforeApi();
    testClass.recordCustomEvent();
    assertAfterApi(1, weavePackageType);
    // Public API
    assertBeforeApi();
    testClass.publicApiCustomParameter();
    assertAfterApi(2, weavePackageType);
    assertBeforeApi();
    testClass.publicApiBrowserHeaderFooter();
    assertAfterApi(0, weavePackageType);
    assertBeforeApi();
    testClass.publicApiIgnoreApdex();
    assertAfterApi(1, weavePackageType);
    assertBeforeApi();
    testClass.publicApiIgnoreTransaction();
    assertAfterApi(1, weavePackageType);
    assertBeforeApi();
    testClass.publicApiNoticeError();
    assertAfterApi(8, weavePackageType);
    assertBeforeApi();
    testClass.publicApiSetAccountName();
    assertAfterApi(1, weavePackageType);
    assertBeforeApi();
    testClass.publicApiSetAppServerPort();
    assertAfterApi(1, weavePackageType);
    assertBeforeApi();
    testClass.publicApiSetInstanceName();
    assertAfterApi(1, weavePackageType);
    assertBeforeApi();
    testClass.publicApiSetProductName();
    assertAfterApi(1, weavePackageType);
    assertBeforeApi();
    testClass.publicApiSetRequestAndResponse();
    assertAfterApi(0, weavePackageType);
    assertBeforeApi();
    testClass.publicApiSetServerInfo();
    assertAfterApi(1, weavePackageType);
    assertBeforeApi();
    testClass.publicApiSetTransactionName();
    assertAfterApi(1, weavePackageType);
    assertBeforeApi();
    testClass.publicApiSetUserName();
    assertAfterApi(1, weavePackageType);
    // Private API
    assertBeforeApi();
    testClass.privateApiCustomAttribute();
    assertAfterApi(3, weavePackageType);
    assertBeforeApi();
    testClass.privateApiMBeanServer();
    assertAfterApi(0, weavePackageType);
    assertBeforeApi();
    testClass.privateApiSampler();
    assertAfterApi(0, weavePackageType);
    assertBeforeApi();
    testClass.privateApiTracerParameter();
    assertAfterApi(0, weavePackageType);
    assertBeforeApi();
    testClass.privateApiReportError();
    assertAfterApi(0, weavePackageType);
    assertBeforeApi();
    testClass.privateApiSetServerInfo();
    assertAfterApi(1, weavePackageType);
    assertBeforeApi();
    testClass.privateApiSetAppServerPort();
    assertAfterApi(1, weavePackageType);
    assertBeforeApi();
    testClass.privateApiSetInstanceName();
    assertAfterApi(1, weavePackageType);
    assertBeforeApi();
    boolean exceptionThrown = false;
    try {
        testClass.throwExceptionTest();
    } catch (IOException e) {
        exceptionThrown = true;
    }
    if (!exceptionThrown) {
        fail("Exception was expected but not thrown");
    }
    assertAfterApi(1, exceptionThrown ? 0 : 1, weavePackageType);
}
Also used : WeavePackageType(com.newrelic.api.agent.weaver.internal.WeavePackageType) Token(com.newrelic.api.agent.Token) IOException(java.io.IOException) Transaction(com.newrelic.agent.bridge.Transaction)

Example 20 with Transaction

use of com.newrelic.agent.bridge.Transaction in project newrelic-java-agent by newrelic.

the class MetricState method getInputStreamPreamble.

public void getInputStreamPreamble(boolean isConnected, HttpURLConnection connection, TracedMethod method) {
    Transaction tx = AgentBridge.getAgent().getTransaction(false);
    if (method.isMetricProducer() && tx != null) {
        if (!recordedANetworkCall) {
            this.recordedANetworkCall = true;
            makeMetric(connection, method, "getInputStream");
        }
        if (!isConnected) {
            tx.getCrossProcessState().processOutboundRequestHeaders(new OutboundWrapper(connection), method);
        }
    }
}
Also used : Transaction(com.newrelic.agent.bridge.Transaction)

Aggregations

Transaction (com.newrelic.agent.bridge.Transaction)40 Trace (com.newrelic.api.agent.Trace)18 TracedMethod (com.newrelic.agent.bridge.TracedMethod)7 AgentBridge (com.newrelic.agent.bridge.AgentBridge)4 WeaveIntoAllMethods (com.newrelic.api.agent.weaver.WeaveIntoAllMethods)4 WeaveWithAnnotation (com.newrelic.api.agent.weaver.WeaveWithAnnotation)4 URI (java.net.URI)4 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)4 Segment (com.newrelic.api.agent.Segment)3 Label (org.objectweb.asm.Label)3 Method (org.objectweb.asm.commons.Method)3 TracedMethod (com.newrelic.api.agent.TracedMethod)2 MuleHttpConnectorRequest (com.nr.agent.instrumentation.mule3.MuleHttpConnectorRequest)2 OutboundWrapper (com.nr.instrumentation.jersey.client.OutboundWrapper)2 IOException (java.io.IOException)2 URISyntaxException (java.net.URISyntaxException)2 GET (javax.ws.rs.GET)2 HEAD (javax.ws.rs.HEAD)2 PUT (javax.ws.rs.PUT)2 Test (org.junit.Test)2