use of com.newrelic.agent.tracers.Tracer in project newrelic-java-agent by newrelic.
the class TransactionDataTest method getBlameOrRootMetricName.
@Test
public void getBlameOrRootMetricName() {
String expected = String.valueOf(System.nanoTime());
Tracer mock = Mockito.mock(Tracer.class);
Mockito.when(mock.getMetricName()).thenReturn(expected);
Mockito.when(tx.getRootTracer()).thenReturn(mock);
TransactionData txd = getTxData(tx);
String result = txd.getBlameOrRootMetricName();
Assert.assertSame(expected, result);
expected = String.valueOf(System.nanoTime());
Mockito.when(tx.getPriorityTransactionName().getName()).thenReturn(expected);
txd = getTxData(tx);
result = txd.getBlameOrRootMetricName();
Assert.assertSame(expected, result);
}
use of com.newrelic.agent.tracers.Tracer in project newrelic-java-agent by newrelic.
the class TransactionDataTestBuilder method build.
public TransactionData build() {
when(tx.getRootTracer()).thenReturn(tracer);
if (synJobId == null || synMonitorId == null || synResourceId == null) {
when(tx.isSynthetic()).thenReturn(false);
} else {
when(tx.isSynthetic()).thenReturn(true);
}
when(tx.getGuid()).thenReturn("guid");
Dispatcher mockDispatcher = mock(Dispatcher.class);
when(mockDispatcher.getUri()).thenReturn(requestUri);
when(mockDispatcher.isWebTransaction()).thenReturn(dispatcher == null ? true : dispatcher.isWebTransaction());
when(tx.getDispatcher()).thenReturn(mockDispatcher);
if (throwable != null) {
when(tx.getThrowable()).thenReturn(new TransactionThrowable(throwable, expectedError, null));
}
PriorityTransactionName priorityTransactionName = mock(PriorityTransactionName.class);
when(priorityTransactionName.getName()).thenReturn(frontendMetricName);
when(tx.getPriorityTransactionName()).thenReturn(priorityTransactionName);
Map<String, Map<String, String>> prefixed = new HashMap<>();
prefixed.put("request.parameters.", requestParams);
when(tx.getPrefixedAgentAttributes()).thenReturn(prefixed);
when(tx.getUserAttributes()).thenReturn(userParams);
when(tx.getAgentAttributes()).thenReturn(agentParams);
when(tx.getErrorAttributes()).thenReturn(errorParams);
when(tx.getIntrinsicAttributes()).thenReturn(intrinsics);
when(tx.isIgnore()).thenReturn(false);
when(tx.getStatus()).thenReturn(responseStatus);
when(tx.getStatusMessage()).thenReturn(statusMessage);
when(tx.isErrorReportableAndNotIgnored()).thenReturn(true);
when(tx.getSpanProxy()).thenReturn(new SpanProxy());
ErrorServiceImpl errorService = mock(ErrorServiceImpl.class);
IRPMService rpmService = mock(IRPMService.class);
when(rpmService.getApplicationName()).thenReturn(appName);
when(rpmService.getErrorService()).thenReturn(errorService);
when(tx.getRPMService()).thenReturn(rpmService);
when(tx.getAgentConfig()).thenReturn(agentConfig);
when(tx.getWallClockStartTimeMs()).thenReturn(startTime);
if (slowQueryListener != null) {
when(tx.getSlowQueryListener(anyBoolean())).thenReturn(slowQueryListener);
}
when(tx.getTracers()).thenReturn(tracers);
CrossProcessTransactionState crossProcessTransactionState = mock(CrossProcessTransactionState.class);
when(tx.getCrossProcessTransactionState()).thenReturn(crossProcessTransactionState);
when(crossProcessTransactionState.getTripId()).thenReturn("tripId");
InboundHeaderState ihs = mock(InboundHeaderState.class);
when(ihs.getSyntheticsJobId()).thenReturn(synJobId);
when(ihs.getSyntheticsMonitorId()).thenReturn(synMonitorId);
when(ihs.getSyntheticsResourceId()).thenReturn(synResourceId);
when(ihs.getSyntheticsVersion()).thenReturn(HeadersUtil.SYNTHETICS_MIN_VERSION);
when(tx.getInboundHeaderState()).thenReturn(ihs);
TransactionTimer timer = new TransactionTimer(tracer.getStartTime());
timer.markTransactionActivityAsDone(tracer.getEndTime(), tracer.getDuration());
timer.markTransactionAsDone();
when(tx.getTransactionTimer()).thenReturn(timer);
// when(tx.getApplicationName()).thenReturn(appName);
Set<TransactionActivity> activities = new HashSet<>();
for (Map.Entry<Long, Collection<Duration>> entry : threadIdToDuration.asMap().entrySet()) {
for (Duration duration : entry.getValue()) {
TransactionActivity activity = mock(TransactionActivity.class);
when(activity.getThreadId()).thenReturn(entry.getKey());
Tracer rootTracer = mock(Tracer.class);
when(rootTracer.getStartTime()).thenReturn(duration.startTime);
when(rootTracer.getEndTime()).thenReturn(duration.endTime);
when(activity.getRootTracer()).thenReturn(rootTracer);
activities.add(activity);
}
}
when(tx.getFinishedChildren()).thenReturn(activities);
if (includeDistributedTracePayload) {
SpanProxy spanProxy = mock(SpanProxy.class);
DistributedTracePayloadImpl payload = DistributedTracePayloadImpl.createDistributedTracePayload("abc", "def", "def", new Random().nextFloat());
when(spanProxy.getInboundDistributedTracePayload()).thenReturn(payload);
when(tx.getSpanProxy()).thenReturn(spanProxy);
}
return new TransactionData(tx, 0);
}
use of com.newrelic.agent.tracers.Tracer in project newrelic-java-agent by newrelic.
the class RequestUriConfigTests method runTracedErrorTest.
public void runTracedErrorTest(RequestUriConfigTransactionTest test) throws Exception {
setupConfig(test);
// run a transaction
Tracer requestDispatcherTracer = startDispatcherTracer(test.getTxnName());
DefaultTracer defaultTracer = startTracer();
NewRelicApiImplementation.initialize();
AgentBridge.publicApi.noticeError("error");
defaultTracer.finish(Opcodes.RETURN, null);
requestDispatcherTracer.finish(Opcodes.RETURN, null);
MockRPMService mockRPMService = runHarvest();
// verify results
List<TracedError> errorTraces = mockRPMService.getErrorService().getAndClearTracedErrors();
assertFalse(errorTraces.isEmpty());
for (TracedError trace : errorTraces) {
Writer writer = new StringWriter();
trace.writeJSONString(writer);
JSONParser parser = new JSONParser();
JSONArray parsedError = (JSONArray) parser.parse(writer.toString());
matchUri(test.getExpectedUriValues(), (String) ((JSONObject) parsedError.get(4)).get("request_uri"));
}
}
use of com.newrelic.agent.tracers.Tracer in project newrelic-java-agent by newrelic.
the class RequestUriConfigTests method runSqlTest.
public void runSqlTest(RequestUriConfigSqlTest test) throws Exception {
setupConfig(test);
// run a transaction
Tracer requestDispatcherTracer = startDispatcherTracer(test.getTxnName());
long duration = 501000000;
startSqlTracer(test.sql, duration).finish(Opcodes.RETURN, null);
requestDispatcherTracer.finish(Opcodes.RETURN, null);
MockRPMService mockRPMService = runHarvest();
// verify results
List<SqlTrace> sqlTraces = mockRPMService.getSqlTraces();
matchUri(test.getExpectedUriValues(), sqlTraces.get(0).getUri());
}
use of com.newrelic.agent.tracers.Tracer 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());
}
Aggregations