sonarqubemeasures

Categorize measures in SonarQube


I want to categorize specific metrics I have in SonarQube to fit my needs.

In detail: I built a Gatling Plugin for SonarQube that reads the stats which Gatling writes and puts them into SonarQube Metrics.

Now I want to categorize my measures according to the requests I did with Gatling. So, for example I have the global stats by Gatling (10,000 requests, 50ms per request, etc.) and I also have the data for the requests (for request 1 there were 1,000 requests, 40ms per request, etc.).

How is it possible to assign the request to a measure in Sonar?

A possible solution I could think about was to link the measures to specific contexts in Sonar. So, I will save the measures for the requests in a special context while the global stats are saved within the global context.

But: how can I access my measures in the Ruby-Template afterwards and what would be a good context to pick?

Update: I tried to save the same measure multiple times with the request name as the Measure.data-parameter but that resulted in an error.

I also tried changing the resources-Context Sonar saves the measure in. But I did not succeed to query the measures afterwards in the widget, they seemed to have disappeared.


Solution

  • After trying to work a bit with a Resource I was able to save the measures and group them by the requests I got from the Gatling files.

    It was a lot of try and error I had to do and the documentation by SonarQube is just awful when it comes to reverse engineering.

    I had to use a deprecated method to index my resources (SensorContext.index(Resource resource)) because I don't know any other way.

    For example: Though there is a new plugin API (which is declared @Beta in the 4.5.1 version of SonarQube) it is not once mentioned in the official plugin documentation and you have to search through the source files to find it. However, I continued working with the old API.

    To retrieve the measures afterwards in the .html.erb-Template, I used the following code in my widget:

    m = measure('your_metric_name')
    ProjectMeasure.find(:all,
                    :from => ['project_measures as p, snapshots as s'],
                    :conditions => ['p.metric_id=? AND s.id = p.snapshot_id 
                    AND s.parent_snapshot_id = ?', m.metric.id, m.snapshot_id]
    

    This will return all the measures you made regarding this project (make sure that your resource's getParent() method returns the project you are analysing with SonarQube). I saved the category via measure.setData("category") in Java and was able to categorize them in the widget through this trick.

    If someone is stuck here too, I will add additional code (especially on how to create a new resource to save your multiple measures). Just comment please.