From f6c0e29213b7f401538e608693ad8e03e2150361 Mon Sep 17 00:00:00 2001 From: Manfred Riem Date: Fri, 10 Jan 2025 17:11:57 -0600 Subject: [PATCH] Fixes #4474 - Add a TraceExtension for tracing purposes (#4489) --- feature/pom.xml | 3 +- feature/trace/pom.xml | 49 ++++++++++ .../piranha/feature/trace/TraceFeature.java | 89 +++++++++++++++++++ feature/trace/src/main/java/module-info.java | 43 +++++++++ .../feature/trace/tests/TraceFeatureTest.java | 52 +++++++++++ feature/trace/src/test/java/module-info.java | 40 +++++++++ 6 files changed, 275 insertions(+), 1 deletion(-) create mode 100644 feature/trace/pom.xml create mode 100644 feature/trace/src/main/java/cloud/piranha/feature/trace/TraceFeature.java create mode 100644 feature/trace/src/main/java/module-info.java create mode 100644 feature/trace/src/test/java/cloud/piranha/feature/trace/tests/TraceFeatureTest.java create mode 100644 feature/trace/src/test/java/module-info.java diff --git a/feature/pom.xml b/feature/pom.xml index 7945aa2b2..bdec7cec5 100644 --- a/feature/pom.xml +++ b/feature/pom.xml @@ -23,9 +23,10 @@ impl isolatedwebapp logging + pid + trace webapp webapps - pid diff --git a/feature/trace/pom.xml b/feature/trace/pom.xml new file mode 100644 index 000000000..e9e12687f --- /dev/null +++ b/feature/trace/pom.xml @@ -0,0 +1,49 @@ + + + + 4.0.0 + + + cloud.piranha.feature + project + 25.1.0-SNAPSHOT + + + piranha-feature-trace + jar + + Piranha - Feature - Trace + + + + cloud.piranha.core + piranha-core-impl + ${project.version} + compile + + + cloud.piranha.extension + piranha-extension-fileupload + ${project.version} + compile + true + + + cloud.piranha.feature + piranha-feature-impl + ${project.version} + compile + + + cloud.piranha.http + piranha-http-webapp + ${project.version} + compile + + + org.junit.jupiter + junit-jupiter-api + test + + + diff --git a/feature/trace/src/main/java/cloud/piranha/feature/trace/TraceFeature.java b/feature/trace/src/main/java/cloud/piranha/feature/trace/TraceFeature.java new file mode 100644 index 000000000..717861ea7 --- /dev/null +++ b/feature/trace/src/main/java/cloud/piranha/feature/trace/TraceFeature.java @@ -0,0 +1,89 @@ +/* + * Copyright (c) 2002-2025 Manorrock.com. All Rights Reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, + * this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. Neither the name of the copyright holder nor the names of its + * contributors may be used to endorse or promote products derived from + * this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE + * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF + * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN + * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE + * POSSIBILITY OF SUCH DAMAGE. + */ +package cloud.piranha.feature.trace; + +import cloud.piranha.core.impl.DefaultFilterChain; +import cloud.piranha.core.impl.DefaultInvocationFinder; +import cloud.piranha.core.impl.DefaultServletRequestManager; +import cloud.piranha.core.impl.DefaultWebApplication; +import cloud.piranha.core.impl.DefaultWebApplicationInputStream; +import cloud.piranha.core.impl.DefaultWebApplicationOutputStream; +import cloud.piranha.extension.fileupload.FileUploadMultiPartManager; +import cloud.piranha.feature.impl.DefaultFeature; +import cloud.piranha.http.webapp.HttpWebApplicationServer; +import cloud.piranha.http.webapp.HttpWebApplicationServerRequestMapper; +import static java.util.logging.Level.FINEST; +import java.util.logging.Logger; + +/** + * The Trace feature. + * + * @author Manfred Riem (mriem@manorrock.com) + */ +public class TraceFeature extends DefaultFeature { + + @Override + public void init() { + boolean enabled = Boolean.parseBoolean("CLOUD_PIRANHA_FEATURE_TRACE_ENABLED"); + if (!enabled) { + enabled = Boolean.parseBoolean( + System.getProperty("cloud.piranha.feature.trace.enabled", "false")); + } + if (enabled) { + Logger logger = Logger.getLogger(DefaultFilterChain.class.getName()); + logger.setLevel(FINEST); + logger = Logger.getLogger(DefaultInvocationFinder.class.getName()); + logger.setLevel(FINEST); + logger = Logger.getLogger(DefaultServletRequestManager.class.getName()); + logger.setLevel(FINEST); + logger = Logger.getLogger(DefaultWebApplication.class.getName()); + logger.setLevel(FINEST); + logger = Logger.getLogger(DefaultWebApplication.class.getName()); + logger.setLevel(FINEST); + logger = Logger.getLogger(DefaultWebApplicationInputStream.class.getName()); + logger.setLevel(FINEST); + logger = Logger.getLogger(DefaultWebApplicationOutputStream.class.getName()); + logger.setLevel(FINEST); + logger = Logger.getLogger(HttpWebApplicationServer.class.getName()); + logger.setLevel(FINEST); + logger = Logger.getLogger(HttpWebApplicationServerRequestMapper.class.getName()); + logger.setLevel(FINEST); + + /* + * The following uses a try / catch as the extension is optionally + * available depending on the runtime. + */ + try { + logger = Logger.getLogger(FileUploadMultiPartManager.class.getName()); + logger.setLevel(FINEST); + } catch(Throwable t) { + // swallow up + } + } + } +} diff --git a/feature/trace/src/main/java/module-info.java b/feature/trace/src/main/java/module-info.java new file mode 100644 index 000000000..6f6ad02bb --- /dev/null +++ b/feature/trace/src/main/java/module-info.java @@ -0,0 +1,43 @@ +/* + * Copyright (c) 2002-2025 Manorrock.com. All Rights Reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, + * this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. Neither the name of the copyright holder nor the names of its + * contributors may be used to endorse or promote products derived from + * this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE + * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF + * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN + * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE + * POSSIBILITY OF SUCH DAMAGE. + */ + +/** + * This module delivers the TraceFeature. + * + * @author Manfred Riem (mriem@manorrock.com) + */ +module cloud.piranha.feature.trace { + + exports cloud.piranha.feature.trace; + opens cloud.piranha.feature.trace; + requires transitive cloud.piranha.core.impl; + requires static cloud.piranha.extension.fileupload; + requires transitive cloud.piranha.feature.impl; + requires transitive cloud.piranha.http.webapp; + requires transitive java.logging; +} diff --git a/feature/trace/src/test/java/cloud/piranha/feature/trace/tests/TraceFeatureTest.java b/feature/trace/src/test/java/cloud/piranha/feature/trace/tests/TraceFeatureTest.java new file mode 100644 index 000000000..d5e88c9cb --- /dev/null +++ b/feature/trace/src/test/java/cloud/piranha/feature/trace/tests/TraceFeatureTest.java @@ -0,0 +1,52 @@ +/* + * Copyright (c) 2002-2025 Manorrock.com. All Rights Reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, + * this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. Neither the name of the copyright holder nor the names of its + * contributors may be used to endorse or promote products derived from + * this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE + * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF + * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN + * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE + * POSSIBILITY OF SUCH DAMAGE. + */ +package cloud.piranha.feature.trace.tests; + +import cloud.piranha.core.impl.DefaultFilterChain; +import cloud.piranha.feature.trace.TraceFeature; +import java.lang.System.Logger; +import static java.lang.System.Logger.Level.TRACE; +import static org.junit.jupiter.api.Assertions.assertTrue; +import org.junit.jupiter.api.Test; + +/** + * The JUnit tests for the TraceFeature class. + * + * @author Manfred Riem (mriem@manorrock.com) + */ +class TraceFeatureTest { + + /** + * Test init method + */ + @Test + void testInit() { + TraceFeature feature = new TraceFeature(); + feature.init(); + } +} diff --git a/feature/trace/src/test/java/module-info.java b/feature/trace/src/test/java/module-info.java new file mode 100644 index 000000000..8884f13c4 --- /dev/null +++ b/feature/trace/src/test/java/module-info.java @@ -0,0 +1,40 @@ +/* + * Copyright (c) 2002-2025 Manorrock.com. All Rights Reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, + * this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. Neither the name of the copyright holder nor the names of its + * contributors may be used to endorse or promote products derived from + * this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE + * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF + * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN + * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE + * POSSIBILITY OF SUCH DAMAGE. + */ + +/** + * This module delivers the tests for the TraceFeature. + * + * @author Manfred Riem (mriem@manorrock.com) + */ +module cloud.piranha.feature.trace.tests { + + exports cloud.piranha.feature.trace.tests; + opens cloud.piranha.feature.trace.tests; + requires cloud.piranha.feature.trace; + requires org.junit.jupiter.api; +}