private void registerOpenSesssionMetrics(Metrics metrics) { MetricsVariable<Integer> openSessionCnt = new MetricsVariable<Integer>() { @Override public Integer getValue() { return getSessions().size(); } }; MetricsVariable<Integer> openSessionTime = new MetricsVariable<Integer>() { @Override public Integer getValue() { long sum = 0; long currentTime = System.currentTimeMillis(); for (HiveSession s : getSessions()) { sum += currentTime - s.getCreationTime(); } // in case of an overflow return -1 return (int) sum != sum ? -1 : (int) sum; } }; metrics.addGauge(MetricsConstant.HS2_OPEN_SESSIONS, openSessionCnt); metrics.addRatio(MetricsConstant.HS2_AVG_OPEN_SESSION_TIME, openSessionTime, openSessionCnt); }
private void registerActiveSesssionMetrics(Metrics metrics) { MetricsVariable<Integer> activeSessionCnt = new MetricsVariable<Integer>() { @Override public Integer getValue() { Iterable<HiveSession> filtered = Iterables.filter(getSessions(), new Predicate<HiveSession>() { @Override public boolean apply(HiveSession hiveSession) { return hiveSession.getNoOperationTime() == 0L; } }); return Iterables.size(filtered); } }; MetricsVariable<Integer> activeSessionTime = new MetricsVariable<Integer>() { @Override public Integer getValue() { long sum = 0; long currentTime = System.currentTimeMillis(); for (HiveSession s : getSessions()) { if (s.getNoOperationTime() == 0L) { sum += currentTime - s.getLastAccessTime(); } } // in case of an overflow return -1 return (int) sum != sum ? -1 : (int) sum; } }; metrics.addGauge(MetricsConstant.HS2_ACTIVE_SESSIONS, activeSessionCnt); metrics.addRatio(MetricsConstant.HS2_AVG_ACTIVE_SESSION_TIME, activeSessionTime, activeSessionCnt); }
@Test(expected = IllegalArgumentException.class) public void testMissingNumeratorRatio() throws Exception { MetricsFactory.getInstance().addRatio("rat", null, new NumericVariable(5)); }
@Test(expected = IllegalArgumentException.class) public void testMissingDenominatorRatio() throws Exception { MetricsFactory.getInstance().addRatio("rat", new NumericVariable(5), null); }
@Test public void testZeroRatio() throws Exception { NumericVariable num = new NumericVariable(10); NumericVariable ord = new NumericVariable(0); MetricsFactory.getInstance().addRatio("rat", num, ord); String json = ((CodahaleMetrics) MetricsFactory.getInstance()).dumpJson(); MetricsTestUtils.verifyMetricsJson(json, MetricsTestUtils.GAUGE, "rat", "NaN"); }
@Test public void testRatioIsCalculated() throws Exception { NumericVariable num = new NumericVariable(10); NumericVariable ord = new NumericVariable(5); MetricsFactory.getInstance().addRatio("rat", num, ord); String json = ((CodahaleMetrics) MetricsFactory.getInstance()).dumpJson(); MetricsTestUtils.verifyMetricsJson(json, MetricsTestUtils.GAUGE, "rat", 2d); }
@Test public void testRatioIsCalculatedNonExact() throws Exception { NumericVariable num = new NumericVariable(20); NumericVariable ord = new NumericVariable(3); MetricsFactory.getInstance().addRatio("rat", num, ord); String json = ((CodahaleMetrics) MetricsFactory.getInstance()).dumpJson(); MetricsTestUtils.verifyMetricsJson(json, MetricsTestUtils.GAUGE, "rat", 6.6666d, 1e-4); }
@Test public void testEmptyRatio() throws Exception { NumericVariable num = new NumericVariable(null); NumericVariable ord = new NumericVariable(null); MetricsFactory.getInstance().addRatio("rat", num, ord); String json = ((CodahaleMetrics) MetricsFactory.getInstance()).dumpJson(); MetricsTestUtils.verifyMetricsJson(json, MetricsTestUtils.GAUGE, "rat", "NaN"); }
private void registerOpenSesssionMetrics(Metrics metrics) { MetricsVariable<Integer> openSessionCnt = new MetricsVariable<Integer>() { @Override public Integer getValue() { return getSessions().size(); } }; MetricsVariable<Integer> openSessionTime = new MetricsVariable<Integer>() { @Override public Integer getValue() { long sum = 0; long currentTime = System.currentTimeMillis(); for (HiveSession s : getSessions()) { sum += currentTime - s.getCreationTime(); } // in case of an overflow return -1 return (int) sum != sum ? -1 : (int) sum; } }; metrics.addGauge(MetricsConstant.HS2_OPEN_SESSIONS, openSessionCnt); metrics.addRatio(MetricsConstant.HS2_AVG_OPEN_SESSION_TIME, openSessionTime, openSessionCnt); }
private void registerActiveSesssionMetrics(Metrics metrics) { MetricsVariable<Integer> activeSessionCnt = new MetricsVariable<Integer>() { @Override public Integer getValue() { Iterable<HiveSession> filtered = Iterables.filter(getSessions(), new Predicate<HiveSession>() { @Override public boolean apply(HiveSession hiveSession) { return hiveSession.getNoOperationTime() == 0L; } }); return Iterables.size(filtered); } }; MetricsVariable<Integer> activeSessionTime = new MetricsVariable<Integer>() { @Override public Integer getValue() { long sum = 0; long currentTime = System.currentTimeMillis(); for (HiveSession s : getSessions()) { if (s.getNoOperationTime() == 0L) { sum += currentTime - s.getLastAccessTime(); } } // in case of an overflow return -1 return (int) sum != sum ? -1 : (int) sum; } }; metrics.addGauge(MetricsConstant.HS2_ACTIVE_SESSIONS, activeSessionCnt); metrics.addRatio(MetricsConstant.HS2_AVG_ACTIVE_SESSION_TIME, activeSessionTime, activeSessionCnt); }