GoogleMonitoringIngester extends DataIngester { void processResponse(ListTimeSeriesResponse response, List<String> allMetrics, Map<String, Map<String, Record>> byTime) { for (TimeSeries ts : response.getTimeSeries()) { if (!allMetrics.contains(ts.getMetric().getType())) { ++skippedTimeSeries; continue; } Map<String, String> attrMap = new TreeMap<>(); boolean hasAllAttributes = true; for (String a : attributes) { String val = ts.getMetric().getLabels().get(a); if (val == null) { val = ts.getResource().getLabels().get(a); if (val == null) { log.debug("Skipping TimeSeries due to missing attribute: " + a); hasAllAttributes = false; break; } } attrMap.put(a, val); } if (!hasAllAttributes) { ++skippedTimeSeries; continue; } String attrKey = attrMap.toString(); for (Point p : ts.getPoints()) { String timestamp = p.getInterval().getEndTime(); byTime.putIfAbsent(timestamp, new HashMap<>()); Record rec = byTime.get(timestamp).putIfAbsent(attrKey, new Record()); if (rec == null) { rec = byTime.get(timestamp).get(attrKey); rec.attributes = attrMap; rec.values = new HashMap<>(); } double val; switch (ts.getValueType()) { case "DOUBLE": val = p.getValue().getDoubleValue(); break; case "INT64": val = p.getValue().getInt64Value(); break; case "BOOL": val = (p.getValue().getBoolValue()) ? 1.0 : 0.0; break; default: continue; } rec.values.put(ts.getMetric().getType(), val); } } } GoogleMonitoringIngester(MacroBaseConf conf); @Override MBStream<Datum> getStream(); static final String GOOGLE_MONITORING_QUERIES; static final String GOOGLE_MONITORING_START_TIME; static final String GOOGLE_MONITORING_END_TIME; }
@Test public void testProcessResponse() throws Exception { Map<String, String> metricLabels1 = new HashMap<>(); metricLabels1.put("attr1", "foo"); metricLabels1.put("attr2", "bar"); Map<String, String> metricLabels2 = new HashMap<>(); metricLabels2.put("attr1", "foo"); metricLabels2.put("attr2", "baz"); Map<String, String> metricLabels3 = new HashMap<>(); metricLabels3.put("attr1", "foo"); Metric metric1 = new Metric(); metric1.setType("custom.googleapis.com/test"); metric1.setLabels(metricLabels1); Metric metric2 = new Metric(); metric2.setType("custom.googleapis.com/skipped"); metric2.setLabels(metricLabels1); Metric metric3 = new Metric(); metric3.setType("custom.googleapis.com/test"); metric3.setLabels(metricLabels2); Metric metric4 = new Metric(); metric4.setType("custom.googleapis.com/test"); metric4.setLabels(metricLabels3); Map<String, String> resourceLabels1 = new HashMap<>(); resourceLabels1.put("project_id", "my-project"); resourceLabels1.put("region", "us-central-1"); Map<String, String> resourceLabels2 = new HashMap<>(); resourceLabels2.put("project_id", "my-project"); resourceLabels2.put("region", "us-central-1"); resourceLabels2.put("attr2", "bar"); MonitoredResource resource1 = new MonitoredResource(); resource1.setType("gce_instance"); resource1.setLabels(resourceLabels1); MonitoredResource resource2 = new MonitoredResource(); resource2.setType("gce_instance"); resource2.setLabels(resourceLabels2); TimeInterval time1 = new TimeInterval(); time1.setEndTime("2016-08-08T00:00:00Z"); TimeInterval time2 = new TimeInterval(); time2.setEndTime("2016-08-08T00:00:01Z"); TimeInterval time3 = new TimeInterval(); time3.setEndTime("2016-08-08T00:00:02Z"); Point p1 = new Point(); p1.setInterval(time1); p1.setValue(new TypedValue()); p1.getValue().setDoubleValue(77.0); Point p2 = new Point(); p2.setInterval(time2); p2.setValue(new TypedValue()); p2.getValue().setDoubleValue(88.0); Point p3 = new Point(); p3.setInterval(time1); p3.setValue(new TypedValue()); p3.getValue().setInt64Value(99l); Point p4 = new Point(); p4.setInterval(time3); p4.setValue(new TypedValue()); p4.getValue().setBoolValue(true); TimeSeries ts1 = new TimeSeries(); ts1.setMetric(metric1); ts1.setResource(resource1); ts1.setPoints(Lists.newArrayList(p1, p2)); ts1.setValueType("DOUBLE"); ts1.setMetricKind("GAUGE"); TimeSeries ts2 = new TimeSeries(); ts2.setMetric(metric2); ts2.setResource(resource1); ts2.setPoints(Lists.newArrayList(p2)); ts2.setValueType("DOUBLE"); ts2.setMetricKind("GAUGE"); TimeSeries ts3 = new TimeSeries(); ts3.setMetric(metric3); ts3.setResource(resource1); ts3.setPoints(Lists.newArrayList(p3)); ts3.setValueType("INT64"); ts3.setMetricKind("GAUGE"); TimeSeries ts4 = new TimeSeries(); ts4.setMetric(metric4); ts4.setResource(resource2); ts4.setPoints(Lists.newArrayList(p4)); ts4.setValueType("BOOL"); ts4.setMetricKind("GAUGE"); ListTimeSeriesResponse response = new ListTimeSeriesResponse(); response.setTimeSeries(Lists.newArrayList(ts1, ts2, ts3, ts4)); List<String> allMetrics = new ArrayList<>(); allMetrics.add("custom.googleapis.com/test"); Map<String, Map<String, Record>> byTime = new TreeMap<>(); GoogleMonitoringIngester ingester = new GoogleMonitoringIngester(conf); ingester.processResponse(response, allMetrics, byTime); assertEquals(3, byTime.size()); Map<String, Record> recordMap1 = byTime.get("2016-08-08T00:00:00Z"); List<Record> records1 = Lists.newArrayList(recordMap1.values()); assertEquals(2, records1.size()); assertEquals(2, records1.get(0).attributes.size()); assertEquals("foo", records1.get(0).attributes.get("attr1")); assertEquals("baz", records1.get(0).attributes.get("attr2")); assertEquals(99, records1.get(0).values.get("custom.googleapis.com/test"), 0.0); assertEquals(2, records1.get(1).attributes.size()); assertEquals("foo", records1.get(1).attributes.get("attr1")); assertEquals("bar", records1.get(1).attributes.get("attr2")); assertEquals(77, records1.get(1).values.get("custom.googleapis.com/test"), 0.0); Map<String, Record> recordMap2 = byTime.get("2016-08-08T00:00:01Z"); List<Record> records2 = Lists.newArrayList(recordMap2.values()); assertEquals(1, records2.size()); assertEquals(2, records2.get(0).attributes.size()); assertEquals("foo", records2.get(0).attributes.get("attr1")); assertEquals("bar", records2.get(0).attributes.get("attr2")); assertEquals(88, records2.get(0).values.get("custom.googleapis.com/test"), 0.0); Map<String, Record> recordMap3 = byTime.get("2016-08-08T00:00:02Z"); List<Record> records3 = Lists.newArrayList(recordMap3.values()); assertEquals(1, records3.size()); assertEquals(2, records3.get(0).attributes.size()); assertEquals("foo", records3.get(0).attributes.get("attr1")); assertEquals("bar", records3.get(0).attributes.get("attr2")); assertEquals(1.0, records3.get(0).values.get("custom.googleapis.com/test"), 0.0); }