Skip to content

Commit 5b68caf

Browse files
authored
add whitelist for prometheus metrics (#280)
* add whitelist for prometheus metrics
1 parent 7239d17 commit 5b68caf

File tree

3 files changed

+41
-5
lines changed

3 files changed

+41
-5
lines changed

src/main/java/com/arpnetworking/metrics/common/sources/PrometheusHttpSource.java

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,11 @@
1616
package com.arpnetworking.metrics.common.sources;
1717

1818
import com.arpnetworking.metrics.mad.parsers.PrometheusToRecordParser;
19+
import com.google.common.collect.Sets;
1920
import net.sf.oval.constraint.NotNull;
2021

22+
import java.util.HashSet;
23+
2124
/**
2225
* Processes Prometheus messages, extracts data and emits metrics.
2326
*
@@ -67,6 +70,18 @@ public Builder setOutputDebugFiles(final Boolean value) {
6770
return this;
6871
}
6972

73+
/**
74+
* Whitelist for reserved metrics names (names with aggregation keys). By default, we ignore metrics with
75+
* aggregation keys unless specified here. Cannot be null.
76+
*
77+
* @param value the value
78+
* @return this {@link Builder}
79+
*/
80+
public Builder setReservedNameWhitelist(final HashSet<String> value) {
81+
_reservedNameWhitelist = value;
82+
return this;
83+
}
84+
7085
@Override
7186
protected Builder self() {
7287
return this;
@@ -78,9 +93,12 @@ protected Builder self() {
7893
@NotNull
7994
private Boolean _outputDebugFiles = false;
8095

96+
@NotNull
97+
private HashSet<String> _reservedNameWhitelist = Sets.newHashSet();
98+
8199
@Override
82100
public PrometheusHttpSource build() {
83-
setParser(new PrometheusToRecordParser(_interpretUnits, _outputDebugFiles));
101+
setParser(new PrometheusToRecordParser(_interpretUnits, _outputDebugFiles, _reservedNameWhitelist));
84102
return super.build();
85103
}
86104
}

src/main/java/com/arpnetworking/metrics/mad/parsers/PrometheusToRecordParser.java

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@
4545
import java.time.Instant;
4646
import java.time.ZoneOffset;
4747
import java.time.ZonedDateTime;
48+
import java.util.HashSet;
4849
import java.util.List;
4950
import java.util.Objects;
5051
import java.util.Optional;
@@ -63,10 +64,15 @@ public final class PrometheusToRecordParser implements Parser<List<Record>, Http
6364
*
6465
* @param interpretUnits specifies whether to interpret units.
6566
* @param outputDebugInfo specifies whether to output debug files.
67+
* @param reservedNameWhitelist specifies the whitelist set for reserved names (names with aggregation keys).
6668
*/
67-
public PrometheusToRecordParser(final boolean interpretUnits, final boolean outputDebugInfo) {
69+
public PrometheusToRecordParser(
70+
final boolean interpretUnits,
71+
final boolean outputDebugInfo,
72+
final HashSet<String> reservedNameWhitelist) {
6873
_interpretUnits = interpretUnits;
6974
_outputDebugInfo = outputDebugInfo;
75+
_reservedNameWhitelist = reservedNameWhitelist;
7076
}
7177

7278
/*
@@ -128,7 +134,7 @@ public List<Record> parse(final HttpRequest data) throws ParsingException {
128134
}
129135
final ParseResult result = parseNameAndUnit(nameOpt.orElse("").trim());
130136
// We don't currently support aggregate metrics from prometheus
131-
if (result.getAggregationKey().isPresent()) {
137+
if (result.getAggregationKey().isPresent() && !_reservedNameWhitelist.contains(result.getName())) {
132138
continue;
133139
}
134140
final String metricName = result.getName();
@@ -204,6 +210,7 @@ private Quantity createQuantity(final Types.Sample sample, final Optional<Unit>
204210
private final boolean _interpretUnits;
205211
private final AtomicInteger _outputFileNumber = new AtomicInteger(0);
206212
private final boolean _outputDebugInfo;
213+
private final HashSet<String> _reservedNameWhitelist;
207214

208215
private static final ImmutableMap<String, Unit> UNIT_MAP = ImmutableMap.of(
209216
"seconds", Unit.SECOND,

src/test/java/com/arpnetworking/metrics/mad/parsers/PrometheusToRecordParserTest.java

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
import com.arpnetworking.metrics.mad.model.Unit;
2727
import com.google.common.collect.ImmutableMap;
2828
import com.google.common.collect.ImmutableMultimap;
29+
import com.google.common.collect.Sets;
2930
import com.google.common.io.Resources;
3031
import org.junit.Assert;
3132
import org.junit.Ignore;
@@ -35,6 +36,7 @@
3536
import java.time.Instant;
3637
import java.time.ZoneOffset;
3738
import java.time.ZonedDateTime;
39+
import java.util.HashSet;
3840
import java.util.List;
3941
import java.util.Optional;
4042
import javax.annotation.Nullable;
@@ -166,6 +168,15 @@ public void testLive1() throws ParsingException, IOException {
166168
Assert.assertEquals(294, records.size());
167169
}
168170

171+
@Test
172+
public void testLive1Whitelist() throws ParsingException, IOException {
173+
final HashSet<String> whitelist = Sets.newHashSet("container_network_transmit_packets_total");
174+
final PrometheusToRecordParser parser = new PrometheusToRecordParser(true, false, whitelist);
175+
final List<Record> records = parseRecords("PrometheusParserTest/testLivePrometheus1", parser);
176+
177+
Assert.assertEquals(500, records.size());
178+
}
179+
169180
private static void testUnitParsing(final String prometheusUnit, final Unit expected) {
170181
assertUnitNewName(prometheusUnit, expected, null);
171182
assertUnitNewName("foo_" + prometheusUnit, expected, null);
@@ -204,9 +215,9 @@ private static List<Record> parseRecords(
204215
}
205216

206217
private static PrometheusToRecordParser createParser() {
207-
return new PrometheusToRecordParser(true, false);
218+
return new PrometheusToRecordParser(true, false, Sets.newHashSet());
208219
}
209220
private static PrometheusToRecordParser createParserWithoutInterpreter() {
210-
return new PrometheusToRecordParser(false, false);
221+
return new PrometheusToRecordParser(false, false, Sets.newHashSet());
211222
}
212223
}

0 commit comments

Comments
 (0)