Skip to content

Instantly share code, notes, and snippets.

@arnobroekhof
Created March 9, 2017 15:08
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save arnobroekhof/38a3aeefd62daef851c0e2e4841b791c to your computer and use it in GitHub Desktop.
Save arnobroekhof/38a3aeefd62daef851c0e2e4841b791c to your computer and use it in GitHub Desktop.
@Service
@ExportMetricWriter
public class MetricsGraphiteExport {
@Autowired
private MetricRepository _metricRepository;
@Value("${graphite.server:'localhost'}")
private String graphiteServer;
@Value("${graphite.port:'2003'}")
private int graphitePort;
@Value("${graphite.protocol:'UDP'}")
private String graphiteProtocol;
@Value("${graphite.format:'^[a-zA-Z1-9-]+$'}")
private String graphiteFormat;
@Scheduled(fixedRateString = "${graphite.interval:'1000'}")
private void exportMetrics() {
_metricRepository.findAll().forEach(this::saveMetric);
}
private void saveMetric(Metric<?> m) {
System.out.println(String.format("%s = %s", m.getName(), m.getValue()));
try {
Socket socket = new Socket(graphiteServer, graphitePort);
OutputStream stream = socket.getOutputStream();
PrintWriter out = new PrintWriter(stream, true);
out.printf("%s %d %d%n", m.getName(), m.getValue().intValue(), System.currentTimeMillis() / 1000);
out.close();
socket.close();
}
catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
_metricRepository.reset(m.getName());
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment