Search in sources :

Example 1 with MetricData

use of com.newrelic.agent.MetricData in project newrelic-java-agent by newrelic.

the class MetricDataTest method invalidStat.

@Test
public void invalidStat() throws IllegalArgumentException, SecurityException, IllegalAccessException, NoSuchFieldException {
    StatsImpl stats = new StatsImpl();
    Field countField = AbstractStats.class.getDeclaredField("count");
    countField.setAccessible(true);
    countField.set(stats, -1);
    MetricData data = MetricData.create(MetricName.create("dude"), stats);
    String json = DataSenderWriter.toJSONString(data);
    Assert.assertEquals("[{\"name\":\"dude\"},[0,0,0,0,0,0]]", json);
}
Also used : Field(java.lang.reflect.Field) MetricData(com.newrelic.agent.MetricData) Test(org.junit.Test)

Example 2 with MetricData

use of com.newrelic.agent.MetricData in project newrelic-java-agent by newrelic.

the class StatsEngineTest method getMetricDataWithJavaOther.

@Test
public void getMetricDataWithJavaOther() {
    try {
        StatsEngineImpl statsEngine = new StatsEngineImpl();
        // create the stats
        Transaction tx = Transaction.getTransaction();
        String methodName1 = "foo";
        ResponseTimeStats fooStats = tx.getTransactionActivity().getTransactionStats().getScopedStats().getOrCreateResponseTimeStats(methodName1);
        fooStats.recordResponseTimeInNanos(2000000000, 1000000000);
        fooStats.recordResponseTimeInNanos(1000000000, 1000000000);
        fooStats.recordResponseTimeInNanos(2000000000, 1000000000);
        String methodName2 = "hi";
        ResponseTimeStats hiStats = tx.getTransactionActivity().getTransactionStats().getScopedStats().getOrCreateResponseTimeStats(methodName2);
        hiStats.recordResponseTimeInNanos(1, 1);
        String methodName3 = "hello";
        ResponseTimeStats helloStats = tx.getTransactionActivity().getTransactionStats().getScopedStats().getOrCreateResponseTimeStats(methodName3);
        helloStats.recordResponseTimeInNanos(2, 1);
        // add the scoped metrics under the scope
        statsEngine.mergeStatsResolvingScope(tx.getTransactionActivity().getTransactionStats(), "theScope");
        MockNormalizer metricNormalizer = new MockNormalizer();
        List<MetricData> data = statsEngine.getMetricData(metricNormalizer);
        Assert.assertEquals(4, data.size());
        // do the validation
        // foo scoped, foo unscoped, hi scoped, hi unscoped
        ResponseTimeStats[] output = new ResponseTimeStats[4];
        for (MetricData d : data) {
            String name = d.getMetricName().getName();
            if (name.equals(methodName1)) {
                if (d.getMetricName().isScoped()) {
                    Assert.assertNull(output[0]);
                    output[0] = (ResponseTimeStats) d.getStats();
                } else {
                    Assert.assertNull(output[1]);
                    output[1] = (ResponseTimeStats) d.getStats();
                }
            } else {
                // since the metrics are so small hi and hello
                // should be combined into a java other metric
                Assert.assertEquals("Java/other", name);
                if (d.getMetricName().isScoped()) {
                    Assert.assertNull(output[2]);
                    output[2] = (ResponseTimeStats) d.getStats();
                } else {
                    Assert.assertNull(output[3]);
                    output[3] = (ResponseTimeStats) d.getStats();
                }
            }
        }
        // validate the stats
        // they should be different objects with the same numbers
        Assert.assertFalse("The object should have the same values but be different objects", output[0] == output[1]);
        Assert.assertFalse("The object should have the same values but be different objects", output[2] == output[3]);
        // validate call count
        Assert.assertEquals(3, output[0].getCallCount());
        Assert.assertEquals("The total was incorrect.", 5.0f, output[0].getTotal(), .001f);
        Assert.assertEquals("The total exclusive was incorrect.", 3.0f, output[0].getTotalExclusiveTime(), .001f);
        Assert.assertEquals(3, output[1].getCallCount());
        Assert.assertEquals("The total was incorrect.", 5.0f, output[1].getTotal(), .001f);
        Assert.assertEquals("The total exclusive was incorrect.", 3.0f, output[1].getTotalExclusiveTime(), .001f);
        Assert.assertEquals(2, output[2].getCallCount());
        Assert.assertEquals("The total was incorrect.", .000000003f, output[2].getTotal(), .001f);
        Assert.assertEquals("The total exclusive was incorrect.", .000000002f, output[2].getTotalExclusiveTime(), .00000000001f);
        Assert.assertEquals(2, output[3].getCallCount());
        Assert.assertEquals("The total was incorrect.", .000000003f, output[3].getTotal(), .001f);
        Assert.assertEquals("The total exclusive was incorrect.", .000000002f, output[3].getTotalExclusiveTime(), .00000000001f);
    } catch (Exception e) {
        e.printStackTrace();
        Assert.fail(e.getMessage());
    }
}
Also used : Transaction(com.newrelic.agent.Transaction) MockNormalizer(com.newrelic.agent.MockNormalizer) MetricData(com.newrelic.agent.MetricData) Test(org.junit.Test)

Example 3 with MetricData

use of com.newrelic.agent.MetricData in project newrelic-java-agent by newrelic.

the class StatsEngineTest method getMetricData.

@Test
public void getMetricData() {
    StatsEngineImpl statsEngine = new StatsEngineImpl();
    Stats stats = statsEngine.getStats("Test");
    stats.recordDataPoint(100);
    stats.recordDataPoint(200);
    MockNormalizer metricNormalizer = new MockNormalizer();
    List<MetricData> data = statsEngine.getMetricData(metricNormalizer);
    Assert.assertEquals(1, data.size());
    Assert.assertEquals(1, statsEngine.getSize());
    Assert.assertEquals("Test", data.get(0).getMetricName().getName());
    Assert.assertNull(data.get(0).getMetricId());
    Assert.assertEquals(300f, ((CountStats) data.get(0).getStats()).getTotal(), 0);
    Assert.assertEquals(2, ((CountStats) data.get(0).getStats()).getCallCount());
}
Also used : MockNormalizer(com.newrelic.agent.MockNormalizer) MetricData(com.newrelic.agent.MetricData) Test(org.junit.Test)

Example 4 with MetricData

use of com.newrelic.agent.MetricData in project newrelic-java-agent by newrelic.

the class StatsEngineTest method getMetricDataNormalizeFromDataUsageStats.

@Test
public void getMetricDataNormalizeFromDataUsageStats() {
    StatsEngineImpl statsEngine = new StatsEngineImpl();
    DataUsageStats stats = statsEngine.getDataUsageStats(MetricName.create("Test"));
    stats.recordDataUsage(100, 5);
    stats.recordDataUsage(300, 10);
    MockNormalizer metricNormalizer = new MockNormalizer();
    Map<String, String> normalizationResults = new HashMap<>();
    normalizationResults.put("Test", "Test2");
    metricNormalizer.setNormalizationResults(normalizationResults);
    List<MetricData> data = statsEngine.getMetricData(metricNormalizer);
    Assert.assertEquals(1, data.size());
    Assert.assertEquals(1, statsEngine.getSize());
    Assert.assertEquals("Test2", data.get(0).getMetricName().getName());
    Assert.assertNull(data.get(0).getMetricId());
    Assert.assertEquals(400, ((DataUsageStats) data.get(0).getStats()).getBytesSent());
    Assert.assertEquals(15, ((DataUsageStats) data.get(0).getStats()).getBytesReceived());
}
Also used : HashMap(java.util.HashMap) MockNormalizer(com.newrelic.agent.MockNormalizer) MetricData(com.newrelic.agent.MetricData) Test(org.junit.Test)

Example 5 with MetricData

use of com.newrelic.agent.MetricData in project newrelic-java-agent by newrelic.

the class StatsEngineTest method getMetricDataScopedAndUnscoped1.

@Test
public void getMetricDataScopedAndUnscoped1() {
    try {
        StatsEngineImpl statsEngine = new StatsEngineImpl();
        // create the stats
        Transaction tx = Transaction.getTransaction();
        String methodName1 = "foo";
        ResponseTimeStats fooStats = tx.getTransactionActivity().getTransactionStats().getScopedStats().getOrCreateResponseTimeStats(methodName1);
        fooStats.setCallCount(1);
        String methodName2 = "hi";
        ResponseTimeStats hiStats = tx.getTransactionActivity().getTransactionStats().getScopedStats().getOrCreateResponseTimeStats(methodName2);
        hiStats.setCallCount(3);
        // add the scoped metrics under the scope
        statsEngine.mergeStatsResolvingScope(tx.getTransactionActivity().getTransactionStats(), "theScope");
        MockNormalizer metricNormalizer = new MockNormalizer();
        List<MetricData> data = statsEngine.getMetricData(metricNormalizer);
        Assert.assertEquals(4, data.size());
        // do the validation
        // foo scoped, foo unscoped, hi scoped, hi unscoped
        ResponseTimeStats[] output = new ResponseTimeStats[4];
        for (MetricData d : data) {
            String name = d.getMetricName().getName();
            if (name.equals(methodName1)) {
                if (d.getMetricName().isScoped()) {
                    Assert.assertNull(output[0]);
                    output[0] = (ResponseTimeStats) d.getStats();
                } else {
                    Assert.assertNull(output[1]);
                    output[1] = (ResponseTimeStats) d.getStats();
                }
            } else {
                Assert.assertEquals(methodName2, name);
                if (d.getMetricName().isScoped()) {
                    Assert.assertNull(output[2]);
                    output[2] = (ResponseTimeStats) d.getStats();
                } else {
                    Assert.assertNull(output[3]);
                    output[3] = (ResponseTimeStats) d.getStats();
                }
            }
        }
        // validate the stats
        // they should be different objects with the same numbers
        Assert.assertFalse("The object should have the same values but be different objects", output[0] == output[1]);
        Assert.assertFalse("The object should have the same values but be different objects", output[2] == output[3]);
        // validate call count
        Assert.assertEquals(1, output[0].getCallCount());
        Assert.assertEquals(1, output[1].getCallCount());
        Assert.assertEquals(3, output[2].getCallCount());
        Assert.assertEquals(3, output[3].getCallCount());
    } catch (Exception e) {
        e.printStackTrace();
        Assert.fail(e.getMessage());
    }
}
Also used : Transaction(com.newrelic.agent.Transaction) MockNormalizer(com.newrelic.agent.MockNormalizer) MetricData(com.newrelic.agent.MetricData) Test(org.junit.Test)

Aggregations

MetricData (com.newrelic.agent.MetricData)19 Test (org.junit.Test)12 MockNormalizer (com.newrelic.agent.MockNormalizer)10 Transaction (com.newrelic.agent.Transaction)4 HashMap (java.util.HashMap)4 CountStats (com.newrelic.agent.stats.CountStats)3 ArrayList (java.util.ArrayList)3 StatsBase (com.newrelic.agent.stats.StatsBase)2 ByteArrayOutputStream (java.io.ByteArrayOutputStream)2 PrintWriter (java.io.PrintWriter)2 AgentConfig (com.newrelic.agent.config.AgentConfig)1 TracedMetricData (com.newrelic.agent.introspec.TracedMetricData)1 MetricName (com.newrelic.agent.metric.MetricName)1 SimpleStatsEngine (com.newrelic.agent.stats.SimpleStatsEngine)1 StatsEngine (com.newrelic.agent.stats.StatsEngine)1 Field (java.lang.reflect.Field)1 ArgumentMatchers.anyString (org.mockito.ArgumentMatchers.anyString)1