PageRenderTime 30ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 0ms

/gmonitor/gmonitor-app/src/main/java/com/kingdeehit/bigdata/appmetric/schedule/MetricTimerTask.java

https://gitlab.com/zhengdingke/gmonitor
Java | 107 lines | 82 code | 12 blank | 13 comment | 4 complexity | 87ba03f050e3228135b1c416fe5e710c MD5 | raw file
  1. package com.kingdeehit.bigdata.appmetric.schedule;
  2. import java.util.Map;
  3. import java.util.TimerTask;
  4. import org.apache.commons.lang3.StringUtils;
  5. import org.slf4j.Logger;
  6. import org.slf4j.LoggerFactory;
  7. import com.alibaba.fastjson.JSON;
  8. import com.google.common.collect.Maps;
  9. import com.kingdeehit.bigdata.appmetric.flume.GangliaFlumeClient;
  10. import com.kingdeehit.bigdata.appmetric.model.MetricInfo;
  11. import com.kingdeehit.bigdata.appmetric.utils.CachedClient;
  12. import com.kingdeehit.bigdata.appmetric.utils.StringUtil;
  13. public class MetricTimerTask extends TimerTask {
  14. private static final Logger log = LoggerFactory.getLogger(MetricTimerTask.class);
  15. private MetricStatistic statstic;
  16. private GangliaFlumeClient client = new GangliaFlumeClient();
  17. public MetricTimerTask(MetricStatistic statstic) {
  18. this.statstic = statstic;
  19. this.client.init();
  20. }
  21. @Override
  22. public void run() {
  23. // log.info("start task!!!!");
  24. long sleepSecond = breakIntervalTime();
  25. long intervalSecond = sleepSecond + statstic.getIntervalSecond();
  26. // long intervalSecond = statstic.getIntervalSecond();
  27. Map<String, String> map = Maps.newHashMap();
  28. String serviceName = statstic.getServiceName();
  29. CachedClient cachedClient = statstic.getCachedClient();
  30. // log.info("metric aggr!!!");
  31. // 指标聚合发送
  32. statstic.getItemMap().entrySet().stream().forEach(entry -> {
  33. // log.info("key:" + entry.getKey() + "|value:" + entry.getValue());
  34. String metricName = entry.getKey();
  35. String agrOper = entry.getValue();
  36. String metricValue = null;
  37. String redisMetricName = serviceName + "_" + metricName;
  38. // log.info("redis");
  39. try {
  40. metricValue = cachedClient.getCached(redisMetricName);
  41. } catch (Exception e) {
  42. log.error("redis getmetric {} error", redisMetricName, e);
  43. }
  44. // log.info("agrOper:" + agrOper);
  45. switch (agrOper) {
  46. case "SUM":
  47. if (StringUtils.isNoneBlank(metricValue)) {
  48. map.put(serviceName + "." + metricName, metricValue);
  49. }
  50. break;
  51. case "MAX":
  52. case "AVGSUM":
  53. if (StringUtils.isNoneBlank(metricValue)) {
  54. map.put(serviceName + "." + metricName, metricValue);
  55. }
  56. try {
  57. cachedClient.setCached(redisMetricName, "0");
  58. } catch (Exception e) {
  59. log.error("redis setmetric {} error", redisMetricName, e);
  60. }
  61. break;
  62. case "AVG":
  63. if (StringUtils.isNoneBlank(metricValue)) {
  64. long metric = Long.parseLong(metricValue);
  65. // log.info("avg metricvalue:" + metricValue);
  66. // log.info("value:" + metric / intervalSecond);
  67. map.put(serviceName + "." + metricName, metric / intervalSecond + "");
  68. }
  69. try {
  70. cachedClient.setCached(redisMetricName, "0");
  71. } catch (Exception e) {
  72. log.error("redis setmetric {} error", redisMetricName, e);
  73. }
  74. break;
  75. default:
  76. break;
  77. }
  78. });
  79. // log.info("send data to flume!");
  80. MetricInfo info = new MetricInfo(serviceName, client.getGmetadHostPort(), client.getLocalHostPort(), client.getGroupName(), map);
  81. // 以json的形式输入flume
  82. // log.info(JSON.toJSONString(info, true));
  83. client.sendDataToFlume(JSON.toJSONString(info));
  84. // log.info("task over!");
  85. }
  86. private long breakIntervalTime() {
  87. long random = StringUtil.random(1, 10000);
  88. try {
  89. Thread.sleep(random);
  90. } catch (InterruptedException e) {
  91. log.error(e.getMessage(), e);
  92. }
  93. return random / 1000;
  94. }
  95. }