Search in sources :

Example 1 with TransactionActivity

use of com.newrelic.agent.TransactionActivity in project newrelic-java-agent by newrelic.

the class TransactionProfile method transactionFinished.

public void transactionFinished(TransactionData transactionData) {
    final List<MetricNameTime> cpuTimes = new ArrayList<>();
    for (TransactionActivity activity : transactionData.getTransactionActivities()) {
        ThreadInfo threadInfo = threadMXBean.getThreadInfo(activity.getThreadId(), 0);
        if (null != threadInfo) {
            final List<List<StackTraceElement>> backtraces = new ArrayList<>();
            Map<Tracer, Collection<Tracer>> tracerTree = buildChildren(activity.getTracers(), backtraces);
            String threadName = threadNameNormalizer.getNormalizedThreadName(new BasicThreadInfo(threadInfo));
            threadActivityProfiles.get(threadName).add(activity, tracerTree);
            if (!backtraces.isEmpty()) {
                ProfileTree tree = threadProfiles.get(threadName);
                for (List<StackTraceElement> stack : backtraces) {
                    stack = DiscoveryProfile.getScrubbedStackTrace(stack);
                    Collections.reverse(stack);
                    tree.addStackTrace(stack, true);
                }
            }
            long cpuTime = activity.getTotalCpuTime();
            if (cpuTime > 0) {
                MetricName name = MetricName.create(transactionData.getBlameMetricName(), threadName);
                cpuTimes.add(new MetricNameTime(name, cpuTime));
            }
        }
    }
    if (!cpuTimes.isEmpty()) {
        ServiceFactory.getStatsService().doStatsWork(new StatsWork() {

            @Override
            public void doWork(StatsEngine statsEngine) {
                for (MetricNameTime time : cpuTimes) {
                    statsEngine.getResponseTimeStats(time.name).recordResponseTime(time.cpuTime, TimeUnit.NANOSECONDS);
                }
            }

            @Override
            public String getAppName() {
                return null;
            }
        }, transactionData.getBlameMetricName());
    }
}
Also used : DefaultTracer(com.newrelic.agent.tracers.DefaultTracer) Tracer(com.newrelic.agent.tracers.Tracer) ArrayList(java.util.ArrayList) TransactionActivity(com.newrelic.agent.TransactionActivity) StatsEngine(com.newrelic.agent.stats.StatsEngine) MetricName(com.newrelic.agent.metric.MetricName) ThreadInfo(java.lang.management.ThreadInfo) BasicThreadInfo(com.newrelic.agent.threads.BasicThreadInfo) StatsWork(com.newrelic.agent.stats.StatsWork) BasicThreadInfo(com.newrelic.agent.threads.BasicThreadInfo) Collection(java.util.Collection) ArrayList(java.util.ArrayList) List(java.util.List)

Example 2 with TransactionActivity

use of com.newrelic.agent.TransactionActivity in project newrelic-java-agent by newrelic.

the class TransactionTraceTest method excludeRootTraceFromTT.

/**
 * This test exercises an edge case in our public tracer api by setting excludeFromTransactionTrace = true on a root
 * tracer.
 *
 * The behavior is undefined, but in practice only the root tracer's segment is preserved in the TT. Scala
 * instrumentation is relying on this behavior by only showing the root scala future but not the consecutive calls.
 */
@Test
public void excludeRootTraceFromTT() throws Exception {
    setUp(true, true, false);
    Transaction tx = Transaction.getTransaction(true);
    TransactionActivity txa = TransactionActivity.get();
    // make sure the test env is sane
    Assert.assertNotNull(tx);
    Assert.assertNotNull(txa);
    Assert.assertEquals(tx, txa.getTransaction());
    final ClassMethodSignature sig = new ClassMethodSignature("Test", "dude", "()V");
    final MetricNameFormat metricNameFormat = new SimpleMetricNameFormat("Test.dude", "Test.dude");
    // @Trace( excludeFromTransactionTrace = true )
    final int excludeFromTTFlags = TracerFlags.GENERATE_SCOPED_METRIC;
    // @Trace
    final int defaultFlags = TracerFlags.GENERATE_SCOPED_METRIC | TracerFlags.TRANSACTION_TRACER_SEGMENT;
    DefaultTracer root = new OtherRootTracer(txa, sig, new Object(), metricNameFormat, excludeFromTTFlags, System.currentTimeMillis());
    txa.addTracer(root);
    Assert.assertEquals(txa, root.getTransactionActivity());
    Assert.assertEquals(tx, root.getTransaction());
    final int numChildren = 10;
    for (int i = 0; i < numChildren; ++i) {
        DefaultTracer child = new DefaultTracer(txa, sig, new Object(), metricNameFormat, excludeFromTTFlags, System.currentTimeMillis());
        txa.addTracer(child);
        child.finish(0, null);
        // child honor the flags and do not make a tt segment
        Assert.assertFalse(child.isTransactionSegment());
    }
    root.finish(0, null);
    Assert.assertEquals("Java/java.lang.Object/dude", TransactionSegment.getMetricName(root));
}
Also used : Transaction(com.newrelic.agent.Transaction) ClassMethodSignature(com.newrelic.agent.tracers.ClassMethodSignature) DefaultTracer(com.newrelic.agent.tracers.DefaultTracer) JSONObject(org.json.simple.JSONObject) TransactionActivity(com.newrelic.agent.TransactionActivity) SimpleMetricNameFormat(com.newrelic.agent.tracers.metricname.SimpleMetricNameFormat) MetricNameFormat(com.newrelic.agent.tracers.metricname.MetricNameFormat) OtherTransSimpleMetricNameFormat(com.newrelic.agent.tracers.metricname.OtherTransSimpleMetricNameFormat) SimpleMetricNameFormat(com.newrelic.agent.tracers.metricname.SimpleMetricNameFormat) OtherTransSimpleMetricNameFormat(com.newrelic.agent.tracers.metricname.OtherTransSimpleMetricNameFormat) OtherRootTracer(com.newrelic.agent.tracers.OtherRootTracer) Test(org.junit.Test)

Example 3 with TransactionActivity

use of com.newrelic.agent.TransactionActivity in project newrelic-java-agent by newrelic.

the class DefaultTracerTest method testExternalParameters.

@Test
public void testExternalParameters() throws URISyntaxException {
    TransactionActivity.clear();
    Transaction.clearTransaction();
    Transaction tx = Transaction.getTransaction();
    TransactionActivity txa = TransactionActivity.get();
    Tracer root = new OtherRootTracer(tx, new ClassMethodSignature("com.newrelic.agent.TracedActivityTest", "makeTransaction", "()V"), null, DefaultTracer.NULL_METRIC_NAME_FORMATTER);
    txa.tracerStarted(root);
    final TransactionStats stats = root.getTransactionActivity().getTransactionStats();
    // http external
    final String library = "unittest";
    final URI uri = new URI("http://localhost");
    final String host = uri.getHost();
    final String procedure = "connect";
    int externalCount = 0;
    {
        // generic external
        DefaultTracer tracer = (DefaultTracer) AgentBridge.instrumentation.createTracer(null, 0, "iamyourchild", DefaultTracer.DEFAULT_TRACER_FLAGS);
        assertExternal(stats, externalCount, host, library, procedure);
        // multiple calls to addExternalParameters should only apply the last call
        tracer.reportAsExternal(GenericParameters.library(library).uri(uri).procedure(procedure).build());
        tracer.reportAsExternal(GenericParameters.library(library).uri(uri).procedure(procedure).build());
        tracer.reportAsExternal(GenericParameters.library(library).uri(uri).procedure(procedure).build());
        // metrics should only be recorded when the tracer finishes
        assertExternal(stats, externalCount, host, library, procedure);
        assertEquals(externalCount, stats.getScopedStats().getOrCreateResponseTimeStats("External/" + host + "/" + library + "/" + procedure).getCallCount());
        tracer.finish(0, null);
        externalCount++;
        assertExternal(stats, externalCount, host, library, procedure);
        assertEquals(externalCount, stats.getScopedStats().getOrCreateResponseTimeStats("External/" + host + "/" + library + "/" + procedure).getCallCount());
    }
    final DatastoreVendor vendor = DatastoreVendor.MySQL;
    final String collection = "stores";
    final String operation = "select";
    final int port = 666;
    int datastoreCount = 0;
    {
        // datastore
        DefaultTracer tracer = (DefaultTracer) AgentBridge.instrumentation.createTracer(null, 0, "iamyourchild", DefaultTracer.DEFAULT_TRACER_FLAGS);
        tracer.reportAsExternal(GenericParameters.library(library).uri(uri).procedure(procedure).build());
        tracer.reportAsExternal(DatastoreParameters.product(vendor.toString()).collection(collection).operation(operation).instance(host, port).build());
        assertDatastore(stats, datastoreCount, vendor.toString(), collection, operation, host, port);
        tracer.finish(0, null);
        datastoreCount++;
        assertDatastore(stats, datastoreCount, vendor.toString(), collection, operation, host, port);
        // http external should be unchanged
        assertExternal(stats, externalCount, host, library, procedure);
    }
    {
        // http + DT
        DefaultTracer tracer = (DefaultTracer) AgentBridge.instrumentation.createTracer(null, 0, "iamyourchild", DefaultTracer.DEFAULT_TRACER_FLAGS);
        tracer.addOutboundRequestHeaders(new Outbound());
        tracer.reportAsExternal(GenericParameters.library(library).uri(uri).procedure(procedure).build());
        tracer.reportAsExternal(HttpParameters.library(library).uri(uri).procedure(procedure).inboundHeaders(new Inbound("Foo")).build());
        assertCat(tracer, false);
        assertExternal(stats, externalCount, host, library, procedure);
        tracer.finish(0, null);
        externalCount++;
        // DT is enabled, there should not be any CAT
        assertCat(tracer, false);
        // ExternalTransaction/localhost/12345/Foo
        assertExternal(stats, externalCount, host, library, procedure);
        assertEquals(0, stats.getScopedStats().getOrCreateResponseTimeStats("ExternalTransaction/" + host + "/12345/Foo").getCallCount());
    }
    {
        // last inboundHeaders win
        DefaultTracer tracer = (DefaultTracer) AgentBridge.instrumentation.createTracer(null, 0, "iamyourchild", DefaultTracer.DEFAULT_TRACER_FLAGS);
        tracer.addOutboundRequestHeaders(new Outbound());
        tracer.reportAsExternal(GenericParameters.library(library).uri(uri).procedure(procedure).build());
        tracer.reportAsExternal(HttpParameters.library(library).uri(uri).procedure(procedure).inboundHeaders(new Inbound("Foo")).build());
        // headers trump the previous call
        tracer.readInboundResponseHeaders(new Inbound("Bar"));
        assertCat(tracer, false);
        assertExternal(stats, externalCount, host, library, procedure);
        tracer.finish(0, null);
        externalCount++;
        // DT is enabled, there should not be any CAT
        assertCat(tracer, false);
        // ExternalTransaction/localhost/12345/Foo
        assertExternal(stats, externalCount, host, library, procedure);
        assertEquals(0, stats.getScopedStats().getOrCreateResponseTimeStats("ExternalTransaction/" + host + "/12345/Bar").getCallCount());
    }
    {
        // set headers manually
        DefaultTracer tracer = (DefaultTracer) AgentBridge.instrumentation.createTracer(null, 0, "iamyourchild", DefaultTracer.DEFAULT_TRACER_FLAGS);
        tracer.addOutboundRequestHeaders(new Outbound());
        tracer.reportAsExternal(GenericParameters.library(library).uri(uri).procedure(procedure).build());
        // headers trump the previous call
        tracer.readInboundResponseHeaders(new Inbound("Baz"));
        assertCat(tracer, false);
        assertExternal(stats, externalCount, host, library, procedure);
        tracer.finish(0, null);
        externalCount++;
        // DT is enabled, there should not be any CAT
        assertCat(tracer, false);
        // ExternalTransaction/localhost/12345/Foo
        assertExternal(stats, externalCount, host, library, procedure);
        assertEquals(0, stats.getScopedStats().getOrCreateResponseTimeStats("ExternalTransaction/" + host + "/12345/Baz").getCallCount());
    }
    root.finish(0, null);
}
Also used : TransactionActivity(com.newrelic.agent.TransactionActivity) URI(java.net.URI) DatastoreVendor(com.newrelic.agent.bridge.datastore.DatastoreVendor) TransactionStats(com.newrelic.agent.stats.TransactionStats) Transaction(com.newrelic.agent.Transaction) Test(org.junit.Test)

Example 4 with TransactionActivity

use of com.newrelic.agent.TransactionActivity in project newrelic-java-agent by newrelic.

the class BoundedConcurrentCacheTest method before.

@Before
public void before() throws Exception {
    Map<String, Object> configMap = createStagingMap();
    createServiceManager(configMap);
    agentConfig = ServiceFactory.getConfigService().getDefaultAgentConfig();
    MockDispatcher dispatcher = new MockDispatcher();
    dispatcher.setWebTransaction(true);
    MockDispatcherTracer rootTracer = new MockDispatcherTracer();
    rootTracer.setDurationInMilliseconds(3000);
    rootTracer.setStartTime(System.nanoTime());
    rootTracer.setEndTime(rootTracer.getStartTime() + TimeUnit.NANOSECONDS.convert(3000, TimeUnit.MILLISECONDS));
    TransactionTracerConfig ttconf = Mockito.mock(TransactionTracerConfig.class);
    when(ttconf.isEnabled()).thenReturn(true);
    when(ttconf.getInsertSqlMaxLength()).thenReturn(10 * 1000);
    tx = mock(Transaction.class);
    TransactionActivity txa = mock(TransactionActivity.class);
    when(txa.getTransaction()).thenReturn(tx);
    when(tx.getTransactionActivity()).thenReturn(txa);
    when(tx.getTransactionTracerConfig()).thenReturn(ttconf);
    cache = new BoundedConcurrentCache<>(MAX_SIZE);
    sqlObfuscator = ServiceFactory.getDatabaseService().getDefaultSqlObfuscator();
}
Also used : Transaction(com.newrelic.agent.Transaction) MockDispatcherTracer(com.newrelic.agent.MockDispatcherTracer) MockDispatcher(com.newrelic.agent.MockDispatcher) TransactionActivity(com.newrelic.agent.TransactionActivity) TransactionTracerConfig(com.newrelic.agent.config.TransactionTracerConfig) Before(org.junit.Before)

Example 5 with TransactionActivity

use of com.newrelic.agent.TransactionActivity in project newrelic-java-agent by newrelic.

the class AsyncTest method verifyCpu.

public void verifyCpu(long minCpu) {
    Assert.assertNotNull(data);
    Assert.assertNotNull(data.getIntrinsicAttributes().get(AttributeNames.CPU_TIME_PARAMETER_NAME));
    Long val = (Long) data.getIntrinsicAttributes().get(AttributeNames.CPU_TIME_PARAMETER_NAME);
    Assert.assertTrue("The cpu should be greater than 0", val > 0);
    Assert.assertTrue("The cpu should be greater than the expeted min value " + minCpu, val > minCpu);
    long cpuTime = 0L;
    Collection<Tracer> tracers = new HashSet<>();
    tracers.add(data.getRootTracer());
    tracers.addAll(data.getTracers());
    Collection<TransactionActivity> txas = new HashSet<>();
    // collect all txas for the transaction
    for (Tracer current : tracers) {
        if (current instanceof TransactionActivityInitiator) {
            txas.add(((DefaultTracer) current).getTransactionActivity());
        }
    }
    for (TransactionActivity txa : txas) {
        cpuTime += txa.getTotalCpuTime();
    }
    Assert.assertEquals("The cpu should be sum of txa cpus ", cpuTime, val.longValue());
}
Also used : TransactionActivityInitiator(com.newrelic.agent.tracers.TransactionActivityInitiator) DefaultTracer(com.newrelic.agent.tracers.DefaultTracer) Tracer(com.newrelic.agent.tracers.Tracer) TransactionActivity(com.newrelic.agent.TransactionActivity) HashSet(java.util.HashSet)

Aggregations

TransactionActivity (com.newrelic.agent.TransactionActivity)19 Transaction (com.newrelic.agent.Transaction)14 Test (org.junit.Test)8 Tracer (com.newrelic.agent.tracers.Tracer)6 DefaultTracer (com.newrelic.agent.tracers.DefaultTracer)5 AgentBridge (com.newrelic.agent.bridge.AgentBridge)4 ClassMethodSignature (com.newrelic.agent.tracers.ClassMethodSignature)4 OtherRootTracer (com.newrelic.agent.tracers.OtherRootTracer)4 MetricNameFormat (com.newrelic.agent.tracers.metricname.MetricNameFormat)3 ArrayList (java.util.ArrayList)3 MockDispatcher (com.newrelic.agent.MockDispatcher)2 MockDispatcherTracer (com.newrelic.agent.MockDispatcherTracer)2 ExitTracer (com.newrelic.agent.bridge.ExitTracer)2 NoOpToken (com.newrelic.agent.bridge.NoOpToken)2 NoOpTransaction (com.newrelic.agent.bridge.NoOpTransaction)2 Token (com.newrelic.agent.bridge.Token)2 TransactionTracerConfig (com.newrelic.agent.config.TransactionTracerConfig)2 DefaultSqlTracer (com.newrelic.agent.tracers.DefaultSqlTracer)2 OtherRootSqlTracer (com.newrelic.agent.tracers.OtherRootSqlTracer)2 TransactionActivityInitiator (com.newrelic.agent.tracers.TransactionActivityInitiator)2