Search in sources :

Example 1 with Counter

use of org.opennms.newts.api.Counter in project newts by OpenNMS.

the class RateTest method testMissing.

@Test
public void testMissing() {
    Results<Sample> input = new Results<>();
    Timestamp start = Timestamp.fromEpochMillis(1000);
    Duration step = Duration.seconds(1);
    // row_1
    input.addElement(new Sample(start, m_resource, m_metrics[0], COUNTER, new Counter(0)));
    input.addElement(new Sample(start, m_resource, m_metrics[1], COUNTER, new Counter(0)));
    // row_2
    input.addElement(new Sample(start.plus(step), m_resource, m_metrics[0], COUNTER, new Counter(100)));
    input.addElement(new Sample(start.plus(step), m_resource, m_metrics[1], COUNTER, new Counter(100)));
    // row_3 (sample for m_metrics[0] missing)
    input.addElement(new Sample(start.plus(step.times(2)), m_resource, m_metrics[1], COUNTER, new Counter(200)));
    // row_4
    input.addElement(new Sample(start.plus(step.times(3)), m_resource, m_metrics[0], COUNTER, new Counter(300)));
    input.addElement(new Sample(start.plus(step.times(3)), m_resource, m_metrics[1], COUNTER, new Counter(300)));
    Iterator<Results.Row<Sample>> output = new Rate(input.iterator(), getMetrics(2)).iterator();
    // result_1 is always null
    assertTrue(output.hasNext());
    assertEquals(new Gauge(Double.NaN), output.next().getElement(m_metrics[0]).getValue());
    // result_2, rate 100
    assertTrue(output.hasNext());
    assertEquals(100.0d, output.next().getElement(m_metrics[0]).getValue().doubleValue(), 0.0d);
    // result_3, missing because sample in row_3 is missing
    assertTrue(output.hasNext());
    assertNull(output.next().getElement(m_metrics[0]));
    // result_4, rate of 100 calculated between row_4 and row_2
    assertTrue(output.hasNext());
    assertEquals(100.0d, output.next().getElement(m_metrics[0]).getValue().doubleValue(), 0.0d);
}
Also used : Counter(org.opennms.newts.api.Counter) Results(org.opennms.newts.api.Results) Sample(org.opennms.newts.api.Sample) Rate(org.opennms.newts.aggregate.Rate) Duration(org.opennms.newts.api.Duration) Row(org.opennms.newts.api.Results.Row) Timestamp(org.opennms.newts.api.Timestamp) Gauge(org.opennms.newts.api.Gauge) Test(org.junit.Test)

Example 2 with Counter

use of org.opennms.newts.api.Counter in project opennms by OpenNMS.

the class NewtsConverter method toSample.

protected static Sample toSample(AbstractDS ds, Resource resource, Timestamp timestamp, double value) {
    final String metric = ds.getName();
    final MetricType type = ds.isCounter() ? MetricType.COUNTER : MetricType.GAUGE;
    final ValueType<?> valueType = ds.isCounter() ? new Counter(UnsignedLong.valueOf(BigDecimal.valueOf(value).toBigInteger())) : new Gauge(value);
    return new Sample(timestamp, resource, metric, type, valueType);
}
Also used : Counter(org.opennms.newts.api.Counter) Sample(org.opennms.newts.api.Sample) MetricType(org.opennms.newts.api.MetricType) Gauge(org.opennms.newts.api.Gauge)

Example 3 with Counter

use of org.opennms.newts.api.Counter in project opennms by OpenNMS.

the class NewtsWriterTest method canWriteToSampleRepositoryUsingMultipleThreads.

/**
 * Uses a latch to verify that multiple that multiple threads
 * are used to concurrently insert samples into the SampleRepository.
 */
@Test
public void canWriteToSampleRepositoryUsingMultipleThreads() {
    int ringBufferSize = 1024;
    int numWriterThreads = 8;
    LatchedSampleRepository sampleRepo = new LatchedSampleRepository(numWriterThreads);
    MetricRegistry registry = new MetricRegistry();
    NewtsWriter writer = new NewtsWriter(1, ringBufferSize, numWriterThreads, registry);
    writer.setSampleRepository(sampleRepo);
    for (int i = 0; i < ringBufferSize * 2; i++) {
        Resource x = new Resource("x");
        Sample s = new Sample(Timestamp.now(), x, "y", MetricType.COUNTER, new Counter(i));
        writer.insert(Lists.newArrayList(s));
    }
}
Also used : Counter(org.opennms.newts.api.Counter) Sample(org.opennms.newts.api.Sample) MetricRegistry(com.codahale.metrics.MetricRegistry) Resource(org.opennms.newts.api.Resource) Test(org.junit.Test)

Example 4 with Counter

use of org.opennms.newts.api.Counter in project opennms by OpenNMS.

the class NewtsWriterTest method samplesAreDroppedWhenRingBufferIsFull.

/**
 * Fills the ring buffer and locks all of the writer threads to verify
 * that samples additional samples are dropped.
 */
@Test
public void samplesAreDroppedWhenRingBufferIsFull() throws Exception {
    Resource x = new Resource("x");
    int ringBufferSize = 1024;
    int numWriterThreads = 8;
    Lock lock = new ReentrantLock();
    LockedSampleRepository sampleRepo = new LockedSampleRepository(lock);
    MetricRegistry registry = new MetricRegistry();
    NewtsWriter writer = new NewtsWriter(1, ringBufferSize, numWriterThreads, registry);
    writer.setSampleRepository(sampleRepo);
    lock.lock();
    for (int i = 0; i < ringBufferSize; i++) {
        Sample s = new Sample(Timestamp.now(), x, "y", MetricType.COUNTER, new Counter(i));
        writer.insert(Lists.newArrayList(s));
    }
    // The ring buffer should be full, and all of the threads should be locked
    Thread.sleep(250);
    assertEquals(numWriterThreads, sampleRepo.getNumThreadsLocked());
    // Attempt to insert another batch of samples
    for (int i = 0; i < 8; i++) {
        Sample s = new Sample(Timestamp.now(), x, "y", MetricType.COUNTER, new Counter(i));
        writer.insert(Lists.newArrayList(s));
    }
    ;
    // Unlock the writer threads and wait for the ring buffer to drain
    lock.unlock();
    writer.destroy();
    // Verify the number of inserted samples
    assertEquals(0, sampleRepo.getNumThreadsLocked());
    assertEquals(ringBufferSize, sampleRepo.getNumSamplesInserted());
}
Also used : ReentrantLock(java.util.concurrent.locks.ReentrantLock) Counter(org.opennms.newts.api.Counter) Sample(org.opennms.newts.api.Sample) MetricRegistry(com.codahale.metrics.MetricRegistry) Resource(org.opennms.newts.api.Resource) ReentrantLock(java.util.concurrent.locks.ReentrantLock) Lock(java.util.concurrent.locks.Lock) Test(org.junit.Test)

Example 5 with Counter

use of org.opennms.newts.api.Counter in project newts by OpenNMS.

the class RateTest method test.

@Test
public void test() {
    Results<Sample> input = new Results<>();
    int rows = 10, cols = 2, rate = 100;
    for (int i = 1; i <= rows; i++) {
        Timestamp t = Timestamp.fromEpochMillis(i * 1000);
        for (int j = 0; j < cols; j++) {
            input.addElement(new Sample(t, m_resource, m_metrics[j], COUNTER, new Counter((i + j) * rate)));
        }
    }
    Iterator<Results.Row<Sample>> output = new Rate(input.iterator(), getMetrics(cols)).iterator();
    for (int i = 1; i <= rows; i++) {
        assertTrue("Insufficient number of results", output.hasNext());
        Results.Row<Sample> row = output.next();
        assertEquals("Unexpected row timestamp", Timestamp.fromEpochMillis(i * 1000), row.getTimestamp());
        assertEquals("Unexpected row resource", m_resource, row.getResource());
        assertEquals("Unexpected number of columns", cols, row.getElements().size());
        for (int j = 0; j < cols; j++) {
            String name = m_metrics[j];
            assertNotNull("Missing sample" + name, row.getElement(name));
            assertEquals("Unexpected sample name", name, row.getElement(name).getName());
            assertEquals("Unexpected sample type", GAUGE, row.getElement(name).getType());
            // Samples in the first row are null, this is normal.
            if (i != 1) {
                assertEquals("Incorrect rate value", 100.0d, row.getElement(name).getValue().doubleValue(), 0.0d);
            }
        }
    }
}
Also used : Counter(org.opennms.newts.api.Counter) Results(org.opennms.newts.api.Results) Sample(org.opennms.newts.api.Sample) Rate(org.opennms.newts.aggregate.Rate) Row(org.opennms.newts.api.Results.Row) Timestamp(org.opennms.newts.api.Timestamp) Test(org.junit.Test)

Aggregations

Counter (org.opennms.newts.api.Counter)6 Sample (org.opennms.newts.api.Sample)6 Test (org.junit.Test)5 MetricRegistry (com.codahale.metrics.MetricRegistry)3 Resource (org.opennms.newts.api.Resource)3 Rate (org.opennms.newts.aggregate.Rate)2 Gauge (org.opennms.newts.api.Gauge)2 Results (org.opennms.newts.api.Results)2 Row (org.opennms.newts.api.Results.Row)2 Timestamp (org.opennms.newts.api.Timestamp)2 BoundStatement (com.datastax.driver.core.BoundStatement)1 PreparedStatement (com.datastax.driver.core.PreparedStatement)1 RegularStatement (com.datastax.driver.core.RegularStatement)1 ResultSetFuture (com.datastax.driver.core.ResultSetFuture)1 Statement (com.datastax.driver.core.Statement)1 List (java.util.List)1 Lock (java.util.concurrent.locks.Lock)1 ReentrantLock (java.util.concurrent.locks.ReentrantLock)1 Duration (org.opennms.newts.api.Duration)1 MetricType (org.opennms.newts.api.MetricType)1