Search in sources :

Example 1 with SpanEvent

use of com.newrelic.agent.model.SpanEvent in project newrelic-java-agent by newrelic.

the class SegmentTest method testSpanParenting.

@Test
public void testSpanParenting() {
    SpanEventsService spanEventService = ServiceFactory.getSpanEventService();
    SamplingPriorityQueue<SpanEvent> eventPool = spanEventService.getOrCreateDistributedSamplingReservoir(APP_NAME);
    Transaction.clearTransaction();
    Tracer rootTracer = makeTransaction();
    rootTracer.getTransactionActivity().getTransaction().setPriorityIfNotNull(1.0f);
    rootTracer.setMetricName("RootTracer");
    Transaction tx = rootTracer.getTransactionActivity().getTransaction();
    Segment segment = tx.startSegment("custom", "segment");
    final HashMap<String, String> headers = new HashMap<>();
    segment.addOutboundRequestHeaders(new OutboundHeaders() {

        @Override
        public HeaderType getHeaderType() {
            return HeaderType.HTTP;
        }

        @Override
        public void setHeader(String name, String value) {
            headers.put(name, value);
        }
    });
    String payload = headers.get("newrelic");
    segment.end();
    rootTracer.finish(Opcodes.ARETURN, null);
    Transaction.clearTransaction();
    Tracer secondRootTracer = makeTransaction();
    secondRootTracer.setMetricName("SecondRootTracer");
    Transaction secondTxn = secondRootTracer.getTransactionActivity().getTransaction();
    secondTxn.acceptDistributedTracePayload(payload);
    secondRootTracer.finish(Opcodes.ARETURN, null);
    List<SpanEvent> spanEvents = eventPool.asList();
    Assert.assertNotNull(spanEvents);
    assertEquals(3, spanEvents.size());
    SpanEvent rootTracerSpanEvent = findSpanByName(spanEvents, "RootTracer");
    SpanEvent segmentSpanEvent = findSpanByName(spanEvents, "custom/segment");
    SpanEvent secondRootTracerSpanEvent = findSpanByName(spanEvents, "SecondRootTracer");
    assertEquals(rootTracerSpanEvent.getTraceId(), segmentSpanEvent.getTraceId());
    assertEquals(segmentSpanEvent.getTraceId(), secondRootTracerSpanEvent.getTraceId());
    assertEquals(rootTracerSpanEvent.getGuid(), segmentSpanEvent.getParentId());
    assertEquals(segmentSpanEvent.getGuid(), secondRootTracerSpanEvent.getParentId());
}
Also used : HashMap(java.util.HashMap) ExitTracer(com.newrelic.agent.bridge.ExitTracer) DefaultTracer(com.newrelic.agent.tracers.DefaultTracer) Tracer(com.newrelic.agent.tracers.Tracer) OtherRootTracer(com.newrelic.agent.tracers.OtherRootTracer) SpanEventsService(com.newrelic.agent.service.analytics.SpanEventsService) HeaderType(com.newrelic.api.agent.HeaderType) SpanEvent(com.newrelic.agent.model.SpanEvent) OutboundHeaders(com.newrelic.api.agent.OutboundHeaders) Test(org.junit.Test)

Example 2 with SpanEvent

use of com.newrelic.agent.model.SpanEvent in project newrelic-java-agent by newrelic.

the class DefaultSqlTracerTest method testSpanEventDatastoreTruncationAtExactLimit.

@Test
public void testSpanEventDatastoreTruncationAtExactLimit() throws SQLException {
    final Connection connection = mock(Connection.class);
    DatastoreInstanceDetection.detectConnectionAddress();
    DatastoreInstanceDetection.associateAddress(connection, new InetSocketAddress("dbserver.nerd.us", 9945));
    DatastoreInstanceDetection.stopDetectingConnectionAddress();
    String longQueryString = "SELECT price, name FROM BOOKS WHERE name = " + Strings.repeat("a", 1957);
    DefaultSqlTracer tracer = newInstanceDBTracer(longQueryString, connection, "MySQL", "mysql");
    tracer.finish(Opcodes.ARETURN, new DummyResultSet());
    SpanEventsService spanEventService = ServiceFactory.getSpanEventService();
    ((SpanEventsServiceImpl) spanEventService).dispatcherTransactionFinished(new TransactionData(tracer.getTransaction(), 1024), new TransactionStats());
    SamplingPriorityQueue<SpanEvent> eventPool = spanEventService.getOrCreateDistributedSamplingReservoir(APP_NAME);
    List<SpanEvent> spanEvents = eventPool.asList();
    assertNotNull(spanEvents);
    assertEquals(1, spanEvents.size());
    SpanEvent spanEvent = Iterables.getFirst(spanEvents, null);
    assertNotNull(spanEvent);
    assertEquals("datastore", spanEvent.getIntrinsics().get("category"));
    assertEquals("MySQL", spanEvent.getIntrinsics().get("component"));
    assertEquals("dbserver.nerd.us", spanEvent.getIntrinsics().get("peer.hostname"));
    assertEquals("dbserver.nerd.us:9945", spanEvent.getIntrinsics().get("peer.address"));
    assertEquals(2000, spanEvent.getIntrinsics().get("db.statement").toString().length());
    // Should not end with ... since it's exactly at the limit
    assertTrue(spanEvent.getIntrinsics().get("db.statement").toString().endsWith("aaa"));
    assertEquals("books", spanEvent.getIntrinsics().get("db.collection"));
    assertEquals("client", spanEvent.getIntrinsics().get("span.kind"));
    assertEquals("Datastore/statement/MySQL/books/select", spanEvent.getName());
    assertNotNull(spanEvent.getTraceId());
    assertNotNull(spanEvent.getGuid());
}
Also used : TransactionStats(com.newrelic.agent.stats.TransactionStats) SpanEventsServiceImpl(com.newrelic.agent.service.analytics.SpanEventsServiceImpl) InetSocketAddress(java.net.InetSocketAddress) SpanEventsService(com.newrelic.agent.service.analytics.SpanEventsService) Connection(java.sql.Connection) DummyConnection(sql.DummyConnection) SpanEvent(com.newrelic.agent.model.SpanEvent) DummyResultSet(sql.DummyResultSet) Test(org.junit.Test)

Example 3 with SpanEvent

use of com.newrelic.agent.model.SpanEvent in project newrelic-java-agent by newrelic.

the class DefaultSqlTracerTest method testSpanEventDatastoreTruncation.

@Test
public void testSpanEventDatastoreTruncation() throws SQLException {
    final Connection connection = mock(Connection.class);
    DatastoreInstanceDetection.detectConnectionAddress();
    DatastoreInstanceDetection.associateAddress(connection, new InetSocketAddress("dbserver.nerd.us", 9945));
    DatastoreInstanceDetection.stopDetectingConnectionAddress();
    String longQueryString = "SELECT price, name FROM BOOKS WHERE name = " + Strings.repeat("a", 4000);
    DefaultSqlTracer tracer = newInstanceDBTracer(longQueryString, connection, "MySQL", "mysql");
    tracer.finish(Opcodes.ARETURN, new DummyResultSet());
    SpanEventsService spanEventService = ServiceFactory.getSpanEventService();
    ((SpanEventsServiceImpl) spanEventService).dispatcherTransactionFinished(new TransactionData(tracer.getTransaction(), 1024), new TransactionStats());
    SamplingPriorityQueue<SpanEvent> eventPool = spanEventService.getOrCreateDistributedSamplingReservoir(APP_NAME);
    List<SpanEvent> spanEvents = eventPool.asList();
    assertNotNull(spanEvents);
    assertEquals(1, spanEvents.size());
    SpanEvent spanEvent = Iterables.getFirst(spanEvents, null);
    assertNotNull(spanEvent);
    assertEquals("datastore", spanEvent.getIntrinsics().get("category"));
    assertEquals("MySQL", spanEvent.getIntrinsics().get("component"));
    assertEquals("dbserver.nerd.us", spanEvent.getIntrinsics().get("peer.hostname"));
    assertEquals("dbserver.nerd.us:9945", spanEvent.getIntrinsics().get("peer.address"));
    assertEquals(2000, spanEvent.getIntrinsics().get("db.statement").toString().length());
    assertTrue(spanEvent.getIntrinsics().get("db.statement").toString().endsWith("a..."));
    assertEquals("books", spanEvent.getIntrinsics().get("db.collection"));
    assertEquals("client", spanEvent.getIntrinsics().get("span.kind"));
    assertEquals("Datastore/statement/MySQL/books/select", spanEvent.getName());
    assertNotNull(spanEvent.getTraceId());
    assertNotNull(spanEvent.getGuid());
}
Also used : TransactionStats(com.newrelic.agent.stats.TransactionStats) SpanEventsServiceImpl(com.newrelic.agent.service.analytics.SpanEventsServiceImpl) InetSocketAddress(java.net.InetSocketAddress) SpanEventsService(com.newrelic.agent.service.analytics.SpanEventsService) Connection(java.sql.Connection) DummyConnection(sql.DummyConnection) SpanEvent(com.newrelic.agent.model.SpanEvent) DummyResultSet(sql.DummyResultSet) Test(org.junit.Test)

Example 4 with SpanEvent

use of com.newrelic.agent.model.SpanEvent in project newrelic-java-agent by newrelic.

the class DefaultTracerTest method testSpanEvent.

@Test
public void testSpanEvent() {
    DefaultTracer tracer = prepareTracer();
    tracer.finish(0, null);
    SpanEventsService spanEventService = ServiceFactory.getSpanEventService();
    ((SpanEventsServiceImpl) spanEventService).dispatcherTransactionFinished(new TransactionData(tracer.getTransaction(), 1024), new TransactionStats());
    SamplingPriorityQueue<SpanEvent> eventPool = spanEventService.getOrCreateDistributedSamplingReservoir(APP_NAME);
    List<SpanEvent> spanEvents = eventPool.asList();
    assertNotNull(spanEvents);
    assertEquals(1, spanEvents.size());
    SpanEvent spanEvent = Iterables.getFirst(spanEvents, null);
    assertNotNull(spanEvent);
    assertNull(spanEvent.getParentId());
    assertEquals(tracer.getGuid(), spanEvent.getGuid());
    assertEquals(tracer.getTransaction().getGuid(), spanEvent.getTransactionId());
    assertEquals("Java/class/method", spanEvent.getName());
    assertEquals(true, spanEvent.getIntrinsics().get("nr.entryPoint"));
    assertEquals((float) tracer.getDurationInMilliseconds() / TimeConversion.MILLISECONDS_PER_SECOND, spanEvent.getDuration(), 0.001f);
}
Also used : TransactionStats(com.newrelic.agent.stats.TransactionStats) SpanEventsServiceImpl(com.newrelic.agent.service.analytics.SpanEventsServiceImpl) SpanEventsService(com.newrelic.agent.service.analytics.SpanEventsService) SpanEvent(com.newrelic.agent.model.SpanEvent) TransactionData(com.newrelic.agent.TransactionData) Test(org.junit.Test)

Example 5 with SpanEvent

use of com.newrelic.agent.model.SpanEvent in project newrelic-java-agent by newrelic.

the class TracerToSpanEventTest method testIntrinsicTransactionAttrsCopiedToSpan.

@Test
public void testIntrinsicTransactionAttrsCopiedToSpan() {
    // setup
    Map<String, Object> intrinsicAttributes = new HashMap<>();
    intrinsicAttributes.put("hot", "sauce");
    intrinsicAttributes.put("saucy", "burrito");
    expectedAgentAttributes.put("hot", "sauce");
    expectedAgentAttributes.put("saucy", "burrito");
    SpanEvent expectedSpanEvent = buildExpectedSpanEvent();
    when(txnData.getIntrinsicAttributes()).thenReturn(intrinsicAttributes);
    TracerToSpanEvent testClass = new TracerToSpanEvent(errorBuilderMap, new AttributeFilter.PassEverythingAttributeFilter(), timestampProvider, environmentService, transactionDataToDistributedTraceIntrinsics, spanErrorBuilder);
    // execution
    SpanEvent spanEvent = testClass.createSpanEvent(tracer, txnData, txnStats, true, false);
    // assertions
    assertEquals(expectedSpanEvent, spanEvent);
}
Also used : HashMap(java.util.HashMap) SpanEvent(com.newrelic.agent.model.SpanEvent) AttributeFilter(com.newrelic.agent.model.AttributeFilter) Test(org.junit.Test)

Aggregations

SpanEvent (com.newrelic.agent.model.SpanEvent)81 Test (org.junit.Test)64 AttributeFilter (com.newrelic.agent.model.AttributeFilter)23 SpanEventsService (com.newrelic.agent.service.analytics.SpanEventsService)19 TransactionStats (com.newrelic.agent.stats.TransactionStats)14 TransactionData (com.newrelic.agent.TransactionData)12 SpanEventsServiceImpl (com.newrelic.agent.service.analytics.SpanEventsServiceImpl)12 Tracer (com.newrelic.agent.tracers.Tracer)9 HashMap (java.util.HashMap)8 Map (java.util.Map)5 TransactionEvent (com.newrelic.agent.service.analytics.TransactionEvent)4 TransactionDataList (com.newrelic.agent.TransactionDataList)3 DistributedTracePayloadImpl (com.newrelic.agent.tracing.DistributedTracePayloadImpl)3 Test (org.junit.jupiter.api.Test)3 SpanConverterTest.buildSpanEvent (com.newrelic.SpanConverterTest.buildSpanEvent)2 BoundTransactionApiImpl (com.newrelic.agent.BoundTransactionApiImpl)2 AttributesService (com.newrelic.agent.attributes.AttributesService)2 AgentConfig (com.newrelic.agent.config.AgentConfig)2 ReservoirManager (com.newrelic.agent.interfaces.ReservoirManager)2 Consumer (com.newrelic.agent.interfaces.backport.Consumer)2