CounterReportEntryMetricExtraction { public static ExtractedMetric extractMetricFromReportEntry(ReportEntry reportEntry, Map<String, LabeledMetricConf> labeledMetricConfByPattern) { return extractMetricFromReportEntry(reportEntry, labeledMetricConfByPattern, LabelValuesExtraction::extractLabelValuesByPosition); } private  CounterReportEntryMetricExtraction(); static ExtractedMetric extractMetricFromReportEntry(ReportEntry reportEntry,
                                                               Map<String, LabeledMetricConf> labeledMetricConfByPattern); static ExtractedMetric extractMetricFromReportEntry(ReportEntry reportEntry,
                                                               Map<String, LabeledMetricConf> labeledMetricConfByPattern,
                                                               LabelValuesExtraction.Extractor labelValuesExtractor);  }
@Test public void shouldExtractMetricWithoutLabels() { ReportEntry reportEntry = new ReportEntry("a.b.c.d.e-f", ReportEntryType.COUNTER, "1"); Map<String, LabeledMetricConf> labeledMetricConfByPattern = Collections.emptyMap(); LabelValuesExtraction.Extractor labelValuesExtractor = mock(LabelValuesExtraction.Extractor.class); ExtractedMetric result = CounterReportEntryMetricExtraction .extractMetricFromReportEntry(reportEntry, labeledMetricConfByPattern, labelValuesExtractor); assertEquals("a_b_c_d_ef", result.getMetricName()); assertTrue(result.getLabelValues().isEmpty()); assertEquals(1.0, result.getValue(), 1e-3); verify(labelValuesExtractor, never()).extract(anyString(), any(LabeledMetricConf.class)); }
@Test public void shouldExtractMetricWithLabels() { ReportEntry reportEntry = new ReportEntry("a.b.c.d", ReportEntryType.COUNTER, "1"); LabeledMetricConf labeledMetricConf = mock(LabeledMetricConf.class); when(labeledMetricConf.getMetricName()).thenReturn("a_b_c"); when(labeledMetricConf.toExtractedMetric(Collections.singletonList("d"), 1d)).thenReturn(mock(ExtractedMetric.class)); Map<String, LabeledMetricConf> labeledMetricConfByPattern = Collections.singletonMap("a\\.b\\.c\\.\\S+", labeledMetricConf); LabelValuesExtraction.Extractor labelValuesExtractor = mock(LabelValuesExtraction.Extractor.class); when(labelValuesExtractor.extract("a.b.c.d", labeledMetricConf)).thenReturn(Collections.singletonList("d")); ExtractedMetric result = CounterReportEntryMetricExtraction .extractMetricFromReportEntry(reportEntry, labeledMetricConfByPattern, labelValuesExtractor); assertNotNull(result); }