Skip to content

Commit

Permalink
review comments
Browse files Browse the repository at this point in the history
Signed-off-by: Adi Muraru <[email protected]>
  • Loading branch information
amuraru authored and fstab committed Mar 7, 2023
1 parent cdc5043 commit 613db0c
Showing 1 changed file with 11 additions and 20 deletions.
31 changes: 11 additions & 20 deletions collector/src/main/java/io/prometheus/jmx/JmxCollector.java
Original file line number Diff line number Diff line change
Expand Up @@ -340,18 +340,18 @@ private static boolean isLegalCharacter(char input) {
/**
* A sample is uniquely identified by its name, labelNames and labelValues
*/
static class SampleKey {
String name;
List<String> labelNames;
List<String> labelValues;
private static class SampleKey {
private final String name;
private final List<String> labelNames;
private final List<String> labelValues;

SampleKey(String name, List<String> labelNames, List<String> labelValues) {
private SampleKey(String name, List<String> labelNames, List<String> labelValues) {
this.name = name;
this.labelNames = labelNames;
this.labelValues = labelValues;
}

static SampleKey of(MetricFamilySamples.Sample sample) {
private static SampleKey of(MetricFamilySamples.Sample sample) {
return new SampleKey(sample.name, sample.labelNames, sample.labelValues);
}

Expand All @@ -363,8 +363,8 @@ public boolean equals(Object o) {
SampleKey sampleKey = (SampleKey) o;

if (name != null ? !name.equals(sampleKey.name) : sampleKey.name != null) return false;
if (labelNames != null ? !labelNames.equals(sampleKey.labelNames) : sampleKey.labelNames != null) return false;
return labelValues != null ? labelValues.equals(sampleKey.labelValues) : sampleKey.labelValues == null;
if (labelValues != null ? !labelValues.equals(sampleKey.labelValues) : sampleKey.labelValues != null) return false;
return labelNames != null ? labelNames.equals(sampleKey.labelNames) : sampleKey.labelNames == null;
}

@Override
Expand All @@ -380,7 +380,7 @@ public int hashCode() {
static class Receiver implements JmxScraper.MBeanReceiver {
Map<String, MetricFamilySamples> metricFamilySamplesMap =
new HashMap<String, MetricFamilySamples>();
Set<SampleKey> uniqueSampleKeys = new HashSet<SampleKey>();
Set<SampleKey> sampleKeys = new HashSet<SampleKey>();

Config config;
MatchedRulesCache.StalenessTracker stalenessTracker;
Expand All @@ -406,7 +406,7 @@ void addSample(MetricFamilySamples.Sample sample, Type type, String help) {
metricFamilySamplesMap.put(sample.name, mfs);
}
SampleKey sampleKey = SampleKey.of(sample);
boolean exists = uniqueSampleKeys.contains(sampleKey);
boolean exists = sampleKeys.contains(sampleKey);
if (exists) {
if (LOGGER.isLoggable(Level.FINE)) {
String labels = "{";
Expand All @@ -418,17 +418,8 @@ void addSample(MetricFamilySamples.Sample sample, Type type, String help) {
}
} else {
mfs.samples.add(sample);
uniqueSampleKeys.add(sampleKey);
}
}

private MetricFamilySamples.Sample findExisting(MetricFamilySamples.Sample sample, MetricFamilySamples mfs) {
for (MetricFamilySamples.Sample existing : mfs.samples) {
if (existing.name.equals(sample.name) && existing.labelValues.equals(sample.labelValues) && existing.labelNames.equals(sample.labelNames)) {
return existing;
}
sampleKeys.add(sampleKey);
}
return null;
}

// Add the matched rule to the cached rules and tag it as not stale
Expand Down

0 comments on commit 613db0c

Please sign in to comment.