From 305d71f14946d6fa883f1b24ca9ab2499b0cfd04 Mon Sep 17 00:00:00 2001 From: =?utf8?q?S=C3=A9bastien=20Lesaint?= Date: Tue, 8 Nov 2016 15:46:35 +0100 Subject: [PATCH] SONAR-8332 add HTTP request UID to WebServer logs --- .../org/sonar/ce/log/CeProcessLogging.java | 2 +- .../server/app/ServerProcessLogging.java | 10 ++- .../server/app/WebServerProcessLogging.java | 2 +- .../platformlevel/PlatformLevel4.java | 5 +- .../web/requestid/HttpRequestUidModule.java | 30 +++++++++ .../web/requestid/RequestUidFilter.java | 65 +++++++++++++++++++ .../web/requestid/RequestUidGenerator.java | 30 +++++++++ .../requestid/RequestUidGeneratorBase.java | 29 +++++++++ .../RequestUidGeneratorBaseImpl.java | 32 +++++++++ .../requestid/RequestUidGeneratorImpl.java | 46 +++++++++++++ .../web/requestid/RequestUidMDCStorage.java | 40 ++++++++++++ .../platform/web/requestid/package-info.java | 23 +++++++ .../app/WebServerProcessLoggingTest.java | 6 +- .../requestid/HttpRequestUidModuleTest.java | 40 ++++++++++++ .../RequestUidGeneratorImplTest.java | 33 ++++++++++ .../requestid/RequestUidMDCStorageTest.java | 44 +++++++++++++ .../sonar-web/src/main/webapp/WEB-INF/web.xml | 8 +++ .../core/util/UuidGeneratorImplTest.java | 19 ++++++ 18 files changed, 455 insertions(+), 9 deletions(-) create mode 100644 server/sonar-server/src/main/java/org/sonar/server/platform/web/requestid/HttpRequestUidModule.java create mode 100644 server/sonar-server/src/main/java/org/sonar/server/platform/web/requestid/RequestUidFilter.java create mode 100644 server/sonar-server/src/main/java/org/sonar/server/platform/web/requestid/RequestUidGenerator.java create mode 100644 server/sonar-server/src/main/java/org/sonar/server/platform/web/requestid/RequestUidGeneratorBase.java create mode 100644 server/sonar-server/src/main/java/org/sonar/server/platform/web/requestid/RequestUidGeneratorBaseImpl.java create mode 100644 server/sonar-server/src/main/java/org/sonar/server/platform/web/requestid/RequestUidGeneratorImpl.java create mode 100644 server/sonar-server/src/main/java/org/sonar/server/platform/web/requestid/RequestUidMDCStorage.java create mode 100644 server/sonar-server/src/main/java/org/sonar/server/platform/web/requestid/package-info.java create mode 100644 server/sonar-server/src/test/java/org/sonar/server/platform/web/requestid/HttpRequestUidModuleTest.java create mode 100644 server/sonar-server/src/test/java/org/sonar/server/platform/web/requestid/RequestUidGeneratorImplTest.java create mode 100644 server/sonar-server/src/test/java/org/sonar/server/platform/web/requestid/RequestUidMDCStorageTest.java diff --git a/server/sonar-server/src/main/java/org/sonar/ce/log/CeProcessLogging.java b/server/sonar-server/src/main/java/org/sonar/ce/log/CeProcessLogging.java index de414ed3956..f932013d170 100644 --- a/server/sonar-server/src/main/java/org/sonar/ce/log/CeProcessLogging.java +++ b/server/sonar-server/src/main/java/org/sonar/ce/log/CeProcessLogging.java @@ -36,7 +36,7 @@ import static org.slf4j.Logger.ROOT_LOGGER_NAME; public class CeProcessLogging extends ServerProcessLogging { public CeProcessLogging() { - super("ce"); + super("ce", "%X{ceTaskUuid}"); } @Override diff --git a/server/sonar-server/src/main/java/org/sonar/server/app/ServerProcessLogging.java b/server/sonar-server/src/main/java/org/sonar/server/app/ServerProcessLogging.java index 7e2125c99fe..2046b2d04a2 100644 --- a/server/sonar-server/src/main/java/org/sonar/server/app/ServerProcessLogging.java +++ b/server/sonar-server/src/main/java/org/sonar/server/app/ServerProcessLogging.java @@ -30,12 +30,16 @@ import org.sonar.server.platform.ServerLogging; public abstract class ServerProcessLogging { private static final String LOG_LEVEL_PROPERTY = "sonar.log.level"; - private static final String LOG_FORMAT = "%d{yyyy.MM.dd HH:mm:ss} %-5level XXXX[%X{ceTaskUuid}][%logger{20}] %msg%n"; + private static final String PROCESS_NAME_PLACEHOLDER = "XXXX"; + private static final String THREAD_ID_PLACEHOLDER = "ZZZZ"; + private static final String LOG_FORMAT = "%d{yyyy.MM.dd HH:mm:ss} %-5level " + PROCESS_NAME_PLACEHOLDER + "[" + THREAD_ID_PLACEHOLDER + "][%logger{20}] %msg%n"; private final String processName; + private final String threadIdField; private final LogbackHelper helper = new LogbackHelper(); - protected ServerProcessLogging(String processName) { + protected ServerProcessLogging(String processName, String threadIdPattern) { this.processName = processName; + this.threadIdField = threadIdPattern; } public LoggerContext configure(Props props) { @@ -53,7 +57,7 @@ public abstract class ServerProcessLogging { } private void configureAppenders(LoggerContext ctx, Props props) { - String logFormat = LOG_FORMAT.replace("XXXX", processName); + String logFormat = LOG_FORMAT.replace(PROCESS_NAME_PLACEHOLDER, processName).replace(THREAD_ID_PLACEHOLDER, threadIdField); configureAppenders(logFormat, ctx, helper, props); } diff --git a/server/sonar-server/src/main/java/org/sonar/server/app/WebServerProcessLogging.java b/server/sonar-server/src/main/java/org/sonar/server/app/WebServerProcessLogging.java index 50709c12565..443809bc9b5 100644 --- a/server/sonar-server/src/main/java/org/sonar/server/app/WebServerProcessLogging.java +++ b/server/sonar-server/src/main/java/org/sonar/server/app/WebServerProcessLogging.java @@ -33,7 +33,7 @@ import org.sonar.process.Props; public class WebServerProcessLogging extends ServerProcessLogging { public WebServerProcessLogging() { - super("web"); + super("web", "%X{UID}"); } @Override diff --git a/server/sonar-server/src/main/java/org/sonar/server/platform/platformlevel/PlatformLevel4.java b/server/sonar-server/src/main/java/org/sonar/server/platform/platformlevel/PlatformLevel4.java index def3adbf166..d45e135c13f 100644 --- a/server/sonar-server/src/main/java/org/sonar/server/platform/platformlevel/PlatformLevel4.java +++ b/server/sonar-server/src/main/java/org/sonar/server/platform/platformlevel/PlatformLevel4.java @@ -120,6 +120,7 @@ import org.sonar.server.platform.monitoring.PluginsMonitor; import org.sonar.server.platform.monitoring.SettingsMonitor; import org.sonar.server.platform.monitoring.SonarQubeMonitor; import org.sonar.server.platform.monitoring.SystemMonitor; +import org.sonar.server.platform.web.requestid.HttpRequestUidModule; import org.sonar.server.platform.ws.ChangeLogLevelAction; import org.sonar.server.platform.ws.DbMigrationStatusAction; import org.sonar.server.platform.ws.InfoAction; @@ -542,8 +543,10 @@ public class PlatformLevel4 extends PlatformLevel { RootWsModule.class, // webhooks - WebhooksWsModule.class); + WebhooksWsModule.class, + // Http Request UID + HttpRequestUidModule.class); addAll(level4AddedComponents); } diff --git a/server/sonar-server/src/main/java/org/sonar/server/platform/web/requestid/HttpRequestUidModule.java b/server/sonar-server/src/main/java/org/sonar/server/platform/web/requestid/HttpRequestUidModule.java new file mode 100644 index 00000000000..745c49ef85a --- /dev/null +++ b/server/sonar-server/src/main/java/org/sonar/server/platform/web/requestid/HttpRequestUidModule.java @@ -0,0 +1,30 @@ +/* + * SonarQube + * Copyright (C) 2009-2016 SonarSource SA + * mailto:contact AT sonarsource DOT com + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 3 of the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with this program; if not, write to the Free Software Foundation, + * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + */ +package org.sonar.server.platform.web.requestid; + +import org.sonar.core.platform.Module; + +public class HttpRequestUidModule extends Module { + @Override + protected void configureModule() { + add(RequestUidGeneratorBaseImpl.class, + RequestUidGeneratorImpl.class); + } +} diff --git a/server/sonar-server/src/main/java/org/sonar/server/platform/web/requestid/RequestUidFilter.java b/server/sonar-server/src/main/java/org/sonar/server/platform/web/requestid/RequestUidFilter.java new file mode 100644 index 00000000000..e9d365c966b --- /dev/null +++ b/server/sonar-server/src/main/java/org/sonar/server/platform/web/requestid/RequestUidFilter.java @@ -0,0 +1,65 @@ +/* + * SonarQube + * Copyright (C) 2009-2016 SonarSource SA + * mailto:contact AT sonarsource DOT com + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 3 of the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with this program; if not, write to the Free Software Foundation, + * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + */ +package org.sonar.server.platform.web.requestid; + +import com.google.common.annotations.VisibleForTesting; +import java.io.IOException; +import javax.servlet.Filter; +import javax.servlet.FilterChain; +import javax.servlet.FilterConfig; +import javax.servlet.ServletException; +import javax.servlet.ServletRequest; +import javax.servlet.ServletResponse; +import org.sonar.server.platform.Platform; + +/** + * A {@link Filter} that puts and removes the HTTP request UID from the {@link org.slf4j.MDC}. + */ +public class RequestUidFilter implements Filter { + private final Platform platform; + + public RequestUidFilter() { + this(Platform.getInstance()); + } + + @VisibleForTesting + RequestUidFilter(Platform platform) { + this.platform = platform; + } + + @Override + public void init(FilterConfig filterConfig) throws ServletException { + // nothing to do + } + + @Override + public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException { + RequestUidGenerator requestUidGenerator = platform.getContainer().getComponentByType(RequestUidGenerator.class); + + try (RequestUidMDCStorage mdcStorage = new RequestUidMDCStorage(requestUidGenerator.generate())) { + chain.doFilter(request, response); + } + } + + @Override + public void destroy() { + // nothing to do + } +} diff --git a/server/sonar-server/src/main/java/org/sonar/server/platform/web/requestid/RequestUidGenerator.java b/server/sonar-server/src/main/java/org/sonar/server/platform/web/requestid/RequestUidGenerator.java new file mode 100644 index 00000000000..90fa322ca26 --- /dev/null +++ b/server/sonar-server/src/main/java/org/sonar/server/platform/web/requestid/RequestUidGenerator.java @@ -0,0 +1,30 @@ +/* + * SonarQube + * Copyright (C) 2009-2016 SonarSource SA + * mailto:contact AT sonarsource DOT com + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 3 of the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with this program; if not, write to the Free Software Foundation, + * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + */ +package org.sonar.server.platform.web.requestid; + +/** + * Generate a Unique Identifier for Http Requests. + */ +public interface RequestUidGenerator { + /** + * Generate a new and unique request id for each call. + */ + String generate(); +} diff --git a/server/sonar-server/src/main/java/org/sonar/server/platform/web/requestid/RequestUidGeneratorBase.java b/server/sonar-server/src/main/java/org/sonar/server/platform/web/requestid/RequestUidGeneratorBase.java new file mode 100644 index 00000000000..4d93d8476c0 --- /dev/null +++ b/server/sonar-server/src/main/java/org/sonar/server/platform/web/requestid/RequestUidGeneratorBase.java @@ -0,0 +1,29 @@ +/* + * SonarQube + * Copyright (C) 2009-2016 SonarSource SA + * mailto:contact AT sonarsource DOT com + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 3 of the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with this program; if not, write to the Free Software Foundation, + * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + */ +package org.sonar.server.platform.web.requestid; + +import org.sonar.core.util.UuidGenerator; + +/** + * Marker interface to be able to add a {@link org.sonar.core.util.UuidGenerator.WithFixedBase} to pico without risk + * of use in another domain. + */ +public interface RequestUidGeneratorBase extends UuidGenerator.WithFixedBase { +} diff --git a/server/sonar-server/src/main/java/org/sonar/server/platform/web/requestid/RequestUidGeneratorBaseImpl.java b/server/sonar-server/src/main/java/org/sonar/server/platform/web/requestid/RequestUidGeneratorBaseImpl.java new file mode 100644 index 00000000000..e6930b96401 --- /dev/null +++ b/server/sonar-server/src/main/java/org/sonar/server/platform/web/requestid/RequestUidGeneratorBaseImpl.java @@ -0,0 +1,32 @@ +/* + * SonarQube + * Copyright (C) 2009-2016 SonarSource SA + * mailto:contact AT sonarsource DOT com + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 3 of the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with this program; if not, write to the Free Software Foundation, + * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + */ +package org.sonar.server.platform.web.requestid; + +import org.sonar.core.util.UuidGenerator; +import org.sonar.core.util.UuidGeneratorImpl; + +public class RequestUidGeneratorBaseImpl implements RequestUidGeneratorBase { + private final UuidGenerator.WithFixedBase delegate = new UuidGeneratorImpl().withFixedBase(); + + @Override + public byte[] generate(int increment) { + return delegate.generate(increment); + } +} diff --git a/server/sonar-server/src/main/java/org/sonar/server/platform/web/requestid/RequestUidGeneratorImpl.java b/server/sonar-server/src/main/java/org/sonar/server/platform/web/requestid/RequestUidGeneratorImpl.java new file mode 100644 index 00000000000..85d2a30ae5e --- /dev/null +++ b/server/sonar-server/src/main/java/org/sonar/server/platform/web/requestid/RequestUidGeneratorImpl.java @@ -0,0 +1,46 @@ +/* + * SonarQube + * Copyright (C) 2009-2016 SonarSource SA + * mailto:contact AT sonarsource DOT com + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 3 of the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with this program; if not, write to the Free Software Foundation, + * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + */ +package org.sonar.server.platform.web.requestid; + +import java.util.Base64; +import java.util.concurrent.atomic.AtomicInteger; +import org.sonar.core.util.UuidGeneratorImpl; + +/** + * This implementation of {@link RequestUidGenerator} creates unique identifiers for HTTP requests leveraging + * {@link UuidGeneratorImpl#withFixedBase()}. + *

+ * This implementation is Thread safe. + *

+ */ +public class RequestUidGeneratorImpl implements RequestUidGenerator { + private final AtomicInteger counter = new AtomicInteger(); + private final RequestUidGeneratorBase requestUidGeneratorBase; + + public RequestUidGeneratorImpl(RequestUidGeneratorBase requestUidGeneratorBase) { + this.requestUidGeneratorBase = requestUidGeneratorBase; + } + + @Override + public String generate() { + return Base64.getEncoder().encodeToString(requestUidGeneratorBase.generate(counter.incrementAndGet())); + } + +} diff --git a/server/sonar-server/src/main/java/org/sonar/server/platform/web/requestid/RequestUidMDCStorage.java b/server/sonar-server/src/main/java/org/sonar/server/platform/web/requestid/RequestUidMDCStorage.java new file mode 100644 index 00000000000..d5c5a3b2ac9 --- /dev/null +++ b/server/sonar-server/src/main/java/org/sonar/server/platform/web/requestid/RequestUidMDCStorage.java @@ -0,0 +1,40 @@ +/* + * SonarQube + * Copyright (C) 2009-2016 SonarSource SA + * mailto:contact AT sonarsource DOT com + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 3 of the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with this program; if not, write to the Free Software Foundation, + * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + */ +package org.sonar.server.platform.web.requestid; + +import org.slf4j.MDC; + +import static java.util.Objects.requireNonNull; + +/** + * Wraps MDC calls to store the HTTP request UID in the {@link MDC} into an {@link AutoCloseable}. + */ +class RequestUidMDCStorage implements AutoCloseable { + private static final String HTTP_REQUEST_UID_MDC_KEY = "HTTP_REQUEST_ID"; + + public RequestUidMDCStorage(String requestUid) { + MDC.put(HTTP_REQUEST_UID_MDC_KEY, requireNonNull(requestUid, "Request UID can't be null")); + } + + @Override + public void close() { + MDC.remove(HTTP_REQUEST_UID_MDC_KEY); + } +} diff --git a/server/sonar-server/src/main/java/org/sonar/server/platform/web/requestid/package-info.java b/server/sonar-server/src/main/java/org/sonar/server/platform/web/requestid/package-info.java new file mode 100644 index 00000000000..ccb97c4bdd2 --- /dev/null +++ b/server/sonar-server/src/main/java/org/sonar/server/platform/web/requestid/package-info.java @@ -0,0 +1,23 @@ +/* + * SonarQube + * Copyright (C) 2009-2016 SonarSource SA + * mailto:contact AT sonarsource DOT com + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 3 of the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with this program; if not, write to the Free Software Foundation, + * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + */ +@ParametersAreNonnullByDefault +package org.sonar.server.platform.web.requestid; + +import javax.annotation.ParametersAreNonnullByDefault; diff --git a/server/sonar-server/src/test/java/org/sonar/server/app/WebServerProcessLoggingTest.java b/server/sonar-server/src/test/java/org/sonar/server/app/WebServerProcessLoggingTest.java index 68d4ef48292..732581d378b 100644 --- a/server/sonar-server/src/test/java/org/sonar/server/app/WebServerProcessLoggingTest.java +++ b/server/sonar-server/src/test/java/org/sonar/server/app/WebServerProcessLoggingTest.java @@ -34,11 +34,11 @@ import org.sonar.process.Props; import static org.assertj.core.api.Assertions.assertThat; public class WebServerProcessLoggingTest { - private WebServerProcessLogging underTest = new WebServerProcessLogging(); - private static final String LOG_LEVEL_PROPERTY = "sonar.log.level"; - Props props = new Props(new Properties()); + private WebServerProcessLogging underTest = new WebServerProcessLogging(); + + private Props props = new Props(new Properties()); @AfterClass public static void resetLogback() throws JoranException { diff --git a/server/sonar-server/src/test/java/org/sonar/server/platform/web/requestid/HttpRequestUidModuleTest.java b/server/sonar-server/src/test/java/org/sonar/server/platform/web/requestid/HttpRequestUidModuleTest.java new file mode 100644 index 00000000000..377df7dc18a --- /dev/null +++ b/server/sonar-server/src/test/java/org/sonar/server/platform/web/requestid/HttpRequestUidModuleTest.java @@ -0,0 +1,40 @@ +/* + * SonarQube + * Copyright (C) 2009-2016 SonarSource SA + * mailto:contact AT sonarsource DOT com + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 3 of the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with this program; if not, write to the Free Software Foundation, + * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + */ +package org.sonar.server.platform.web.requestid; + +import org.junit.Test; +import org.sonar.core.platform.ComponentContainer; + +import static org.assertj.core.api.Assertions.assertThat; + +public class HttpRequestUidModuleTest { + private static final int COMPONENTS_HARDCODED_IN_CONTAINER = 2; + + private HttpRequestUidModule underTest = new HttpRequestUidModule(); + + @Test + public void count_components_in_module() { + ComponentContainer container = new ComponentContainer(); + underTest.configure(container); + + assertThat(container.getPicoContainer().getComponentAdapters()) + .hasSize(COMPONENTS_HARDCODED_IN_CONTAINER + 2); + } +} diff --git a/server/sonar-server/src/test/java/org/sonar/server/platform/web/requestid/RequestUidGeneratorImplTest.java b/server/sonar-server/src/test/java/org/sonar/server/platform/web/requestid/RequestUidGeneratorImplTest.java new file mode 100644 index 00000000000..755d1d25b12 --- /dev/null +++ b/server/sonar-server/src/test/java/org/sonar/server/platform/web/requestid/RequestUidGeneratorImplTest.java @@ -0,0 +1,33 @@ +/* + * SonarQube + * Copyright (C) 2009-2016 SonarSource SA + * mailto:contact AT sonarsource DOT com + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 3 of the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with this program; if not, write to the Free Software Foundation, + * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + */ +package org.sonar.server.platform.web.requestid; + +import org.junit.Test; + +import static org.assertj.core.api.Assertions.assertThat; + +public class RequestUidGeneratorImplTest { + @Test + public void generate_returns_value_if_RequestUidGeneratorBase_generate_encoded_in_base64() { + RequestUidGeneratorImpl underTest = new RequestUidGeneratorImpl(increment -> new byte[] {124, 22, 66, 96, 55, 88, 2, 9}); + + assertThat(underTest.generate()).isEqualTo("fBZCYDdYAgk="); + } +} diff --git a/server/sonar-server/src/test/java/org/sonar/server/platform/web/requestid/RequestUidMDCStorageTest.java b/server/sonar-server/src/test/java/org/sonar/server/platform/web/requestid/RequestUidMDCStorageTest.java new file mode 100644 index 00000000000..81d991f3813 --- /dev/null +++ b/server/sonar-server/src/test/java/org/sonar/server/platform/web/requestid/RequestUidMDCStorageTest.java @@ -0,0 +1,44 @@ +package org.sonar.server.platform.web.requestid; + +import org.apache.log4j.MDC; +import org.junit.After; +import org.junit.Rule; +import org.junit.Test; +import org.junit.rules.ExpectedException; + +import static org.assertj.core.api.Assertions.assertThat; + +public class RequestUidMDCStorageTest { + @Rule + public ExpectedException expectedException = ExpectedException.none(); + + @After + public void tearDown() throws Exception { + MDC.clear(); + } + + @Test + public void constructor_fails_with_NPE_when_argument_is_null() { + expectedException.expect(NullPointerException.class); + expectedException.expectMessage("Request UID can't be null"); + + new RequestUidMDCStorage(null); + } + + @Test + public void constructor_adds_specified_value_in_MDC_under_HTTP_REQUEST_ID_key() { + new RequestUidMDCStorage("toto"); + + assertThat(MDC.get("HTTP_REQUEST_ID")).isEqualTo("toto"); + } + + @Test + public void close_removes_value_from_MDC() { + RequestUidMDCStorage underTest = new RequestUidMDCStorage("boum"); + assertThat(MDC.get("HTTP_REQUEST_ID")).isEqualTo("boum"); + + underTest.close(); + + assertThat(MDC.get("HTTP_REQUEST_ID")).isNull(); + } +} diff --git a/server/sonar-web/src/main/webapp/WEB-INF/web.xml b/server/sonar-web/src/main/webapp/WEB-INF/web.xml index 1a8aa2dc8ad..33a2fb2bd9c 100644 --- a/server/sonar-web/src/main/webapp/WEB-INF/web.xml +++ b/server/sonar-web/src/main/webapp/WEB-INF/web.xml @@ -64,12 +64,20 @@ RoutesFilter org.sonar.server.platform.web.RoutesFilter + + RequestUidFilter + org.sonar.server.platform.web.requestid.RequestUidFilter + RootFilter /* + + RequestUidFilter + /* + RoutesFilter /* diff --git a/sonar-core/src/test/java/org/sonar/core/util/UuidGeneratorImplTest.java b/sonar-core/src/test/java/org/sonar/core/util/UuidGeneratorImplTest.java index b5a3d85d200..21200b0aa40 100644 --- a/sonar-core/src/test/java/org/sonar/core/util/UuidGeneratorImplTest.java +++ b/sonar-core/src/test/java/org/sonar/core/util/UuidGeneratorImplTest.java @@ -1,3 +1,22 @@ +/* + * SonarQube + * Copyright (C) 2009-2016 SonarSource SA + * mailto:contact AT sonarsource DOT com + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 3 of the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with this program; if not, write to the Free Software Foundation, + * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + */ package org.sonar.core.util; import java.util.Base64; -- 2.39.5