ReportEntryToMetricConverter { public static List<Gauge> convert(List<ReportEntry> reportEntries, String path, Map<String, LabeledMetricConf> labeledMetricConfByPattern) { return convert(reportEntries, path, labeledMetricConfByPattern, CounterReportEntryMetricExtraction::extractMetricFromReportEntry, DurationReportEntryMetricExtraction::extractMetricFromReportEntry, GaugesCreation::buildGaugeWithoutLabels, GaugesCreation::buildGaugeWithLabels); }  static List<Gauge> convert(List<ReportEntry> reportEntries,
                                      String path,
                                      Map<String, LabeledMetricConf> labeledMetricConfByPattern); static List<Gauge> convert(List<ReportEntry> reportEntries,
                                      String path,
                                      Map<String, LabeledMetricConf> labeledMetricConfByPattern,
                                      CounterReportEntryMetricExtraction.Extractor counterReportEntryMetricExtractor,
                                      DurationReportEntryMetricExtraction.Extractor durationReportEntryMetricExtractor,
                                      GaugesCreation.BuilderWithoutLabels gaugesBuilderWithoutLabels,
                                      GaugesCreation.BuilderWithLabels gaugesBuilderWithLabelsBuilder); static X uniqueElementOrThrow(List<E> list, Function<E, X> mapper); static final String REPORT_ENTRY_KEY_SEP; static final String METRIC_NAME_SEP; static final String LABEL_PATTERN_VALUE_TOKEN; static final String DURATION_METRIC_NAME_SUFFIX; }
@Test public void convertShouldReturnEmptyListOfGaugesOnEmptyReportEntries() { List<Gauge> gauges = ReportEntryToMetricConverter.convert(Collections.emptyList(), "path", Collections.emptyMap()); assertTrue(gauges.isEmpty()); }
@Test public void convertShouldReturnNonEmptyListOfGaugesOnNonEmptyCounterReportEntries() { ReportEntry counterReportEntryWithoutLabel = mock(ReportEntry.class); when(counterReportEntryWithoutLabel.getType()).thenReturn(ReportEntryType.COUNTER); ReportEntry counterReportEntryWithLabel = mock(ReportEntry.class); when(counterReportEntryWithLabel.getType()).thenReturn(ReportEntryType.COUNTER); List<ReportEntry> reportEntries = Arrays.asList(counterReportEntryWithoutLabel, counterReportEntryWithLabel); LabelConf labelConf = mock(LabelConf.class); when(labelConf.getLabelName()).thenReturn("label_name"); LabeledMetricConf labeledMetricConf = mock(LabeledMetricConf.class); when(labeledMetricConf.getMetricName()).thenReturn("metric_name_with_labels"); when(labeledMetricConf.getLabelConfs()).thenReturn(Collections.singletonList(labelConf)); Map<String, LabeledMetricConf> labeledMetricConfByPattern = Collections.singletonMap("key", labeledMetricConf); ExtractedMetric extractedMetricWithoutLabel = mock(ExtractedMetric.class); when(extractedMetricWithoutLabel.getMetricName()).thenReturn("metric_name_without_labels"); when(extractedMetricWithoutLabel.getLabelValues()).thenReturn(Collections.emptyList()); when(extractedMetricWithoutLabel.getValue()).thenReturn(1d); ExtractedMetric extractedMetricWithLabel = mock(ExtractedMetric.class); when(extractedMetricWithLabel.getMetricName()).thenReturn("metric_name_with_labels"); when(extractedMetricWithLabel.getLabelNames()).thenReturn(Collections.singletonList("label_name")); when(extractedMetricWithLabel.getLabelValues()).thenReturn(Collections.singletonList("label_value")); when(extractedMetricWithLabel.getValue()).thenReturn(10d); CounterReportEntryMetricExtraction.Extractor counterMetricExtractor = mock(CounterReportEntryMetricExtraction.Extractor.class); when(counterMetricExtractor.extract(counterReportEntryWithoutLabel, labeledMetricConfByPattern)).thenReturn(extractedMetricWithoutLabel); when(counterMetricExtractor.extract(counterReportEntryWithLabel, labeledMetricConfByPattern)).thenReturn(extractedMetricWithLabel); Gauge gaugeWithoutLabels = mock(Gauge.class); Gauge gaugeWithLabel = mock(Gauge.class); GaugesCreation.BuilderWithoutLabels builderWithoutLabels = mock(GaugesCreation.BuilderWithoutLabels.class); when(builderWithoutLabels.build("metric_name_without_labels", "location:path", 1d)) .thenReturn(gaugeWithoutLabels); GaugesCreation.BuilderWithLabels builderWithLabels = mock(GaugesCreation.BuilderWithLabels.class); when(builderWithLabels.build("metric_name_with_labels", "location:path", Collections.singletonList("label_name"), Collections.singletonList(Collections.singletonList("label_value")), Collections.singletonList(10d))).thenReturn(gaugeWithLabel); List<Gauge> gauges = ReportEntryToMetricConverter.convert(reportEntries, "path", labeledMetricConfByPattern, counterMetricExtractor, mock(DurationReportEntryMetricExtraction.Extractor.class), builderWithoutLabels, builderWithLabels ); assertEquals(Arrays.asList(gaugeWithLabel, gaugeWithoutLabels), gauges); }
@Test(expected = RuntimeException.class) public void convertShouldThrowOnLabelValueExtractionError() { ReportEntry counterReportEntryWithoutLabel = mock(ReportEntry.class); when(counterReportEntryWithoutLabel.getType()).thenReturn(ReportEntryType.COUNTER); List<ReportEntry> reportEntries = Collections.singletonList(counterReportEntryWithoutLabel); Map<String, LabeledMetricConf> labeledMetricConfByPattern = Collections.singletonMap("key", mock(LabeledMetricConf.class)); ExtractedMetric extractedMetricWithoutLabel = mock(ExtractedMetric.class); when(extractedMetricWithoutLabel.getMetricName()).thenReturn("metric_name_without_labels"); when(extractedMetricWithoutLabel.getLabelValues()).thenReturn(Collections.emptyList()); when(extractedMetricWithoutLabel.getValue()).thenReturn(null); CounterReportEntryMetricExtraction.Extractor counterMetricExtractor = mock(CounterReportEntryMetricExtraction.Extractor.class); when(counterMetricExtractor.extract(counterReportEntryWithoutLabel, labeledMetricConfByPattern)).thenReturn(extractedMetricWithoutLabel); ReportEntryToMetricConverter.convert(reportEntries, "path", labeledMetricConfByPattern, counterMetricExtractor, mock(DurationReportEntryMetricExtraction.Extractor.class), mock(GaugesCreation.BuilderWithoutLabels.class), mock(GaugesCreation.BuilderWithLabels.class) ); }
@Test public void convertShouldReturnNonEmptyListOfGaugesOnNonEmptyDurationReportEntries() { ReportEntry durationReportEntry = mock(ReportEntry.class); when(durationReportEntry.getType()).thenReturn(ReportEntryType.DURATION); List<ReportEntry> reportEntries = Collections.singletonList(durationReportEntry); Map<String, LabeledMetricConf> labeledMetricConfByPattern = Collections.singletonMap("key", mock(LabeledMetricConf.class)); ExtractedMetric extractedMetric = mock(ExtractedMetric.class); when(extractedMetric.getMetricName()).thenReturn("metric_name"); when(extractedMetric.getValue()).thenReturn(1d); DurationReportEntryMetricExtraction.Extractor durationMetricExtractor = mock(DurationReportEntryMetricExtraction.Extractor.class); when(durationMetricExtractor.extract(durationReportEntry)).thenReturn(extractedMetric); Gauge gauge = mock(Gauge.class); GaugesCreation.BuilderWithoutLabels builderWithoutLabels = mock(GaugesCreation.BuilderWithoutLabels.class); when(builderWithoutLabels.build("metric_name", "location:path", 1d)).thenReturn(gauge); List<Gauge> gauges = ReportEntryToMetricConverter.convert(reportEntries, "path", labeledMetricConfByPattern, mock(CounterReportEntryMetricExtraction.Extractor.class), durationMetricExtractor, builderWithoutLabels, mock(GaugesCreation.BuilderWithLabels.class) ); assertEquals(Collections.singletonList(gauge), gauges); }