Skip to content

awaescher/sonar-custom-metric-plugin

 
 

Repository files navigation

SonarQube Custom Metric Plugin

Quality Gate Coverage Bug

This is a custom plugin for SonarQube - Continuous code quality inspection tool.

This plugin will allow to add custom metrics project to let quality gate checking for it.

Building

  • Clone this repository
  • Run ./gradle build or .\gradlew build on Windows
  • Find sonar-custom-metric-plugin-*.*.jar in ./build/libs

Installing

Copy the built sonar-custom-metric-plugin-*.*.jar plugin to SonarQube server plugin directory in <SonarQubeHome>/extensions/plugins/. Restart the SonarQube process.

SonarQube on Docker If you are using Docker compose file, copy the plugin to the container volume:

// sonarqube_sonarqube_1 is container name
docker cp sonar-customer-metric-plugin-0.1.jar sonarqube_sonarqube_1:/opt/sonarqube/extensions/plugins/
docker-compose restart

Defining metrics

The metrics have to be defined so that SonarQube can interpret them. This has to be done in .\src\main\java\jp\co\atware\sonar\custommetric\CustomMetrics.java.

This file contains more information on how to define these metrics.

Example:

	static final List<Metric> METRICS = Arrays.asList(

		new Metric.Builder(
            "MemoryUsage", "Memory usage", Metric.ValueType.INT)
            .setDirection(Metric.DIRECTION_WORST)
            .setQualitative(true)
            .setDomain(CoreMetrics.DOMAIN_RELIABILITY)
            .create(),

		new Metric.Builder(
            "MemoryUsagePercent", "Memory usage (in percent)", Metric.ValueType.FLOAT)
            .setDirection(Metric.DIRECTION_WORST)
            .setQualitative(true)
            .setDomain(CoreMetrics.DOMAIN_RELIABILITY)
            .create(),	

		new Metric.Builder(
            "Status", "Api status response", Metric.ValueType.STRING)
            .setDirection(Metric.DIRECTION_NONE)
            .setQualitative(true)
            .setDomain(CoreMetrics.DOMAIN_RELIABILITY)
            .create(),	

	);

Changing metric definitions requires a new version of the plugin, installing it and restarting the SonarQube process.

Writing custom metrics

Now that we defined the custom metrics, we have to collect and write the according values during the build and/or test process. These values need to be written into a file in the project file structure. This file uses a metricId,value format.

Example:

MemoryUsage,380
MemoryUsagePercent,0.34
Status,OK

The values can be various from int, double to string, depending on the metric definitions in the plugin (see above).

Transferring custom metrics

Once the metrics file is written, its relative path needs to be transferred to the SonarQube server. Use the variable name custom.metrics.reportFilePath

Example:

begin
  /d:sonar.host.url="..."
  /d:custom.metrics.reportFilePath="./custom-sonar-metrics.csv"
  ...

About

SonarQube custom metric plugin

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages

  • Java 100.0%