1616// under the License.
1717package org .apache .cloudstack .context ;
1818
19+ import java .io .File ;
20+ import java .io .IOException ;
1921import java .util .ArrayList ;
2022import java .util .HashMap ;
2123import java .util .Map ;
24+ import java .util .Properties ;
2225import java .util .UUID ;
2326
27+ import com .cloud .utils .PropertiesUtil ;
28+ import com .cloud .utils .StringUtils ;
2429import org .apache .logging .log4j .Logger ;
2530import org .apache .logging .log4j .LogManager ;
2631
@@ -54,9 +59,43 @@ public class LogContext {
5459 private long userId ;
5560 private final Map <String , String > context = new HashMap <String , String >();
5661
57- public final static String X_B3_TRACEID_KEY = "traceid" ;
58- public final static String MOSAIC_TRACE_ID_KEY = "mosaic_trace_id" ;
59- public final static String MOSAIC_SPAN_ID_KEY = "mosaic_span_id" ;
62+ public final static String TRACEID_KEY = "traceid" ;
63+
64+ /**
65+ * MDC keys under which the active OpenTelemetry ids are published. The names are
66+ * deployment specific, so they are read from server.properties and fall back to a
67+ * neutral default when the property is absent or blank.
68+ */
69+ public final static String TRACE_ID_KEY_PROPERTY = "otel.trace.id.mdc.key" ;
70+ public final static String SPAN_ID_KEY_PROPERTY = "otel.span.id.mdc.key" ;
71+
72+ public final static String DEFAULT_TRACE_ID_KEY = "otel_trace_id" ;
73+ public final static String DEFAULT_SPAN_ID_KEY = "otel_span_id" ;
74+
75+ private final static Properties SERVER_PROPERTIES = loadServerProperties ();
76+
77+ public final static String TRACE_ID_KEY = traceKeyFromProperties ();
78+ public final static String SPAN_ID_KEY = spanKeyFromProperties ();
79+
80+ private static Properties loadServerProperties () {
81+ try {
82+ File file = PropertiesUtil .findConfigFile ("server.properties" );
83+ return file == null ? new Properties () : PropertiesUtil .loadFromFile (file );
84+ } catch (IOException e ) {
85+ LOGGER .warn ("Could not read server.properties, using the default MDC key names" , e );
86+ return new Properties ();
87+ }
88+ }
89+
90+ private static String traceKeyFromProperties () {
91+ String value = SERVER_PROPERTIES .getProperty (TRACE_ID_KEY_PROPERTY );
92+ return StringUtils .isBlank (value ) ? DEFAULT_TRACE_ID_KEY : value .trim ();
93+ }
94+
95+ private static String spanKeyFromProperties () {
96+ String value = SERVER_PROPERTIES .getProperty (SPAN_ID_KEY_PROPERTY );
97+ return StringUtils .isBlank (value ) ? DEFAULT_SPAN_ID_KEY : value .trim ();
98+ }
6099
61100 static EntityManager s_entityMgr ;
62101
@@ -83,12 +122,17 @@ protected LogContext(User user, Account account, String logContextId) {
83122
84123 public void putContextParameter (String key , String value ) {
85124 context .put (key , value );
86- MDC .put (key , value );
125+ ThreadContext .put (key , value );
126+ if (value == null ) {
127+ ThreadContext .remove (key );
128+ } else {
129+ ThreadContext .put (key , value );
130+ }
87131 }
88132
89133 public void removeContextParameter (String key ) {
90134 context .remove (key );
91- MDC .remove (key );
135+ ThreadContext .remove (key );
92136 }
93137
94138 public void removeContextParameters () {
0 commit comments