CloudWatchEmbeddedMetricsReporter implements Reporter { public static String getCloudWatchEmbeddedMetricsJson(String namespace, long timestamp, Map<String, String> dimensions, List<Stat> stats, Gson gson) { Map<String, Object> embeddedMetricsObject = new HashMap<>(); dimensions.forEach(embeddedMetricsObject::put); List<CloudWatchMetricObject> cwMetricObjects = new ArrayList<>(); stats.forEach(s -> { List<String> allDimensions = new ArrayList<>(dimensions.keySet()); List<Dimension> statDimensions = tagsToDimensions(s.getTags()); allDimensions.addAll(statDimensions.stream() .map(Dimension::getName) .collect(Collectors.toList())); allDimensions = allDimensions.stream() .distinct() .collect(Collectors.toList()); statDimensions.forEach(d -> embeddedMetricsObject.put(d.getName(), d.getValue())); embeddedMetricsObject.put(s.getName(), s.getValue()); cwMetricObjects.add(new CloudWatchMetricObject(namespace, Collections.singletonList(allDimensions), Collections.singletonList(new MetricPOJO(s.getName(), StandardUnit.None.toString()))) ); }); embeddedMetricsObject.put("_aws", new AWSMetricObject(timestamp, cwMetricObjects)); return gson.toJson(embeddedMetricsObject); } CloudWatchEmbeddedMetricsReporter(final String namespace,
                                             final List<StatFilter> statFilters); @Override void write(ArrayList<Stat> stats, long invokeTimeMs, Set<Tag> tags); @Override List<StatFilter> getStatFilters(); static String getCloudWatchEmbeddedMetricsJson(String namespace,
                                                          long timestamp,
                                                          Map<String, String> dimensions,
                                                          List<Stat> stats,
                                                          Gson gson);  }
@Test public void testWithNoStats() throws Exception { String actualJson = CloudWatchEmbeddedMetricsReporter.getCloudWatchEmbeddedMetricsJson("nextdoor", timestamp, Collections.emptyMap(), Collections.emptyList(), gson); String expectedJson = IOUtils.toString( new InputStreamReader(this.getClass().getResourceAsStream("empty_metrics.json"), "UTF-8")); assertEquals(expectedJson, actualJson); }
@Test public void testWithNoDimensionsAndStats() throws Exception { List<Stat> stats = Arrays.asList(new Stat("counts", 20), new Stat("errors", 2)); String actualJson = CloudWatchEmbeddedMetricsReporter.getCloudWatchEmbeddedMetricsJson("nextdoor", timestamp, Collections.emptyMap(), stats, gson); String expectedJson = IOUtils.toString( new InputStreamReader(this.getClass().getResourceAsStream("metrics_with_stats_no_dimensions.json"), "UTF-8")); assertEquals(expectedJson, actualJson); }
@Test public void testWithDimensionsOnSomeStats() throws Exception { Stat s1 = new Stat("counts", 20); s1.addTag("stage", "prod"); s1.addTag("region", "us-east-1"); Stat s2 = new Stat("errors", 2); List<Stat> stats = Arrays.asList(s1, s2); String actualJson = CloudWatchEmbeddedMetricsReporter.getCloudWatchEmbeddedMetricsJson("nextdoor", timestamp, Collections.emptyMap(), stats, gson); String expectedJson = IOUtils.toString( new InputStreamReader(this.getClass().getResourceAsStream("metrics_with_stats_varying_dimensions.json"), "UTF-8")); assertEquals(expectedJson, actualJson); }
@Test public void testWithOverallAndGranularDimensions() throws Exception { Stat s1 = new Stat("counts", 20); s1.addTag("stage", "prod"); s1.addTag("region", "us-east-1"); Stat s2 = new Stat("errors", 2); List<Stat> stats = Arrays.asList(s1, s2); Map<String, String> dimensions = new HashMap<>(); dimensions.put("team", "systems"); dimensions.put("company", "nextdoor"); String actualJson = CloudWatchEmbeddedMetricsReporter.getCloudWatchEmbeddedMetricsJson("nextdoor", timestamp, dimensions, stats, gson); String expectedJson = IOUtils.toString( new InputStreamReader(this.getClass().getResourceAsStream("metrics_with_stats_many_dimensions.json"), "UTF-8")); assertEquals(expectedJson, actualJson); }