From 08c5dd48c832db1adb710a184caa15b0f4bc6d4e Mon Sep 17 00:00:00 2001 From: Aidan Leuck <48965785+aidanleuck@users.noreply.github.com> Date: Fri, 12 Jan 2024 01:27:59 -0700 Subject: [PATCH] Fix namespace issue for build counters (#616) --- .../collectors/builds/CounterManager.java | 16 +++++++++++++- .../collectors/builds/CounterManagerTest.java | 21 +++++++++++++++++++ 2 files changed, 36 insertions(+), 1 deletion(-) diff --git a/src/main/java/org/jenkinsci/plugins/prometheus/collectors/builds/CounterManager.java b/src/main/java/org/jenkinsci/plugins/prometheus/collectors/builds/CounterManager.java index f81133b4c..6dddf8101 100644 --- a/src/main/java/org/jenkinsci/plugins/prometheus/collectors/builds/CounterManager.java +++ b/src/main/java/org/jenkinsci/plugins/prometheus/collectors/builds/CounterManager.java @@ -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; @@ -80,6 +82,9 @@ private static class CounterEntry { // Prefix of the counter. private String prefix; + // namespace of the counter + private String namespace; + /* * Creates new counter entry */ @@ -87,6 +92,7 @@ public CounterEntry(CollectorType type, String[] labels, String prefix) { this.labels = labels; this.type = type; this.prefix = prefix; + this.namespace = ConfigurationUtils.getNamespace(); } @Override @@ -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)){ + return false; + } + // Compare labels return Arrays.equals(labels, entry.labels); } @@ -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; + int result = 31 * (typeHash + Arrays.hashCode(labels) + prefixHash + namespaceHash); return result; } } diff --git a/src/test/java/org/jenkinsci/plugins/prometheus/collectors/builds/CounterManagerTest.java b/src/test/java/org/jenkinsci/plugins/prometheus/collectors/builds/CounterManagerTest.java index a35252789..f284fc4df 100644 --- a/src/test/java/org/jenkinsci/plugins/prometheus/collectors/builds/CounterManagerTest.java +++ b/src/test/java/org/jenkinsci/plugins/prometheus/collectors/builds/CounterManagerTest.java @@ -37,6 +37,27 @@ public void TestEquivalentEntryReturnsCounter() { } } + @Test + public void TestNamespaceChangeReturnsNewCounter() { + String[] labels = new String[] { "TestLabel" }; + + try (MockedStatic 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"};