use of com.newrelic.agent.tracers.ClassMethodSignature in project newrelic-java-agent by newrelic.
the class TransactionTraceTest method serializeAttsDisabled.
@Test
public void serializeAttsDisabled() throws Exception {
setUp(false, 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);
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(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());
Assert.assertEquals(startTime, jsonArray.get(0));
Assert.assertEquals(5000L, jsonArray.get(1));
Assert.assertEquals(frontendMetricName, jsonArray.get(2));
Object serializedRequestUri = jsonArray.get(3);
Assert.assertEquals(null, serializedRequestUri);
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());
}
use of com.newrelic.agent.tracers.ClassMethodSignature 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.tracers.ClassMethodSignature in project newrelic-java-agent by newrelic.
the class TransactionTraceTest method testGetMetricName.
@Test
public void testGetMetricName() throws Exception {
setUp(true, true, false);
DefaultTracer tracer = new DefaultTracer(Transaction.getTransaction(), new ClassMethodSignature("Test", "dude", "()V"), this, new SimpleMetricNameFormat("hello"));
Assert.assertEquals("hello", TransactionSegment.getMetricName(tracer));
tracer = new DefaultTracer(Transaction.getTransaction(), new ClassMethodSignature("Test", "dude", "()V"), this, new SimpleMetricNameFormat("hello", "segmentName"));
Assert.assertEquals("segmentName", TransactionSegment.getMetricName(tracer));
tracer = new DefaultTracer(Transaction.getTransaction(), new ClassMethodSignature("Test", "dude", "()V"), this, new SimpleMetricNameFormat("hello", "1234"));
Assert.assertEquals("1234", TransactionSegment.getMetricName(tracer));
// Writing this test based on the current code. No clue why we would ever want a segment named after the
// tracer. This test proves that you can not get a null transaction segment metric name.
tracer = new DefaultTracer(Transaction.getTransaction(), new ClassMethodSignature("Test", "dude", "()V"), this, new SimpleMetricNameFormat("hello", ""));
Assert.assertEquals("com.newrelic.agent.tracers.DefaultTracer*", TransactionSegment.getMetricName(tracer));
tracer = new DefaultTracer(Transaction.getTransaction(), new ClassMethodSignature("Test", "dude", "()V"), this, new SimpleMetricNameFormat("hello", null));
Assert.assertEquals("com.newrelic.agent.tracers.DefaultTracer*", TransactionSegment.getMetricName(tracer));
tracer = new DefaultTracer(Transaction.getTransaction(), new ClassMethodSignature("Test", "dude", "()V"), this, new SimpleMetricNameFormat(null, null));
Assert.assertEquals("com.newrelic.agent.tracers.DefaultTracer*", TransactionSegment.getMetricName(tracer));
tracer = new DefaultTracer(Transaction.getTransaction(), new ClassMethodSignature("Test", "dude", "()V"), this, new SimpleMetricNameFormat("hello", " "));
Assert.assertEquals("com.newrelic.agent.tracers.DefaultTracer*", TransactionSegment.getMetricName(tracer));
}
use of com.newrelic.agent.tracers.ClassMethodSignature in project newrelic-java-agent by newrelic.
the class TransactionTraceTest method serializeUserAgentIntrinsicsEnabled.
@SuppressWarnings("unchecked")
@Test
public void serializeUserAgentIntrinsicsEnabled() 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);
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> errorParams = new HashMap<>();
errorParams.put("error1", "valueError");
Map<String, Object> agentParams = new HashMap<>();
agentParams.put("key3", "value3");
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";
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(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;
long txDurMs = transactionData.getTransactionTime().getTransactionDurationInMilliseconds();
long responseDurMs = transactionData.getTransactionTime().getResponseTimeInMilliseconds();
System.out.println(responseDurMs);
System.out.println(txDurMs);
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(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.tracers.ClassMethodSignature in project newrelic-java-agent by newrelic.
the class TransactionTraceTest method serializeUserAgentAttsEnabled.
@SuppressWarnings("unchecked")
@Test
public void serializeUserAgentAttsEnabled() 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);
Mockito.when(tracer.getEndTime()).thenReturn(5000000000L);
// duration of the tracer is no longer used in the calculation
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, 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"));
}
Aggregations