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();
}
}
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"));
}
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);
}
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);
}
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.");
}
Aggregations