* Send given metrics to the Graphite server.
*/
@Override
public void write(Iterable<QueryResult> results) {
logger.debug("Export to '{}' results {}", graphiteServerHostAndPort, results);
SocketOutputStream socketOutputStream = null;
try {
socketOutputStream = socketOutputStreamPool.borrowObject(graphiteServerHostAndPort);
PyList list = new PyList();
for (QueryResult result : results) {
String metricName = metricPathPrefix + result.getName();
int time = (int) result.getEpoch(TimeUnit.SECONDS);
PyObject pyValue;
if (result.getValue() instanceof Integer) {
pyValue = new PyInteger((Integer) result.getValue());
} else if (result.getValue() instanceof Long) {
pyValue = new PyLong((Long) result.getValue());
} else if (result.getValue() instanceof Float) {
pyValue = new PyFloat((Float) result.getValue());
} else if (result.getValue() instanceof Double) {
pyValue = new PyFloat((Double) result.getValue());
} else if (result.getValue() instanceof Date) {
pyValue = new PyLong(TimeUnit.SECONDS.convert(((Date) result.getValue()).getTime(), TimeUnit.MILLISECONDS));
} else {
pyValue = new PyString(result.getValue().toString());
}
list.add(new PyTuple(new PyString(metricName), new PyTuple(new PyInteger(time), pyValue)));
logger.debug("Export '{}': ", metricName, result);
}
PyString payload = cPickle.dumps(list);
byte[] header = ByteBuffer.allocate(4).putInt(payload.__len__()).array();
socketOutputStream.write(header);
socketOutputStream.write(payload.toBytes());
socketOutputStream.flush();
socketOutputStreamPool.returnObject(graphiteServerHostAndPort, socketOutputStream);
} catch (Exception e) {
logger.warn("Failure to send result to graphite server '{}' with {}", graphiteServerHostAndPort, socketOutputStream, e);
if (socketOutputStream != null) {
try {