SonarQube New Code analysis is off by 5 hours

  • We are trying to achieve Quality gate to act on new code as soon as the second analysis is performed on the newly created project.
  • We are using SonarQube 8.9 LTS (8.9.0.43852)
  • Issue: When we have new project created, SonarQube is not showing the analysis on new code for next 5 hours (Time between Project Creation Time till +5 hrs)
  • Steps to reproduce:
  1. check in a baseline build of any code
  2. Build the code and get a Sonar Report
  3. Check in changes to TFS. These should be analyzed as new code
  4. View the timestamp on the change in Sonar (this is the bug, the timestamp doesn’t match TFS)
  5. The timestamp reflected in Sonar Matches the actual time of checkin.
  6. New code checked in after initial build is analyzed
  • We tried building the project multiple times by changing the code and the scanner didn’t show the changes on the new code. We see the updates on new code only after 5 Hours (UTC to CST time difference)
  • We had similar issue in 7.9 LTS version so we upgraded to 8.9 LTS assuming the issue could be fixed.
  • We also tried updating the Time zone in sonar.properties file to use UTC, America/Chicago and the default values for the below properties but didn’t help.

sonar.web.javaAdditionalOpts=-Duser.timezone=
sonar.search.javaAdditionalOpts=-Duser.timezone=
sonar.ce.javaAdditionalOpts=-Duser.timezone=






Hi @sai,

I’m sorry, I’m having trouble understanding the issue. Is the problem that the timestamp in SonarQube doesn’t match the one in TFS? Or is the issue that it takes 5 hours for you to see any results in the SonarQube UI?

Hi @Wouter_Admiraal,

Issue is that SonarQube is not showing Analysis on New coverage for 5 hours from the time of Initial analysis. Question was why is SonarQube not analyzing or showing analysis on new code.
Ideal scenario is when i run the 2nd build/analysis, it should show the code coverage on new code but its not happening the way it is supposed to.
I verified Server time zone, configurations and everything is the same (CST timezone).

I’m terribly sorry, but this still isn’t clear to me. So, if I try to recap:

  • You run an initial analysis at 1pm → quickly, SonarQube shows you the result, as expected.
  • You run a second analysis at 1:30pm → you see nothing in SonarQube.
  • At 6:30pm, all of a sudden, you see your second analysis in SonarQube. This is much later than you expect, plus the results aren’t what you expect.

Is this correct? I’m sorry if not, but I just don’t understand the problem from the screenshots you’ve posted.

About the 5h delay, if that’s the main issue here, I suggest looking into how/where you run your analysis. Do you always see this 5h delay, or did it happen a single time? Could it be your CI server’s queue? Did you check SonarQube’s own Background Tasks queue?

So here is what happens.

  • I run the analysis at 1pm → I can see the project created in the SonarQube with Baseline Analysis
  • I run another build at 1:20 PM with new code added → SonarQube doesn’t show the analysis on new code, but i can see the new code is added in the file and I can also see the overall number of lines of code count is increased.
  • When i run the 3rd build at 6:30 with new code added again, then i’m able to see the analysis results on the new code as shown in the screenshots above.

NOTE: I need to run the build at 6:30 inorder to see the analysis results on the newly added code. That being said, within this 5 hr period, i will not see any analysis on New Code doesn’t matter how many times you run the build by adding new lines of code.

We run the analysis from TFS and both TFS database, Agent server and Sonarqube are all in the same datacenter and has same timezone. And we don’t see anything from the Background Tasks. I hope you get this now.

Hi,

Did you get a change to look at my last message. Do you need any other clarification on this issue?

Wouter,
Is there a chance to take this as an issue or a bug instead of ‘Help’ and verify/ get help at the earliest

@Wouter_Admiraal @ganncamp Can we check this issue as we are having lot of impact on the analysis and see if this has to be considered as a bug? Your help and Effort is appreciated…

@Wouter_Admiraal @sai

Was this issue ever resolved ? we just upgraded to 8.9 with postgres 12.4, and are having the same issue, sonarqube 8.9 is virtually unusable.

Thanks

@kdivecha No, Issue still persist. I’m waiting for SonarQube Team to verify and provide a fix.

@sai @Alexander_Dragun

We followed Alexander’s suggestion below, it seemed to help with background tasks, try increasing postgres work_mem to 64mb (default is 4mb) assuming you are using postgres, we use aws rds for postgres so we did it via rds param group, reboot sonarqube after updating work_mem just in case.

Thanks

Hello, also I would recommend considering re-analyzing the statistics for the postgres query engine, since in my case, the main problem was that the query planner was building incorrect query plans due to incorrect statistics.

1 Like

We are not using Postgres and as explained above, its nothing related to memory size… I don’t have any issues related to background task because i can see the analysis happening but the analysis on new code wasn’t showing for the 1st 5 hr period (compare from base code) and unable to check with quality Gate.

@sai

  • What is the current new code definition of your project? You can find it under Project Settings > New code.
  • Could you reproduce the issue with all 3 analyses you mentioned (1PM/1:30:PM/6:30PM)? Are those analyses sharing the same version? Please provide activity page screenshots once it is reproduced.

++

@Philippe_Perrin ,
Thanks for the reply.
Please find the details below.

  1. Analysis one:

As you can see total lines of code for the 1st Analysis is 1,343

  1. Analysis two: Added few more lines of code and ran the analysis in less than 30 Min from the initial analysis, but the below screenshot shows 0 new lines

As you can see, the total lines of code is now 1,359. And Lines to cover is still 0 On New Code

  1. Analysis Three: We ran this analysis after few hours which is more that 6 hrs. Added few more lines and now it started showing the Analysis on new code.

In the below screenshot, total lines are now 1,367 and you can see total new lines are 8 and they are not covered

  • Sonar Activity Screenshot: As you can see there is increase in the total lines between 1st and 2nd analysis which didn’t showup. But in the 3rd analysis which we ran today morning (time gap is more than 6 hrs), shows the correct analysis on the new code.

Let me know if you need any other explanation.

Hi sai,

Terribly sorry for the late reply, I’m just back from paternity leave, and did not think of passing this to a colleague before I left.

I’m re-reading all this, and I’m still very, very confused about this “5 hours” thing. Analyses are a sequential thing. SonarQube doesn’t care about the time between different analyses. You could send several of them only seconds apart, it will work. In fact, that’s exactly what we do with our automated QA’s, which are run dozens, if not hundreds of times every day. The fact you have to wait 5 hours before sending a 3rd analysis, which would then only detect new code, sounds like a red herring. I doubt the timing between analyses has much to do with it, unless your Background Tasks queue is completely saturated.

Now, I’m not saying there’s not a bug here somewhere. But I have the feeling concentrating on this “5h” is leading us astray, at least as far as New Code detection is concerned.

I have some extra questions:

  • You said you were using TFS. Are you also using TFS as your SCM (meaning, you’re not using Git or SVN)? If yes, what plugin are you using (as SonarQube doesn’t provide TFS SCM support out of the box)?
  • Is this happening on several projects? Or only a single one? Or perhaps a specific category of projects (language, framework, etc) ?
  • If you submit those 3 analyses in rapid succession, do you see all 3 of them in the Background Tasks? How long before they’re all treated by SonarQube?
  • Are you using DataCenter Edition? If so, do you have several nodes?

@Wouter_Admiraal

I worked on Different Sonarqube Versions before and never had this issue. All i want from you is how can i find whether its Sonarqube Bug or do i need to make any settings inorder to work. I keep saying 5 hours because any new code which is analyzed after it is treated as new code, and anything before that is never considered as new code (It doesn’t matter how many times you run like 100 or 200). I have sent many screenshots with examples for the reference.
All i need is Why this is happening and how can i check and fix it.

For other questions

  • Yes, we use TFVC as our SCM. That being said, we are able to get the analysis on new code after 5 hr period. So i think there is no issue here.
  • It is happening on all the Newly added projects to Sonarqube. We are using C# with .net framework 4.7 or above, .net core 2.2 & 3.1
  • All background tasks are getting completed in regular manner (running about 1 min or so depending on the size of the project). As i said before, its not just 3 but as many as we run for the 1st 5 hr period.
  • Yes, we are using Datacenter Edition. We only have 1 node Instance

Let me know if you need any other information.

OK, and have you tried adjusting your new code period setting? Right now, I see it’s set to 400 days, which might be part of the problem (would be a bug, in that case). I’ve tried reproducing with such a high value, but no luck. Is there a reason for using 400 days as a new code period?

We tried setting it to use Previous Version but it didn’t work. (Worked after 5 hr period again). So everything is revolving around this time period. We also tried setting the Java Time Zone to use CST, New York and UTC and none of them worked. Also tried commenting this setting in sonar.properties file

Hi,

The “new code” in a branch is always the result of comparing the last modification date of each line (usually collected by the scanner from the SCM) with a threshold which was calculated based on the “New Code Period” setting. One of the dates may be wrong due to the timezone.

Could you please check the date when the New Code starts by checking the field leakPeriodDate in the response of
https://my.sonarqube.com/api/components/show?component=[projectKey]&branch=[branch]

Then, for the source code file where you expected to find lines classified as ‘new’, please request:
https://my.sonarqube.com/api/sources/lines?key=[fileKey]&from=[lineNumber]&to=[lineNumber]&branch=[branch]

And check the field scmDate. The file key will be shown in the URL bar when you navigate to the file in SonarQube web interface.