Search in sources :

Example 21 with TransactionData

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

the class DistributedTraceServiceImplTest method txnFinishedInboundPayload.

@Test
public void txnFinishedInboundPayload() {
    Map<String, Object> connectInfo = new HashMap<>();
    connectInfo.put(DistributedTracingConfig.ACCOUNT_ID, "1acct234");
    connectInfo.put(DistributedTracingConfig.TRUSTED_ACCOUNT_KEY, "67890");
    AgentConfig agentConfig = AgentHelper.createAgentConfig(true, Collections.<String, Object>emptyMap(), connectInfo);
    distributedTraceService.connected(null, agentConfig);
    TransactionStats transactionStats = new TransactionStats();
    Map<String, Object> intrinsicAttributes = new HashMap<>();
    long payloadSendTimestamp = 1002000;
    long startTimeInMillis = 1005000;
    long responseTimeInNanos = TimeUnit.MILLISECONDS.toNanos(1350);
    String json = new DistributedTracePayloadBuilder().setTimestamp(payloadSendTimestamp).setHost("datanerd.us.com").setParentType("Browser").setAccountId("123456").setTrustKey("67890").setApplicationId("6789").setTransactionId("badcafe3").setTripId("cattrip").setPriority(0.0f).setDepth(0).setSyntheticsJob(null).setSyntheticsMonitor(null).setSyntheticsResource(null).createJsonPayload();
    DistributedTracePayloadImpl payload = new DistributedTracePayloadParser(NewRelic.getAgent().getMetricAggregator(), distributedTraceService, Agent.LOG).parse(null, json);
    TransactionData transactionData = createTransactionData(intrinsicAttributes, startTimeInMillis, responseTimeInNanos, payload);
    distributedTraceService.dispatcherTransactionFinished(transactionData, transactionStats);
    SimpleStatsEngine unscopedStats = transactionStats.getUnscopedStats();
    assertEquals(1.35f, unscopedStats.getOrCreateResponseTimeStats("DurationByCaller/Browser/123456/6789/HTTPS/all").getTotal(), 0.01f);
    assertEquals(3.0f, unscopedStats.getOrCreateResponseTimeStats("TransportDuration/Browser/123456/6789/HTTPS/all").getTotal(), 0.01f);
}
Also used : AgentConfig(com.newrelic.agent.config.AgentConfig) TransactionStats(com.newrelic.agent.stats.TransactionStats) DistributedTracePayloadBuilder(com.newrelic.agent.DistributedTracePayloadBuilder) HashMap(java.util.HashMap) SimpleStatsEngine(com.newrelic.agent.stats.SimpleStatsEngine) TransactionData(com.newrelic.agent.TransactionData) Test(org.junit.Test)

Example 22 with TransactionData

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

the class BoundedConcurrentCacheTest method testMultithreadedBehavior.

@Test
public void testMultithreadedBehavior() throws Exception {
    RandomSql sql;
    SlowQueryInfo info;
    SqlTracer tracer;
    long duration;
    // fastest sql
    sql = createRandomSql();
    String fastestSql = sql.getSql();
    String fastestObfuscatedSql = sqlObfuscator.obfuscateSql(fastestSql);
    duration = 1;
    tracer = createSqlTracer(duration, sql);
    final TransactionData td = createTransactionData(0);
    info = new SlowQueryInfo(td, tracer, fastestSql, fastestObfuscatedSql, agentConfig.getSqlTraceConfig());
    info.aggregate(td, tracer);
    cache.putIfAbsent(fastestSql, info);
    // create 5 threads that each do similar work. This does its best to verify multithreaded behavior is safe
    Collection<Callable<Void>> slowSqlCallables = new ArrayList<>();
    final Object cacheClearLock = new Object();
    for (int i = 0; i < 5; i++) {
        final int current = i;
        slowSqlCallables.add(new Callable<Void>() {

            @Override
            public Void call() {
                // slower sql
                for (int i = 0; i < MAX_SIZE * 2; i++) {
                    if (current == 1 && i == 50) {
                        synchronized (cacheClearLock) {
                            cache.clear();
                        }
                    }
                    long duration = 10 + i;
                    RandomSql sql = createRandomSql();
                    String obfuscatedSql = sqlObfuscator.obfuscateSql(sql.getSql());
                    SqlTracer tracer = createSqlTracer(duration, sql);
                    SlowQueryInfo info = new SlowQueryInfo(td, tracer, sql.getSql(), obfuscatedSql, agentConfig.getSqlTraceConfig());
                    info.aggregate(td, tracer);
                    synchronized (cacheClearLock) {
                        cache.putIfAbsent(sql.getSql(), info);
                        Assert.assertNotNull(cache.get(sql.getSql()));
                    }
                    cache.putReplace(sql.getSql(), info);
                    if (cache.size() == 0) {
                        cache.putReplace(sql.getSql(), info);
                    }
                    Assert.assertTrue(cache.size() > 0);
                }
                return null;
            }
        });
    }
    ExecutorService executorService = Executors.newFixedThreadPool(5);
    List<Future<Void>> futures = executorService.invokeAll(slowSqlCallables);
    for (Future<Void> future : futures) {
        future.get(10000, TimeUnit.SECONDS);
    }
    // Verify that the multithreaded nature of the test did not affect the results
    Assert.assertEquals(null, cache.get(fastestObfuscatedSql));
    Assert.assertEquals(MAX_SIZE, cache.size());
    List<SlowQueryInfo> slowQueryInfos = cache.asList();
    Assert.assertNotNull(slowQueryInfos);
    Assert.assertEquals(MAX_SIZE, slowQueryInfos.size());
    for (SlowQueryInfo slowQueryInfo : slowQueryInfos) {
        // We shouldn't find the "fastest" sql in the slow query list
        Assert.assertNotEquals(fastestObfuscatedSql, slowQueryInfo.getObfuscatedQuery());
    }
    cache.clear();
    Assert.assertEquals(0, cache.size());
    slowQueryInfos = cache.asList();
    Assert.assertNotNull(slowQueryInfos);
    Assert.assertEquals(0, slowQueryInfos.size());
}
Also used : ArrayList(java.util.ArrayList) Callable(java.util.concurrent.Callable) ExecutorService(java.util.concurrent.ExecutorService) Future(java.util.concurrent.Future) DefaultSqlTracer(com.newrelic.agent.tracers.DefaultSqlTracer) SqlTracer(com.newrelic.agent.tracers.SqlTracer) TransactionData(com.newrelic.agent.TransactionData) Test(org.junit.Test)

Example 23 with TransactionData

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

the class SlowQueryAggregatorImplTest method testSqlTracerSizeCap.

@Test
public void testSqlTracerSizeCap() throws Exception {
    Map<String, Object> configMap = createStagingMap();
    createServiceManager(configMap);
    int sqlInfoCount;
    List<SqlTrace> tracers;
    // Upper bound
    for (int i = 0; i < SlowQueryAggregatorImpl.MAX_SLOW_QUERY_STATEMENTS + 1; ++i) {
        TransactionData td = createTransactionData(APP_NAME, "SizeCapTest", 50);
        this.aggregator.addSlowQueriesFromTransaction(td);
    }
    sqlInfoCount = aggregator.getSlowQueryCount();
    tracers = aggregator.getAndClearSlowQueries();
    Assert.assertNotNull(tracers);
    Assert.assertEquals(SlowQueryAggregatorImpl.SLOW_QUERY_LIMIT_PER_REPORTING_PERIOD, tracers.size());
    Assert.assertEquals(SlowQueryAggregatorImpl.MAX_SLOW_QUERY_STATEMENTS, sqlInfoCount);
    // Upper bound again
    for (int i = 0; i < SlowQueryAggregatorImpl.MAX_SLOW_QUERY_STATEMENTS + 1; ++i) {
        TransactionData td = createTransactionData(APP_NAME, "SizeCapTest", 50);
        this.aggregator.addSlowQueriesFromTransaction(td);
    }
    sqlInfoCount = aggregator.getSlowQueryCount();
    tracers = aggregator.getAndClearSlowQueries();
    Assert.assertNotNull(tracers);
    Assert.assertEquals(SlowQueryAggregatorImpl.SLOW_QUERY_LIMIT_PER_REPORTING_PERIOD, tracers.size());
    Assert.assertEquals(SlowQueryAggregatorImpl.MAX_SLOW_QUERY_STATEMENTS, sqlInfoCount);
}
Also used : TransactionData(com.newrelic.agent.TransactionData) Test(org.junit.Test)

Example 24 with TransactionData

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

the class SlowQueryInfoTest method testDTAttributes.

@Test
public void testDTAttributes() {
    Map<String, Object> settings = new HashMap<>();
    Map<String, Object> dtMap = new HashMap<>();
    dtMap.put("enabled", true);
    settings.put("distributed_tracing", dtMap);
    Map<String, Object> spanConfig = new HashMap<>();
    spanConfig.put("collect_span_events", true);
    settings.put("span_events", spanConfig);
    setupServiceManager(settings);
    DistributedTraceServiceImpl dts = (DistributedTraceServiceImpl) ServiceFactory.getServiceManager().getDistributedTraceService();
    Map<String, Object> configMap = ImmutableMap.<String, Object>builder().put("distributed_tracing", ImmutableMap.builder().put("primary_application_id", "1app23").put("trusted_account_key", "1tak23").put("account_id", "1acct234").build()).build();
    dts.connected(null, AgentConfigFactory.createAgentConfig(configMap, null, null));
    Transaction.clearTransaction();
    Transaction transaction = Transaction.getTransaction();
    transaction.getTransactionActivity().tracerStarted(new OtherRootTracer(transaction, new ClassMethodSignature("class", "method", "desc"), null, new SimpleMetricNameFormat("test")));
    String inboundPayload = "{\n" + "  \"v\": [\n" + "    0,\n" + "    2\n" + "  ],\n" + "  \"d\": {\n" + "    \"ty\": \"App\",\n" + "    \"ac\": \"1acct789\",\n" + "    \"ap\": \"1app23\",\n" + "    \"id\": \"27856f70d3d314b7\",\n" + "    \"tr\": \"3221bf09aa0bcf0d\",\n" + "    \"tk\": \"1tak23\",\n" + "    \"pr\": 1.0002,\n" + "    \"sa\": true,\n" + "    \"tx\": \"123456\",\n" + "    \"ti\": 1482959525577\n" + "  }\n" + "}";
    transaction.acceptDistributedTracePayload(inboundPayload);
    TransactionData data = new TransactionData(transaction, 100);
    Tracer tracer = new DefaultTracer(transaction, new ClassMethodSignature("ClassName", "methodName", "methodDesc"), null, null, TracerFlags.DISPATCHER);
    String obfuscatedQuery = "select ? from ?";
    HashMap<String, Object> sqlMap = new HashMap<>();
    ServerProp serverProp = ServerProp.createPropObject(SqlTraceConfigImpl.DEFAULT_USE_LONGER_SQL_ID);
    sqlMap.put(SqlTraceConfigImpl.USE_LONGER_SQL_ID, serverProp);
    SqlTraceConfig sqlTraceConfig = SqlTraceConfigImpl.createSqlTraceConfig(sqlMap);
    SlowQueryInfo slowQueryInfo = new SlowQueryInfo(data, tracer, "select * from person", obfuscatedQuery, sqlTraceConfig);
    assertNotNull(transaction.getSpanProxy().getInboundDistributedTracePayload());
    Map<String, Object> parameters = slowQueryInfo.getParameters();
    assertEquals("App", parameters.get("parent.type"));
    assertEquals("1app23", parameters.get("parent.app"));
    assertEquals("1acct789", parameters.get("parent.account"));
    assertNotNull(parameters.get("parent.transportType"));
    assertNotNull(parameters.get("parent.transportDuration"));
    assertNotNull(parameters.get("guid"));
    assertEquals("3221bf09aa0bcf0d", parameters.get("traceId"));
    assertNotNull(parameters.get("priority"));
    assertEquals(true, parameters.get("sampled"));
}
Also used : HashMap(java.util.HashMap) DistributedTraceServiceImpl(com.newrelic.agent.tracing.DistributedTraceServiceImpl) DefaultTracer(com.newrelic.agent.tracers.DefaultTracer) Tracer(com.newrelic.agent.tracers.Tracer) OtherRootTracer(com.newrelic.agent.tracers.OtherRootTracer) OtherRootTracer(com.newrelic.agent.tracers.OtherRootTracer) SqlTraceConfig(com.newrelic.agent.config.SqlTraceConfig) Transaction(com.newrelic.agent.Transaction) ClassMethodSignature(com.newrelic.agent.tracers.ClassMethodSignature) DefaultTracer(com.newrelic.agent.tracers.DefaultTracer) TransactionData(com.newrelic.agent.TransactionData) ServerProp(com.newrelic.agent.config.ServerProp) SimpleMetricNameFormat(com.newrelic.agent.tracers.metricname.SimpleMetricNameFormat) Test(org.junit.Test)

Example 25 with TransactionData

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

the class SlowQueryInfoTest method testLongerSqlId.

@Test
public void testLongerSqlId() {
    setupServiceManager(new HashMap<String, Object>());
    Transaction transaction = Transaction.getTransaction();
    HashMap<String, Object> sqlMap = new HashMap<>();
    ServerProp serverProp = ServerProp.createPropObject(!SqlTraceConfigImpl.DEFAULT_USE_LONGER_SQL_ID);
    sqlMap.put(SqlTraceConfigImpl.USE_LONGER_SQL_ID, serverProp);
    SqlTraceConfig sqlTraceConfig = SqlTraceConfigImpl.createSqlTraceConfig(sqlMap);
    MockDispatcher dispatcher = new MockDispatcher();
    dispatcher.setUri("http://jvm.agent.uri");
    transaction.setDispatcher(dispatcher);
    String obfuscatedQuery = "select ? from ?";
    TransactionData data = new TransactionData(transaction, 100);
    Tracer tracer = new DefaultTracer(transaction, new ClassMethodSignature("ClassName", "methodName", "methodDesc"), null, null, TracerFlags.DISPATCHER);
    SlowQueryInfo slowQueryInfo = new SlowQueryInfo(data, tracer, "select * from person", obfuscatedQuery, sqlTraceConfig);
    SqlTrace sqlTrace = slowQueryInfo.asSqlTrace();
    long hashedQuery = (long) obfuscatedQuery.hashCode();
    long longerHash = SlowQueryInfo.createLongerHashCode(hashedQuery);
    assertEquals(longerHash, sqlTrace.getId());
}
Also used : HashMap(java.util.HashMap) DefaultTracer(com.newrelic.agent.tracers.DefaultTracer) Tracer(com.newrelic.agent.tracers.Tracer) OtherRootTracer(com.newrelic.agent.tracers.OtherRootTracer) MockDispatcher(com.newrelic.agent.MockDispatcher) SqlTraceConfig(com.newrelic.agent.config.SqlTraceConfig) Transaction(com.newrelic.agent.Transaction) DefaultTracer(com.newrelic.agent.tracers.DefaultTracer) ClassMethodSignature(com.newrelic.agent.tracers.ClassMethodSignature) TransactionData(com.newrelic.agent.TransactionData) ServerProp(com.newrelic.agent.config.ServerProp) Test(org.junit.Test)

Aggregations

TransactionData (com.newrelic.agent.TransactionData)145 Test (org.junit.Test)136 TransactionDataList (com.newrelic.agent.TransactionDataList)46 TransactionStats (com.newrelic.agent.stats.TransactionStats)40 HashMap (java.util.HashMap)34 StatsEngine (com.newrelic.agent.stats.StatsEngine)29 Tracer (com.newrelic.agent.tracers.Tracer)23 OtherRootTracer (com.newrelic.agent.tracers.OtherRootTracer)17 ClassMethodSignature (com.newrelic.agent.tracers.ClassMethodSignature)16 Transaction (com.newrelic.agent.Transaction)15 EventTestHelper.generateTransactionData (com.newrelic.agent.service.analytics.EventTestHelper.generateTransactionData)15 DefaultTracer (com.newrelic.agent.tracers.DefaultTracer)15 JSONArray (org.json.simple.JSONArray)15 MockRPMService (com.newrelic.agent.MockRPMService)14 Map (java.util.Map)14 TransactionService (com.newrelic.agent.TransactionService)12 Java7IncompatibleTest (com.newrelic.test.marker.Java7IncompatibleTest)12 JSONObject (org.json.simple.JSONObject)12 TransactionDataTestBuilder (com.newrelic.agent.TransactionDataTestBuilder)10 SpanEvent (com.newrelic.agent.model.SpanEvent)10