Search in sources :

Example 6 with SpanEventsServiceImpl

use of com.newrelic.agent.service.analytics.SpanEventsServiceImpl 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 7 with SpanEventsServiceImpl

use of com.newrelic.agent.service.analytics.SpanEventsServiceImpl 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 8 with SpanEventsServiceImpl

use of com.newrelic.agent.service.analytics.SpanEventsServiceImpl in project newrelic-java-agent by newrelic.

the class SegmentTest method cleanup.

@After
public void cleanup() {
    TransactionActivity.clear();
    SpanEventsServiceImpl spanEventsService = (SpanEventsServiceImpl) ServiceFactory.getSpanEventService();
    spanEventsService.clearReservoir();
    Transaction.clearTransaction();
    ServiceFactory.getTransactionService().removeTransactionListener(this);
}
Also used : SpanEventsServiceImpl(com.newrelic.agent.service.analytics.SpanEventsServiceImpl) After(org.junit.After)

Example 9 with SpanEventsServiceImpl

use of com.newrelic.agent.service.analytics.SpanEventsServiceImpl in project newrelic-java-agent by newrelic.

the class DefaultTracerTest method testTransactionABParenting.

/**
 * Transaction A has spans 1, 2, and 3. Span 2 is active when a distributed tracing payload is created.
 * Transaction B has spans 5 and 6. The payload from span 2 is accepted when span 5 is active.
 *
 * Verify that all the parenting attributes are correct.
 */
@Test
public void testTransactionABParenting() {
    DistributedTraceServiceImpl dts = (DistributedTraceServiceImpl) ServiceFactory.getServiceManager().getDistributedTraceService();
    Map<String, Object> configMap = ImmutableMap.<String, Object>builder().put("distributed_tracing", ImmutableMap.builder().put("account_id", "12345").put("trusted_account_key", "67890").put("primary_application_id", "789").build()).build();
    dts.connected(null, AgentConfigFactory.createAgentConfig(configMap, null, null));
    TransactionActivity.clear();
    Transaction.clearTransaction();
    Transaction tx = Transaction.getTransaction();
    DefaultTracer span1Tracer = new OtherRootTracer(tx, new ClassMethodSignature("class", "span1", "()V"), null, DefaultTracer.NULL_METRIC_NAME_FORMATTER);
    tx.getTransactionActivity().tracerStarted(span1Tracer);
    tx.setTransactionName(com.newrelic.api.agent.TransactionNamePriority.CUSTOM_HIGH, true, "Transaction A");
    DefaultTracer span2Tracer = new OtherRootTracer(tx, new ClassMethodSignature("class", "span2", "()V"), null, DefaultTracer.NULL_METRIC_NAME_FORMATTER);
    tx.getTransactionActivity().tracerStarted(span2Tracer);
    DistributedTracePayload payload = new BoundTransactionApiImpl(span2Tracer.getTransaction()).createDistributedTracePayload();
    DefaultTracer span3Tracer = new OtherRootTracer(tx, new ClassMethodSignature("class", "span3", "()V"), null, DefaultTracer.NULL_METRIC_NAME_FORMATTER);
    tx.getTransactionActivity().tracerStarted(span3Tracer);
    span3Tracer.finish(Opcodes.ARETURN, null);
    span2Tracer.finish(Opcodes.ARETURN, null);
    span1Tracer.finish(Opcodes.ARETURN, null);
    TransactionActivity.clear();
    Transaction.clearTransaction();
    Transaction txB = Transaction.getTransaction();
    DefaultTracer span5Tracer = new OtherRootTracer(txB, new ClassMethodSignature("class", "span5", "()V"), null, DefaultTracer.NULL_METRIC_NAME_FORMATTER);
    txB.getTransactionActivity().tracerStarted(span5Tracer);
    span5Tracer.getTransaction().acceptDistributedTracePayload(payload);
    txB.setTransactionName(com.newrelic.api.agent.TransactionNamePriority.CUSTOM_HIGH, true, "Transaction B");
    txB.setThrowable(new Throwable(), TransactionErrorPriority.API, false);
    DefaultTracer span6Tracer = new OtherRootTracer(txB, new ClassMethodSignature("class", "span6", "()V"), null, DefaultTracer.NULL_METRIC_NAME_FORMATTER);
    txB.getTransactionActivity().tracerStarted(span6Tracer);
    span6Tracer.finish(Opcodes.ARETURN, null);
    span5Tracer.finish(Opcodes.ARETURN, null);
    // assert traceId
    SpanEventsService spanEventService = ServiceFactory.getSpanEventService();
    final TransactionData tdA = new TransactionData(tx, 1024);
    ((SpanEventsServiceImpl) spanEventService).dispatcherTransactionFinished(tdA, new TransactionStats());
    final TransactionData tdB = new TransactionData(txB, 1024);
    ((SpanEventsServiceImpl) spanEventService).dispatcherTransactionFinished(tdB, new TransactionStats());
    SamplingPriorityQueue<SpanEvent> eventPool = spanEventService.getOrCreateDistributedSamplingReservoir(APP_NAME);
    List<SpanEvent> spanEvents = eventPool.asList();
    assertEquals(5, spanEvents.size());
    TransactionEvent txAEvent = ServiceFactory.getTransactionEventsService().createEvent(tdA, new TransactionStats(), tdA.getBlameMetricName());
    TransactionEvent txBEvent = ServiceFactory.getTransactionEventsService().createEvent(tdB, new TransactionStats(), tdB.getBlameMetricName());
    SpanEvent span1 = getSpanByName(eventPool, "Java/class/span1");
    SpanEvent span2 = getSpanByName(eventPool, "Java/class/span2");
    SpanEvent span3 = getSpanByName(eventPool, "Java/class/span3");
    SpanEvent span5 = getSpanByName(eventPool, "Java/class/span5");
    SpanEvent span6 = getSpanByName(eventPool, "Java/class/span6");
    DistributedTracePayloadImpl parsedPayload = new DistributedTracePayloadParser(NewRelic.getAgent().getMetricAggregator(), ServiceFactory.getDistributedTraceService(), Agent.LOG).parse(null, payload.text());
    assertEquals(tx.getGuid(), parsedPayload.txnId);
    assertEquals(tx.sampled(), parsedPayload.sampled.booleanValue());
    assertEquals(tx.getPriority(), parsedPayload.priority, 0.0f);
    assertEquals(span2Tracer.getGuid(), parsedPayload.guid);
    assertEquals(span2.getGuid(), parsedPayload.guid);
    assertEquals(txAEvent.getTripId(), parsedPayload.traceId);
    assertEquals(txBEvent.getTripId(), parsedPayload.traceId);
    assertEquals(span1.getTraceId(), span2.getTraceId());
    assertEquals(span2.getTraceId(), span3.getTraceId());
    assertEquals(span3.getTraceId(), span5.getTraceId());
    assertEquals(span5.getTraceId(), span6.getTraceId());
    assertEquals(txAEvent.getGuid(), span1.getTransactionId());
    assertEquals(span1.getGuid(), span2.getParentId());
    assertEquals(txAEvent.getGuid(), span2.getTransactionId());
    assertEquals(span2.getGuid(), span3.getParentId());
    assertEquals(txAEvent.getGuid(), span3.getTransactionId());
    assertEquals(txAEvent.getGuid(), txBEvent.getParentId());
    assertEquals(span2.getGuid(), txBEvent.getParenSpanId());
    assertEquals(span2.getGuid(), span5.getParentId());
    assertEquals(txBEvent.getGuid(), span5.getTransactionId());
    assertEquals(span5.getGuid(), span6.getParentId());
    assertEquals(txBEvent.getGuid(), span6.getTransactionId());
    ServiceFactory.getTransactionService().transactionFinished(tdB, new TransactionStats());
    ErrorServiceImpl errorService = (ErrorServiceImpl) txB.getRPMService().getErrorService();
    DistributedSamplingPriorityQueue<ErrorEvent> reservoir = errorService.getReservoir(ServiceFactory.getConfigService().getDefaultAgentConfig().getApplicationName());
    List<ErrorEvent> errorEvents = reservoir.asList();
    assertEquals(1, errorEvents.size());
    ErrorEvent errorEvent = errorEvents.get(0);
    Map<String, Object> errorDtAtts = errorEvent.getDistributedTraceIntrinsics();
    assertEquals(txBEvent.getTripId(), errorDtAtts.get("traceId"));
    assertEquals(txBEvent.getGuid(), errorEvent.getTransactionGuid());
}
Also used : ErrorServiceImpl(com.newrelic.agent.errors.ErrorServiceImpl) DistributedTracePayload(com.newrelic.api.agent.DistributedTracePayload) DistributedTracePayloadImpl(com.newrelic.agent.tracing.DistributedTracePayloadImpl) TransactionEvent(com.newrelic.agent.service.analytics.TransactionEvent) TransactionData(com.newrelic.agent.TransactionData) BoundTransactionApiImpl(com.newrelic.agent.BoundTransactionApiImpl) DistributedTraceServiceImpl(com.newrelic.agent.tracing.DistributedTraceServiceImpl) SpanEventsService(com.newrelic.agent.service.analytics.SpanEventsService) DistributedTracePayloadParser(com.newrelic.agent.tracing.DistributedTracePayloadParser) TransactionStats(com.newrelic.agent.stats.TransactionStats) Transaction(com.newrelic.agent.Transaction) SpanEventsServiceImpl(com.newrelic.agent.service.analytics.SpanEventsServiceImpl) SpanEvent(com.newrelic.agent.model.SpanEvent) ErrorEvent(com.newrelic.agent.model.ErrorEvent) Test(org.junit.Test)

Example 10 with SpanEventsServiceImpl

use of com.newrelic.agent.service.analytics.SpanEventsServiceImpl in project newrelic-java-agent by newrelic.

the class DefaultTracerTest method testMultiTransactionSpanEvents.

@Test
public void testMultiTransactionSpanEvents() {
    DistributedTraceServiceImpl dts = (DistributedTraceServiceImpl) ServiceFactory.getServiceManager().getDistributedTraceService();
    Map<String, Object> configMap = ImmutableMap.<String, Object>builder().put("cross_application_tracer", ImmutableMap.builder().put("cross_process_id", "12345#whatever").put("trusted_account_key", "67890").build()).build();
    dts.connected(null, AgentConfigFactory.createAgentConfig(configMap, null, null));
    DefaultTracer firstTracer = prepareTracer();
    BoundTransactionApiImpl firstTxn = new BoundTransactionApiImpl(firstTracer.getTransaction());
    DistributedTracePayload payload = firstTxn.createDistributedTracePayload();
    firstTracer.finish(0, null);
    Transaction.clearTransaction();
    DefaultTracer secondTracer = prepareTracer();
    BoundTransactionApiImpl secondTxn = new BoundTransactionApiImpl(secondTracer.getTransaction());
    secondTxn.acceptDistributedTracePayload(payload);
    DistributedTracePayload secondPayload = secondTxn.createDistributedTracePayload();
    secondTracer.finish(0, null);
    Transaction.clearTransaction();
    DefaultTracer thirdTracer = prepareTracer();
    BoundTransactionApiImpl thirdTxn = new BoundTransactionApiImpl(thirdTracer.getTransaction());
    thirdTxn.acceptDistributedTracePayload(secondPayload);
    DistributedTracePayload thirdPayload = thirdTxn.createDistributedTracePayload();
    thirdTracer.finish(0, null);
    Transaction.clearTransaction();
    DefaultTracer fourthTracer = prepareTracer();
    BoundTransactionApiImpl fourthTxn = new BoundTransactionApiImpl(fourthTracer.getTransaction());
    fourthTxn.acceptDistributedTracePayload(thirdPayload);
    fourthTracer.finish(0, null);
    SpanEventsService spanEventService = ServiceFactory.getSpanEventService();
    ((SpanEventsServiceImpl) spanEventService).dispatcherTransactionFinished(new TransactionData(firstTracer.getTransaction(), 1024), new TransactionStats());
    ((SpanEventsServiceImpl) spanEventService).dispatcherTransactionFinished(new TransactionData(secondTracer.getTransaction(), 1024), new TransactionStats());
    ((SpanEventsServiceImpl) spanEventService).dispatcherTransactionFinished(new TransactionData(thirdTracer.getTransaction(), 1024), new TransactionStats());
    ((SpanEventsServiceImpl) spanEventService).dispatcherTransactionFinished(new TransactionData(fourthTracer.getTransaction(), 1024), new TransactionStats());
    SamplingPriorityQueue<SpanEvent> eventPool = spanEventService.getOrCreateDistributedSamplingReservoir(APP_NAME);
    List<SpanEvent> spanEvents = eventPool.asList();
    assertNotNull(spanEvents);
    assertEquals(4, spanEvents.size());
    // Make sure traceIds of all span events matches trace id of first payload sent
    DistributedTracePayloadImpl parsedPayload = new DistributedTracePayloadParser(NewRelic.getAgent().getMetricAggregator(), ServiceFactory.getDistributedTraceService(), Agent.LOG).parse(null, payload.text());
    assertNotNull(parsedPayload);
    for (SpanEvent event : spanEvents) {
        assertEquals("Span events must have same trace id", parsedPayload.traceId, event.getTraceId());
    }
}
Also used : BoundTransactionApiImpl(com.newrelic.agent.BoundTransactionApiImpl) DistributedTraceServiceImpl(com.newrelic.agent.tracing.DistributedTraceServiceImpl) DistributedTracePayload(com.newrelic.api.agent.DistributedTracePayload) SpanEventsService(com.newrelic.agent.service.analytics.SpanEventsService) DistributedTracePayloadParser(com.newrelic.agent.tracing.DistributedTracePayloadParser) DistributedTracePayloadImpl(com.newrelic.agent.tracing.DistributedTracePayloadImpl) TransactionStats(com.newrelic.agent.stats.TransactionStats) SpanEventsServiceImpl(com.newrelic.agent.service.analytics.SpanEventsServiceImpl) SpanEvent(com.newrelic.agent.model.SpanEvent) TransactionData(com.newrelic.agent.TransactionData) Test(org.junit.Test)

Aggregations

SpanEventsServiceImpl (com.newrelic.agent.service.analytics.SpanEventsServiceImpl)14 SpanEvent (com.newrelic.agent.model.SpanEvent)12 TransactionStats (com.newrelic.agent.stats.TransactionStats)11 Test (org.junit.Test)11 SpanEventsService (com.newrelic.agent.service.analytics.SpanEventsService)10 TransactionData (com.newrelic.agent.TransactionData)7 AgentConfig (com.newrelic.agent.config.AgentConfig)3 DistributedTracePayload (com.newrelic.api.agent.DistributedTracePayload)3 InetSocketAddress (java.net.InetSocketAddress)3 Connection (java.sql.Connection)3 DummyConnection (sql.DummyConnection)3 DummyResultSet (sql.DummyResultSet)3 BoundTransactionApiImpl (com.newrelic.agent.BoundTransactionApiImpl)2 TransactionEvent (com.newrelic.agent.service.analytics.TransactionEvent)2 DistributedTracePayloadImpl (com.newrelic.agent.tracing.DistributedTracePayloadImpl)2 DistributedTracePayloadParser (com.newrelic.agent.tracing.DistributedTracePayloadParser)2 DistributedTraceServiceImpl (com.newrelic.agent.tracing.DistributedTraceServiceImpl)2 AgentImpl (com.newrelic.agent.AgentImpl)1 MockRPMServiceManager (com.newrelic.agent.MockRPMServiceManager)1 MockServiceManager (com.newrelic.agent.MockServiceManager)1