com.signalfuse.metrics.flush.AggregateMetricSender 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.flush;
import java.io.Closeable;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;
import com.signalfuse.metrics.SignalfuseMetricsException;
import com.signalfuse.metrics.auth.AuthToken;
import com.signalfuse.metrics.auth.NoAuthTokenException;
import com.signalfuse.metrics.connection.DataPointReceiver;
import com.signalfuse.metrics.connection.DataPointReceiverFactory;
import com.signalfuse.metrics.errorhandler.MetricErrorImpl;
import com.signalfuse.metrics.errorhandler.MetricErrorType;
import com.signalfuse.metrics.errorhandler.OnSendErrorHandler;
import com.signalfuse.metrics.protobuf.SignalFuseProtocolBuffers;
/**
* The primary java class to send metrics. To use this class, create a session, add points to
* the session, and when you are done, close the session. For example:
*
*
* {@code
* AggregateMetricSender sender;
* try (AggregateMetricSender.Session i = mf.createSession()) {
* i.incrementCounter("testcounter2", 1);
* i.setDatapoint(
* SignalFuseProtocolBuffers.DataPoint.newBuilder()
* .setMetric("curtime")
* .setValue(
* SignalFuseProtocolBuffers.Datum.newBuilder()
* .setIntValue(System.currentTimeMillis()))
* .addDimensions(
* SignalFuseProtocolBuffers.Dimension.newBuilder()
* .setKey("source")
* .setValue("java"))
* .build());
* }
* }
*
*/
public class AggregateMetricSender {
private final String defaultSourceName;
private final Set registeredMetricPairs;
private final DataPointReceiverFactory dataPointReceiverFactory;
private final AuthToken authToken;
private final Collection onSendErrorHandlerCollection;
public AggregateMetricSender(String defaultSourceName, DataPointReceiverFactory dataPointReceiverFactory,
AuthToken authToken,
Collection onSendErrorHandlerCollection) {
this.defaultSourceName = defaultSourceName;
registeredMetricPairs = new HashSet();
this.dataPointReceiverFactory = dataPointReceiverFactory;
this.authToken = authToken;
this.onSendErrorHandlerCollection = onSendErrorHandlerCollection;
}
public String getDefaultSourceName() {
return defaultSourceName;
}
private void communicateError(String message, MetricErrorType code,
SignalfuseMetricsException signalfuseMetricsException) {
for (OnSendErrorHandler onSendErrorHandler : onSendErrorHandlerCollection) {
onSendErrorHandler
.handleError(new MetricErrorImpl(message, code, signalfuseMetricsException));
}
}
public Session createSession() {
return new SessionImpl();
}
private final class SessionImpl implements Session {
private final Map toBeRegisteredMetricPairs;
private final List pointsToFlush;
private SessionImpl() {
toBeRegisteredMetricPairs = new HashMap();
pointsToFlush = new ArrayList();
}
public Session setCumulativeCounter(String metric, long value) {
return setCumulativeCounter(defaultSourceName, metric, value);
}
public Session setCumulativeCounter(String source, String metric, long value) {
setDatapoint(source, metric, SignalFuseProtocolBuffers.MetricType.CUMULATIVE_COUNTER, value);
return this;
}
public Session incrementCounter(String metric, long value) {
return incrementCounter(defaultSourceName, metric, value);
}
public Session incrementCounter(String source, String metric, long value) {
setDatapoint(source, metric, SignalFuseProtocolBuffers.MetricType.COUNTER, value);
return this;
}
@Override
public Session setDatapoint(String source, String metric,
SignalFuseProtocolBuffers.MetricType metricType,
long value) {
check(metric, metricType);
pointsToFlush.add(SignalFuseProtocolBuffers.DataPoint.newBuilder()
.setSource(source)
.setMetricType(metricType)
.setMetric(metric).setValue(
SignalFuseProtocolBuffers.Datum.newBuilder().setIntValue(value).build())
.build());
return this;
}
@Override
public Session setDatapoint(String source, String metric,
SignalFuseProtocolBuffers.MetricType metricType,
double value) {
check(metric, metricType);
pointsToFlush.add(SignalFuseProtocolBuffers.DataPoint.newBuilder()
.setSource(source)
.setMetricType(metricType)
.setMetric(metric).setValue(
SignalFuseProtocolBuffers.Datum.newBuilder().setDoubleValue(value).build())
.build());
return this;
}
@Override
public Session setDatapoint(SignalFuseProtocolBuffers.DataPoint datapoint) {
check(datapoint.getMetric(), datapoint.getMetricType());
pointsToFlush.add(datapoint);
return this;
}
public Session setGauge(String metric, long value) {
return setGauge(defaultSourceName, metric, value);
}
public Session setGauge(String source, String metric, long value) {
setDatapoint(source, metric, SignalFuseProtocolBuffers.MetricType.GAUGE, value);
return this;
}
public Session setGauge(String metric, double value) {
return setGauge(defaultSourceName, metric, value);
}
public Session setGauge(String source, String metric, double value) {
setDatapoint(source, metric, SignalFuseProtocolBuffers.MetricType.GAUGE, value);
return this;
}
private void check(String metricPair,
com.signalfuse.metrics.protobuf.SignalFuseProtocolBuffers.MetricType
metricType) {
if (!registeredMetricPairs.contains(metricPair)) {
toBeRegisteredMetricPairs.put(metricPair, metricType);
}
}
public void close() {
final String authTokenStr;
try {
authTokenStr = authToken.getAuthToken();
} catch (NoAuthTokenException e) {
communicateError("Unable to get auth token", MetricErrorType.AUTH_TOKEN_ERROR,
e);
return;
}
DataPointReceiver dataPointReceiver = dataPointReceiverFactory.createDataPointReceiver();
if (!toBeRegisteredMetricPairs.isEmpty()) {
try {
Map registeredPairs = dataPointReceiver.registerMetrics(authTokenStr,
toBeRegisteredMetricPairs);
for (Map.Entry i: registeredPairs.entrySet()) {
if (i.getValue()) {
registeredMetricPairs.add(i.getKey());
}
}
} catch (SignalfuseMetricsException e) {
communicateError("Unable to register metrics",
MetricErrorType.REGISTRATION_ERROR,
e);
return;
}
}
Iterator i = pointsToFlush.iterator();
while (i.hasNext()) {
SignalFuseProtocolBuffers.DataPoint currentEntry = i.next();
if (!registeredMetricPairs.contains(currentEntry.getMetric())) {
i.remove();
}
}
if (!pointsToFlush.isEmpty()) {
try {
dataPointReceiver.addDataPoints(authTokenStr, pointsToFlush);
} catch (SignalfuseMetricsException e) {
communicateError("Unable to send datapoints",
MetricErrorType.DATAPOINT_SEND_ERROR,
e);
return;
}
}
}
}
public interface Session extends Closeable {
Session setCumulativeCounter(String metric, long value);
Session setCumulativeCounter(String source, String metric, long value);
Session setGauge(String metric, long value);
Session setGauge(String source, String metric, long value);
Session setGauge(String metric, double value);
Session setGauge(String source, String metric, double value);
Session incrementCounter(String metric, long value);
Session incrementCounter(String source, String metric, long value);
Session setDatapoint(String source, String metric, SignalFuseProtocolBuffers.MetricType metricType, long value);
Session setDatapoint(String source, String metric, SignalFuseProtocolBuffers.MetricType metricType, double value);
Session setDatapoint(SignalFuseProtocolBuffers.DataPoint datapoint);
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy