Skip to content

Commit

Permalink
Code coverage will not show job full name. Provide a default value of…
Browse files Browse the repository at this point in the history
… -1 if a specific coverage is not available in the coverage report (#612)

* Returning job full name

* Coverage will return a -1 if a specific coverage is not available now to make it clear that nothing has been found.

* Adding line coverage metrics and a percentage gauge for all current coverage metrics
  • Loading branch information
Waschndolos authored Jan 9, 2024
1 parent f0e2864 commit d64bcde
Show file tree
Hide file tree
Showing 45 changed files with 1,081 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -63,20 +63,29 @@ private List<MetricFamilySamples> collectCoverageMetricForJob(Job<?,?> job) {
collectors.add(factory.createCoverageRunCollector(CollectorType.COVERAGE_CLASS_COVERED, new String[]{jobAttributeName}));
collectors.add(factory.createCoverageRunCollector(CollectorType.COVERAGE_CLASS_MISSED, new String[]{jobAttributeName}));
collectors.add(factory.createCoverageRunCollector(CollectorType.COVERAGE_CLASS_TOTAL, new String[]{jobAttributeName}));
collectors.add(factory.createCoverageRunCollector(CollectorType.COVERAGE_CLASS_PERCENT, new String[]{jobAttributeName}));

collectors.add(factory.createCoverageRunCollector(CollectorType.COVERAGE_BRANCH_COVERED, new String[]{jobAttributeName}));
collectors.add(factory.createCoverageRunCollector(CollectorType.COVERAGE_BRANCH_MISSED, new String[]{jobAttributeName}));
collectors.add(factory.createCoverageRunCollector(CollectorType.COVERAGE_BRANCH_TOTAL, new String[]{jobAttributeName}));
collectors.add(factory.createCoverageRunCollector(CollectorType.COVERAGE_BRANCH_PERCENT, new String[]{jobAttributeName}));

collectors.add(factory.createCoverageRunCollector(CollectorType.COVERAGE_INSTRUCTION_COVERED, new String[]{jobAttributeName}));
collectors.add(factory.createCoverageRunCollector(CollectorType.COVERAGE_INSTRUCTION_MISSED, new String[]{jobAttributeName}));
collectors.add(factory.createCoverageRunCollector(CollectorType.COVERAGE_INSTRUCTION_TOTAL, new String[]{jobAttributeName}));
collectors.add(factory.createCoverageRunCollector(CollectorType.COVERAGE_INSTRUCTION_PERCENT, new String[]{jobAttributeName}));

collectors.add(factory.createCoverageRunCollector(CollectorType.COVERAGE_FILE_COVERED, new String[]{jobAttributeName}));
collectors.add(factory.createCoverageRunCollector(CollectorType.COVERAGE_FILE_MISSED, new String[]{jobAttributeName}));
collectors.add(factory.createCoverageRunCollector(CollectorType.COVERAGE_FILE_TOTAL, new String[]{jobAttributeName}));
collectors.add(factory.createCoverageRunCollector(CollectorType.COVERAGE_FILE_PERCENT, new String[]{jobAttributeName}));

collectors.forEach(c -> c.calculateMetric(lastBuild, new String[]{job.getName()}));
collectors.add(factory.createCoverageRunCollector(CollectorType.COVERAGE_LINE_COVERED, new String[]{jobAttributeName}));
collectors.add(factory.createCoverageRunCollector(CollectorType.COVERAGE_LINE_MISSED, new String[]{jobAttributeName}));
collectors.add(factory.createCoverageRunCollector(CollectorType.COVERAGE_LINE_TOTAL, new String[]{jobAttributeName}));
collectors.add(factory.createCoverageRunCollector(CollectorType.COVERAGE_LINE_PERCENT, new String[]{jobAttributeName}));

collectors.forEach(c -> c.calculateMetric(lastBuild, new String[]{job.getFullName()}));

return collectors.stream()
.map(MetricCollector::collect)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,18 +45,27 @@ public enum CollectorType {
COVERAGE_CLASS_COVERED("coverage_class_covered"),
COVERAGE_CLASS_MISSED("coverage_class_missed"),
COVERAGE_CLASS_TOTAL("coverage_class_total"),
COVERAGE_CLASS_PERCENT("coverage_class_percent"),

COVERAGE_BRANCH_COVERED("coverage_branch_covered"),
COVERAGE_BRANCH_MISSED("coverage_branch_missed"),
COVERAGE_BRANCH_TOTAL("coverage_branch_total"),
COVERAGE_BRANCH_PERCENT("coverage_branch_percent"),

COVERAGE_INSTRUCTION_COVERED("coverage_instruction_covered"),
COVERAGE_INSTRUCTION_MISSED("coverage_instruction_missed"),
COVERAGE_INSTRUCTION_TOTAL("coverage_instruction_total"),
COVERAGE_INSTRUCTION_PERCENT("coverage_instruction_percent"),

COVERAGE_FILE_COVERED("coverage_file_covered"),
COVERAGE_FILE_MISSED("coverage_file_missed"),
COVERAGE_FILE_TOTAL("coverage_file_total"),
COVERAGE_FILE_PERCENT("coverage_file_percent"),

COVERAGE_LINE_COVERED("coverage_line_covered"),
COVERAGE_LINE_MISSED("coverage_line_missed"),
COVERAGE_LINE_TOTAL("coverage_line_total"),
COVERAGE_LINE_PERCENT("coverage_line_percent"),

JOB_LOG_UPDATED_GAUGE("job_log_updated");

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ public void calculateMetric(Run<?, ?> jenkinsObject, String[] labelValues) {

Optional<Coverage> optional = getCoverage(jenkinsObject, Metric.BRANCH, Baseline.PROJECT);
if (optional.isEmpty()) {
collector.labels(labelValues).set(-1);
return;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ public void calculateMetric(Run<?, ?> jenkinsObject, String[] labelValues) {

Optional<Coverage> optional = getCoverage(jenkinsObject, Metric.BRANCH, Baseline.PROJECT);
if (optional.isEmpty()) {
collector.labels(labelValues).set(-1);
return;
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
package org.jenkinsci.plugins.prometheus.collectors.coverage;

import edu.hm.hafner.coverage.Coverage;
import edu.hm.hafner.coverage.Metric;
import hudson.model.Run;
import io.jenkins.plugins.coverage.metrics.model.Baseline;
import io.prometheus.client.Gauge;
import io.prometheus.client.SimpleCollector;
import org.jenkinsci.plugins.prometheus.collectors.CollectorType;

import java.util.Optional;

public class CoverageBranchPercentGauge extends CoverageMetricsCollector<Run<?, ?>, Gauge> {

protected CoverageBranchPercentGauge(String[] labelNames, String namespace, String subsystem) {
super(labelNames, namespace, subsystem);
}

@Override
protected CollectorType getCollectorType() {
return CollectorType.COVERAGE_BRANCH_PERCENT;
}

@Override
protected String getHelpText() {
return "Returns the coverage of branches in percent";
}

@Override
protected SimpleCollector.Builder<?, Gauge> getCollectorBuilder() {
return Gauge.build();
}

@Override
public void calculateMetric(Run<?, ?> jenkinsObject, String[] labelValues) {

Optional<Coverage> optional = getCoverage(jenkinsObject, Metric.BRANCH, Baseline.PROJECT);
if (optional.isEmpty()) {
collector.labels(labelValues).set(-1);
return;
}

Coverage coverage = optional.get();
collector.labels(labelValues).set(calculatePercentage(coverage));
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ public void calculateMetric(Run<?, ?> jenkinsObject, String[] labelValues) {

Optional<Coverage> optional = getCoverage(jenkinsObject, Metric.BRANCH, Baseline.PROJECT);
if (optional.isEmpty()) {
collector.labels(labelValues).set(-1);
return;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ public void calculateMetric(Run<?, ?> jenkinsObject, String[] labelValues) {

Optional<Coverage> optional = getCoverage(jenkinsObject, Metric.CLASS, Baseline.PROJECT);
if (optional.isEmpty()) {
collector.labels(labelValues).set(-1);
return;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ public void calculateMetric(Run<?, ?> jenkinsObject, String[] labelValues) {

Optional<Coverage> optional = getCoverage(jenkinsObject, Metric.CLASS, Baseline.PROJECT);
if (optional.isEmpty()) {
collector.labels(labelValues).set(-1);
return;
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
package org.jenkinsci.plugins.prometheus.collectors.coverage;

import edu.hm.hafner.coverage.Coverage;
import edu.hm.hafner.coverage.Metric;
import hudson.model.Run;
import io.jenkins.plugins.coverage.metrics.model.Baseline;
import io.prometheus.client.Gauge;
import io.prometheus.client.SimpleCollector;
import org.jenkinsci.plugins.prometheus.collectors.CollectorType;

import java.util.Optional;

public class CoverageClassPercentGauge extends CoverageMetricsCollector<Run<?, ?>, Gauge> {

protected CoverageClassPercentGauge(String[] labelNames, String namespace, String subsystem) {
super(labelNames, namespace, subsystem);
}

@Override
protected CollectorType getCollectorType() {
return CollectorType.COVERAGE_CLASS_PERCENT;
}

@Override
protected String getHelpText() {
return "Returns the coverage of classes in percent";
}

@Override
protected SimpleCollector.Builder<?, Gauge> getCollectorBuilder() {
return Gauge.build();
}

@Override
public void calculateMetric(Run<?, ?> jenkinsObject, String[] labelValues) {

Optional<Coverage> optional = getCoverage(jenkinsObject, Metric.CLASS, Baseline.PROJECT);
if (optional.isEmpty()) {
collector.labels(labelValues).set(-1);
return;
}

Coverage coverage = optional.get();
collector.labels(labelValues).set(calculatePercentage(coverage));
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ public void calculateMetric(Run<?, ?> jenkinsObject, String[] labelValues) {

Optional<Coverage> optional = getCoverage(jenkinsObject, Metric.CLASS, Baseline.PROJECT);
if (optional.isEmpty()) {
collector.labels(labelValues).set(-1);
return;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,24 +17,45 @@ public class CoverageCollectorFactory extends BaseCollectorFactory {
return saveBuildCollector(new CoverageClassMissedGauge(labelNames, namespace, subsystem));
case COVERAGE_CLASS_TOTAL:
return saveBuildCollector(new CoverageClassTotalGauge(labelNames, namespace, subsystem));
case COVERAGE_CLASS_PERCENT:
return saveBuildCollector(new CoverageClassPercentGauge(labelNames, namespace, subsystem));

case COVERAGE_BRANCH_COVERED:
return saveBuildCollector(new CoverageBranchCoveredGauge(labelNames, namespace, subsystem));
case COVERAGE_BRANCH_MISSED:
return saveBuildCollector(new CoverageBranchMissedGauge(labelNames, namespace, subsystem));
case COVERAGE_BRANCH_TOTAL:
return saveBuildCollector(new CoverageBranchTotalGauge(labelNames, namespace, subsystem));
case COVERAGE_BRANCH_PERCENT:
return saveBuildCollector(new CoverageBranchPercentGauge(labelNames, namespace, subsystem));

case COVERAGE_INSTRUCTION_COVERED:
return saveBuildCollector(new CoverageInstructionCoveredGauge(labelNames, namespace, subsystem));
case COVERAGE_INSTRUCTION_MISSED:
return saveBuildCollector(new CoverageInstructionMissedGauge(labelNames, namespace, subsystem));
case COVERAGE_INSTRUCTION_TOTAL:
return saveBuildCollector(new CoverageInstructionTotalGauge(labelNames, namespace, subsystem));
case COVERAGE_INSTRUCTION_PERCENT:
return saveBuildCollector(new CoverageInstructionPercentGauge(labelNames, namespace, subsystem));

case COVERAGE_FILE_COVERED:
return saveBuildCollector(new CoverageFileCoveredGauge(labelNames, namespace, subsystem));
case COVERAGE_FILE_MISSED:
return saveBuildCollector(new CoverageFileMissedGauge(labelNames, namespace, subsystem));
case COVERAGE_FILE_TOTAL:
return saveBuildCollector(new CoverageFileTotalGauge(labelNames, namespace, subsystem));
case COVERAGE_FILE_PERCENT:
return saveBuildCollector(new CoverageFilePercentGauge(labelNames, namespace, subsystem));

case COVERAGE_LINE_COVERED:
return saveBuildCollector(new CoverageLineCoveredGauge(labelNames, namespace, subsystem));
case COVERAGE_LINE_MISSED:
return saveBuildCollector(new CoverageLineMissedGauge(labelNames, namespace, subsystem));
case COVERAGE_LINE_TOTAL:
return saveBuildCollector(new CoverageLineTotalGauge(labelNames, namespace, subsystem));
case COVERAGE_LINE_PERCENT:
return saveBuildCollector(new CoverageLinePercentGauge(labelNames, namespace, subsystem));

default:
return new NoOpMetricCollector<>();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ public void calculateMetric(Run<?, ?> jenkinsObject, String[] labelValues) {

Optional<Coverage> optional = getCoverage(jenkinsObject, Metric.FILE, Baseline.PROJECT);
if (optional.isEmpty()) {
collector.labels(labelValues).set(-1);
return;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ public void calculateMetric(Run<?, ?> jenkinsObject, String[] labelValues) {

Optional<Coverage> optional = getCoverage(jenkinsObject, Metric.FILE, Baseline.PROJECT);
if (optional.isEmpty()) {
collector.labels(labelValues).set(-1);
return;
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
package org.jenkinsci.plugins.prometheus.collectors.coverage;

import edu.hm.hafner.coverage.Coverage;
import edu.hm.hafner.coverage.Metric;
import hudson.model.Run;
import io.jenkins.plugins.coverage.metrics.model.Baseline;
import io.prometheus.client.Gauge;
import io.prometheus.client.SimpleCollector;
import org.jenkinsci.plugins.prometheus.collectors.CollectorType;

import java.util.Optional;

public class CoverageFilePercentGauge extends CoverageMetricsCollector<Run<?, ?>, Gauge> {

protected CoverageFilePercentGauge(String[] labelNames, String namespace, String subsystem) {
super(labelNames, namespace, subsystem);
}

@Override
protected CollectorType getCollectorType() {
return CollectorType.COVERAGE_FILE_PERCENT;
}

@Override
protected String getHelpText() {
return "Returns the coverage of files in percent";
}

@Override
protected SimpleCollector.Builder<?, Gauge> getCollectorBuilder() {
return Gauge.build();
}

@Override
public void calculateMetric(Run<?, ?> jenkinsObject, String[] labelValues) {

Optional<Coverage> optional = getCoverage(jenkinsObject, Metric.FILE, Baseline.PROJECT);
if (optional.isEmpty()) {
collector.labels(labelValues).set(-1);
return;
}

Coverage coverage = optional.get();
collector.labels(labelValues).set(calculatePercentage(coverage));
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ public void calculateMetric(Run<?, ?> jenkinsObject, String[] labelValues) {

Optional<Coverage> optional = getCoverage(jenkinsObject, Metric.FILE, Baseline.PROJECT);
if (optional.isEmpty()) {
collector.labels(labelValues).set(-1);
return;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ public void calculateMetric(Run<?, ?> jenkinsObject, String[] labelValues) {

Optional<Coverage> optional = getCoverage(jenkinsObject, Metric.INSTRUCTION, Baseline.PROJECT);
if (optional.isEmpty()) {
collector.labels(labelValues).set(-1);
return;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ public void calculateMetric(Run<?, ?> jenkinsObject, String[] labelValues) {

Optional<Coverage> optional = getCoverage(jenkinsObject, Metric.INSTRUCTION, Baseline.PROJECT);
if (optional.isEmpty()) {
collector.labels(labelValues).set(-1);
return;
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
package org.jenkinsci.plugins.prometheus.collectors.coverage;

import edu.hm.hafner.coverage.Coverage;
import edu.hm.hafner.coverage.Metric;
import hudson.model.Run;
import io.jenkins.plugins.coverage.metrics.model.Baseline;
import io.prometheus.client.Gauge;
import io.prometheus.client.SimpleCollector;
import org.jenkinsci.plugins.prometheus.collectors.CollectorType;

import java.util.Optional;

public class CoverageInstructionPercentGauge extends CoverageMetricsCollector<Run<?, ?>, Gauge> {

protected CoverageInstructionPercentGauge(String[] labelNames, String namespace, String subsystem) {
super(labelNames, namespace, subsystem);
}

@Override
protected CollectorType getCollectorType() {
return CollectorType.COVERAGE_INSTRUCTION_PERCENT;
}

@Override
protected String getHelpText() {
return "Returns the coverage of instructions in percent";
}

@Override
protected SimpleCollector.Builder<?, Gauge> getCollectorBuilder() {
return Gauge.build();
}

@Override
public void calculateMetric(Run<?, ?> jenkinsObject, String[] labelValues) {

Optional<Coverage> optional = getCoverage(jenkinsObject, Metric.INSTRUCTION, Baseline.PROJECT);
if (optional.isEmpty()) {
collector.labels(labelValues).set(-1);
return;
}

Coverage coverage = optional.get();
collector.labels(labelValues).set(calculatePercentage(coverage));
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ public void calculateMetric(Run<?, ?> jenkinsObject, String[] labelValues) {

Optional<Coverage> optional = getCoverage(jenkinsObject, Metric.INSTRUCTION, Baseline.PROJECT);
if (optional.isEmpty()) {
collector.labels(labelValues).set(-1);
return;
}

Expand Down
Loading

0 comments on commit d64bcde

Please sign in to comment.