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());
}
}
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));
}
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);
}
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();
}
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());
}
Aggregations