Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve integration with Metrics plugin #148

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
package com.sonyericsson.jenkins.plugins.bfa;

import static com.sonyericsson.jenkins.plugins.bfa.MetricsManager.incCounters;
import static com.sonyericsson.jenkins.plugins.bfa.MetricsManager.UNKNOWNCAUSE;

import com.sonyericsson.jenkins.plugins.bfa.model.FailureCause;
import com.sonyericsson.jenkins.plugins.bfa.model.FailureCauseBuildAction;
Expand Down Expand Up @@ -190,7 +191,6 @@ public static void scan(Run build, PrintStream scanLog) {
foundCauseList = foundCauseListToLog;
}

incCounters(foundCauseList, PluginImpl.getInstance().isMetricSquashingEnabled());

List<String> fallbackCategories = PluginImpl.getInstance().getFallbackCategories();

Expand All @@ -217,6 +217,16 @@ public static void scan(Run build, PrintStream scanLog) {
}
}


if (!foundCauseList.isEmpty()) {
incCounters(foundCauseList, PluginImpl.getInstance().isMetricSquashingEnabled());
} else {
incCounters(
new ArrayList<>(Collections.singletonList(UNKNOWNCAUSE)),
PluginImpl.getInstance().isMetricSquashingEnabled()
);
}

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

            if (!foundCauseList.isEmpty()) {
                incCounters(foundCauseList, PluginImpl.getInstance().isMetricSquashingEnabled());
            } else {
                incCounters(new ArrayList<>(Collections.singletonList(UNKNOWNCAUSE)),
                        PluginImpl.getInstance().isMetricSquashingEnabled());
            }

FailureCauseBuildAction buildAction = new FailureCauseBuildAction(foundCauseList);
buildAction.setBuild(build);
build.addAction(buildAction);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.sonyericsson.jenkins.plugins.bfa;

import com.codahale.metrics.MetricRegistry;
import com.sonyericsson.jenkins.plugins.bfa.model.FailureCause;
import com.sonyericsson.jenkins.plugins.bfa.model.IFailureCauseMetricData;
import jenkins.metrics.api.Metrics;

Expand All @@ -13,6 +14,9 @@ public final class MetricsManager {
static final String CAUSEPREFIX = "jenkins_bfa.cause.";
static final String CATEGORYPREFIX = "jenkins_bfa.category.";

/**A magic cause to represent builds that match no causes in the database. */
public static final FailureCause UNKNOWNCAUSE = new FailureCause("no matching cause", "");

private MetricsManager() {
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
package com.sonyericsson.jenkins.plugins.bfa.db;

import static com.sonyericsson.jenkins.plugins.bfa.MetricsManager.addMetric;
import static com.sonyericsson.jenkins.plugins.bfa.MetricsManager.UNKNOWNCAUSE;
import static hudson.Util.fixEmpty;

import java.io.IOException;
Expand Down Expand Up @@ -191,6 +192,7 @@ public void start() {
for (Map.Entry<String, FailureCause> entry : causes.entrySet()) {
addMetric(entry.getValue());
}
addMetric(UNKNOWNCAUSE);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
import static com.mongodb.client.model.Filters.exists;
import static com.mongodb.client.model.Filters.eq;
import static com.sonyericsson.jenkins.plugins.bfa.MetricsManager.addMetric;
import static com.sonyericsson.jenkins.plugins.bfa.MetricsManager.UNKNOWNCAUSE;

import com.mongodb.client.MongoClients;
import com.mongodb.client.MongoCursor;
Expand Down Expand Up @@ -215,6 +216,7 @@ public synchronized void start() {
for (FailureCause entry : getCauseNames()) {
addMetric(entry);
}
addMetric(UNKNOWNCAUSE);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@

package com.sonyericsson.jenkins.plugins.bfa;

import com.codahale.metrics.MetricRegistry;
import com.gargoylesoftware.htmlunit.html.DomElement;
import com.gargoylesoftware.htmlunit.html.HtmlElement;
import com.gargoylesoftware.htmlunit.html.HtmlPage;
Expand Down Expand Up @@ -51,6 +52,7 @@
import hudson.model.listeners.RunListener;
import hudson.model.queue.QueueTaskFuture;
import hudson.tasks.junit.JUnitResultArchiver;
import jenkins.metrics.api.Metrics;
import jenkins.model.Jenkins;
import org.junit.Rule;
import org.junit.Test;
Expand Down Expand Up @@ -148,6 +150,9 @@ public void testOneIndicationFound() throws Exception {

assertNotNull(error);
assertEquals("Error message not found: ", BUILD_LOG_FIRST_LINE, error.getTextContent().trim());

MetricRegistry metricRegistry = Metrics.metricRegistry();
assertEquals(1, metricRegistry.counter("jenkins_bfa.cause.Error").getCount());
}

/**
Expand Down Expand Up @@ -178,6 +183,12 @@ public void testOnlyOneGenericIndicationFound() throws Exception {
.getFoundFailureCauses();
assertTrue(findCauseInList(causeListFromAction, genericFailureCause));
assertFalse(findCauseInList(causeListFromAction, specificFailureCause));

MetricRegistry metricRegistry = Metrics.metricRegistry();
assertEquals(1, metricRegistry.counter("jenkins_bfa.cause.Generic Error").getCount());
assertEquals(1, metricRegistry.counter("jenkins_bfa.category.Generic").getCount());
assertEquals(0, metricRegistry.counter("jenkins_bfa.cause.Specific Error").getCount());
assertEquals(0, metricRegistry.counter("jenkins_bfa.category.Specific").getCount());
}

/**
Expand Down Expand Up @@ -208,6 +219,12 @@ public void testGenericFailureCauseIsDroppedForSpecificOne() throws Exception {
.getFoundFailureCauses();
assertFalse(findCauseInList(causeListFromAction, genericFailureCause));
assertTrue(findCauseInList(causeListFromAction, specificFailureCause));

MetricRegistry metricRegistry = Metrics.metricRegistry();
assertEquals(0, metricRegistry.counter("jenkins_bfa.cause.Generic Error").getCount());
assertEquals(0, metricRegistry.counter("jenkins_bfa.category.Generic").getCount());
assertEquals(1, metricRegistry.counter("jenkins_bfa.cause.Specific Error").getCount());
assertEquals(1, metricRegistry.counter("jenkins_bfa.category.Specific").getCount());
}

/**
Expand Down Expand Up @@ -249,6 +266,10 @@ public void testOneMultilineIndicationFound() throws Exception {
assertEquals("Error message not found: ",
new StringTokenizer(BUILD_LOG).nextToken("\n"),
error.getTextContent().trim());

MetricRegistry metricRegistry = Metrics.metricRegistry();
assertEquals(1, metricRegistry.counter("jenkins_bfa.cause.Error").getCount());
assertEquals(1, metricRegistry.counter("jenkins_bfa.category.category").getCount());
}

/**
Expand Down Expand Up @@ -314,6 +335,11 @@ public void testTwoIndicationsSameLine() throws Exception {
HtmlElement error = errorElements.get(0);
assertNotNull(error);
assertEquals("Error message not found: ", BUILD_LOG_FIRST_LINE, error.getTextContent().trim());

MetricRegistry metricRegistry = Metrics.metricRegistry();
assertEquals(1, metricRegistry.counter("jenkins_bfa.cause.Error").getCount());
assertEquals(1, metricRegistry.counter("jenkins_bfa.cause.Other cause").getCount());
assertEquals(2, metricRegistry.counter("jenkins_bfa.category.category").getCount());
}

/**
Expand Down