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_SonarSource - 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_SonarSource It seems that
curl -k -X GET “https://sonarqube/api/issues/search?componentKeys=MYPROJECT&branch=master&type=CODESMELL&resolved=false

Works

1 Like