Logging metrics e.g. as json

  • which versions are you using: SonarQube - Developer Edition Version 8.4.2 (build 36762)
  • what are you trying to achieve: I am analyzing C code using
sonar-scanner \
 -Dsonar.projectKey=my_code \
 -Dsonar.sources=. \
 -Dsonar.host.url=http://server:8082/ \
 -Dsonar.login=key \
 -Dsonar.cpp.file.suffixes=- \
 -Dsonar.objc.file.suffixes=- \
 -Dsonar.inclusions=src/**,test/** \
 -Dsonar.branch.name=${BRANCH} \
 -Dsonar.cfamily.threads=8 \
 -Dsonar.cfamily.build-wrapper-output=build/sonar

It works like a charm, but I need to make graph per project (overall metrics)

  • Number of Bugs
  • Number of Code smells
  • Number of Vulnerabilities
  • Coverage

I have these numbers from the web UI, but I need to interface to a grafana graph system automagically. I like to either

  • log a json/any-structured-format of these numbers to a file every time I scan, or
  • access the current level via a pull to the sonarqube system

Any ideas for this?

Hey there.

The Web API (documentation linked in the footer of your instance) can be used to pull data from SonarQube – and here’s a trick, you can use your browser’s dev tools to see what API calls are being made whenever you view a page in SonarQube.

For what you’ve mentioned, GET api/measures/component and GET api/measures/search_history will probably be useful for you.

Out of curiosity, what makes viewing these measures in Grafana better/more useful/more interesting than right in SonarQube?

thanx @Colin - the thing is that the sonarqube is just one part of many metrics that I need to track.
The UI in https://sonarqube/project/activity?id=somekey is really good (and similar to what I need)

Looks to be the right direction - I found the “Web API” next to “About” - LOTS of good stuff in there. Now I just need the first example

I am a Linux user, so it seems to be in the line of
$ curl -X GET -u sonaruserid:ColinIsANicePerson https://sonarqube/api/measures/component

But it barfs “{“errors”:[{“msg”:“The ‘component’ parameter is missing”}]}”
which seems to be project id - that I have not supplied yet…

(Do we have have the same password G)

@Colin It seems that
curl -k -X GET “https://sonarqube/api/issues/search?componentKeys=MYPROJECT&branch=master&type=CODESMELL&resolved=false”

Works

1 Like

@Colin is there a similar access to e.g. coverage i.e.
https://sonarqube/component_measures?id=MYPROJECT&metric=coverage
or some of the other metrics… again via https://sonarqube/api/

Checkout GET api/measures/component

And, anywhere you see data in SonarQube, you can open your browser’s Dev Tools and see what network calls are being made. Makes finding the right API (to then go find in the docs for more details) super easy.

Huuh - I get
$ curl -k -X GET “https://sonarqube/api/measures/component”
{“errors”:[{“msg”:“The ‘component’ parameter is missing”}]}

@Colin or others - it would be fantastic with a hint here

Hi Daniel,

The error message is pretty clear. You need to add query parameters (such as component)

// 20210217101141
// https://next.sonarqube.com/sonarqube/api/measures/component?component=org.sonarsource.javascript%3Ajavascript&metricKeys=coverage

{
  "component": {
    "id": "5eab015a-1f76-4ba4-bd89-bf547132d673",
    "key": "org.sonarsource.javascript:javascript",
    "name": "SonarJS",
    "description": "SonarQube JavaScript/TypeScript Analyzer",
    "qualifier": "TRK",
    "measures": [
      {
        "metric": "coverage",
        "value": "95.8",
        "bestValue": false
      }
    ]
  }
}

I still think following this tip would have helped you.

@Colin THANX - you are golden :slight_smile: