use of com.newrelic.agent.tracers.Tracer 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.Tracer 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"));
}
use of com.newrelic.agent.tracers.Tracer in project newrelic-java-agent by newrelic.
the class TransactionTraceTest method serializeAgentOnlyEnabled.
@SuppressWarnings("unchecked")
@Test
public void serializeAgentOnlyEnabled() 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, String> requestParams = new HashMap<>();
requestParams.put("key1", "value1");
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";
TransactionData transactionData = new TransactionDataTestBuilder(appName, iAgentConfig, tracer).setStartTime(startTime).setRequestUri(requestUri).setFrontendMetricName(frontendMetricName).setTracers(Collections.singletonList(rootTracer)).setRequestParams(requestParams).setAgentParams(agentParams).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;
Assert.assertEquals(requestUri, jsonArray.get(3));
Assert.assertEquals(5, jsonRootSegment.size());
JSONObject atts = (JSONObject) jsonRootSegment.get(4);
Assert.assertEquals(2, 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("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.Tracer in project newrelic-java-agent by newrelic.
the class TracerToSpanEventTest method testParentIdFromParentSpan.
@Test
public void testParentIdFromParentSpan() {
// setup
String parentGuid = "98765";
expectedIntrinsicAttributes.put("parentId", parentGuid);
SpanEvent expectedSpanEvent = buildExpectedSpanEvent();
Tracer parentTracer = mock(Tracer.class);
when(parentTracer.getGuid()).thenReturn(parentGuid);
when(parentTracer.isTransactionSegment()).thenReturn(true);
when(spanErrorBuilder.buildSpanError(tracer, false, responseStatus, statusMessage, throwable)).thenReturn(spanError);
when(tracer.getParentTracer()).thenReturn(parentTracer);
TracerToSpanEvent testClass = new TracerToSpanEvent(errorBuilderMap, new AttributeFilter.PassEverythingAttributeFilter(), timestampProvider, environmentService, transactionDataToDistributedTraceIntrinsics, spanErrorBuilder);
// execution
SpanEvent spanEvent = testClass.createSpanEvent(tracer, txnData, txnStats, isRoot, false);
// assertions
assertEquals(expectedSpanEvent, spanEvent);
}
use of com.newrelic.agent.tracers.Tracer in project newrelic-java-agent by newrelic.
the class TracerToSpanEventTest method testCrossProcessOnly.
@Test
public void testCrossProcessOnly() {
// setup
String parentGuid = "98765";
SpanEvent expectedSpanEvent = buildExpectedSpanEvent();
Tracer parentTracer = mock(Tracer.class);
// the parent tracer and guid are used to make sure the test fails if crossProcessOnly isn't set
when(parentTracer.getGuid()).thenReturn(parentGuid);
when(parentTracer.isTransactionSegment()).thenReturn(true);
when(spanErrorBuilder.buildSpanError(tracer, false, responseStatus, statusMessage, throwable)).thenReturn(spanError);
when(tracer.getParentTracer()).thenReturn(parentTracer);
TracerToSpanEvent testClass = new TracerToSpanEvent(errorBuilderMap, new AttributeFilter.PassEverythingAttributeFilter(), timestampProvider, environmentService, transactionDataToDistributedTraceIntrinsics, spanErrorBuilder);
// execution
SpanEvent spanEvent = testClass.createSpanEvent(tracer, txnData, txnStats, isRoot, true);
// assertions
assertEquals(expectedSpanEvent, spanEvent);
}
Aggregations