Skip to content

Instantly share code, notes, and snippets.

@tuan3w
Created April 19, 2016 14:21
Show Gist options
  • Save tuan3w/1d8d6e09340c985908f18b2ae612fe39 to your computer and use it in GitHub Desktop.
Save tuan3w/1d8d6e09340c985908f18b2ae612fe39 to your computer and use it in GitHub Desktop.
package com.vcc.bigdata.monitoring.graphite
import java.net.Socket
import java.io.PrintWriter
import java.util.Collection
import scala.collection.JavaConversions._
import java.io.DataOutputStream
import java.io.OutputStreamWriter
import java.io.BufferedWriter
import java.nio.charset.Charset
case class Metric(metricPath: String, value: Double, timeStamp: Long)
class SimpleGraphiteClient(
host: String,
port: Int) extends Serializable {
var socket = new Socket(host, port);
var stream = socket.getOutputStream();
var out = new PrintWriter(stream, true);
// val writer = new BufferedWriter(new OutputStreamWriter(socket.getOutputStream(), Charset.forName("UTF-8")));
def sendMetrics(metric: Metric) {
out.println(metric.metricPath + " " + metric.value.toString() + " " + metric.timeStamp)
out.flush()
}
def sendMetrics(metrics: Collection[Metric]) {
for (metric <- metrics) {
sendMetrics(metric)
}
}
def close() {
try {
out.close()
stream.close()
socket.close()
} catch {
case e: Throwable => {
e.printStackTrace()
}
}
}
def reconnect() {
socket = new Socket(host, port);
stream = socket.getOutputStream();
out = new PrintWriter(stream, true);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment