Search in sources :

Example 86 with StatsEngine

use of com.newrelic.agent.stats.StatsEngine in project newrelic-java-agent by newrelic.

the class JmxGetTest method testJmxGetStatsMulti.

@Test
public void testJmxGetStatsMulti() throws MalformedObjectNameException {
    StatsEngine stats = new StatsEngineImpl();
    MBeanServer server = ManagementFactory.getPlatformMBeanServer();
    List<JmxMetric> metrics = new ArrayList<>();
    metrics.add(JmxMetric.create("hello1", JmxType.SIMPLE));
    metrics.add(JmxMetric.create("goodbye1", JmxType.MONOTONICALLY_INCREASING));
    JmxGet object = new JmxMultiMBeanGet("ThreadPool:type=rara,key1=*", "ThreadPool:type=rara,key1=*", null, metrics, null, null);
    Map<String, Float> values = new HashMap<>();
    values.put("hello1", 5f);
    values.put("goodbye1", 4f);
    object.recordStats(stats, createMap("ThreadPool:type=rara,key1=a", values), server);
    Assert.assertEquals(5f, stats.getStats("JMX/ThreadPool/rara/a/hello1").getTotal(), .001);
    Assert.assertEquals(4f, stats.getStats("JMX/ThreadPool/rara/a/goodbye1").getTotal(), .001);
    values.clear();
    values.put("hello1", 6f);
    values.put("goodbye1", 7f);
    object.recordStats(stats, createMap("ThreadPool:type=rara,key1=a", values), server);
    Assert.assertEquals(11f, stats.getStats("JMX/ThreadPool/rara/a/hello1").getTotal(), .001);
    Assert.assertEquals(7f, stats.getStats("JMX/ThreadPool/rara/a/goodbye1").getTotal(), .001);
    Assert.assertEquals(2f, stats.getStats("JMX/ThreadPool/rara/a/hello1").getCallCount(), .001);
    Assert.assertEquals(2f, stats.getStats("JMX/ThreadPool/rara/a/goodbye1").getCallCount(), .001);
}
Also used : StatsEngineImpl(com.newrelic.agent.stats.StatsEngineImpl) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) JmxMetric(com.newrelic.agent.jmx.metrics.JmxMetric) StatsEngine(com.newrelic.agent.stats.StatsEngine) MBeanServer(javax.management.MBeanServer) Test(org.junit.Test)

Example 87 with StatsEngine

use of com.newrelic.agent.stats.StatsEngine in project newrelic-java-agent by newrelic.

the class JmxObjectFactoryTest method verifyYml.

private void verifyYml(List<JmxGet> actual) throws MalformedObjectNameException {
    StatsEngine stats = new StatsEngineImpl();
    Map<String, Float> values = new HashMap<>();
    MBeanServer server = ManagementFactory.getPlatformMBeanServer();
    Assert.assertEquals(4, actual.size());
    JmxGet one = actual.get(3);
    Assert.assertEquals("solr*:type=queryResultCache,*", one.getObjectName().toString());
    Assert.assertTrue(one.getAttributes().contains("lookups"));
    // LOOKUPS
    values.put("lookups", 1f);
    one.recordStats(stats, createMap("solr:type=test", values), server);
    Assert.assertEquals(1f, stats.getStats("JMX/solr/test/lookups").getTotal(), .001);
    // it should be monotonically increasing
    values.clear();
    values.put("lookups", 4f);
    one.recordStats(stats, createMap("solr:type=test", values), server);
    Assert.assertEquals(4f, stats.getStats("JMX/solr/test/lookups").getTotal(), .001);
    // HITS
    values.clear();
    values.put("hits", 1f);
    one.recordStats(stats, createMap("solr:type=test", values), server);
    Assert.assertEquals(1f, stats.getStats("JMX/solr/test/hits").getTotal(), .001);
    // it should be monotonically increasing
    values.clear();
    values.put("hits", 4f);
    one.recordStats(stats, createMap("solr:type=test", values), server);
    Assert.assertEquals(4f, stats.getStats("JMX/solr/test/hits").getTotal(), .001);
    // hitratio
    values.clear();
    values.put("hitratio", 1f);
    one.recordStats(stats, createMap("solr:type=test", values), server);
    Assert.assertEquals(1f, stats.getStats("JMX/solr/test/hitratio").getTotal(), .001);
    // it should be monotonically increasing
    values.clear();
    values.put("hitratio", 4f);
    one.recordStats(stats, createMap("solr:type=test", values), server);
    Assert.assertEquals(4f, stats.getStats("JMX/solr/test/hitratio").getTotal(), .001);
    // size
    values.clear();
    values.put("size", 1f);
    one.recordStats(stats, createMap("solr:type=test", values), server);
    Assert.assertEquals(1f, stats.getStats("JMX/solr/test/size").getTotal(), .001);
    // it should be monotonically increasing
    values.clear();
    values.put("size", 4f);
    one.recordStats(stats, createMap("solr:type=test", values), server);
    Assert.assertEquals(4f, stats.getStats("JMX/solr/test/size").getTotal(), .001);
    // cumulative_hitratio
    values.clear();
    values.put("cumulative_hitratio", 1f);
    one.recordStats(stats, createMap("solr:type=test", values), server);
    Assert.assertEquals(1f, stats.getStats("JMX/solr/test/cumulative_hitratio").getTotal(), .001);
    // it should be monotonically increasing
    values.clear();
    values.put("cumulative_hitratio", 4f);
    one.recordStats(stats, createMap("solr:type=test", values), server);
    Assert.assertEquals(4f, stats.getStats("JMX/solr/test/cumulative_hitratio").getTotal(), .001);
    stats = new StatsEngineImpl();
    JmxGet two = actual.get(2);
    // lookups
    values.clear();
    values.put("lookups", 1f);
    two.recordStats(stats, createMap("solr:type=test", values), server);
    Assert.assertEquals(1f, stats.getStats("JMX/solr/test/lookups").getTotal(), .001);
    // it should be monotonically increasing
    values.clear();
    values.put("lookups", 4f);
    two.recordStats(stats, createMap("solr:type=test", values), server);
    Assert.assertEquals(4f, stats.getStats("JMX/solr/test/lookups").getTotal(), .001);
    // size
    values.clear();
    values.put("size", 1f);
    two.recordStats(stats, createMap("solr:type=test", values), server);
    Assert.assertEquals(1f, stats.getStats("JMX/solr/test/size").getTotal(), .001);
    // it should be simple
    values.clear();
    values.put("size", 4f);
    two.recordStats(stats, createMap("solr:type=test", values), server);
    Assert.assertEquals(5f, stats.getStats("JMX/solr/test/size").getTotal(), .001);
    // cumulative_hitratio
    values.clear();
    values.put("cumulative_hitratio", 1f);
    two.recordStats(stats, createMap("solr:type=test", values), server);
    Assert.assertEquals(1f, stats.getStats("JMX/solr/test/cumulative_hitratio").getTotal(), .001);
    // it should be simple
    values.clear();
    values.put("cumulative_hitratio", 4f);
    two.recordStats(stats, createMap("solr:type=test", values), server);
    Assert.assertEquals(5f, stats.getStats("JMX/solr/test/cumulative_hitratio").getTotal(), .001);
}
Also used : StatsEngineImpl(com.newrelic.agent.stats.StatsEngineImpl) HashMap(java.util.HashMap) StatsEngine(com.newrelic.agent.stats.StatsEngine) MBeanServer(javax.management.MBeanServer)

Example 88 with StatsEngine

use of com.newrelic.agent.stats.StatsEngine in project newrelic-java-agent by newrelic.

the class Harvestable method recordIntervalMetric.

private void recordIntervalMetric() {
    long startTimeInNanos = System.nanoTime();
    final long harvestIntervalInNanos = startTimeInNanos - lastHarvest;
    lastHarvest = startTimeInNanos;
    ServiceFactory.getStatsService().doStatsWork(new StatsWork() {

        @Override
        public void doWork(StatsEngine statsEngine) {
            if (harvestIntervalInNanos > 0) {
                statsEngine.getResponseTimeStats(service.getEventHarvestIntervalMetric()).recordResponseTime(harvestIntervalInNanos, TimeUnit.NANOSECONDS);
            }
        }

        @Override
        public String getAppName() {
            return appName;
        }
    }, "HarvestInterval");
}
Also used : StatsWork(com.newrelic.agent.stats.StatsWork) StatsEngine(com.newrelic.agent.stats.StatsEngine)

Example 89 with StatsEngine

use of com.newrelic.agent.stats.StatsEngine 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 90 with StatsEngine

use of com.newrelic.agent.stats.StatsEngine in project newrelic-java-agent by newrelic.

the class SpanEventsServiceImpl method harvestEvents.

@Override
public void harvestEvents(final String appName) {
    if (!spanEventsConfig.isEnabled() || reservoirManager.getMaxSamplesStored() <= 0) {
        clearReservoir();
        return;
    }
    long startTimeInNanos = System.nanoTime();
    final ReservoirManager.HarvestResult result = reservoirManager.attemptToSendReservoir(appName, collectorSender, logger);
    if (result != null) {
        final long durationInNanos = System.nanoTime() - startTimeInNanos;
        ServiceFactory.getStatsService().doStatsWork(new StatsWork() {

            @Override
            public void doWork(StatsEngine statsEngine) {
                recordSupportabilityMetrics(statsEngine, durationInNanos, result.sent, result.seen);
            }

            @Override
            public String getAppName() {
                return appName;
            }
        }, "HarvestResult");
    }
}
Also used : StatsWork(com.newrelic.agent.stats.StatsWork) ReservoirManager(com.newrelic.agent.interfaces.ReservoirManager) StatsEngine(com.newrelic.agent.stats.StatsEngine)

Aggregations

StatsEngine (com.newrelic.agent.stats.StatsEngine)96 Test (org.junit.Test)78 TransactionDataList (com.newrelic.agent.TransactionDataList)36 StatsEngineImpl (com.newrelic.agent.stats.StatsEngineImpl)31 TransactionData (com.newrelic.agent.TransactionData)29 HashMap (java.util.HashMap)25 JmxMetric (com.newrelic.agent.jmx.metrics.JmxMetric)20 StatsWork (com.newrelic.agent.stats.StatsWork)8 MockRPMService (com.newrelic.agent.MockRPMService)7 ArrayList (java.util.ArrayList)6 AgentConfigFactoryTest (com.newrelic.agent.config.AgentConfigFactoryTest)5 Environment (com.newrelic.agent.environment.Environment)5 MetricName (com.newrelic.agent.metric.MetricName)5 CountDownLatch (java.util.concurrent.CountDownLatch)5 Attribute (javax.management.Attribute)5 MBeanServer (javax.management.MBeanServer)5 ResponseTimeStats (com.newrelic.agent.stats.ResponseTimeStats)4 Stats (com.newrelic.agent.stats.Stats)4 HttpError (com.newrelic.agent.transport.HttpError)4 CountStatistic (javax.management.j2ee.statistics.CountStatistic)4