Search in sources :

Example 16 with TransactionDataTestBuilder

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

the class TransactionTraceTest method serializeUserOnlyEnabled.

@SuppressWarnings("unchecked")
@Test
public void serializeUserOnlyEnabled() throws Exception {
    setUp(true, true, false);
    Tracer rootTracer = new TestMethodExitTracer();
    Map<String, Object> parameters = Collections.emptyMap();
    Tracer tracer = Mockito.mock(Tracer.class);
    Mockito.when(tracer.getClassMethodSignature()).thenReturn(new ClassMethodSignature(getClass().getName(), "testMethod", "testSignature"));
    Mockito.when(tracer.getAgentAttributes()).thenReturn(parameters);
    long rootTracerDurationInMilliseconds = 100L;
    Mockito.when(tracer.getDurationInMilliseconds()).thenReturn(rootTracerDurationInMilliseconds);
    Map<String, Object> userParams = new HashMap<>();
    userParams.put("key2", "value2");
    Map<String, Object> errorParams = new HashMap<>();
    errorParams.put("error1", "valueError");
    Map<String, Object> intrinsics = new HashMap<>();
    intrinsics.put("key4", "value4");
    intrinsics.put("key5", 5L);
    long startTime = System.currentTimeMillis();
    String frontendMetricName = "Frontend/dude";
    String requestUri = "/dude";
    String appName = "dude";
    TransactionData transactionData = new TransactionDataTestBuilder(appName, iAgentConfig, tracer).setStartTime(startTime).setRequestUri(requestUri).setFrontendMetricName(frontendMetricName).setTracers(Collections.singletonList(rootTracer)).setRequestParams(null).setUserParams(userParams).setAgentParams(null).setErrorParams(errorParams).setIntrinsics(intrinsics).build();
    TransactionTrace trace = TransactionTrace.getTransactionTrace(transactionData, SqlObfuscator.getDefaultSqlObfuscator());
    JSONParser parser = new JSONParser();
    // Get the raw data
    JSONArray jsonArray = (JSONArray) AgentHelper.serializeJSON(trace);
    // Manually create an "inflated" version for comparison with simple compression
    JSONArray inflatedJsonArray = new JSONArray();
    inflatedJsonArray.addAll(jsonArray);
    // Extract and inflate
    Object decodedTTData = decodeTransactionTraceData(inflatedJsonArray.get(4));
    // Store back in array to compare with simple compression below
    inflatedJsonArray.set(4, decodedTTData);
    Assert.assertNotNull(jsonArray);
    // Second, serialize with simple compression off (data will be deflated)
    byte[] jsonByteArray = AgentHelper.serializeJSONToByteArray(trace);
    JSONArray parsedJsonByteArray = (JSONArray) parser.parse(new String(jsonByteArray, StandardCharsets.UTF_8));
    Assert.assertEquals(jsonArray, parsedJsonByteArray);
    // Third, turn on simple compression and compare with raw data above
    setUp(true, true, true);
    byte[] simpleCompressionJsonByteArray = AgentHelper.serializeJSONToByteArray(trace);
    JSONArray parsedSimpleCompressionJsonByteArray = (JSONArray) parser.parse(new String(simpleCompressionJsonByteArray, StandardCharsets.UTF_8));
    Assert.assertEquals(inflatedJsonArray, parsedSimpleCompressionJsonByteArray);
    JSONArray jsonRootSegment = (JSONArray) decodedTTData;
    // request uri
    Assert.assertEquals(requestUri, jsonArray.get(3));
    Assert.assertEquals(5, jsonRootSegment.size());
    JSONObject atts = (JSONObject) jsonRootSegment.get(4);
    Assert.assertEquals(3, atts.size());
    Map<String, Object> innerAtts = (Map<String, Object>) atts.get("userAttributes");
    Assert.assertEquals(1, innerAtts.size());
    Assert.assertEquals("value2", innerAtts.get("key2"));
    innerAtts = (Map<String, Object>) atts.get("intrinsics");
    Assert.assertEquals(3, innerAtts.size());
    Assert.assertEquals("value4", innerAtts.get("key4"));
    Assert.assertEquals(5L, innerAtts.get("key5"));
    Assert.assertNotNull(innerAtts.get("totalTime"));
}
Also used : HashMap(java.util.HashMap) DefaultTracer(com.newrelic.agent.tracers.DefaultTracer) Tracer(com.newrelic.agent.tracers.Tracer) MethodExitTracer(com.newrelic.agent.tracers.MethodExitTracer) OtherRootTracer(com.newrelic.agent.tracers.OtherRootTracer) JSONArray(org.json.simple.JSONArray) ClassMethodSignature(com.newrelic.agent.tracers.ClassMethodSignature) JSONObject(org.json.simple.JSONObject) JSONObject(org.json.simple.JSONObject) JSONParser(org.json.simple.parser.JSONParser) TransactionData(com.newrelic.agent.TransactionData) TransactionDataTestBuilder(com.newrelic.agent.TransactionDataTestBuilder) Map(java.util.Map) ImmutableMap(com.google.common.collect.ImmutableMap) HashMap(java.util.HashMap) Test(org.junit.Test)

Example 17 with TransactionDataTestBuilder

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

the class TransactionTraceTest method testExecContext.

@Test
public void testExecContext() throws Exception {
    setUp(false, true, false);
    Transaction tx = Transaction.getTransaction();
    Tracer rootTracer = new OtherRootTracer(tx, new ClassMethodSignature("Test", "root", "()V"), this, new OtherTransSimpleMetricNameFormat("myMetricName"));
    DefaultTracer tracer1 = new DefaultTracer(tx, new ClassMethodSignature("Test", "dude", "()V"), this);
    DefaultTracer tracer2 = new DefaultTracer(tx, new ClassMethodSignature("Test", "dude2", "()V"), this);
    tx.getTransactionActivity().tracerStarted(rootTracer);
    tx.getTransactionActivity().tracerStarted(tracer1);
    tx.getTransactionActivity().tracerStarted(tracer2);
    tracer2.finish(0, null);
    tracer1.finish(0, null);
    rootTracer.finish(0, null);
    long startTime = System.currentTimeMillis();
    String frontendMetricName = "Frontend/dude";
    String requestUri = "/dude";
    String appName = "dude";
    TransactionData transactionData = new TransactionDataTestBuilder(appName, iAgentConfig, rootTracer).setStartTime(startTime).setRequestUri(requestUri).setFrontendMetricName(frontendMetricName).setTracers(Arrays.asList(rootTracer, tracer1, tracer2)).build();
    TransactionTrace trace = TransactionTrace.getTransactionTrace(transactionData, SqlObfuscator.getDefaultSqlObfuscator());
    JSONParser parser = new JSONParser();
    // Get the raw data
    JSONArray jsonArray = (JSONArray) AgentHelper.serializeJSON(trace);
    // Manually create an "inflated" version for comparison with simple compression
    JSONArray inflatedJsonArray = new JSONArray();
    inflatedJsonArray.addAll(jsonArray);
    // Extract and inflate
    Object decodedTTData = decodeTransactionTraceData(inflatedJsonArray.get(4));
    // Store back in array to compare with simple compression below
    inflatedJsonArray.set(4, decodedTTData);
    Assert.assertNotNull(jsonArray);
    // Second, serialize with simple compression off (data will be deflated)
    byte[] jsonByteArray = AgentHelper.serializeJSONToByteArray(trace);
    JSONArray parsedJsonByteArray = (JSONArray) parser.parse(new String(jsonByteArray, StandardCharsets.UTF_8));
    Assert.assertEquals(jsonArray, parsedJsonByteArray);
    // Third, turn on simple compression and compare with raw data above
    setUp(false, true, true);
    byte[] simpleCompressionJsonByteArray = AgentHelper.serializeJSONToByteArray(trace);
    JSONArray parsedSimpleCompressionJsonByteArray = (JSONArray) parser.parse(new String(simpleCompressionJsonByteArray, StandardCharsets.UTF_8));
    Assert.assertEquals(inflatedJsonArray, parsedSimpleCompressionJsonByteArray);
    JSONArray jsonRootSegment = (JSONArray) decodedTTData;
    Assert.assertEquals(8, jsonArray.size());
    JSONArray rootSeg = (JSONArray) jsonRootSegment.get(3);
    Assert.assertNotNull(rootSeg);
    verifyAllTraceSegmentsHaveExecContext(rootSeg);
}
Also used : DefaultTracer(com.newrelic.agent.tracers.DefaultTracer) Tracer(com.newrelic.agent.tracers.Tracer) MethodExitTracer(com.newrelic.agent.tracers.MethodExitTracer) OtherRootTracer(com.newrelic.agent.tracers.OtherRootTracer) OtherTransSimpleMetricNameFormat(com.newrelic.agent.tracers.metricname.OtherTransSimpleMetricNameFormat) JSONArray(org.json.simple.JSONArray) OtherRootTracer(com.newrelic.agent.tracers.OtherRootTracer) Transaction(com.newrelic.agent.Transaction) ClassMethodSignature(com.newrelic.agent.tracers.ClassMethodSignature) DefaultTracer(com.newrelic.agent.tracers.DefaultTracer) JSONParser(org.json.simple.parser.JSONParser) JSONObject(org.json.simple.JSONObject) TransactionData(com.newrelic.agent.TransactionData) TransactionDataTestBuilder(com.newrelic.agent.TransactionDataTestBuilder) Test(org.junit.Test)

Example 18 with TransactionDataTestBuilder

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

the class TransactionTraceTest method serializeUserAgentAttsEnabledRequestUriCanNotBeDisabled.

@SuppressWarnings("unchecked")
@Test
public void serializeUserAgentAttsEnabledRequestUriCanNotBeDisabled() throws Exception {
    setUp(true, true, false, false);
    Tracer rootTracer = new TestMethodExitTracer();
    Map<String, Object> parameters = Collections.emptyMap();
    Tracer tracer = Mockito.mock(Tracer.class);
    Mockito.when(tracer.getClassMethodSignature()).thenReturn(new ClassMethodSignature(getClass().getName(), "testMethod", "testSignature"));
    Mockito.when(tracer.getAgentAttributes()).thenReturn(parameters);
    Mockito.when(tracer.getEndTime()).thenReturn(5000000000L);
    Mockito.when(tracer.getDuration()).thenReturn(3000000000L);
    Map<String, String> requestParams = new HashMap<>();
    requestParams.put("key1", "value1");
    Map<String, Object> userParams = new HashMap<>();
    userParams.put("key2", "value2");
    Map<String, Object> agentParams = new HashMap<>();
    agentParams.put("key3", "value3");
    long startTime = System.currentTimeMillis();
    String frontendMetricName = "Frontend/dude";
    String requestUri = "/dude";
    String appName = "dude";
    String transactionGuid = "guid";
    TransactionData transactionData = new TransactionDataTestBuilder(appName, iAgentConfig, tracer).setStartTime(startTime).setRequestUri(requestUri).setFrontendMetricName(frontendMetricName).setTracers(Collections.singletonList(rootTracer)).setRequestParams(requestParams).setUserParams(userParams).setAgentParams(agentParams).setErrorParams(null).setIntrinsics(null).build();
    TransactionTrace trace = TransactionTrace.getTransactionTrace(transactionData, SqlObfuscator.getDefaultSqlObfuscator());
    JSONParser parser = new JSONParser();
    // Get the raw data
    JSONArray jsonArray = (JSONArray) AgentHelper.serializeJSON(trace);
    // Manually create an "inflated" version for comparison with simple compression
    JSONArray inflatedJsonArray = new JSONArray();
    inflatedJsonArray.addAll(jsonArray);
    // Extract and inflate
    Object decodedTTData = decodeTransactionTraceData(inflatedJsonArray.get(4));
    // Store back in array to compare with simple compression below
    inflatedJsonArray.set(4, decodedTTData);
    Assert.assertNotNull(jsonArray);
    // Second, serialize with simple compression off (data will be deflated)
    byte[] jsonByteArray = AgentHelper.serializeJSONToByteArray(trace);
    JSONArray parsedJsonByteArray = (JSONArray) parser.parse(new String(jsonByteArray, StandardCharsets.UTF_8));
    Assert.assertEquals(jsonArray, parsedJsonByteArray);
    // Third, turn on simple compression and compare with raw data above
    setUp(true, true, false, true);
    byte[] simpleCompressionJsonByteArray = AgentHelper.serializeJSONToByteArray(trace);
    JSONArray parsedSimpleCompressionJsonByteArray = (JSONArray) parser.parse(new String(simpleCompressionJsonByteArray, StandardCharsets.UTF_8));
    Assert.assertEquals(inflatedJsonArray, parsedSimpleCompressionJsonByteArray);
    JSONArray jsonRootSegment = (JSONArray) decodedTTData;
    Assert.assertEquals(8, jsonArray.size());
    Assert.assertEquals(startTime, jsonArray.get(0));
    Assert.assertEquals(5000L, jsonArray.get(1));
    Assert.assertEquals(frontendMetricName, jsonArray.get(2));
    Assert.assertEquals(requestUri, jsonArray.get(3));
    Assert.assertEquals(transactionGuid, jsonArray.get(5));
    Assert.assertNull(jsonArray.get(6));
    // obsolete forcePersist flag
    Assert.assertEquals(false, jsonArray.get(7));
    Assert.assertEquals(5, jsonRootSegment.size());
    Assert.assertEquals(startTime, jsonRootSegment.get(0));
    JSONObject emptyObject = (JSONObject) jsonRootSegment.get(1);
    Assert.assertEquals(0, emptyObject.size());
    emptyObject = (JSONObject) jsonRootSegment.get(2);
    Assert.assertEquals(0, emptyObject.size());
    JSONObject atts = (JSONObject) jsonRootSegment.get(4);
    Assert.assertEquals(3, atts.size());
    Map<String, Object> innerAtts = (Map<String, Object>) atts.get("agentAttributes");
    Assert.assertEquals(3, innerAtts.size());
    Assert.assertEquals("value1", innerAtts.get("request.parameters.key1"));
    Assert.assertEquals("value3", innerAtts.get("key3"));
    innerAtts = (Map<String, Object>) atts.get("userAttributes");
    Assert.assertEquals(1, innerAtts.size());
    Assert.assertEquals("value2", innerAtts.get("key2"));
    innerAtts = (Map<String, Object>) atts.get("intrinsics");
    Assert.assertEquals(1, innerAtts.size());
    Assert.assertNotNull(innerAtts.get("totalTime"));
}
Also used : HashMap(java.util.HashMap) DefaultTracer(com.newrelic.agent.tracers.DefaultTracer) Tracer(com.newrelic.agent.tracers.Tracer) MethodExitTracer(com.newrelic.agent.tracers.MethodExitTracer) OtherRootTracer(com.newrelic.agent.tracers.OtherRootTracer) JSONArray(org.json.simple.JSONArray) ClassMethodSignature(com.newrelic.agent.tracers.ClassMethodSignature) JSONObject(org.json.simple.JSONObject) JSONObject(org.json.simple.JSONObject) JSONParser(org.json.simple.parser.JSONParser) TransactionData(com.newrelic.agent.TransactionData) TransactionDataTestBuilder(com.newrelic.agent.TransactionDataTestBuilder) Map(java.util.Map) ImmutableMap(com.google.common.collect.ImmutableMap) HashMap(java.util.HashMap) Test(org.junit.Test)

Example 19 with TransactionDataTestBuilder

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

the class SlowQueryAggregatorImplTest method createTransactionData.

private TransactionData createTransactionData(String appName, String transactionName, long durationInMillis) {
    AgentConfig agentConfig = ServiceFactory.getConfigService().getDefaultAgentConfig();
    MockDispatcher dispatcher = new MockDispatcher();
    dispatcher.setWebTransaction(true);
    MockDispatcherTracer rootTracer = new MockDispatcherTracer();
    rootTracer.setDurationInMilliseconds(durationInMillis);
    rootTracer.setStartTime(System.nanoTime());
    rootTracer.setEndTime(rootTracer.getStartTime() + TimeUnit.NANOSECONDS.convert(durationInMillis, TimeUnit.MILLISECONDS));
    TransactionTracerConfig ttconf = Mockito.mock(TransactionTracerConfig.class);
    when(ttconf.isEnabled()).thenReturn(true);
    when(ttconf.getInsertSqlMaxLength()).thenReturn(10 * 1000);
    Transaction tx = mock(Transaction.class);
    TransactionActivity txa = mock(TransactionActivity.class);
    when(txa.getTransaction()).thenReturn(tx);
    when(tx.getTransactionActivity()).thenReturn(txa);
    when(tx.getTransactionTracerConfig()).thenReturn(ttconf);
    List<SlowQueryInfo> tracers = new ArrayList<>(1);
    String sql = "select * from TABLE" + Integer.toString(count.incrementAndGet()) + " where Time = 'nigh'";
    String obfuscatedSql = sqlObfuscator.obfuscateSql(sql);
    SqlTracer sqlTracer = new NoOpTrackingSqlTracer(tx, sql);
    SlowQueryInfo slowQueryInfo = new SlowQueryInfo(null, sqlTracer, sql, obfuscatedSql, agentConfig.getSqlTraceConfig());
    tracers.add(slowQueryInfo);
    SlowQueryListener slowQueryListener = Mockito.mock(SlowQueryListener.class);
    org.mockito.Mockito.when(slowQueryListener.getSlowQueries()).thenReturn(tracers);
    return new TransactionDataTestBuilder(appName, agentConfig, rootTracer).setTx(tx).setRequestUri(transactionName).setFrontendMetricName(transactionName).setSlowQueryListener(slowQueryListener).build();
}
Also used : MockDispatcherTracer(com.newrelic.agent.MockDispatcherTracer) ArrayList(java.util.ArrayList) MockDispatcher(com.newrelic.agent.MockDispatcher) TransactionActivity(com.newrelic.agent.TransactionActivity) AgentConfig(com.newrelic.agent.config.AgentConfig) Transaction(com.newrelic.agent.Transaction) NoOpTrackingSqlTracer(com.newrelic.agent.instrumentation.sql.NoOpTrackingSqlTracer) NoOpTrackingSqlTracer(com.newrelic.agent.instrumentation.sql.NoOpTrackingSqlTracer) SqlTracer(com.newrelic.agent.tracers.SqlTracer) TransactionTracerConfig(com.newrelic.agent.config.TransactionTracerConfig) TransactionDataTestBuilder(com.newrelic.agent.TransactionDataTestBuilder)

Example 20 with TransactionDataTestBuilder

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

the class TransactionTraceBucketTest method createTransactionData.

private TransactionData createTransactionData(String transactionName, long durationInMillis) {
    AgentConfig agentConfig = ServiceFactory.getConfigService().getDefaultAgentConfig();
    MockDispatcher dispatcher = new MockDispatcher();
    dispatcher.setWebTransaction(true);
    MockDispatcherTracer rootTracer = new MockDispatcherTracer();
    rootTracer.setDurationInMilliseconds(durationInMillis);
    rootTracer.setStartTime(System.nanoTime());
    rootTracer.setEndTime(rootTracer.getStartTime() + TimeUnit.NANOSECONDS.convert(durationInMillis, TimeUnit.MILLISECONDS));
    return new TransactionDataTestBuilder(APP_NAME, agentConfig, rootTracer).setDispatcher(dispatcher).setRequestUri(transactionName).setFrontendMetricName(transactionName).build();
}
Also used : AgentConfig(com.newrelic.agent.config.AgentConfig) MockDispatcherTracer(com.newrelic.agent.MockDispatcherTracer) MockDispatcher(com.newrelic.agent.MockDispatcher) TransactionDataTestBuilder(com.newrelic.agent.TransactionDataTestBuilder)

Aggregations

TransactionDataTestBuilder (com.newrelic.agent.TransactionDataTestBuilder)20 MockDispatcherTracer (com.newrelic.agent.MockDispatcherTracer)11 MockDispatcher (com.newrelic.agent.MockDispatcher)10 TransactionData (com.newrelic.agent.TransactionData)10 OtherRootTracer (com.newrelic.agent.tracers.OtherRootTracer)10 Tracer (com.newrelic.agent.tracers.Tracer)10 Test (org.junit.Test)10 AgentConfig (com.newrelic.agent.config.AgentConfig)9 ClassMethodSignature (com.newrelic.agent.tracers.ClassMethodSignature)9 DefaultTracer (com.newrelic.agent.tracers.DefaultTracer)9 MethodExitTracer (com.newrelic.agent.tracers.MethodExitTracer)9 JSONArray (org.json.simple.JSONArray)9 JSONObject (org.json.simple.JSONObject)9 JSONParser (org.json.simple.parser.JSONParser)9 HashMap (java.util.HashMap)8 ImmutableMap (com.google.common.collect.ImmutableMap)7 Map (java.util.Map)7 Transaction (com.newrelic.agent.Transaction)2 TransactionActivity (com.newrelic.agent.TransactionActivity)1 ErrorCollectorConfig (com.newrelic.agent.config.ErrorCollectorConfig)1