/** * Converts a single key-value pair into a metric. * * @param name name of the metric * @param value value of the metric to report * @param path additional suffixes to further identify the meaning of the reported value * @return a {@link org.apache.gobblin.metrics.Metric}. */ protected Metric serializeValue(String name, Number value, String... path) { return new Metric(MetricRegistry.name(name, path), value.doubleValue()); } }
/** * Expect a list of metrics with specific values. * Fail if not all metrics are received, or some metric has the wrong value. * @param report MetricReport. * @param expected map of expected metric names and their values * @throws IOException */ private void expectMetricsWithValues(MetricReport report, Map<String, Double> expected) throws IOException { List<Metric> metricIterator = report.getMetrics(); for (Metric metric : metricIterator) { if (expected.containsKey(metric.getName())) { Assert.assertEquals(expected.get(metric.getName()), metric.getValue()); expected.remove(metric.getName()); } } Assert.assertTrue(expected.isEmpty()); }
/** * Expect a set of metric names. Will fail if not all of these metrics are received. * @param report MetricReport * @param expected set of expected metric names * @param strict if set to true, will fail if receiving any metric that is not expected * @throws IOException */ private void expectMetrics(MetricReport report, Set<String> expected, boolean strict) throws IOException { List<Metric> metricIterator = report.getMetrics(); for (Metric metric : metricIterator) { //System.out.println(String.format("expectedSet.add(\"%s\")", metric.name)); if (expected.contains(metric.getName())) { expected.remove(metric.getName()); } else if (strict && !metric.getName().contains(MetricContext.GOBBLIN_METRICS_NOTIFICATIONS_TIMER_NAME)) { Assert.assertTrue(false, "Metric present in report not expected: " + metric.toString()); } } Assert.assertTrue(expected.isEmpty()); }
/** * Extracts metrics from {@link com.codahale.metrics.Gauge}. * * @param name name of the {@link com.codahale.metrics.Gauge}. * @param gauge instance of {@link com.codahale.metrics.Gauge} to serialize. * @return a list of {@link org.apache.gobblin.metrics.Metric}. */ protected List<Metric> serializeGauge(String name, Gauge gauge) { List<Metric> metrics = Lists.newArrayList(); try { metrics.add(new Metric(name, Double.parseDouble(gauge.getValue().toString()))); } catch(NumberFormatException exception) { LOGGER.info("Failed to serialize gauge metric. Not compatible with double value.", exception); } return metrics; }
@Override public Metric build() { try { Metric record = new Metric(); record.name = fieldSetFlags()[0] ? this.name : (java.lang.String) defaultValue(fields()[0]); record.value = fieldSetFlags()[1] ? this.value : (java.lang.Double) defaultValue(fields()[1]); return record; } catch (Exception e) { throw new org.apache.avro.AvroRuntimeException(e); } } }
@Test public void test() throws Exception { MetricReport metricReport = new MetricReport(); metricReport.setTags(ImmutableMap.of("tag", "value", "tag2", "value2")); metricReport.setTimestamp(10L); metricReport.setMetrics(Lists.newArrayList(new Metric("metric", 1.0), new Metric("metric2", 2.0))); AvroSerializer<MetricReport> serializer = new AvroBinarySerializer<>(MetricReport.SCHEMA$, new NoopSchemaVersionWriter()); serializer.serializeRecord(metricReport); Schema metricReportUtf8 = new Schema.Parser().parse(this.getClass().getClassLoader().getResourceAsStream("MetricReport.avsc")); GenericRecord genericRecordMetric = AvroUtils.slowDeserializeGenericRecord(serializer.serializeRecord(metricReport), metricReportUtf8); GobblinMetricsPinotFlattenerConverter converter = new GobblinMetricsPinotFlattenerConverter(); Schema outputSchema = converter.convertSchema(MetricReport.SCHEMA$, new WorkUnitState()); Iterable<GenericRecord> converted = converter.convertRecord(outputSchema, genericRecordMetric, new WorkUnitState()); List<GenericRecord> convertedList = Lists.newArrayList(converted); Assert.assertEquals(convertedList.size(), 2); Assert.assertEquals(Sets.newHashSet((List<Utf8>) convertedList.get(0).get("tags")), Sets.newHashSet("tag:value", "tag2:value2")); Assert.assertEquals(convertedList.get(0).get("timestamp"), 10L); Assert.assertEquals(convertedList.get(0).get("metricName").toString(), "metric"); Assert.assertEquals(convertedList.get(0).get("metricValue"), 1.0); Assert.assertEquals(Sets.newHashSet((List<Utf8>) convertedList.get(1).get("tags")), Sets.newHashSet("tag:value", "tag2:value2")); Assert.assertEquals(convertedList.get(1).get("timestamp"), 10L); Assert.assertEquals(convertedList.get(1).get("metricName").toString(), "metric2"); Assert.assertEquals(convertedList.get(1).get("metricValue"), 2.0); }
/** * Converts a single key-value pair into a metric. * * @param name name of the metric * @param value value of the metric to report * @param path additional suffixes to further identify the meaning of the reported value * @return a {@link org.apache.gobblin.metrics.Metric}. */ protected Metric serializeValue(String name, Number value, String... path) { return new Metric(MetricRegistry.name(name, path), value.doubleValue()); } }
/** * Extracts metrics from {@link com.codahale.metrics.Gauge}. * * @param name name of the {@link com.codahale.metrics.Gauge}. * @param gauge instance of {@link com.codahale.metrics.Gauge} to serialize. * @return a list of {@link org.apache.gobblin.metrics.Metric}. */ protected List<Metric> serializeGauge(String name, Gauge gauge) { List<Metric> metrics = Lists.newArrayList(); try { metrics.add(new Metric(name, Double.parseDouble(gauge.getValue().toString()))); } catch(NumberFormatException exception) { LOGGER.info("Failed to serialize gauge metric. Not compatible with double value.", exception); } return metrics; }
@Override public Metric build() { try { Metric record = new Metric(); record.name = fieldSetFlags()[0] ? this.name : (java.lang.String) defaultValue(fields()[0]); record.value = fieldSetFlags()[1] ? this.value : (java.lang.Double) defaultValue(fields()[1]); return record; } catch (Exception e) { throw new org.apache.avro.AvroRuntimeException(e); } } }