-
Notifications
You must be signed in to change notification settings - Fork 145
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #141 from jamesrobson-secondmind/metrics-plugin-in…
…tegration Integration with the Metrics plugin
- Loading branch information
Showing
15 changed files
with
304 additions
and
11 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
# Metrics Integration | ||
|
||
This is a guide for the integration with the [Metrics plugin](https://plugins.jenkins.io/metrics/). | ||
|
||
## Metrics | ||
|
||
The integration provides counters for each individual cause and category that you create. These counters will reset to zero when jenkins is restarted. The format for the metrics created is `jenkins_bfa_category_<category name>` for each category and `jenkins_bfa_cause_<cause name>` for each cause. The category and cause names will be escaped by the metrics api to replace any spaces with underscores. | ||
|
||
## Exporting | ||
|
||
To export the BFA metrics you can use any plugin that integrates with the Metrics plugin. | ||
|
||
### Prometheus | ||
The [prometheus plugin](https://plugins.jenkins.io/prometheus/) | ||
To restructure the metrics into the form that prometheus expects you can add the following into your scrape config: | ||
|
||
```yaml | ||
metric_relabel_configs: | ||
- source_labels: [__name__] | ||
regex: 'jenkins_bfa_category_(.*)' | ||
target_label: 'category' | ||
- source_labels: [__name__] | ||
regex: 'jenkins_bfa_cause_(.*)' | ||
target_label: 'cause' | ||
- source_labels: [__name__] | ||
regex: 'jenkins_bfa_(.*)_(.*)' | ||
replacement: 'jenkins_bfa' | ||
target_label: __name__ | ||
``` | ||
|
||
This will provide a metric called `jenkins_bfa` with labels for the category and specific cause. |
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
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
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(); | ||
} |
82 changes: 82 additions & 0 deletions
82
src/test/java/com/sonyericsson/jenkins/plugins/bfa/MetricsManagerTest.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,82 @@ | ||
package com.sonyericsson.jenkins.plugins.bfa; | ||
|
||
import com.codahale.metrics.Counter; | ||
import com.codahale.metrics.MetricRegistry; | ||
|
||
import com.sonyericsson.jenkins.plugins.bfa.model.FailureCause; | ||
import com.sonyericsson.jenkins.plugins.bfa.model.indication.BuildLogIndication; | ||
import com.sonyericsson.jenkins.plugins.bfa.model.indication.Indication; | ||
import jenkins.metrics.api.Metrics; | ||
|
||
import org.junit.Before; | ||
import org.junit.runner.RunWith; | ||
import org.mockito.Mock; | ||
import org.mockito.Mockito; | ||
import org.powermock.api.mockito.PowerMockito; | ||
import org.powermock.core.classloader.annotations.PrepareForTest; | ||
import org.powermock.modules.junit4.PowerMockRunner; | ||
|
||
import java.util.Date; | ||
import java.util.LinkedList; | ||
import java.util.List; | ||
|
||
import static com.sonyericsson.jenkins.plugins.bfa.MetricsManager.addMetric; | ||
import static com.sonyericsson.jenkins.plugins.bfa.MetricsManager.incCounters; | ||
import static org.mockito.Mockito.times; | ||
import static org.mockito.Mockito.verify; | ||
|
||
|
||
/** | ||
* Tests for {@link MetricsManager}. | ||
*/ | ||
|
||
@RunWith(PowerMockRunner.class) | ||
@PrepareForTest({MetricRegistry.class}) | ||
public class MetricsManagerTest { | ||
@Mock | ||
private MetricRegistry metricRegistry; | ||
@Mock | ||
private Counter counter; | ||
|
||
private List<Indication> indications; | ||
private Indication indication; | ||
private FailureCause mockedCause; | ||
|
||
|
||
/** | ||
* Common stuff to set up for the tests. | ||
*/ | ||
@Before | ||
public void setUp() { | ||
indications = new LinkedList<Indication>(); | ||
indication = new BuildLogIndication("something"); | ||
indications.add(indication); | ||
mockedCause = new FailureCause("id", "myFailureCause", "description", "comment", new Date(), | ||
"category", indications, null); | ||
|
||
PowerMockito.mockStatic(Metrics.class); | ||
PowerMockito.when(Metrics.metricRegistry()).thenReturn(metricRegistry); | ||
PowerMockito.when(metricRegistry.counter(Mockito.anyString())).thenReturn(counter); | ||
} | ||
|
||
/** | ||
* Test that the case and category counters are created from a FailureCause. | ||
*/ | ||
public void testAddMetric() { | ||
addMetric(mockedCause); | ||
|
||
verify(metricRegistry, times(1)).counter("jenkins_bfa.cause.myFailureCause"); | ||
verify(metricRegistry, times(1)).counter("jenkins_bfa.category.category"); | ||
} | ||
|
||
/** | ||
* Test that the cause and category counters are incremented for a Failurecasue. | ||
*/ | ||
public void testIncCounters() { | ||
incCounters(mockedCause); | ||
|
||
verify(metricRegistry, times(1)).counter("jenkins_bfa.cause.myFailureCause").inc(); | ||
verify(metricRegistry, times(1)).counter("jenkins_bfa.category.category").inc(); | ||
} | ||
|
||
} |
Oops, something went wrong.