Created
March 9, 2017 15:08
-
-
Save arnobroekhof/38a3aeefd62daef851c0e2e4841b791c to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@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