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