Skip to content

Commit

Permalink
Fix namespace issue for build counters (#616)
Browse files Browse the repository at this point in the history
  • Loading branch information
aidanleuck authored Jan 12, 2024
1 parent fe461d2 commit 08c5dd4
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@

import org.jenkinsci.plugins.prometheus.collectors.CollectorType;
import org.jenkinsci.plugins.prometheus.collectors.MetricCollector;
import org.jenkinsci.plugins.prometheus.config.PrometheusConfiguration;
import org.jenkinsci.plugins.prometheus.util.ConfigurationUtils;

import hudson.model.Run;
import io.prometheus.client.Collector;
Expand Down Expand Up @@ -80,13 +82,17 @@ private static class CounterEntry {
// Prefix of the counter.
private String prefix;

// namespace of the counter
private String namespace;

/*
* Creates new counter entry
*/
public CounterEntry(CollectorType type, String[] labels, String prefix) {
this.labels = labels;
this.type = type;
this.prefix = prefix;
this.namespace = ConfigurationUtils.getNamespace();
}

@Override
Expand All @@ -98,14 +104,21 @@ public boolean equals(Object obj) {

CounterEntry entry = (CounterEntry) obj;

// Compare the prefix
if(this.prefix != null && !this.prefix.equals(entry.prefix)){
return false;
}

// Compare the entry Counter type
if(this.type != entry.type){
return false;
}

// Compare namespace values.
if(this.namespace != null && !this.namespace.equals(entry.namespace)){

Check warning on line 118 in src/main/java/org/jenkinsci/plugins/prometheus/collectors/builds/CounterManager.java

View check run for this annotation

ci.jenkins.io / Code Coverage

Partially covered line

Line 118 is only partially covered, 2 branches are missing
return false;

Check warning on line 119 in src/main/java/org/jenkinsci/plugins/prometheus/collectors/builds/CounterManager.java

View check run for this annotation

ci.jenkins.io / Code Coverage

Not covered line

Line 119 is not covered by tests
}

// Compare labels
return Arrays.equals(labels, entry.labels);
}
Expand All @@ -114,7 +127,8 @@ public boolean equals(Object obj) {
public int hashCode() {
int typeHash = type != null ? type.hashCode() : 0;
int prefixHash = prefix != null ? prefix.hashCode() : 0;
int result = 31 * (typeHash + Arrays.hashCode(labels) + prefixHash);
int namespaceHash = namespace != null ? namespace.hashCode() : 0;

Check warning on line 130 in src/main/java/org/jenkinsci/plugins/prometheus/collectors/builds/CounterManager.java

View check run for this annotation

ci.jenkins.io / Code Coverage

Partially covered line

Line 130 is only partially covered, one branch is missing
int result = 31 * (typeHash + Arrays.hashCode(labels) + prefixHash + namespaceHash);
return result;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,27 @@ public void TestEquivalentEntryReturnsCounter() {
}
}

@Test
public void TestNamespaceChangeReturnsNewCounter() {
String[] labels = new String[] { "TestLabel" };

try (MockedStatic<PrometheusConfiguration> configStatic = mockStatic(PrometheusConfiguration.class)) {
PrometheusConfiguration config = mock(PrometheusConfiguration.class);

// Use default namespace for one counter
when(config.getDefaultNamespace()).thenReturn(getNamespace());
configStatic.when(PrometheusConfiguration::get).thenReturn(config);
var retrievedCounter = manager.getCounter(CollectorType.BUILD_SUCCESSFUL_COUNTER, labels, null);

// Second counter returns modified namespace
when(config.getDefaultNamespace()).thenReturn("modified_namespace");
var retrievedCounter2 = manager.getCounter(CollectorType.BUILD_SUCCESSFUL_COUNTER, labels, null);

// Should be a value reference comparison. They should not be the same metric since the namespace has changed.
Assert.assertNotEquals(retrievedCounter, retrievedCounter2);
}
}

@Test
public void TestLabelChangeReturnsNewCounter(){
String[] label1 = new String[]{"labels"};
Expand Down

0 comments on commit 08c5dd4

Please sign in to comment.