com.signalfuse.metrics.connection.StoredDataPointReceiver Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of signalfuse-java Show documentation
Show all versions of signalfuse-java Show documentation
Bare minimum core library needed to sending metrics to SignalFuse from Java clients
package com.signalfuse.metrics.connection;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.apache.commons.lang3.tuple.Pair;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import com.signalfuse.metrics.SignalfuseMetricsException;
import com.signalfuse.metrics.protobuf.SignalFuseProtocolBuffers;
/**
* Factory that just stores results to later be tested.
*
* @author jack
*/
public class StoredDataPointReceiver implements DataPointReceiver {
public final List addDataPoints;
private final Map, List> pointsFor;
public final Map registeredMetrics;
public boolean throwOnAdd = false;
public StoredDataPointReceiver() {
addDataPoints = Collections
.synchronizedList(new ArrayList());
registeredMetrics = Collections.synchronizedMap(new HashMap());
pointsFor = Maps.newHashMap();
}
@Override
public void addDataPoints(String auth, List dataPoints)
throws SignalfuseMetricsException {
if (throwOnAdd) {
throw new SignalfuseMetricsException("Flag set to true");
}
addDataPoints.addAll(dataPoints);
for (SignalFuseProtocolBuffers.DataPoint dp: dataPoints) {
Pair key = Pair.of(dp.getSource(), dp.getMetric());
if (pointsFor.containsKey(key)) {
pointsFor.get(key).add(dp.getValue());
} else {
pointsFor.put(key, Lists.newArrayList(dp.getValue()));
}
}
}
@Override
public void backfillDataPoints(String auth, String source, String metric,
List datumPoints)
throws SignalfuseMetricsException {}
@Override
public Map registerMetrics(String auth,
Map metricTypes)
throws SignalfuseMetricsException {
registeredMetrics.putAll(metricTypes);
Map ret = new HashMap();
for (Map.Entry i: metricTypes.entrySet()) {
ret.put(i.getKey(), true);
}
return ret;
}
public List valuesFor(String source, String metric) {
Pair key = Pair.of(source, metric);
List ret = pointsFor.get(key);
if (ret == null) {
return Collections.emptyList();
} else {
return Collections.unmodifiableList(ret);
}
}
public SignalFuseProtocolBuffers.Datum lastValueFor(String source, String metric) {
List vals = valuesFor(source, metric);
if (vals.isEmpty()) {
throw new RuntimeException("No value for source/metric");
} else {
return vals.get(vals.size() - 1);
}
}
public boolean clearValues(String source, String metric) {
Pair key = Pair.of(source, metric);
return pointsFor.remove(key) != null;
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy