/** * 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); } } }
@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); } } }