MetricQueryService { public MetricData queryMetricSource(Config metricSourceSinkConfig, MetricFunctionsSpec metricFunctionsSpec, Instant instant) throws IllegalStateException, MetricQueryServiceException { String metricSourceName = metricSourceSinkConfig.getString(METRIC_SOURCE_KEY); switch (metricSourceName) { case "graphite": return queryGraphite(metricSourceSinkConfig, metricFunctionsSpec, instant); default: throw new IllegalStateException(String.format("Unknown metric source '%s'.", metricSourceName)); } } MetricQueryService();  MetricQueryService(HttpClientWrapper httpClientWrapper); MetricData queryMetricSource(Config metricSourceSinkConfig, MetricFunctionsSpec metricFunctionsSpec,
            Instant instant);  }
@Test public void testValidGraphiteMetricQueryResult() throws Exception { Instant fixedInstant = Instant.parse("2020-03-01T05:05:39Z"); MetricQueryService metricQueryService = new MetricQueryService(httpClient); MetricData metricDataResult = metricQueryService.queryMetricSource(metricSourceSinkConfig, metricFunctionsSpec, fixedInstant); assertEquals(12.0, metricDataResult.getValue(), 0.1); assertEquals(1583039100, metricDataResult.getTimestamp()); Map<String, String> tags = metricDataResult.getMetricDefinition().getTags().getKv(); assertEquals(6, tags.size()); assertEquals("sample_app1", tags.get("app_name")); assertEquals("test", tags.get("env")); assertEquals("custom_tag_value", tags.get("custom_tag")); assertEquals("sum", tags.get("aggregatedBy")); assertEquals("sumSeries(a.b.c)", tags.get("name")); assertEquals("added_tag_value", tags.get("added_tag")); }
@Test public void testValidGraphiteMetricQueryResultMergeTagsFalse() throws Exception { String functionsInputFile = "config/functions-mergeTags-false-test.txt"; MetricFunctionsSpec metricFunctionsSpec = MetricFunctionsReader .readFromInputFile(ClassLoader.getSystemResource(functionsInputFile).getPath()).get(0); Instant fixedInstant = Instant.parse("2020-03-01T05:05:39Z"); MetricQueryService metricQueryService = new MetricQueryService(httpClient); MetricData metricDataResult = metricQueryService.queryMetricSource(metricSourceSinkConfig, metricFunctionsSpec, fixedInstant); assertEquals(12.0, metricDataResult.getValue(), 0.1); assertEquals(1583039100, metricDataResult.getTimestamp()); Map<String, String> tags = metricDataResult.getMetricDefinition().getTags().getKv(); assertEquals(2, tags.size()); assertEquals("sample_app2", tags.get("app_name")); assertEquals("test", tags.get("env")); }
@Test public void testValidMetrictankMetricQueryResult() throws Exception { Map<String, String> metrictankMetricSourceSinkConfigMap = new HashMap<>(); metrictankMetricSourceSinkConfigMap.put("urlTemplate", "http: metrictankMetricSourceSinkConfigMap.put("is-graphite-server-metrictank", "metrictank"); metrictankMetricSourceSinkConfigMap.put("metric-source", "graphite"); Config metrictankMetricSourceSinkConfig = ConfigFactory.parseMap(metrictankMetricSourceSinkConfigMap); Instant fixedInstant = Instant.parse("2020-03-01T05:05:39Z"); MetricQueryService metricQueryService = new MetricQueryService(httpClient); MetricData metricDataResult = metricQueryService.queryMetricSource(metrictankMetricSourceSinkConfig, metricFunctionsSpec, fixedInstant); assertEquals(12.0, metricDataResult.getValue(), 0.1); assertEquals(1583039100, metricDataResult.getTimestamp()); Map<String, String> tags = metricDataResult.getMetricDefinition().getTags().getKv(); assertEquals(6, tags.size()); assertEquals("sample_app1", tags.get("app_name")); assertEquals("test", tags.get("env")); assertEquals("custom_tag_value", tags.get("custom_tag")); assertEquals("sum", tags.get("aggregatedBy")); assertEquals("sumSeries(a.b.c)", tags.get("name")); assertEquals("added_tag_value", tags.get("added_tag")); }
@Test public void testValidGraphiteMetricQueryResultWithNull() throws Exception { Instant fixedInstant = Instant.parse("2020-03-02T05:05:39Z"); MetricQueryService metricQueryService = new MetricQueryService(httpClient); MetricData metricDataResult = metricQueryService.queryMetricSource(metricSourceSinkConfig, metricFunctionsSpec, fixedInstant); assertEquals(155.0, metricDataResult.getValue(), 0.1); assertEquals(1583125500, metricDataResult.getTimestamp()); }
@Test public void testInvalidGraphiteMetricQueryResult() { try { Instant fixedInstant = Instant.parse("2020-03-03T05:05:39Z"); MetricQueryService metricQueryService = new MetricQueryService(httpClient); metricQueryService.queryMetricSource(metricSourceSinkConfig, metricFunctionsSpec, fixedInstant); } catch (MetricQueryServiceException e) { assertTrue(true); } }
@Test public void testInvalidMetricQuerySource() { try { Map<String, String> invalidMetricSourceSinkConfigMap = new HashMap<>(); invalidMetricSourceSinkConfigMap.put("urlTemplate", "http: invalidMetricSourceSinkConfigMap.put("metric-source", "bad-metric-source"); Config invalidMetricSourceSinkConfig = ConfigFactory.parseMap(invalidMetricSourceSinkConfigMap); Instant fixedInstant = Instant.parse("2020-03-03T05:05:39Z"); MetricQueryService metricQueryService = new MetricQueryService(httpClient); metricQueryService.queryMetricSource(invalidMetricSourceSinkConfig, metricFunctionsSpec, fixedInstant); } catch (IllegalStateException e) { assertTrue(true); } }
@Test public void testEmptyGraphiteMetricQueryResult() { try { Instant fixedInstant = Instant.parse("2020-03-04T05:05:00Z"); MetricQueryService metricQueryService = new MetricQueryService(httpClient); metricQueryService.queryMetricSource(metricSourceSinkConfig, metricFunctionsSpec, fixedInstant); } catch (MetricQueryServiceException e) { assertTrue(true); } }