Search in sources :

Example 56 with Tracer

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);
}
Also used : Tracer(com.newrelic.agent.tracers.Tracer) Test(org.junit.Test)

Example 57 with Tracer

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);
}
Also used : ErrorServiceImpl(com.newrelic.agent.errors.ErrorServiceImpl) HashMap(java.util.HashMap) SpanProxy(com.newrelic.agent.tracing.SpanProxy) Tracer(com.newrelic.agent.tracers.Tracer) PriorityTransactionName(com.newrelic.agent.transaction.PriorityTransactionName) Dispatcher(com.newrelic.agent.dispatchers.Dispatcher) DistributedTracePayloadImpl(com.newrelic.agent.tracing.DistributedTracePayloadImpl) Random(java.util.Random) TransactionThrowable(com.newrelic.agent.transaction.TransactionThrowable) Collection(java.util.Collection) TransactionTimer(com.newrelic.agent.transaction.TransactionTimer) HashMap(java.util.HashMap) Map(java.util.Map) HashSet(java.util.HashSet)

Example 58 with Tracer

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"));
    }
}
Also used : DefaultTracer(com.newrelic.agent.tracers.DefaultTracer) TracedError(com.newrelic.agent.errors.TracedError) StringWriter(java.io.StringWriter) JSONObject(org.json.simple.JSONObject) DefaultTracer(com.newrelic.agent.tracers.DefaultTracer) SqlTracer(com.newrelic.agent.tracers.SqlTracer) OtherRootSqlTracer(com.newrelic.agent.tracers.OtherRootSqlTracer) BasicRequestRootTracer(com.newrelic.agent.tracers.servlet.BasicRequestRootTracer) Tracer(com.newrelic.agent.tracers.Tracer) OtherRootTracer(com.newrelic.agent.tracers.OtherRootTracer) JSONArray(org.json.simple.JSONArray) JSONParser(org.json.simple.parser.JSONParser) MockRPMService(com.newrelic.agent.MockRPMService) Writer(java.io.Writer) StringWriter(java.io.StringWriter)

Example 59 with Tracer

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());
}
Also used : SqlTrace(com.newrelic.agent.sql.SqlTrace) DefaultTracer(com.newrelic.agent.tracers.DefaultTracer) SqlTracer(com.newrelic.agent.tracers.SqlTracer) OtherRootSqlTracer(com.newrelic.agent.tracers.OtherRootSqlTracer) BasicRequestRootTracer(com.newrelic.agent.tracers.servlet.BasicRequestRootTracer) Tracer(com.newrelic.agent.tracers.Tracer) OtherRootTracer(com.newrelic.agent.tracers.OtherRootTracer) MockRPMService(com.newrelic.agent.MockRPMService)

Example 60 with Tracer

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());
}
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) Test(org.junit.Test)

Aggregations

Tracer (com.newrelic.agent.tracers.Tracer)263 Test (org.junit.Test)195 OtherRootTracer (com.newrelic.agent.tracers.OtherRootTracer)104 DefaultTracer (com.newrelic.agent.tracers.DefaultTracer)99 Transaction (com.newrelic.agent.Transaction)86 BasicRequestRootTracer (com.newrelic.agent.tracers.servlet.BasicRequestRootTracer)54 ClassMethodSignature (com.newrelic.agent.tracers.ClassMethodSignature)41 ExitTracer (com.newrelic.agent.bridge.ExitTracer)39 TokenImpl (com.newrelic.agent.TokenImpl)35 StartAndThenLink (com.newrelic.agent.TransactionAsyncUtility.StartAndThenLink)31 OtherRootSqlTracer (com.newrelic.agent.tracers.OtherRootSqlTracer)25 TransactionData (com.newrelic.agent.TransactionData)24 HashMap (java.util.HashMap)24 SqlTracer (com.newrelic.agent.tracers.SqlTracer)20 ArrayList (java.util.ArrayList)19 UltraLightTracer (com.newrelic.agent.tracers.UltraLightTracer)17 TransactionStats (com.newrelic.agent.stats.TransactionStats)16 MockRPMService (com.newrelic.agent.MockRPMService)15 JSONArray (org.json.simple.JSONArray)15 JSONObject (org.json.simple.JSONObject)14