Search in sources :

Example 11 with TransactionSegment

use of com.newrelic.agent.trace.TransactionSegment in project newrelic-java-agent by newrelic.

the class TransactionTraceImpl method createTransactionTrace.

public static TransactionTraceImpl createTransactionTrace(TransactionTrace trace) throws Exception {
    TransactionSegment funnyRootNode = trace.getRootSegment();
    Collection<TransactionSegment> segments = funnyRootNode.getChildren();
    // transaction wall clock time. This node should always only have one child which is the real root node.
    if (segments.size() != 1) {
        throw new Exception("The transaction segment has an invalid format. The ROOT node should only have one child.");
    }
    TraceSegmentImpl rootSeg = TraceSegmentImpl.createTraceSegment(trace, segments.iterator().next());
    TransactionTraceImpl output = new TransactionTraceImpl();
    output.startTime = trace.getStartTime();
    // the duration on the trace is ms - converting to sec
    output.responseTimeSec = (trace.getDuration() / 1000.0f);
    output.rootSegment = rootSeg;
    // end time in ms - converting to sec
    output.wallClockDurationSec = (funnyRootNode.getEndTime() / 1000.0f);
    output.intrinsics = Maps.newHashMap(trace.getIntrinsicsShallowCopy());
    return output;
}
Also used : TransactionSegment(com.newrelic.agent.trace.TransactionSegment)

Example 12 with TransactionSegment

use of com.newrelic.agent.trace.TransactionSegment in project newrelic-java-agent by newrelic.

the class JsonTraceSegment method validateTraceSegment.

public void validateTraceSegment(TransactionSegment actualSegment) {
    // System.out.println(DataSenderWriter.toJSONString(actualSegment));
    String message = fileName + " \"" + testName + "\" " + segName + " Invalid ";
    Assert.assertEquals(message + "startime", start, actualSegment.getStartTime());
    Assert.assertEquals(message + "endtime", end, actualSegment.getEndTime());
    Assert.assertEquals(message + "segname", segName, actualSegment.getMetricName());
    Assert.assertEquals(message + "className", className, actualSegment.getClassName());
    Assert.assertEquals(message + "methodname", methodName, actualSegment.getMethodName());
    Assert.assertEquals(message + "request param size", requestParams.size(), actualSegment.getTraceParameters().size());
    for (Entry<String, Object> current : requestParams.entrySet()) {
        Object actual = actualSegment.getTraceParameters().get(current.getKey());
        Assert.assertNotNull(actual);
        // star means we are not validating the actual value
        if (!current.getValue().equals("*")) {
            Assert.assertEquals(fileName + "\"" + testName + "\" Att " + current.getKey() + " has invalid value on segment " + segName, current.getValue(), actual);
        }
    }
    Assert.assertEquals(message + "children size", children.size(), actualSegment.getChildren().size());
    List<TransactionSegment> sortedActual = new ArrayList<>(actualSegment.getChildren());
    Collections.sort(sortedActual, new Comparator<TransactionSegment>() {

        @Override
        public int compare(TransactionSegment first, TransactionSegment second) {
            if (first.getStartTime() == second.getStartTime()) {
                return String.CASE_INSENSITIVE_ORDER.compare(first.getMetricName(), second.getMetricName());
            } else if (first.getStartTime() > second.getStartTime()) {
                return 1;
            } else {
                return -1;
            }
        }
    });
    List<JsonTraceSegment> sortedExpected = new ArrayList<>(children);
    Collections.sort(sortedExpected, new Comparator<JsonTraceSegment>() {

        @Override
        public int compare(JsonTraceSegment first, JsonTraceSegment second) {
            if (first.start == second.start) {
                return String.CASE_INSENSITIVE_ORDER.compare(first.segName, second.segName);
            } else if (first.start > second.start) {
                return 1;
            } else {
                return -1;
            }
        }
    });
    for (int i = 0; i < sortedExpected.size(); i++) {
        sortedExpected.get(i).validateTraceSegment(sortedActual.get(i));
    }
}
Also used : TransactionSegment(com.newrelic.agent.trace.TransactionSegment) ArrayList(java.util.ArrayList) JSONObject(org.json.simple.JSONObject)

Example 13 with TransactionSegment

use of com.newrelic.agent.trace.TransactionSegment in project newrelic-java-agent by newrelic.

the class JsonTransactionTrace method validateTransactionTrace.

public void validateTransactionTrace(TransactionTrace actualTrace) {
    String msg = fileName + " \"" + testName + "\"" + " Invalid ";
    Assert.assertEquals(msg + "duration", duration, actualTrace.getDuration());
    Assert.assertEquals(msg + "scope", scope, actualTrace.getRootMetricName());
    TransactionSegment actualRoot = actualTrace.getRootSegment();
    rootSegment.validateTraceSegment(actualRoot);
    // System.out.println(DataSenderWriter.toJSONString(actualTrace));
    Map<String, Object> actualInt = actualTrace.getIntrinsicsShallowCopy();
    Assert.assertEquals(intrinsics.size(), actualInt.size());
    for (Entry<String, Object> curr : intrinsics.entrySet()) {
        Object value = actualInt.get(curr.getKey());
        Assert.assertNotNull("value for " + curr.getKey() + " should not be null", value);
        // do not check value if it is a star
        if (!"*".equals(curr.getValue())) {
            if (curr.getValue() instanceof Number) {
                Assert.assertEquals(((Number) curr.getValue()).floatValue(), ((Number) value).floatValue(), .001);
            } else if (curr.getValue() instanceof String) {
                Assert.assertEquals(curr.getValue().toString(), value.toString());
            } else {
                Assert.assertEquals(curr.getValue(), value);
            }
        }
    }
}
Also used : TransactionSegment(com.newrelic.agent.trace.TransactionSegment) JSONObject(org.json.simple.JSONObject)

Example 14 with TransactionSegment

use of com.newrelic.agent.trace.TransactionSegment in project newrelic-java-agent by newrelic.

the class DefaultTracerTest method stackTrace.

@Test
public void stackTrace() throws Exception {
    Transaction transaction = Transaction.getTransaction();
    ClassMethodSignature sig = new ClassMethodSignature("java.lang.String", "valueof", "(J)Ljava/lang/String;");
    DefaultTracer tracer = new DefaultTracer(transaction, sig, "12345");
    tracer.storeStackTrace();
    SqlObfuscator sqlObfuscator = ServiceFactory.getDatabaseService().getDefaultSqlObfuscator();
    TransactionTracerConfig ttConfig = ServiceFactory.getConfigService().getDefaultAgentConfig().getTransactionTracerConfig();
    TransactionSegment segment = new TransactionSegment(ttConfig, sqlObfuscator, 0, tracer);
    JSONArray json = (JSONArray) AgentHelper.serializeJSON(segment);
    Map params = (Map) json.get(3);
    List backtrace = (List) params.get(DefaultSqlTracer.BACKTRACE_PARAMETER_NAME);
    Assert.assertTrue(backtrace.size() > 2);
}
Also used : TransactionSegment(com.newrelic.agent.trace.TransactionSegment) Transaction(com.newrelic.agent.Transaction) JSONArray(org.json.simple.JSONArray) SqlObfuscator(com.newrelic.agent.database.SqlObfuscator) List(java.util.List) TransactionTracerConfig(com.newrelic.agent.config.TransactionTracerConfig) Map(java.util.Map) ImmutableMap(com.google.common.collect.ImmutableMap) Test(org.junit.Test)

Example 15 with TransactionSegment

use of com.newrelic.agent.trace.TransactionSegment in project newrelic-java-agent by newrelic.

the class DefaultSqlTracerTest method testSqlInParameterMap.

@SuppressWarnings("unchecked")
@Test
public void testSqlInParameterMap() throws Exception {
    String inputSql = "select * from dudette";
    DefaultSqlTracer tracer = newTracer(inputSql);
    assertTrue(tracer.getAgentAttributes().isEmpty());
    SqlObfuscator sqlObfuscator = ServiceFactory.getDatabaseService().getDefaultSqlObfuscator();
    TransactionTracerConfig ttConfig = ServiceFactory.getConfigService().getDefaultAgentConfig().getTransactionTracerConfig();
    TransactionSegment segment = new TransactionSegment(ttConfig, sqlObfuscator, 0, tracer);
    // exclusive_duration_millis and sql
    assertEquals(2, tracer.getAgentAttributes().size());
    assertEquals(inputSql, (String) tracer.getAgentAttributes().get("sql"));
    JSONArray json = (JSONArray) AgentHelper.serializeJSON(segment);
    Map params = (Map) json.get(3);
    // the name changes when it is obfuscated
    String sqlStatement = (String) params.get("sql_obfuscated");
    assertEquals(inputSql, sqlStatement);
}
Also used : TransactionSegment(com.newrelic.agent.trace.TransactionSegment) JSONArray(org.json.simple.JSONArray) SqlObfuscator(com.newrelic.agent.database.SqlObfuscator) TransactionTracerConfig(com.newrelic.agent.config.TransactionTracerConfig) Test(org.junit.Test)

Aggregations

TransactionSegment (com.newrelic.agent.trace.TransactionSegment)16 TransactionTracerConfig (com.newrelic.agent.config.TransactionTracerConfig)8 SqlObfuscator (com.newrelic.agent.database.SqlObfuscator)8 Test (org.junit.Test)8 JSONArray (org.json.simple.JSONArray)6 JSONObject (org.json.simple.JSONObject)5 TransactionTrace (com.newrelic.agent.trace.TransactionTrace)3 LinkedList (java.util.LinkedList)3 AgentConfig (com.newrelic.agent.config.AgentConfig)2 TransactionStats (com.newrelic.agent.stats.TransactionStats)2 ImmutableMap (com.google.common.collect.ImmutableMap)1 Transaction (com.newrelic.agent.Transaction)1 ArrayList (java.util.ArrayList)1 List (java.util.List)1 Map (java.util.Map)1