Search in sources :

Example 41 with SpanEvent

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

the class SpanEventsServiceFactory method build.

public SpanEventsService build() {
    ReservoirManager.EventSender<SpanEvent> collectorSpanEventSender = new CollectorSpanEventSender(rpmServiceManager);
    AgentConfig agentConfig = configService.getDefaultAgentConfig();
    Consumer<SpanEvent> eventStorageBackend = buildStorageBackendConsumer(reservoirManager);
    SpanErrorBuilder defaultSpanErrorBuilder = buildDefaultSpanErrorBuilder(agentConfig);
    Map<String, SpanErrorBuilder> errorBuilderForApp = buildSpanEventErrorBuilder(agentConfig, defaultSpanErrorBuilder);
    configureUpdateOnConfigChange(errorBuilderForApp);
    TracerToSpanEvent tracerToSpanEvent = new TracerToSpanEvent(errorBuilderForApp, environmentService, transactionDataToDistributedTraceIntrinsics, defaultSpanErrorBuilder);
    SpanEventsServiceImpl result = SpanEventsServiceImpl.builder().agentConfig(agentConfig).reservoirManager(reservoirManager).collectorSender(collectorSpanEventSender).eventBackendStorage(eventStorageBackend).spanEventCreationDecider(spanEventCreationDecider).tracerToSpanEvent(tracerToSpanEvent).build();
    configService.addIAgentConfigListener(result);
    transactionService.addTransactionListener(result);
    return result;
}
Also used : AgentConfig(com.newrelic.agent.config.AgentConfig) SpanEventsServiceImpl(com.newrelic.agent.service.analytics.SpanEventsServiceImpl) CollectorSpanEventSender(com.newrelic.agent.service.analytics.CollectorSpanEventSender) SpanErrorBuilder(com.newrelic.agent.service.analytics.SpanErrorBuilder) TracerToSpanEvent(com.newrelic.agent.service.analytics.TracerToSpanEvent) SpanEvent(com.newrelic.agent.model.SpanEvent) ReservoirManager(com.newrelic.agent.interfaces.ReservoirManager) CollectorSpanEventReservoirManager(com.newrelic.agent.service.analytics.CollectorSpanEventReservoirManager) TracerToSpanEvent(com.newrelic.agent.service.analytics.TracerToSpanEvent)

Example 42 with SpanEvent

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

the class DataCollectionConfigCrossAgentTest method createAndVerifySpanEvents.

private void createAndVerifySpanEvents(Long expectedCount, Long expectedEndpointCount) {
    SpanEventsService spanEventsService = setUpSpanEventsService();
    long eventsToCreate = 1;
    if (expectedCount > 1) {
        eventsToCreate = expectedCount;
    }
    for (long i = 0; i < eventsToCreate; i++) {
        spanEventsService.storeEvent(new SpanEventFactory(APP_NAME).setName("Span").build());
    }
    // Verify that the correct number of events were stored in the reservoir
    SamplingPriorityQueue<SpanEvent> eventQueue = spanEventsService.getOrCreateDistributedSamplingReservoir(APP_NAME);
    assertNotNull(eventQueue);
    assertEquals(expectedCount.intValue(), eventQueue.size());
    // Verify that we sent (or didn't send) the appropriate events
    spanEventsService.harvestEvents(APP_NAME);
    int spanEventsSeen = ((MockRPMService) serviceManager.getRPMServiceManager().getRPMService(APP_NAME)).getSpanEventsSeen();
    assertEquals(expectedEndpointCount.intValue(), spanEventsSeen);
}
Also used : SpanEventFactory(com.newrelic.agent.service.analytics.SpanEventFactory) SpanEventsService(com.newrelic.agent.service.analytics.SpanEventsService) SpanEvent(com.newrelic.agent.model.SpanEvent)

Example 43 with SpanEvent

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

the class DistributedTraceCrossAgentTest method runTest.

@Test
public void runTest() throws ParseException, IOException {
    String accountKey = (String) jsonTest.get("trusted_account_key");
    String accountId = (String) jsonTest.get("account_id");
    String transportType = (String) jsonTest.get("transport_type");
    Boolean webTransaction = (Boolean) jsonTest.get("web_transaction");
    Boolean raisesException = (Boolean) jsonTest.get("raises_exception");
    Boolean forceSampledTrue = (Boolean) jsonTest.get("force_sampled_true");
    Long majorVersion = (Long) jsonTest.get("major_version");
    Long minorVersion = (Long) jsonTest.get("minor_version");
    Boolean spanEventsEnabled = (Boolean) jsonTest.get("span_events_enabled");
    replaceConfig(spanEventsEnabled);
    Assert.assertEquals(majorVersion.intValue(), ServiceFactory.getDistributedTraceService().getMajorSupportedCatVersion());
    Assert.assertEquals(minorVersion.intValue(), ServiceFactory.getDistributedTraceService().getMinorSupportedCatVersion());
    JSONArray outbound_payloads = (JSONArray) jsonTest.get("outbound_payloads");
    JSONArray inbound_payloads = (JSONArray) jsonTest.get("inbound_payloads");
    ArrayList expectedMetrics = (ArrayList) jsonTest.get("expected_metrics");
    Map<String, Object> intrinsics = (Map<String, Object>) jsonTest.get("intrinsics");
    Map<String, Object> commonAssertions = (Map<String, Object>) intrinsics.get("common");
    ArrayList targetEvents = (ArrayList) intrinsics.get("target_events");
    Map<String, Object> transactionAssertions = (Map<String, Object>) intrinsics.get("Transaction");
    Map<String, Object> spanAssertions = (Map<String, Object>) intrinsics.get("Span");
    Map<String, Object> connectInfo = new HashMap<>();
    connectInfo.put(DistributedTracingConfig.ACCOUNT_ID, accountId);
    connectInfo.put(DistributedTracingConfig.TRUSTED_ACCOUNT_KEY, accountKey);
    AgentConfig agentConfig = AgentHelper.createAgentConfig(true, Collections.<String, Object>emptyMap(), connectInfo);
    distributedTraceService.connected(null, agentConfig);
    Transaction.clearTransaction();
    TransactionActivity.clear();
    spanEventsService.clearReservoir();
    Transaction tx = Transaction.getTransaction();
    TransactionData transactionData = new TransactionData(tx, 0);
    TransactionStats transactionStats = transactionData.getTransaction().getTransactionActivity().getTransactionStats();
    SamplingPriorityQueue<SpanEvent> eventPool = spanEventsService.getOrCreateDistributedSamplingReservoir(APP_NAME);
    Tracer rootTracer;
    if (webTransaction) {
        rootTracer = TransactionAsyncUtility.createDispatcherTracer(this, "WebTransaction");
    } else {
        rootTracer = TransactionAsyncUtility.createOtherTracer("OtherTransaction");
    }
    tx.getTransactionActivity().tracerStarted(rootTracer);
    if (raisesException) {
        tx.setThrowable(new Throwable(), TransactionErrorPriority.API, false);
    }
    setTransportType(tx, transportType);
    if (inbound_payloads != null) {
        for (Object payload : inbound_payloads) {
            tx.acceptDistributedTracePayload(payload.toString());
        }
    } else {
        tx.acceptDistributedTracePayload((String) null);
    }
    if (forceSampledTrue) {
        tx.setPriorityIfNotNull(new Random().nextFloat() + 1.0f);
    }
    if (outbound_payloads != null) {
        for (Object assertion : outbound_payloads) {
            JSONObject payloadAssertions = (JSONObject) assertion;
            DistributedTracePayload payload = new BoundTransactionApiImpl(tx).createDistributedTracePayload();
            assertOutboundPayload(payloadAssertions, payload);
        }
    }
    rootTracer.finish(Opcodes.RETURN, 0);
    distributedTraceService.dispatcherTransactionFinished(transactionData, transactionStats);
    ((SpanEventsServiceImpl) spanEventsService).dispatcherTransactionFinished(transactionData, transactionStats);
    List<SpanEvent> spans = eventPool.asList();
    TransactionEvent transactionEvent = ServiceFactory.getTransactionEventsService().createEvent(transactionData, transactionStats, "wat");
    JSONObject txnEvents = serializeAndParseEvents(transactionEvent);
    assertExpectedMetrics(expectedMetrics, transactionStats);
    for (Object event : targetEvents) {
        if (event.toString().startsWith("Transaction") && transactionAssertions != null) {
            assertTransactionEvents(transactionAssertions, txnEvents);
            assertTransactionEvents(commonAssertions, txnEvents);
        } else if (event.toString().startsWith("Span") && spanAssertions != null) {
            assertNotEquals("Expected some spans!", 0, spans.size());
            assertSpanEvents(spanAssertions, spans);
            assertSpanEvents(commonAssertions, spans);
        }
    }
}
Also used : HashMap(java.util.HashMap) Tracer(com.newrelic.agent.tracers.Tracer) DistributedTracePayload(com.newrelic.api.agent.DistributedTracePayload) JSONArray(org.json.simple.JSONArray) ArrayList(java.util.ArrayList) AgentConfig(com.newrelic.agent.config.AgentConfig) TransactionEvent(com.newrelic.agent.service.analytics.TransactionEvent) TransactionStats(com.newrelic.agent.stats.TransactionStats) Random(java.util.Random) JSONObject(org.json.simple.JSONObject) SpanEventsServiceImpl(com.newrelic.agent.service.analytics.SpanEventsServiceImpl) SpanEvent(com.newrelic.agent.model.SpanEvent) JSONObject(org.json.simple.JSONObject) Map(java.util.Map) HashMap(java.util.HashMap) Test(org.junit.Test)

Example 44 with SpanEvent

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

the class DistributedTracingTest method testCrossApplicationTracingDisabled.

@Test
public void testCrossApplicationTracingDisabled() throws Exception {
    EnvironmentHolder holder = setupEnvironemntHolder("cross_application_disabled_test");
    TransactionService transactionService = ServiceFactory.getTransactionService();
    final CountDownLatch latch = new CountDownLatch(1);
    TransactionStatsListener listener = new TransactionStatsListener() {

        @Override
        public void dispatcherTransactionStatsFinished(TransactionData transactionData, TransactionStats transactionStats) {
            // Use this to ensure that the transaction fully finished and that it
            // didn't bail out early (transaction stats listeners are fired at the end of tx processing)
            latch.countDown();
        }
    };
    try {
        transactionService.addTransactionStatsListener(listener);
        noCreateOrAcceptPayload();
        // Wait up to 30 seconds for the transaction to finish, if it doesn't then it means we encountered an issue and it never finished
        latch.await(30, TimeUnit.SECONDS);
        SpanEventsService spanEventsService = ServiceFactory.getServiceManager().getSpanEventsService();
        String appName = ServiceFactory.getConfigService().getDefaultAgentConfig().getApplicationName();
        SamplingPriorityQueue<SpanEvent> spanEventsPool = spanEventsService.getOrCreateDistributedSamplingReservoir(appName);
        assertNotNull(spanEventsPool);
        List<SpanEvent> spanEvents = spanEventsPool.asList();
        assertNotNull(spanEvents);
        assertEquals(1, spanEvents.size());
        spanEventsPool.clear();
        SpanEvent firstSpanEvent = Iterables.getFirst(spanEvents, null);
        assertNotNull(firstSpanEvent);
        String traceId = firstSpanEvent.getTraceId();
        for (SpanEvent event : spanEvents) {
            // Assert that all tracers have the same traceId
            assertEquals(traceId, event.getTraceId());
        }
        TransactionDataList transactionList = holder.getTransactionList();
        assertNotNull(transactionList);
        assertEquals(1, transactionList.size());
    } finally {
        transactionService.removeTransactionStatsListener(listener);
        holder.close();
    }
}
Also used : TransactionStatsListener(com.newrelic.agent.TransactionStatsListener) TransactionDataList(com.newrelic.agent.TransactionDataList) TransactionStats(com.newrelic.agent.stats.TransactionStats) TransactionService(com.newrelic.agent.TransactionService) SpanEventsService(com.newrelic.agent.service.analytics.SpanEventsService) SpanEvent(com.newrelic.agent.model.SpanEvent) TransactionData(com.newrelic.agent.TransactionData) CountDownLatch(java.util.concurrent.CountDownLatch) Test(org.junit.Test)

Example 45 with SpanEvent

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

the class RootSpanStatusErrorsTest method assertRootSpanAttributes.

private void assertRootSpanAttributes(Integer expectedStatus, String errorClass) {
    SamplingPriorityQueue<SpanEvent> reservoir = ServiceFactory.getSpanEventService().getOrCreateDistributedSamplingReservoir(APP_NAME);
    assertTrue(reservoir.asList().size() > 0);
    List<String> seenNames = new LinkedList<>();
    for (SpanEvent event : reservoir.asList()) {
        if (!event.getName().startsWith("Java/")) {
            seenNames.add(event.getName());
            continue;
        }
        assertEquals("wrong error.status attribute", expectedStatus, event.getAgentAttributes().get("error.status"));
        assertEquals("wrong error.class attribute", errorClass, event.getAgentAttributes().get("error.class"));
        return;
    }
    assertEquals("Did not find root span!", Collections.emptyList(), seenNames);
}
Also used : SpanEvent(com.newrelic.agent.model.SpanEvent) LinkedList(java.util.LinkedList)

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