MetricsService { @SuppressWarnings("unchecked") public AtlasMetrics getMetrics(boolean ignoreCache) { if (ignoreCache || !isCacheValid()) { AtlasMetrics metrics = new AtlasMetrics(); for (MetricQuery metricQuery : MetricQuery.values()) { try { if (LOG.isDebugEnabled()) { LOG.debug("Executing query: {}", metricQuery); } executeGremlinQuery(metrics, metricQuery.group, metricQuery.name, metricQuery.query); } catch (AtlasBaseException e) { if (LOG.isDebugEnabled()) { LOG.debug("Gremlin execution failed for metric {}", metricQuery, e); } else { LOG.warn("Gremlin execution failed for metric {}", metricQuery); } } } long collectionTime = System.currentTimeMillis(); metrics.addData(GENERAL, METRIC_COLLECTION_TIME, collectionTime); this.cachedMetrics = metrics; this.cacheExpirationTime = (collectionTime + cacheTTLInSecs * 1000); } return cachedMetrics; } @Inject  MetricsService(AtlasGraph atlasGraph); @VisibleForTesting  MetricsService(Configuration configuration, AtlasGraph graph); @SuppressWarnings("unchecked") AtlasMetrics getMetrics(boolean ignoreCache); static final String TYPE; static final String ENTITY; static final String TAG; static final String GENERAL; static final String METRIC_QUERY_PREFIX; static final String METRIC_QUERY_CACHE_TTL; static final int DEFAULT_CACHE_TTL_IN_SECS; static final String METRIC_COLLECTION_TIME; }
@Test public void testGetMetrics() throws InterruptedException, AtlasBaseException { assertNotNull(metricsService); AtlasMetrics metrics = metricsService.getMetrics(false); assertNotNull(metrics); Number aCount = metrics.getMetric("entity", "a"); assertNotNull(aCount); assertEquals(aCount, 1); Number bCount = metrics.getMetric("entity", "b"); assertNotNull(bCount); assertEquals(bCount, 2); Number cCount = metrics.getMetric("entity", "c"); assertNotNull(cCount); assertEquals(cCount, 3); Number aTags = metrics.getMetric("tag", "a"); assertNotNull(aTags); assertEquals(aTags, 1); Number bTags = metrics.getMetric("tag", "b"); assertNotNull(bTags); assertEquals(bTags, 2); Number cTags = metrics.getMetric("tag", "c"); assertNotNull(cTags); assertEquals(cTags, 3); verify(mockGraph, atLeastOnce()).executeGremlinScript(anyString(), anyBoolean()); metricsService.getMetrics(false); verifyZeroInteractions(mockGraph); Thread.sleep(6000); metricsService.getMetrics(true); verify(mockGraph, atLeastOnce()).executeGremlinScript(anyString(), anyBoolean()); }