Search in sources :

Example 1 with DistributedSamplingPriorityQueue

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

the class DistributedTraceServiceImplTest method testShouldSampleFirst10.

@Test
public void testShouldSampleFirst10() {
    TransactionEventsService transactionEventsService = Mockito.mock(TransactionEventsService.class);
    serviceManager.setTransactionEventsService(transactionEventsService);
    distributedTraceService = new DistributedTraceServiceImpl();
    serviceManager.setDistributedTraceService(distributedTraceService);
    DistributedSamplingPriorityQueue reservoir = Mockito.mock(DistributedSamplingPriorityQueue.class);
    doReturn(reservoir).when(transactionEventsService).getOrCreateDistributedSamplingReservoir("Test");
    when(reservoir.getTarget()).thenReturn(10);
    for (int i = 0; i < 10; i++) {
        doReturn(i + 1).when(reservoir).getNumberOfTries();
        assertTrue(DistributedTraceUtil.isSampledPriority(DistributedTraceServiceImplTest.distributedTraceService.calculatePriority(null, reservoir)));
    }
    // Now that we've sampled the first 10, the rest will be ignored until after the harvest
    doReturn(100001).when(reservoir).getNumberOfTries();
    doReturn(10).when(reservoir).getDecided();
    // fake a very high target to ensure a sample later on
    doReturn(100000).when(reservoir).getTarget();
    assertTrue(distributedTraceService.calculatePriority(null, reservoir) < 1.0f);
    // Fake a harvest
    distributedTraceService.beforeHarvest("Test", null);
    // We should have "adaptively" sampled at least one more trace
    assertTrue(DistributedTraceUtil.isSampledPriority(DistributedTraceServiceImplTest.distributedTraceService.calculatePriority(null, reservoir)));
}
Also used : TransactionEventsService(com.newrelic.agent.service.analytics.TransactionEventsService) DistributedSamplingPriorityQueue(com.newrelic.agent.service.analytics.DistributedSamplingPriorityQueue) Test(org.junit.Test)

Example 2 with DistributedSamplingPriorityQueue

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

the class LogSenderServiceImpl method harvestEvents.

/**
 * Harvest and send the LogEvents
 *
 * @param appName the application to harvest for
 */
public void harvestEvents(final String appName) {
    if (!getIsEnabledForApp(ServiceFactory.getConfigService().getAgentConfig(appName), appName)) {
        reservoirForApp.remove(appName);
        return;
    }
    if (maxSamplesStored <= 0) {
        clearReservoir(appName);
        return;
    }
    long startTimeInNanos = System.nanoTime();
    final DistributedSamplingPriorityQueue<LogEvent> reservoir = this.reservoirForApp.put(appName, new DistributedSamplingPriorityQueue<>(appName, LOG_SENDER_SERVICE, maxSamplesStored));
    if (reservoir != null && reservoir.size() > 0) {
        try {
            // Send LogEvents
            ServiceFactory.getRPMServiceManager().getOrCreateRPMService(appName).sendLogEvents(Collections.unmodifiableList(reservoir.asList()));
            final long durationInNanos = System.nanoTime() - startTimeInNanos;
            ServiceFactory.getStatsService().doStatsWork(new StatsWork() {

                @Override
                public void doWork(StatsEngine statsEngine) {
                    recordSupportabilityMetrics(statsEngine, durationInNanos, reservoir);
                }

                @Override
                public String getAppName() {
                    return appName;
                }
            }, LogSenderServiceImpl.class.getName());
            if (reservoir.size() < reservoir.getNumberOfTries()) {
                int dropped = reservoir.getNumberOfTries() - reservoir.size();
                Agent.LOG.log(Level.FINE, "Dropped {0} log events out of {1}.", dropped, reservoir.getNumberOfTries());
            }
        } catch (HttpError e) {
            if (!e.discardHarvestData()) {
                Agent.LOG.log(Level.FINE, "Unable to send log events. Unsent events will be included in the next harvest.", e);
                // Save unsent data by merging it with current data using reservoir algorithm
                DistributedSamplingPriorityQueue<LogEvent> currentReservoir = reservoirForApp.get(appName);
                currentReservoir.retryAll(reservoir);
            } else {
                // discard harvest data
                reservoir.clear();
                Agent.LOG.log(Level.FINE, "Unable to send log events. Unsent events will be dropped.", e);
            }
        } catch (Exception e) {
            // discard harvest data
            reservoir.clear();
            Agent.LOG.log(Level.FINE, "Unable to send log events. Unsent events will be dropped.", e);
        }
    }
}
Also used : LogEvent(com.newrelic.agent.model.LogEvent) StatsEngine(com.newrelic.agent.stats.StatsEngine) StatsWork(com.newrelic.agent.stats.StatsWork) HttpError(com.newrelic.agent.transport.HttpError) DistributedSamplingPriorityQueue(com.newrelic.agent.service.analytics.DistributedSamplingPriorityQueue)

Example 3 with DistributedSamplingPriorityQueue

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

the class ErrorServiceImpl method harvestEvents.

public void harvestEvents(final String appName) {
    boolean eventsEnabled = isEventsEnabledForApp(appName);
    if (!eventsEnabled) {
        reservoirForApp.remove(appName);
        return;
    }
    if (maxSamplesStored <= 0) {
        clearReservoir(appName);
        return;
    }
    long startTimeInNanos = System.nanoTime();
    final DistributedSamplingPriorityQueue<ErrorEvent> reservoir = reservoirForApp.put(appName, new DistributedSamplingPriorityQueue<ErrorEvent>(appName, "Error Service", maxSamplesStored));
    if (reservoir != null && reservoir.size() > 0) {
        try {
            ServiceFactory.getRPMServiceManager().getOrCreateRPMService(appName).sendErrorEvents(maxSamplesStored, reservoir.getNumberOfTries(), Collections.unmodifiableList(reservoir.asList()));
            final long durationInNanos = System.nanoTime() - startTimeInNanos;
            ServiceFactory.getStatsService().doStatsWork(new StatsWork() {

                @Override
                public void doWork(StatsEngine statsEngine) {
                    recordSupportabilityMetrics(statsEngine, durationInNanos, reservoir);
                }

                @Override
                public String getAppName() {
                    return appName;
                }
            }, reservoir.getServiceName());
            if (reservoir.size() < reservoir.getNumberOfTries()) {
                int dropped = reservoir.getNumberOfTries() - reservoir.size();
                Agent.LOG.log(Level.FINE, "Dropped {0} error events out of {1}.", dropped, reservoir.getNumberOfTries());
            }
        } catch (HttpError e) {
            if (!e.discardHarvestData()) {
                Agent.LOG.log(Level.FINE, "Unable to send error events. Unsent events will be included in the next harvest.", e);
                // Save unsent data by merging it with current data using reservoir algorithm
                DistributedSamplingPriorityQueue<ErrorEvent> currentReservoir = reservoirForApp.get(appName);
                currentReservoir.retryAll(reservoir);
            } else {
                // discard harvest data
                reservoir.clear();
                Agent.LOG.log(Level.FINE, "Unable to send error events. Unsent events will be dropped.", e);
            }
        } catch (Exception e) {
            // discard harvest data
            reservoir.clear();
            Agent.LOG.log(Level.FINE, "Unable to send error events. Unsent events will be dropped.", e);
        }
    }
}
Also used : StatsWork(com.newrelic.agent.stats.StatsWork) ErrorEvent(com.newrelic.agent.model.ErrorEvent) HttpError(com.newrelic.agent.transport.HttpError) DistributedSamplingPriorityQueue(com.newrelic.agent.service.analytics.DistributedSamplingPriorityQueue) StatsEngine(com.newrelic.agent.stats.StatsEngine)

Example 4 with DistributedSamplingPriorityQueue

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

the class MockSpanEventReservoirManager method createDistributedSamplingReservoir.

private SamplingPriorityQueue<SpanEvent> createDistributedSamplingReservoir(String appName, int decidedLast) {
    SpanEventsConfig spanEventsConfig = configService.getDefaultAgentConfig().getSpanEventsConfig();
    int target = spanEventsConfig.getTargetSamplesStored();
    return new DistributedSamplingPriorityQueue<>(appName, "Span Event Service", maxSamplesStored, decidedLast, target, SPAN_EVENT_COMPARATOR);
}
Also used : SpanEventsConfig(com.newrelic.agent.config.SpanEventsConfig) DistributedSamplingPriorityQueue(com.newrelic.agent.service.analytics.DistributedSamplingPriorityQueue)

Example 5 with DistributedSamplingPriorityQueue

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

the class DistributedTraceServiceImplTest method testPriorityIsUsed.

@Test
public void testPriorityIsUsed() {
    rpmServiceManager.getOrCreateRPMService("Test");
    // Create reservoir
    ServiceFactory.getTransactionEventsService().harvestEvents("Test");
    DistributedSamplingPriorityQueue<TransactionEvent> reservoir = new DistributedSamplingPriorityQueue<>(3);
    TransactionEventBuilder eventBuilder = new TransactionEventBuilder();
    eventBuilder.setPriority(.5f);
    TransactionEvent transactionEvent5 = eventBuilder.build();
    reservoir.add(transactionEvent5);
    eventBuilder.setPriority(.6f);
    TransactionEvent transactionEvent6 = eventBuilder.build();
    reservoir.add(transactionEvent6);
    eventBuilder.setPriority(.7f);
    TransactionEvent transactionEvent7 = eventBuilder.build();
    reservoir.add(transactionEvent7);
    eventBuilder.setPriority(.8f);
    TransactionEvent transactionEvent8 = eventBuilder.build();
    reservoir.add(transactionEvent8);
    eventBuilder.setPriority(.9f);
    TransactionEvent transactionEvent9 = eventBuilder.build();
    reservoir.add(transactionEvent9);
    List<TransactionEvent> events = reservoir.asList();
    assertTrue(events.contains(transactionEvent7));
    assertTrue(events.contains(transactionEvent8));
    assertTrue(events.contains(transactionEvent9));
    assertEquals(3, events.size());
    eventBuilder.setPriority(.4f);
    TransactionEvent transactionEvent4 = eventBuilder.build();
    reservoir.add(transactionEvent4);
    eventBuilder.setPriority(.3f);
    TransactionEvent transactionEvent3 = eventBuilder.build();
    reservoir.add(transactionEvent3);
    events = reservoir.asList();
    assertTrue(events.contains(transactionEvent7));
    assertTrue(events.contains(transactionEvent8));
    assertTrue(events.contains(transactionEvent9));
    assertEquals(3, events.size());
}
Also used : TransactionEvent(com.newrelic.agent.service.analytics.TransactionEvent) DistributedSamplingPriorityQueue(com.newrelic.agent.service.analytics.DistributedSamplingPriorityQueue) TransactionEventBuilder(com.newrelic.agent.service.analytics.TransactionEventBuilder) Test(org.junit.Test)

Aggregations

DistributedSamplingPriorityQueue (com.newrelic.agent.service.analytics.DistributedSamplingPriorityQueue)5 StatsEngine (com.newrelic.agent.stats.StatsEngine)2 StatsWork (com.newrelic.agent.stats.StatsWork)2 HttpError (com.newrelic.agent.transport.HttpError)2 Test (org.junit.Test)2 SpanEventsConfig (com.newrelic.agent.config.SpanEventsConfig)1 ErrorEvent (com.newrelic.agent.model.ErrorEvent)1 LogEvent (com.newrelic.agent.model.LogEvent)1 TransactionEvent (com.newrelic.agent.service.analytics.TransactionEvent)1 TransactionEventBuilder (com.newrelic.agent.service.analytics.TransactionEventBuilder)1 TransactionEventsService (com.newrelic.agent.service.analytics.TransactionEventsService)1