From 1067fc0a3adb177489c261b955b7ef3fe623a1be Mon Sep 17 00:00:00 2001 From: Simon Brandhof Date: Mon, 28 Sep 2015 15:37:17 +0200 Subject: [PATCH] Add to sonar.log the beginning and end of CE tasks --- .../server/computation/log/CeLogging.java | 4 +++ .../queue/CeWorkerRunnableImpl.java | 27 ++++++++++++++++--- .../sonar/core/util/logs/DefaultProfiler.java | 7 ++++- .../sonar/core/util/logs/NullProfiler.java | 5 ++++ .../org/sonar/core/util/logs/Profiler.java | 3 ++- .../core/util/logs/NullProfilerTest.java | 1 + 6 files changed, 41 insertions(+), 6 deletions(-) diff --git a/server/sonar-server/src/main/java/org/sonar/server/computation/log/CeLogging.java b/server/sonar-server/src/main/java/org/sonar/server/computation/log/CeLogging.java index 6b90db47fdc..55f76de99e8 100644 --- a/server/sonar-server/src/main/java/org/sonar/server/computation/log/CeLogging.java +++ b/server/sonar-server/src/main/java/org/sonar/server/computation/log/CeLogging.java @@ -81,6 +81,8 @@ public class CeLogging { /** * Initialize logging of a Compute Engine task. Must be called * before first writing of log. + *

After this method is executed, then Compute Engine logs are + * written to a dedicated appender and are removed from sonar.log.

*/ public void initForTask(CeTask task) { LogFileRef ref = LogFileRef.from(task); @@ -92,6 +94,8 @@ public class CeLogging { /** * Clean-up the logging of a task. Must be called after the last writing * of log. + *

After this method is executed, then Compute Engine logs are + * written to sonar.log only.

*/ public void clearForTask() { String relativePath = (String) MDC.get(MDC_LOG_PATH); diff --git a/server/sonar-server/src/main/java/org/sonar/server/computation/queue/CeWorkerRunnableImpl.java b/server/sonar-server/src/main/java/org/sonar/server/computation/queue/CeWorkerRunnableImpl.java index 7501cf430a8..5456814ddfe 100644 --- a/server/sonar-server/src/main/java/org/sonar/server/computation/queue/CeWorkerRunnableImpl.java +++ b/server/sonar-server/src/main/java/org/sonar/server/computation/queue/CeWorkerRunnableImpl.java @@ -64,18 +64,37 @@ public class CeWorkerRunnableImpl implements CeWorkerRunnable { } private void executeTask(CeTask task) { + // logging twice: once in sonar.log and once in CE appender + Profiler regularProfiler = startProfiler(task); ceLogging.initForTask(task); - Profiler profiler = Profiler.create(LOG).startInfo(format("Analysis of project %s (report %s)", task.getComponentKey(), task.getUuid())); + Profiler ceProfiler = startProfiler(task); + + CeActivityDto.Status status = CeActivityDto.Status.FAILED; try { // TODO delegate the message to the related task processor, according to task type reportTaskProcessor.process(task); - queue.remove(task, CeActivityDto.Status.SUCCESS); + status = CeActivityDto.Status.SUCCESS; + queue.remove(task, status); } catch (Throwable e) { LOG.error(format("Failed to process task %s", task.getUuid()), e); - queue.remove(task, CeActivityDto.Status.FAILED); + queue.remove(task, status); } finally { - profiler.stopInfo(String.format("Total thread execution of project %s (report %s)", task.getComponentUuid(), task.getUuid())); + // logging twice: once in sonar.log and once in CE appender + stopProfiler(ceProfiler, task, status); ceLogging.clearForTask(); + stopProfiler(regularProfiler, task, status); + } + } + + private static Profiler startProfiler(CeTask task) { + return Profiler.create(LOG).startInfo("Process task | project={} | id={}", task.getComponentKey(), task.getUuid()); + } + + private static void stopProfiler(Profiler profiler, CeTask task, CeActivityDto.Status status) { + if (status == CeActivityDto.Status.FAILED) { + profiler.stopError("Processed task | project={} | id={}", task.getComponentKey(), task.getUuid()); + } else { + profiler.stopInfo("Processed task | project={} | id={}", task.getComponentKey(), task.getUuid()); } } } diff --git a/sonar-core/src/main/java/org/sonar/core/util/logs/DefaultProfiler.java b/sonar-core/src/main/java/org/sonar/core/util/logs/DefaultProfiler.java index fbfce14c40f..9709521ae16 100644 --- a/sonar-core/src/main/java/org/sonar/core/util/logs/DefaultProfiler.java +++ b/sonar-core/src/main/java/org/sonar/core/util/logs/DefaultProfiler.java @@ -135,6 +135,11 @@ class DefaultProfiler extends Profiler { return doStop(LoggerLevel.INFO, message, args, ""); } + @Override + public long stopError(String message, Object... args) { + return doStop(LoggerLevel.ERROR, message, args, ""); + } + private Profiler doStart(LoggerLevel logLevel, String message, Object... args) { init(message, args); logStartMessage(logLevel, message, args); @@ -159,7 +164,7 @@ class DefaultProfiler extends Profiler { StringBuilder sb = new StringBuilder(); sb.append(message); appendContext(sb); - logger.trace(sb.toString(), args); + log(loggerLevel, sb.toString(), args); } } diff --git a/sonar-core/src/main/java/org/sonar/core/util/logs/NullProfiler.java b/sonar-core/src/main/java/org/sonar/core/util/logs/NullProfiler.java index eec8190f4db..fc17f498206 100644 --- a/sonar-core/src/main/java/org/sonar/core/util/logs/NullProfiler.java +++ b/sonar-core/src/main/java/org/sonar/core/util/logs/NullProfiler.java @@ -118,6 +118,11 @@ class NullProfiler extends Profiler { return 0; } + @Override + public long stopError(String message, Object... args) { + return 0; + } + @Override public Profiler addContext(String key, @Nullable Object value) { // nothing to do diff --git a/sonar-core/src/main/java/org/sonar/core/util/logs/Profiler.java b/sonar-core/src/main/java/org/sonar/core/util/logs/Profiler.java index ee1487e54dd..15148450f3e 100644 --- a/sonar-core/src/main/java/org/sonar/core/util/logs/Profiler.java +++ b/sonar-core/src/main/java/org/sonar/core/util/logs/Profiler.java @@ -85,9 +85,10 @@ public abstract class Profiler { public abstract long stopInfo(String message, Object... args); + public abstract long stopError(String message, Object... args); + /** * Context information is removed if value is null. */ public abstract Profiler addContext(String key, @Nullable Object value); - } diff --git a/sonar-core/src/test/java/org/sonar/core/util/logs/NullProfilerTest.java b/sonar-core/src/test/java/org/sonar/core/util/logs/NullProfilerTest.java index 6572d4956d9..b9b404efce6 100644 --- a/sonar-core/src/test/java/org/sonar/core/util/logs/NullProfilerTest.java +++ b/sonar-core/src/test/java/org/sonar/core/util/logs/NullProfilerTest.java @@ -48,6 +48,7 @@ public class NullProfilerTest { assertThat(underTest.stopDebug("msg")).isEqualTo(0); assertThat(underTest.stopTrace()).isEqualTo(0); assertThat(underTest.stopTrace("msg")).isEqualTo(0); + assertThat(underTest.stopError("msg")).isEqualTo(0); } } -- 2.39.5