forked from lsjostro/prometheus-plugin
-
Notifications
You must be signed in to change notification settings - Fork 152
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fixing filtering of external metrics (#558)
- Loading branch information
Waschndolos
authored
Sep 18, 2023
1 parent
7a4429f
commit bc33d87
Showing
3 changed files
with
88 additions
and
16 deletions.
There are no files selected for viewing
41 changes: 41 additions & 0 deletions
41
...va/org/jenkinsci/plugins/prometheus/config/disabledmetrics/FilteredMetricEnumeration.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
package org.jenkinsci.plugins.prometheus.config.disabledmetrics; | ||
|
||
import io.prometheus.client.Collector; | ||
|
||
import java.util.*; | ||
|
||
public class FilteredMetricEnumeration implements Enumeration<Collector.MetricFamilySamples> { | ||
|
||
private final Iterator<Collector.MetricFamilySamples> filteredList; | ||
|
||
public FilteredMetricEnumeration(Iterator<Collector.MetricFamilySamples> fullList) { | ||
this.filteredList = filterList(fullList); | ||
} | ||
|
||
private Iterator<Collector.MetricFamilySamples> filterList(Iterator<Collector.MetricFamilySamples> fullList) { | ||
List<Collector.MetricFamilySamples> filteredList = new ArrayList<>(); | ||
while (fullList.hasNext()) { | ||
Collector.MetricFamilySamples familySamples = fullList.next(); | ||
if (MetricStatusChecker.isEnabled(familySamples.name)) { | ||
filteredList.add(familySamples); | ||
} | ||
} | ||
return filteredList.iterator(); | ||
} | ||
|
||
|
||
@Override | ||
public boolean hasMoreElements() { | ||
return filteredList.hasNext(); | ||
} | ||
|
||
@Override | ||
public Collector.MetricFamilySamples nextElement() { | ||
return filteredList.next(); | ||
} | ||
|
||
@Override | ||
public Iterator<Collector.MetricFamilySamples> asIterator() { | ||
return filteredList; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
45 changes: 45 additions & 0 deletions
45
...rg/jenkinsci/plugins/prometheus/config/disabledmetrics/FilteredMetricEnumerationTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
package org.jenkinsci.plugins.prometheus.config.disabledmetrics; | ||
|
||
import io.prometheus.client.Collector; | ||
import org.junit.jupiter.api.Assertions; | ||
import org.junit.jupiter.api.Test; | ||
import org.mockito.MockedStatic; | ||
|
||
import java.util.Enumeration; | ||
import java.util.Iterator; | ||
import java.util.List; | ||
|
||
import static org.mockito.Mockito.mockStatic; | ||
|
||
public class FilteredMetricEnumerationTest { | ||
|
||
@Test | ||
void testFilterMatches() { | ||
try (MockedStatic<MetricStatusChecker> statusCheckerMockedStatic = mockStatic(MetricStatusChecker.class)) { | ||
|
||
statusCheckerMockedStatic.when(() -> MetricStatusChecker.isEnabled("metric_1")).thenReturn(false); | ||
statusCheckerMockedStatic.when(() -> MetricStatusChecker.isEnabled("metric_2")).thenReturn(true); | ||
|
||
List<Collector.MetricFamilySamples> list = List.of( | ||
new Collector.MetricFamilySamples("metric_1", Collector.Type.GAUGE, "help1", List.of()), | ||
new Collector.MetricFamilySamples("metric_2", Collector.Type.GAUGE, "help2", List.of()) | ||
); | ||
|
||
Iterator<Collector.MetricFamilySamples> iterator = list.iterator(); | ||
|
||
|
||
FilteredMetricEnumeration filteredMetricEnumeration = new FilteredMetricEnumeration(iterator); | ||
|
||
int foundElements = 0; | ||
String foundKey = ""; | ||
while (filteredMetricEnumeration.hasMoreElements()) { | ||
Collector.MetricFamilySamples familySamples = filteredMetricEnumeration.nextElement(); | ||
foundKey = familySamples.name; | ||
foundElements++; | ||
} | ||
Assertions.assertEquals(1, foundElements); | ||
Assertions.assertEquals("metric_2", foundKey); | ||
|
||
} | ||
} | ||
} |