Search in sources :

Example 71 with SpanEvent

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

the class DistributedSamplingPriorityQueueTest method seedEventPool.

private void seedEventPool(DistributedSamplingPriorityQueue<SpanEvent> eventPool) {
    // Custom events
    SpanEvent spanEvent = new SpanEventFactory("appName").setGuid("1").setCategory(SpanCategory.generic).setPriority(5.0f).build();
    eventPool.add(spanEvent);
    spanEvent = new SpanEventFactory("appName").setGuid("2").setCategory(SpanCategory.generic).setPriority(3.0f).build();
    eventPool.add(spanEvent);
    spanEvent = new SpanEventFactory("appName").setGuid("3").setCategory(SpanCategory.generic).setPriority(1.0f).build();
    eventPool.add(spanEvent);
    // Datastore events
    spanEvent = new SpanEventFactory("appName").setGuid("4").setCategory(SpanCategory.datastore).setPriority(5.0f).build();
    eventPool.add(spanEvent);
    spanEvent = new SpanEventFactory("appName").setGuid("5").setCategory(SpanCategory.datastore).setPriority(3.0f).build();
    eventPool.add(spanEvent);
    spanEvent = new SpanEventFactory("appName").setGuid("6").setCategory(SpanCategory.datastore).setPriority(1.0f).build();
    eventPool.add(spanEvent);
    // External events
    spanEvent = new SpanEventFactory("appName").setGuid("7").setCategory(SpanCategory.http).setPriority(5.0f).build();
    eventPool.add(spanEvent);
    spanEvent = new SpanEventFactory("appName").setGuid("8").setCategory(SpanCategory.http).setPriority(3.0f).build();
    eventPool.add(spanEvent);
    spanEvent = new SpanEventFactory("appName").setGuid("9").setCategory(SpanCategory.http).setPriority(1.0f).build();
    eventPool.add(spanEvent);
}
Also used : SpanEvent(com.newrelic.agent.model.SpanEvent)

Example 72 with SpanEvent

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

the class CollectorSpanEventReservoirManager method attemptToSendReservoir.

@Override
public HarvestResult attemptToSendReservoir(final String appName, EventSender<SpanEvent> eventSender, Logger logger) {
    if (getMaxSamplesStored() <= 0) {
        clearReservoir();
        return null;
    }
    SpanEventsConfig config = configService.getAgentConfig(appName).getSpanEventsConfig();
    int decidedLast = AdaptiveSampling.decidedLast(spanReservoirsForApp.get(appName), config.getTargetSamplesStored());
    // save a reference to the old reservoir to finish harvesting, and create a new one
    final SamplingPriorityQueue<SpanEvent> toSend = spanReservoirsForApp.get(appName);
    spanReservoirsForApp.put(appName, createDistributedSamplingReservoir(appName, decidedLast));
    if (toSend == null || toSend.size() <= 0) {
        return null;
    }
    try {
        eventSender.sendEvents(appName, config.getMaxSamplesStored(), toSend.getNumberOfTries(), Collections.unmodifiableList(toSend.asList()));
        if (toSend.size() < toSend.getNumberOfTries()) {
            int dropped = toSend.getNumberOfTries() - toSend.size();
            logger.log(Level.FINE, "Dropped {0} span events out of {1}.", dropped, toSend.getNumberOfTries());
        }
        return new HarvestResult(toSend.getNumberOfTries(), toSend.size());
    } catch (HttpError e) {
        if (!e.discardHarvestData()) {
            logger.log(Level.FINE, "Unable to send span events. Unsent events will be included in the next harvest.", e);
            // Save unsent data by merging it with toSend data using reservoir algorithm
            spanReservoirsForApp.get(appName).retryAll(toSend);
        } else {
            // discard harvest data
            toSend.clear();
            logger.log(Level.FINE, "Unable to send span events. Unsent events will be dropped.", e);
        }
    } catch (Exception e) {
        // discard harvest data
        toSend.clear();
        logger.log(Level.FINE, "Unable to send span events. Unsent events will be dropped.", e);
    }
    return null;
}
Also used : SpanEventsConfig(com.newrelic.agent.config.SpanEventsConfig) SpanEvent(com.newrelic.agent.model.SpanEvent) HttpError(com.newrelic.agent.transport.HttpError)

Example 73 with SpanEvent

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

the class SpanEventsServiceImpl method createAndStoreSpanEvent.

private void createAndStoreSpanEvent(Tracer tracer, TransactionData transactionData, boolean isRoot, TransactionStats transactionStats) {
    boolean crossProcessOnly = spanEventsConfig.isCrossProcessOnly();
    if (crossProcessOnly && !isCrossProcessTracer(tracer)) {
        // We are in "cross_process_only" mode and we have a non datastore/external tracer. Return before we create anything.
        return;
    }
    String appName = transactionData.getApplicationName();
    SamplingPriorityQueue<SpanEvent> reservoir = getOrCreateDistributedSamplingReservoir(appName);
    if (reservoir.isFull() && reservoir.getMinPriority() >= transactionData.getPriority()) {
        // The reservoir is full and this event wouldn't make it in, so lets prevent some object allocations
        reservoir.incrementNumberOfTries();
        return;
    }
    SpanEvent spanEvent = tracerToSpanEvent.createSpanEvent(tracer, transactionData, transactionStats, isRoot, crossProcessOnly);
    storeEvent(spanEvent);
}
Also used : SpanEvent(com.newrelic.agent.model.SpanEvent)

Example 74 with SpanEvent

use of com.newrelic.agent.model.SpanEvent 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 75 with SpanEvent

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

the class DefaultTracerTest method before.

@Before
public void before() throws Exception {
    SpanEventsService spanEventService = ServiceFactory.getSpanEventService();
    APP_NAME = ServiceFactory.getConfigService().getDefaultAgentConfig().getApplicationName();
    SamplingPriorityQueue<SpanEvent> eventPool = spanEventService.getOrCreateDistributedSamplingReservoir(APP_NAME);
    eventPool.clear();
}
Also used : SpanEventsService(com.newrelic.agent.service.analytics.SpanEventsService) SpanEvent(com.newrelic.agent.model.SpanEvent) Before(org.junit.Before)

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