From f71fef6defcd8cb39031a6036d032e1e42623be7 Mon Sep 17 00:00:00 2001 From: sebawo Date: Tue, 26 Mar 2019 14:29:55 +0100 Subject: [PATCH 1/5] Add job_branch and job_domain fields to jenkins_build_data measurement --- pom.xml | 2 +- .../generators/JenkinsBasePointGenerator.java | 17 ++++++++++++++++- 2 files changed, 17 insertions(+), 2 deletions(-) diff --git a/pom.xml b/pom.xml index ecffd7f..0a02c95 100644 --- a/pom.xml +++ b/pom.xml @@ -10,7 +10,7 @@ com.getbase.jenkins.plugins build-history-metrics - 1.1.1 + 1.1.2 Build history metrics plugin Jenkins plugin to publish build metrics to external storage hpi diff --git a/src/main/java/com/getbase/jenkins/plugins/metrics/history/influxdb/generators/JenkinsBasePointGenerator.java b/src/main/java/com/getbase/jenkins/plugins/metrics/history/influxdb/generators/JenkinsBasePointGenerator.java index 48d79ef..034ba12 100644 --- a/src/main/java/com/getbase/jenkins/plugins/metrics/history/influxdb/generators/JenkinsBasePointGenerator.java +++ b/src/main/java/com/getbase/jenkins/plugins/metrics/history/influxdb/generators/JenkinsBasePointGenerator.java @@ -3,6 +3,8 @@ import com.synopsys.arc.jenkins.plugins.ownership.jobs.JobOwnerJobAction; import hudson.model.Result; import hudson.model.Run; +import hudson.model.ParametersAction; +import hudson.model.ParameterValue; import jenkins.metrics.impl.TimeInQueueAction; import org.influxdb.dto.Point; @@ -21,6 +23,8 @@ public class JenkinsBasePointGenerator implements PointGenerator { private static final String JOB_SCORE = "job_score"; private static final String BUILD_URL = "build_url"; private static final String JOB_URL = "job_url"; + private static final String JOB_BRANCH = "job_branch"; + private static final String JOB_DOMAIN = "job_domain"; private final Run build; @@ -35,6 +39,10 @@ public Point[] generate() { long dt = currTime - startTime; long duration = build.getDuration() == 0 ? dt : build.getDuration(); + ParametersAction params = build.getAction(ParametersAction.class); + ParameterValue branch = params.getParameter("branch"); + ParameterValue domain = params.getParameter("domain"); + TimeInQueueAction action = build.getAction(TimeInQueueAction.class); String owner = build.getParent().getAction(JobOwnerJobAction.class).getOwnership().getPrimaryOwnerEmail(); int score = build.getParent().getBuildHealth().getScore(); @@ -63,7 +71,14 @@ public Point[] generate() { .addField(TOTAL_DURATION, duration + action.getQueuingDurationMillis()) .addField(BUILD_STATUS_MESSAGE, build.getBuildStatusSummary().message); - return new Point[]{point.build()}; + if (branch != null) { + point.addField(JOB_BRANCH, branch.getValue().toString()); + } + if (domain != null) { + point.addField(JOB_DOMAIN, domain.getValue().toString()); + } + + return new Point[] {point.build()}; } From 30f80a54efeee4a0c9b177a8b2d7c7c57004dc41 Mon Sep 17 00:00:00 2001 From: sebawo Date: Tue, 2 Apr 2019 10:53:25 +0200 Subject: [PATCH 2/5] Add all job parameters values to influx db --- .../generators/JenkinsBasePointGenerator.java | 42 ++++++++++++------- 1 file changed, 28 insertions(+), 14 deletions(-) diff --git a/src/main/java/com/getbase/jenkins/plugins/metrics/history/influxdb/generators/JenkinsBasePointGenerator.java b/src/main/java/com/getbase/jenkins/plugins/metrics/history/influxdb/generators/JenkinsBasePointGenerator.java index 034ba12..0c54d4e 100644 --- a/src/main/java/com/getbase/jenkins/plugins/metrics/history/influxdb/generators/JenkinsBasePointGenerator.java +++ b/src/main/java/com/getbase/jenkins/plugins/metrics/history/influxdb/generators/JenkinsBasePointGenerator.java @@ -1,10 +1,14 @@ package com.getbase.jenkins.plugins.metrics.history.influxdb.generators; +import java.util.HashMap; +import java.util.List; +import java.util.Map; + import com.synopsys.arc.jenkins.plugins.ownership.jobs.JobOwnerJobAction; +import hudson.model.ParameterValue; +import hudson.model.ParametersAction; import hudson.model.Result; import hudson.model.Run; -import hudson.model.ParametersAction; -import hudson.model.ParameterValue; import jenkins.metrics.impl.TimeInQueueAction; import org.influxdb.dto.Point; @@ -32,6 +36,25 @@ public JenkinsBasePointGenerator(Run build) { this.build = build; } + public static Map getBuildParameters(Run build) { + List actions = build.getActions(ParametersAction.class); + if (actions != null) { + Map parametersMap = new HashMap<>(); + for (ParametersAction action : actions) { + List parameters = action.getParameters(); + if (parameters != null) { + for (ParameterValue parameter : parameters) { + String name = parameter.getName(); + Object value = parameter.getValue(); + parametersMap.put(name, value); + } + } + } + return parametersMap; + } + return null; + } + public Point[] generate() { // Build is not finished when running with pipelines. Duration must be calculated manually long startTime = build.getTimeInMillis(); @@ -39,10 +62,6 @@ public Point[] generate() { long dt = currTime - startTime; long duration = build.getDuration() == 0 ? dt : build.getDuration(); - ParametersAction params = build.getAction(ParametersAction.class); - ParameterValue branch = params.getParameter("branch"); - ParameterValue domain = params.getParameter("domain"); - TimeInQueueAction action = build.getAction(TimeInQueueAction.class); String owner = build.getParent().getAction(JobOwnerJobAction.class).getOwnership().getPrimaryOwnerEmail(); int score = build.getParent().getBuildHealth().getScore(); @@ -51,6 +70,7 @@ public Point[] generate() { final Integer resultInt = resultStr.equals("SUCCESS") ? 1 : 0; final String jobAbsoluteURL = build.getParent().getAbsoluteUrl(); final String buildAbsoluteURL = jobAbsoluteURL + build.getNumber(); + final Map fields = getBuildParameters(build); Point.Builder point = Point .measurement(MEASUREMENT_NAME) @@ -69,14 +89,8 @@ public Point[] generate() { .addField(BUILD_DURATION, duration) .addField(QUEUING_DURATION, action.getQueuingDurationMillis()) .addField(TOTAL_DURATION, duration + action.getQueuingDurationMillis()) - .addField(BUILD_STATUS_MESSAGE, build.getBuildStatusSummary().message); - - if (branch != null) { - point.addField(JOB_BRANCH, branch.getValue().toString()); - } - if (domain != null) { - point.addField(JOB_DOMAIN, domain.getValue().toString()); - } + .addField(BUILD_STATUS_MESSAGE, build.getBuildStatusSummary().message) + .fields(fields); return new Point[] {point.build()}; } From dc834d231ebf4788c7948a526079d43b996a02ab Mon Sep 17 00:00:00 2001 From: sebawo Date: Tue, 2 Apr 2019 12:16:46 +0200 Subject: [PATCH 3/5] Remove unused fields --- .../history/influxdb/generators/JenkinsBasePointGenerator.java | 2 -- 1 file changed, 2 deletions(-) diff --git a/src/main/java/com/getbase/jenkins/plugins/metrics/history/influxdb/generators/JenkinsBasePointGenerator.java b/src/main/java/com/getbase/jenkins/plugins/metrics/history/influxdb/generators/JenkinsBasePointGenerator.java index 0c54d4e..46a9880 100644 --- a/src/main/java/com/getbase/jenkins/plugins/metrics/history/influxdb/generators/JenkinsBasePointGenerator.java +++ b/src/main/java/com/getbase/jenkins/plugins/metrics/history/influxdb/generators/JenkinsBasePointGenerator.java @@ -27,8 +27,6 @@ public class JenkinsBasePointGenerator implements PointGenerator { private static final String JOB_SCORE = "job_score"; private static final String BUILD_URL = "build_url"; private static final String JOB_URL = "job_url"; - private static final String JOB_BRANCH = "job_branch"; - private static final String JOB_DOMAIN = "job_domain"; private final Run build; From 3b927e5bcb4f2011a2174debe1fb152cb9c51768 Mon Sep 17 00:00:00 2001 From: sebawo Date: Tue, 2 Apr 2019 12:35:23 +0200 Subject: [PATCH 4/5] Add meaningful name to params Map --- .../influxdb/generators/JenkinsBasePointGenerator.java | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/main/java/com/getbase/jenkins/plugins/metrics/history/influxdb/generators/JenkinsBasePointGenerator.java b/src/main/java/com/getbase/jenkins/plugins/metrics/history/influxdb/generators/JenkinsBasePointGenerator.java index 46a9880..b8186be 100644 --- a/src/main/java/com/getbase/jenkins/plugins/metrics/history/influxdb/generators/JenkinsBasePointGenerator.java +++ b/src/main/java/com/getbase/jenkins/plugins/metrics/history/influxdb/generators/JenkinsBasePointGenerator.java @@ -68,7 +68,7 @@ public Point[] generate() { final Integer resultInt = resultStr.equals("SUCCESS") ? 1 : 0; final String jobAbsoluteURL = build.getParent().getAbsoluteUrl(); final String buildAbsoluteURL = jobAbsoluteURL + build.getNumber(); - final Map fields = getBuildParameters(build); + final Map buildParameters = getBuildParameters(build); Point.Builder point = Point .measurement(MEASUREMENT_NAME) @@ -88,7 +88,7 @@ public Point[] generate() { .addField(QUEUING_DURATION, action.getQueuingDurationMillis()) .addField(TOTAL_DURATION, duration + action.getQueuingDurationMillis()) .addField(BUILD_STATUS_MESSAGE, build.getBuildStatusSummary().message) - .fields(fields); + .fields(buildParameters); return new Point[] {point.build()}; } From c17412bd23ab951e8a6746b5e7c839f22029d8b3 Mon Sep 17 00:00:00 2001 From: sebawo Date: Tue, 2 Apr 2019 13:02:37 +0200 Subject: [PATCH 5/5] Check whether build message is not null before adding it to point builder --- .../influxdb/generators/JenkinsBasePointGenerator.java | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/src/main/java/com/getbase/jenkins/plugins/metrics/history/influxdb/generators/JenkinsBasePointGenerator.java b/src/main/java/com/getbase/jenkins/plugins/metrics/history/influxdb/generators/JenkinsBasePointGenerator.java index b8186be..53c780b 100644 --- a/src/main/java/com/getbase/jenkins/plugins/metrics/history/influxdb/generators/JenkinsBasePointGenerator.java +++ b/src/main/java/com/getbase/jenkins/plugins/metrics/history/influxdb/generators/JenkinsBasePointGenerator.java @@ -34,7 +34,7 @@ public JenkinsBasePointGenerator(Run build) { this.build = build; } - public static Map getBuildParameters(Run build) { + public Map getBuildParameters(Run build) { List actions = build.getActions(ParametersAction.class); if (actions != null) { Map parametersMap = new HashMap<>(); @@ -68,6 +68,7 @@ public Point[] generate() { final Integer resultInt = resultStr.equals("SUCCESS") ? 1 : 0; final String jobAbsoluteURL = build.getParent().getAbsoluteUrl(); final String buildAbsoluteURL = jobAbsoluteURL + build.getNumber(); + final Run.Summary buildSummary = build.getBuildStatusSummary(); final Map buildParameters = getBuildParameters(build); Point.Builder point = Point @@ -87,11 +88,11 @@ public Point[] generate() { .addField(BUILD_DURATION, duration) .addField(QUEUING_DURATION, action.getQueuingDurationMillis()) .addField(TOTAL_DURATION, duration + action.getQueuingDurationMillis()) - .addField(BUILD_STATUS_MESSAGE, build.getBuildStatusSummary().message) .fields(buildParameters); + if (buildSummary != null) { + point.addField(BUILD_STATUS_MESSAGE, buildSummary.message); + } return new Point[] {point.build()}; } - - }