Skip to content

Commit f96c857

Browse files
log4j2 and otel trace config changes
1 parent 90b0202 commit f96c857

9 files changed

Lines changed: 82 additions & 204 deletions

File tree

‎api/pom.xml‎

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,15 @@
8383
</dependencies>
8484
<build>
8585
<plugins>
86+
<plugin>
87+
<groupId>org.apache.maven.plugins</groupId>
88+
<artifactId>maven-surefire-plugin</artifactId>
89+
<configuration>
90+
<systemPropertyVariables>
91+
<log4j2.isThreadContextMapInheritable>true</log4j2.isThreadContextMapInheritable>
92+
</systemPropertyVariables>
93+
</configuration>
94+
</plugin>
8695
<plugin>
8796
<groupId>org.apache.maven.plugins</groupId>
8897
<artifactId>maven-jar-plugin</artifactId>

‎api/src/main/java/org/apache/cloudstack/context/TraceContextMdcWrapper.java‎

Lines changed: 0 additions & 87 deletions
This file was deleted.
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
// Licensed to the Apache Software Foundation (ASF) under one
2+
// or more contributor license agreements. See the NOTICE file
3+
// distributed with this work for additional information
4+
// regarding copyright ownership. The ASF licenses this file
5+
// to you under the Apache License, Version 2.0 (the
6+
// "License"); you may not use this file except in compliance
7+
// with the License. You may obtain a copy of the License at
8+
//
9+
// http://www.apache.org/licenses/LICENSE-2.0
10+
//
11+
// Unless required by applicable law or agreed to in writing,
12+
// software distributed under the License is distributed on an
13+
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14+
// KIND, either express or implied. See the License for the
15+
// specific language governing permissions and limitations
16+
// under the License.
17+
package org.apache.cloudstack.context;
18+
19+
import java.util.concurrent.atomic.AtomicReference;
20+
21+
import org.apache.logging.log4j.ThreadContext;
22+
import org.junit.After;
23+
import org.junit.Assert;
24+
import org.junit.Test;
25+
26+
/**
27+
* Log4j 1.x backed the MDC with an InheritableThreadLocal, so a thread spawned while an API
28+
* request was being served saw the request's trace id for free. Log4j2 uses a plain ThreadLocal
29+
* unless log4j2.isThreadContextMapInheritable is set, and the ids would silently vanish from
30+
* every thread a request spawns. The management server sets the flag in JAVA_OPTS
31+
* (packaging/systemd/cloudstack-management.default) and surefire sets it for this module; this
32+
* test fails if either is dropped.
33+
*/
34+
public class ThreadContextInheritanceTest {
35+
36+
private static final String TRACE_ID = "trace-from-parent";
37+
38+
@After
39+
public void tearDown() {
40+
ThreadContext.clearMap();
41+
}
42+
43+
@Test
44+
public void childThreadInheritsContextOfSpawningThread() throws InterruptedException {
45+
ThreadContext.put(LogContext.TRACEID_KEY, TRACE_ID);
46+
47+
AtomicReference<String> seenByChild = new AtomicReference<>();
48+
Thread child = new Thread(() -> seenByChild.set(ThreadContext.get(LogContext.TRACEID_KEY)));
49+
child.start();
50+
child.join();
51+
52+
Assert.assertEquals(TRACE_ID, seenByChild.get());
53+
}
54+
55+
@Test
56+
public void childThreadDoesNotLeakContextBackToParent() throws InterruptedException {
57+
Thread child = new Thread(() -> ThreadContext.put(LogContext.TRACEID_KEY, "trace-from-child"));
58+
child.start();
59+
child.join();
60+
61+
Assert.assertNull(ThreadContext.get(LogContext.TRACEID_KEY));
62+
}
63+
}

‎api/src/test/java/org/apache/cloudstack/context/TraceContextMdcWrapperTest.java‎

Lines changed: 0 additions & 96 deletions
This file was deleted.

‎client/conf/log4j-cloud.xml.in‎

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ under the License.
3434
<Policies>
3535
<TimeBasedTriggeringPolicy/>
3636
</Policies>
37-
<PatternLayout pattern="%d{DEFAULT} %-5p [%c{1.}] (%t:%x) (logid:%X{logcontextid}) (traceid:%X{traceid}) %m%ex{filters(${filters})}%n"/>
37+
<PatternLayout pattern="%d{DEFAULT} %-5p [%c{1.}] (%t:%x) (logid:%X{logcontextid}) (traceid:%X{traceid}) (trace_id:%X{trace_id} span_id:%X{span_id}) %m%ex{filters(${filters})}%n"/>
3838
</RollingFile>
3939

4040

@@ -43,7 +43,7 @@ under the License.
4343
<Policies>
4444
<TimeBasedTriggeringPolicy/>
4545
</Policies>
46-
<PatternLayout pattern="%d{DEFAULT} %-5p [%c{1.}] (%t:%x) (logid:%X{logcontextid}) (traceid:%X{traceid}) %m%ex{filters(${filters})}%n"/>
46+
<PatternLayout pattern="%d{DEFAULT} %-5p [%c{1.}] (%t:%x) (logid:%X{logcontextid}) (traceid:%X{traceid}) (trace_id:%X{trace_id} span_id:%X{span_id}) %m%ex{filters(${filters})}%n"/>
4747
</RollingFile>
4848

4949
<!-- ============================== -->
@@ -52,7 +52,7 @@ under the License.
5252

5353
<Syslog name="SYSLOG" host="localhost" facility="LOCAL6">
5454
<ThresholdFilter level="WARN" onMatch="ACCEPT" onMismatch="DENY"/>
55-
<PatternLayout pattern="%d{DEFAULT} %-5p [%c{1.}] (%t:%x) (logid:%X{logcontextid}) (traceid:%X{traceid}) %m%ex{filters(${filters})}%n"/>
55+
<PatternLayout pattern="%d{DEFAULT} %-5p [%c{1.}] (%t:%x) (logid:%X{logcontextid}) (traceid:%X{traceid}) (trace_id:%X{trace_id} span_id:%X{span_id}) %m%ex{filters(${filters})}%n"/>
5656
</Syslog>
5757

5858
<!-- ============================== -->
@@ -61,7 +61,7 @@ under the License.
6161

6262
<AlertSyslogAppender name="ALERTSYSLOG" syslogHosts="" facility="LOCAL6">
6363
<ThresholdFilter level="WARN" onMatch="ACCEPT" onMismatch="DENY"/>
64-
<PatternLayout pattern="%d{DEFAULT} %-5p [%c{1.}] (%t:%x) (logid:%X{logcontextid}) (traceid:%X{traceid}) %m%ex{filters(${filters})}%n"/>
64+
<PatternLayout pattern="%d{DEFAULT} %-5p [%c{1.}] (%t:%x) (logid:%X{logcontextid}) (traceid:%X{traceid}) (trace_id:%X{trace_id} span_id:%X{span_id}) %m%ex{filters(${filters})}%n"/>
6565
</AlertSyslogAppender>
6666

6767
<!-- ============================== -->
@@ -70,7 +70,7 @@ under the License.
7070

7171
<Console name="CONSOLE" target="SYSTEM_OUT">
7272
<ThresholdFilter level="OFF" onMatch="ACCEPT" onMismatch="DENY"/>
73-
<PatternLayout pattern="%-5p [%c{1.}] (%t:%x) (logid:%X{logcontextid}) (traceid:%X{traceid}) %m%ex{filters(${filters})}%n"/>
73+
<PatternLayout pattern="%-5p [%c{1.}] (%t:%x) (logid:%X{logcontextid}) (traceid:%X{traceid}) (trace_id:%X{trace_id} span_id:%X{span_id}) %m%ex{filters(${filters})}%n"/>
7474
</Console>
7575

7676
<!-- ============================== -->

‎client/pom.xml‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -750,6 +750,10 @@
750750
<name>log4j2.configurationFile</name>
751751
<value>log4j-cloud.xml</value>
752752
</systemProperty>
753+
<systemProperty>
754+
<name>log4j2.isThreadContextMapInheritable</name>
755+
<value>true</value>
756+
</systemProperty>
753757
</systemProperties>
754758
</configuration>
755759
</plugin>

‎client/src/main/java/org/apache/cloudstack/ServerDaemon.java‎

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -55,8 +55,6 @@
5555
import com.cloud.utils.PropertiesUtil;
5656
import com.cloud.utils.server.ServerProperties;
5757

58-
import org.apache.cloudstack.context.TraceContextMdcWrapper;
59-
6058
/***
6159
* The ServerDaemon class implements the embedded server, it can be started either
6260
* using JSVC or directly from the JAR along with additional jars not shaded in the uber-jar.
@@ -110,9 +108,6 @@ public class ServerDaemon implements Daemon {
110108
//////////////////////////////////////////////////
111109

112110
public static void main(final String... anArgs) throws Exception {
113-
// Install the trace-context to MDC hook before the server starts, so every
114-
// thread with an active OpenTelemetry span carries the trace id in its logs.
115-
TraceContextMdcWrapper.register();
116111
final ServerDaemon daemon = new ServerDaemon();
117112
daemon.init(null);
118113
daemon.start();

‎client/src/main/webapp/WEB-INF/web.xml‎

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -64,16 +64,6 @@
6464
<load-on-startup>6</load-on-startup>
6565
</servlet>
6666

67-
<filter>
68-
<filter-name>apiTraceFilter</filter-name>
69-
<filter-class>org.apache.cloudstack.api.filter.ApiTraceFilter</filter-class>
70-
</filter>
71-
72-
<filter-mapping>
73-
<filter-name>apiTraceFilter</filter-name>
74-
<url-pattern>/api/*</url-pattern>
75-
</filter-mapping>
76-
7767
<servlet-mapping>
7868
<servlet-name>apiServlet</servlet-name>
7969
<url-pattern>/api/*</url-pattern>

‎server/conf/log4j-cloud.xml.in‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ under the License.
4040
<Policies>
4141
<TimeBasedTriggeringPolicy/>
4242
</Policies>
43-
<PatternLayout pattern="%d{DEFAULT} %-5p [%c{1.}] (%t:%x) (logid:%X{logcontextid}) (traceid:%X{traceid}) %m%ex%n"/>
43+
<PatternLayout pattern="%d{DEFAULT} %-5p [%c{1.}] (%t:%x) (logid:%X{logcontextid}) (traceid:%X{traceid}) (trace_id:%X{trace_id} span_id:%X{span_id}) %m%ex%n"/>
4444
</RollingFile>
4545

4646

0 commit comments

Comments
 (0)