-
Notifications
You must be signed in to change notification settings - Fork 149
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Initial integration with the Metrics plugin
- Loading branch information
1 parent
c047ef9
commit 8a6197b
Showing
10 changed files
with
169 additions
and
7 deletions.
There are no files selected for viewing
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
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
58 changes: 58 additions & 0 deletions
58
src/main/java/com/sonyericsson/jenkins/plugins/bfa/MetricsManager.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,58 @@ | ||
package com.sonyericsson.jenkins.plugins.bfa; | ||
|
||
import com.codahale.metrics.MetricRegistry; | ||
import com.sonyericsson.jenkins.plugins.bfa.model.IFailureCauseMetricData; | ||
import jenkins.metrics.api.Metrics; | ||
|
||
import java.util.HashSet; | ||
import java.util.List; | ||
import java.util.Set; | ||
import java.util.SortedSet; | ||
|
||
public final class MetricsManager { | ||
static final String CAUSEPREFIX = "jenkins_bfa.cause."; | ||
static final String CATEGORYPREFIX = "jenkins_bfa.category."; | ||
|
||
private MetricsManager() { | ||
} | ||
|
||
private static Set<String> getMetricNames(IFailureCauseMetricData cause) { | ||
Set<String> metrics = new HashSet<String>(); | ||
metrics.add(CAUSEPREFIX + cause.getName()); | ||
List<String> categoriesForCause = cause.getCategories(); | ||
if (categoriesForCause != null) { | ||
for (String string : categoriesForCause) { | ||
metrics.add(CATEGORYPREFIX + string); | ||
} | ||
} | ||
return metrics; | ||
} | ||
|
||
/** | ||
* Add metrics into the MetricRegistry from the Metrics plugin. | ||
* | ||
* @param cause The Cause to add metrics for | ||
*/ | ||
public static void addMetric(IFailureCauseMetricData cause) { | ||
MetricRegistry metricRegistry = Metrics.metricRegistry(); | ||
SortedSet<String> existingMetrics = metricRegistry.getNames(); | ||
Set<String> metrics = getMetricNames(cause); | ||
for (String metric : metrics) { | ||
if (!existingMetrics.contains(metric)) { | ||
metricRegistry.counter(metric); | ||
} | ||
} | ||
} | ||
|
||
/** | ||
* Increment caounters for the metric and its categories. | ||
* @param cause The cause to increment counters for | ||
*/ | ||
public static void incCounters(IFailureCauseMetricData cause) { | ||
MetricRegistry metricRegistry = Metrics.metricRegistry(); | ||
Set<String> metrics = getMetricNames(cause); | ||
for (String metric : metrics) { | ||
metricRegistry.counter(metric).inc(); | ||
} | ||
} | ||
} |
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
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 |
---|---|---|
|
@@ -69,7 +69,7 @@ | |
* @author Tomas Westling <[email protected]> | ||
*/ | ||
@JsonIgnoreProperties(ignoreUnknown = true) | ||
public class FailureCause implements Serializable, Action, Describable<FailureCause> { | ||
public class FailureCause implements Serializable, Action, Describable<FailureCause>, IFailureCauseMetricData { | ||
private static final Logger logger = Logger.getLogger(FailureCause.class.getName()); | ||
private String id; | ||
private String name; | ||
|
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 |
---|---|---|
|
@@ -42,7 +42,7 @@ | |
* @author Tomas Westling <[email protected]> | ||
*/ | ||
@ExportedBean | ||
public class FoundFailureCause { | ||
public class FoundFailureCause implements IFailureCauseMetricData { | ||
private static final Logger logger = Logger.getLogger(FoundFailureCause.class.getName()); | ||
|
||
private final String id; | ||
|
17 changes: 17 additions & 0 deletions
17
src/main/java/com/sonyericsson/jenkins/plugins/bfa/model/IFailureCauseMetricData.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,17 @@ | ||
package com.sonyericsson.jenkins.plugins.bfa.model; | ||
|
||
import java.util.List; | ||
|
||
public interface IFailureCauseMetricData { | ||
/** | ||
* Getter for the name. | ||
* @return the name | ||
*/ | ||
String getName(); | ||
|
||
/** | ||
* Getter for the categories. | ||
* @return the categories | ||
*/ | ||
List<String> getCategories(); | ||
} |
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 |
---|---|---|
|
@@ -24,15 +24,19 @@ | |
|
||
package com.sonyericsson.jenkins.plugins.bfa.db; | ||
|
||
import com.codahale.metrics.MetricRegistry; | ||
import com.sonyericsson.jenkins.plugins.bfa.PluginImpl; | ||
import com.sonyericsson.jenkins.plugins.bfa.model.FailureCause; | ||
import com.sonyericsson.jenkins.plugins.bfa.model.indication.BuildLogIndication; | ||
import com.sonyericsson.jenkins.plugins.bfa.model.FailureCauseModification; | ||
import com.sonyericsson.jenkins.plugins.bfa.model.indication.Indication; | ||
import hudson.util.CopyOnWriteList; | ||
import jenkins.metrics.api.Metrics; | ||
import jenkins.model.Jenkins; | ||
import org.junit.Before; | ||
import org.junit.Test; | ||
import org.junit.runner.RunWith; | ||
import org.mockito.Mock; | ||
import org.powermock.api.mockito.PowerMockito; | ||
import org.powermock.core.classloader.annotations.PrepareForTest; | ||
import org.powermock.modules.junit4.PowerMockRunner; | ||
|
@@ -57,13 +61,20 @@ | |
* @author Robert Sandell <[email protected]> | ||
*/ | ||
@RunWith(PowerMockRunner.class) | ||
@PrepareForTest(PluginImpl.class) | ||
@PrepareForTest({PluginImpl.class, Jenkins.class, Metrics.class, MetricRegistry.class}) | ||
public class LocalFileKnowledgeBaseTest { | ||
|
||
private CopyOnWriteList<FailureCause> oldCauses; | ||
private FailureCause olle; | ||
private FailureCause existingCause; | ||
|
||
@Mock | ||
private Jenkins jenkins; | ||
@Mock | ||
private Metrics metricsPlugin; | ||
@Mock | ||
private MetricRegistry metricRegistry; | ||
|
||
/** | ||
* Some usable test data for most tests. | ||
*/ | ||
|
@@ -80,6 +91,12 @@ public void setUp() { | |
PluginImpl mock = PowerMockito.mock(PluginImpl.class); | ||
PowerMockito.mockStatic(PluginImpl.class); | ||
PowerMockito.when(PluginImpl.getInstance()).thenReturn(mock); | ||
|
||
PowerMockito.mockStatic(Jenkins.class); | ||
PowerMockito.mockStatic(Metrics.class); | ||
PowerMockito.when(Jenkins.getInstance()).thenReturn(jenkins); | ||
PowerMockito.when(jenkins.getPlugin(Metrics.class)).thenReturn(metricsPlugin); | ||
PowerMockito.when(metricsPlugin.metricRegistry()).thenReturn(metricRegistry); | ||
} | ||
|
||
/** | ||
|
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 |
---|---|---|
|
@@ -24,6 +24,7 @@ | |
|
||
package com.sonyericsson.jenkins.plugins.bfa.model; | ||
|
||
import com.codahale.metrics.MetricRegistry; | ||
import com.sonyericsson.jenkins.plugins.bfa.CauseManagement; | ||
import com.sonyericsson.jenkins.plugins.bfa.PluginImpl; | ||
import com.sonyericsson.jenkins.plugins.bfa.db.KnowledgeBase; | ||
|
@@ -33,6 +34,7 @@ | |
import hudson.model.AutoCompletionCandidates; | ||
import hudson.model.Failure; | ||
import hudson.util.FormValidation; | ||
import jenkins.metrics.api.Metrics; | ||
import jenkins.model.Jenkins; | ||
import net.sf.json.JSONObject; | ||
import org.junit.Before; | ||
|
@@ -41,6 +43,7 @@ | |
import org.kohsuke.stapler.StaplerRequest; | ||
import org.kohsuke.stapler.StaplerResponse; | ||
import org.mockito.Matchers; | ||
import org.mockito.Mock; | ||
import org.powermock.api.mockito.PowerMockito; | ||
import org.powermock.core.classloader.annotations.PrepareForTest; | ||
import org.powermock.modules.junit4.PowerMockRunner; | ||
|
@@ -69,13 +72,20 @@ | |
* @author Robert Sandell <[email protected]> | ||
*/ | ||
@RunWith(PowerMockRunner.class) | ||
@PrepareForTest({Jenkins.class, PluginImpl.class }) | ||
@PrepareForTest({Jenkins.class, PluginImpl.class, Metrics.class, MetricRegistry.class }) | ||
public class FailureCauseTest { | ||
|
||
private PluginImpl pluginMock; | ||
private KnowledgeBase baseMock; | ||
private FailureCause.FailureCauseDescriptor descriptor; | ||
|
||
@Mock | ||
private Jenkins jenkinsMock; | ||
@Mock | ||
private Metrics metricsPlugin; | ||
@Mock | ||
private MetricRegistry metricRegistry; | ||
|
||
/** | ||
* Runs before every test. | ||
* Mocks {@link com.sonyericsson.jenkins.plugins.bfa.PluginImpl#getInstance()} to avoid NPE's | ||
|
@@ -87,14 +97,17 @@ public void setUp() { | |
mockStatic(PluginImpl.class); | ||
when(PluginImpl.getInstance()).thenReturn(pluginMock); | ||
|
||
Jenkins jenkinsMock = mock(Jenkins.class); | ||
mockStatic(Jenkins.class); | ||
when(Jenkins.getInstance()).thenReturn(jenkinsMock); | ||
doCallRealMethod().when(Jenkins.class); | ||
Jenkins.checkGoodName(any()); | ||
|
||
descriptor = new FailureCause.FailureCauseDescriptor(); | ||
when(jenkinsMock.getDescriptorByType(FailureCause.FailureCauseDescriptor.class)).thenReturn(descriptor); | ||
|
||
PowerMockito.mockStatic(Metrics.class); | ||
PowerMockito.when(jenkinsMock.getPlugin(Metrics.class)).thenReturn(metricsPlugin); | ||
PowerMockito.when(metricsPlugin.metricRegistry()).thenReturn(metricRegistry); | ||
} | ||
|
||
/** | ||
|
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 |
---|---|---|
|
@@ -23,6 +23,7 @@ | |
*/ | ||
package com.sonyericsson.jenkins.plugins.bfa.sod; | ||
|
||
import com.codahale.metrics.MetricRegistry; | ||
import com.google.common.collect.Lists; | ||
import com.sonyericsson.jenkins.plugins.bfa.PluginImpl; | ||
import com.sonyericsson.jenkins.plugins.bfa.model.FoundFailureCause; | ||
|
@@ -43,10 +44,13 @@ | |
import java.util.Collections; | ||
import java.util.LinkedList; | ||
import java.util.List; | ||
|
||
import jenkins.metrics.api.Metrics; | ||
import jenkins.model.Jenkins; | ||
import org.junit.Before; | ||
import org.junit.Test; | ||
import org.junit.runner.RunWith; | ||
import org.mockito.Mock; | ||
import org.powermock.api.mockito.PowerMockito; | ||
import org.powermock.core.classloader.annotations.PrepareForTest; | ||
import org.powermock.modules.junit4.PowerMockRunner; | ||
|
@@ -64,12 +68,26 @@ | |
* @author [email protected]> | ||
*/ | ||
@RunWith(PowerMockRunner.class) | ||
@PrepareForTest({Jenkins.class, PluginImpl.class, ScanOnDemandQueue.class, ScanOnDemandTask.class }) | ||
@PrepareForTest({ | ||
Jenkins.class, | ||
PluginImpl.class, | ||
ScanOnDemandQueue.class, | ||
ScanOnDemandTask.class, | ||
Metrics.class, | ||
MetricRegistry.class | ||
}) | ||
public class ScanOnDemandTaskTest { | ||
|
||
private AbstractProject mockproject; | ||
private PluginImpl pluginMock; | ||
|
||
@Mock | ||
private Jenkins jenkins; | ||
@Mock | ||
private Metrics metricsPlugin; | ||
@Mock | ||
private MetricRegistry metricRegistry; | ||
|
||
/** | ||
* Runs before every test. | ||
* Mocks {@link com.sonyericsson.jenkins.plugins.bfa.PluginImpl#getInstance()} to avoid NPE's | ||
|
@@ -81,6 +99,12 @@ public void setUp() { | |
mockStatic(PluginImpl.class); | ||
when(PluginImpl.getInstance()).thenReturn(pluginMock); | ||
when(PluginImpl.needToAnalyze(Result.FAILURE)).thenReturn(true); | ||
|
||
PowerMockito.mockStatic(Jenkins.class); | ||
PowerMockito.mockStatic(Metrics.class); | ||
PowerMockito.when(Jenkins.getInstance()).thenReturn(jenkins); | ||
PowerMockito.when(jenkins.getPlugin(Metrics.class)).thenReturn(metricsPlugin); | ||
PowerMockito.when(metricsPlugin.metricRegistry()).thenReturn(metricRegistry); | ||
} | ||
|
||
/** | ||
|