Search in sources :

Example 6 with Sample

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

the class CassandraSampleRepository method insert.

@Override
public void insert(Collection<Sample> samples, boolean calculateTimeToLive) {
    Timer.Context timer = m_insertTimer.time();
    Timestamp now = Timestamp.now();
    Batch batch = unloggedBatch();
    for (Sample m : samples) {
        int ttl = m_ttl;
        if (calculateTimeToLive) {
            ttl -= (int) (now.asSeconds() - m.getTimestamp().asSeconds());
            if (ttl <= 0) {
                LOG.debug("Skipping expired sample: {}", m);
                continue;
            }
        }
        Duration resourceShard = m_contextConfigurations.getResourceShard(m.getContext());
        Insert insert = insertInto(SchemaConstants.T_SAMPLES).value(SchemaConstants.F_CONTEXT, m.getContext().getId()).value(SchemaConstants.F_PARTITION, m.getTimestamp().stepFloor(resourceShard).asSeconds()).value(SchemaConstants.F_RESOURCE, m.getResource().getId()).value(SchemaConstants.F_COLLECTED, m.getTimestamp().asMillis()).value(SchemaConstants.F_METRIC_NAME, m.getName()).value(SchemaConstants.F_VALUE, ValueType.decompose(m.getValue()));
        // for any sample that has not specified them.
        if (m.getAttributes() != null) {
            insert.value(SchemaConstants.F_ATTRIBUTES, m.getAttributes());
        }
        // Use the context specific consistency level
        insert.setConsistencyLevel(m_contextConfigurations.getWriteConsistency(m.getContext()));
        batch.add(insert.using(ttl(ttl)));
    }
    try {
        m_session.execute(batch);
        if (m_processorService != null) {
            m_processorService.submit(samples);
        }
        m_samplesInserted.mark(samples.size());
    } finally {
        timer.stop();
    }
}
Also used : Timer(com.codahale.metrics.Timer) Batch(com.datastax.driver.core.querybuilder.Batch) QueryBuilder.unloggedBatch(com.datastax.driver.core.querybuilder.QueryBuilder.unloggedBatch) Sample(org.opennms.newts.api.Sample) Duration(org.opennms.newts.api.Duration) Insert(com.datastax.driver.core.querybuilder.Insert) Timestamp(org.opennms.newts.api.Timestamp)

Example 7 with Sample

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

the class GraphiteHandlerTest method testParseSample.

@Test
public void testParseSample() {
    Sample sample = parseSample("foo.bar.baz 5 10000");
    assertThat(sample.getResource().getId(), is("foo:bar"));
    assertThat(sample.getName(), is("baz"));
    assertThat(sample.getValue().intValue(), equalTo(5));
    assertThat(sample.getTimestamp().asSeconds(), equalTo(10000L));
    sample = parseSample("foo 5 10000");
    assertThat(sample.getResource().getId(), is("foo"));
    assertThat(sample.getName(), is("value"));
}
Also used : GraphiteHandler.parseSample(org.opennms.newts.graphite.GraphiteHandler.parseSample) Sample(org.opennms.newts.api.Sample) Test(org.junit.Test)

Example 8 with Sample

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

the class NewtsReporter method report.

@Override
@SuppressWarnings("rawtypes")
public void report(SortedMap<String, Gauge> gauges, SortedMap<String, Counter> counters, SortedMap<String, Histogram> histograms, SortedMap<String, Meter> meters, SortedMap<String, Timer> timers) {
    Timestamp timestamp = Timestamp.fromEpochMillis(clock.getTime());
    List<Sample> samples = Lists.newArrayList();
    for (Map.Entry<String, Gauge> entry : gauges.entrySet()) {
        reportGauge(samples, timestamp, entry.getKey(), entry.getValue());
    }
    for (Map.Entry<String, Counter> entry : counters.entrySet()) {
        reportCounter(samples, timestamp, entry.getKey(), entry.getValue());
    }
    for (Map.Entry<String, Histogram> entry : histograms.entrySet()) {
        reportHistogram(samples, timestamp, entry.getKey(), entry.getValue());
    }
    for (Map.Entry<String, Meter> entry : meters.entrySet()) {
        reportMeter(samples, timestamp, entry.getKey(), entry.getValue());
    }
    for (Map.Entry<String, Timer> entry : timers.entrySet()) {
        reportTimer(samples, timestamp, entry.getKey(), entry.getValue());
    }
    this.repository.insert(samples);
}
Also used : Histogram(com.codahale.metrics.Histogram) Meter(com.codahale.metrics.Meter) Sample(org.opennms.newts.api.Sample) Timestamp(org.opennms.newts.api.Timestamp) Gauge(com.codahale.metrics.Gauge) Counter(com.codahale.metrics.Counter) Timer(com.codahale.metrics.Timer) Map(java.util.Map) SortedMap(java.util.SortedMap)

Example 9 with Sample

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

the class NewtsReporter method reportG.

private void reportG(List<Sample> samples, Timestamp timestamp, String resource, String metricName, double val) {
    Sample s = new Sample(timestamp, resourceFor(resource), metricName, MetricType.GAUGE, gauge(val));
    samples.add(s);
}
Also used : Sample(org.opennms.newts.api.Sample)

Example 10 with Sample

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

the class InsertDispatcher method go.

@Override
void go() throws InterruptedException {
    createThreads();
    SampleGenerator[] generators = getSampleGenerators();
    List<Sample> samples = Lists.newArrayList();
    boolean isExhausted = false;
    Meter meter = m_metricRegistry.meter(MetricRegistry.name(getClass(), "samples"));
    outer: while (true) {
        for (int i = 0; i < generators.length; i++) {
            if (generators[i].hasNext()) {
                Optional<Sample> s = generators[i].next();
                if (s.isPresent()) {
                    samples.add(s.get());
                    meter.mark();
                }
            } else {
                // All the iterators yield the same number, when one is exhausted, all are.
                isExhausted = true;
            }
            // Final queue insertion before shutdown
            if (isExhausted) {
                LOG.debug("Queuing {} samples for insert", samples.size());
                m_samplesQueue.put(samples);
                break outer;
            }
            if (samples.size() >= m_config.getBatchSize()) {
                LOG.debug("Queuing {} samples for insert", samples.size());
                m_samplesQueue.put(samples);
                samples = Lists.newArrayList();
            }
        }
    }
    shutdown();
    LOG.debug("Done.");
}
Also used : Optional(com.google.common.base.Optional) Meter(com.codahale.metrics.Meter) Sample(org.opennms.newts.api.Sample)

Aggregations

Sample (org.opennms.newts.api.Sample)37 Resource (org.opennms.newts.api.Resource)18 Test (org.junit.Test)14 Timestamp (org.opennms.newts.api.Timestamp)12 MetricRegistry (com.codahale.metrics.MetricRegistry)9 Gauge (org.opennms.newts.api.Gauge)6 ContextConfigurations (org.opennms.newts.cassandra.ContextConfigurations)6 Timer (com.codahale.metrics.Timer)5 List (java.util.List)5 Counter (org.opennms.newts.api.Counter)5 Row (org.opennms.newts.api.Results.Row)5 CassandraSession (org.opennms.newts.cassandra.CassandraSession)5 Map (java.util.Map)4 Context (org.opennms.newts.api.Context)4 Results (org.opennms.newts.api.Results)4 Meter (com.codahale.metrics.Meter)3 BoundStatement (com.datastax.driver.core.BoundStatement)3 PreparedStatement (com.datastax.driver.core.PreparedStatement)3 RegularStatement (com.datastax.driver.core.RegularStatement)3 ResultSetFuture (com.datastax.driver.core.ResultSetFuture)3