7
7
8
8
import static org .assertj .core .api .Assertions .assertThat ;
9
9
10
+ import ch .qos .logback .classic .spi .ILoggingEvent ;
11
+ import ch .qos .logback .core .read .ListAppender ;
12
+ import ch .qos .logback .core .spi .AppenderAttachable ;
10
13
import io .opentelemetry .api .OpenTelemetry ;
14
+ import io .opentelemetry .api .baggage .Baggage ;
11
15
import io .opentelemetry .api .common .AttributeKey ;
12
16
import io .opentelemetry .api .common .Attributes ;
17
+ import io .opentelemetry .api .trace .Span ;
18
+ import io .opentelemetry .context .Scope ;
13
19
import io .opentelemetry .instrumentation .logback .appender .v1_0 .OpenTelemetryAppender ;
14
20
import io .opentelemetry .instrumentation .testing .internal .AutoCleanupExtension ;
15
21
import io .opentelemetry .instrumentation .testing .junit .InstrumentationExtension ;
21
27
import org .junit .jupiter .api .BeforeEach ;
22
28
import org .junit .jupiter .api .Test ;
23
29
import org .junit .jupiter .api .extension .RegisterExtension ;
30
+ import org .slf4j .Logger ;
24
31
import org .slf4j .LoggerFactory ;
25
32
import org .slf4j .MDC ;
26
33
import org .springframework .boot .SpringApplication ;
@@ -66,6 +73,9 @@ void shouldInitializeAppender() {
66
73
ConfigurableApplicationContext context = app .run ();
67
74
cleanup .deferCleanup (context );
68
75
76
+ ListAppender <ILoggingEvent > listAppender = getListAppender ();
77
+ listAppender .list .clear ();
78
+
69
79
MDC .put ("key1" , "val1" );
70
80
MDC .put ("key2" , "val2" );
71
81
try {
@@ -91,6 +101,14 @@ void shouldInitializeAppender() {
91
101
.containsEntry (AttributeKey .stringKey ("key1" ), "val1" )
92
102
.containsEntry (AttributeKey .stringKey ("key2" ), "val2" );
93
103
});
104
+
105
+ assertThat (listAppender .list )
106
+ .satisfiesExactly (
107
+ event ->
108
+ assertThat (event )
109
+ .satisfies (
110
+ e -> assertThat (e .getMessage ()).isEqualTo ("test log message" ),
111
+ e -> assertThat (e .getMDCPropertyMap ()).containsOnlyKeys ("key1" , "key2" )));
94
112
}
95
113
96
114
@ Test
@@ -110,4 +128,123 @@ void shouldNotInitializeAppenderWhenDisabled() {
110
128
111
129
assertThat (testing .logRecords ()).isEmpty ();
112
130
}
131
+
132
+ @ Test
133
+ void mdcAppender () {
134
+ Map <String , Object > properties = new HashMap <>();
135
+ properties .put ("logging.config" , "classpath:logback-test.xml" );
136
+ properties .put ("otel.instrumentation.logback-appender.enabled" , "false" );
137
+ properties .put ("otel.instrumentation.logback-mdc.add-baggage" , "true" );
138
+ properties .put ("otel.instrumentation.common.logging.trace-id" , "traceid" );
139
+ properties .put ("otel.instrumentation.common.logging.span-id" , "spanid" );
140
+ properties .put ("otel.instrumentation.common.logging.trace-flags" , "traceflags" );
141
+
142
+ SpringApplication app =
143
+ new SpringApplication (
144
+ TestingOpenTelemetryConfiguration .class , OpenTelemetryAppenderAutoConfiguration .class );
145
+ app .setDefaultProperties (properties );
146
+ ConfigurableApplicationContext context = app .run ();
147
+ cleanup .deferCleanup (context );
148
+
149
+ ListAppender <ILoggingEvent > listAppender = getListAppender ();
150
+ listAppender .list .clear ();
151
+
152
+ try (Scope ignore = Baggage .current ().toBuilder ().put ("key" , "value" ).build ().makeCurrent ()) {
153
+ Span span = testing .getOpenTelemetry ().getTracer ("test" ).spanBuilder ("test" ).startSpan ();
154
+ try (Scope ignore2 = span .makeCurrent ()) {
155
+ LoggerFactory .getLogger ("test" ).info ("test log message" );
156
+ }
157
+ }
158
+
159
+ assertThat (testing .logRecords ()).isEmpty ();
160
+ assertThat (listAppender .list )
161
+ .satisfiesExactly (
162
+ event ->
163
+ assertThat (event )
164
+ .satisfies (
165
+ e -> assertThat (e .getMessage ()).isEqualTo ("test log message" ),
166
+ e ->
167
+ assertThat (e .getMDCPropertyMap ())
168
+ .containsOnlyKeys (
169
+ "traceid" , "spanid" , "traceflags" , "baggage.key" )));
170
+ }
171
+
172
+ @ Test
173
+ void shouldInitializeMdcAppender () {
174
+ Map <String , Object > properties = new HashMap <>();
175
+ properties .put ("logging.config" , "classpath:logback-no-otel-appenders.xml" );
176
+ properties .put ("otel.instrumentation.logback-appender.enabled" , "false" );
177
+
178
+ SpringApplication app =
179
+ new SpringApplication (
180
+ TestingOpenTelemetryConfiguration .class , OpenTelemetryAppenderAutoConfiguration .class );
181
+ app .setDefaultProperties (properties );
182
+ ConfigurableApplicationContext context = app .run ();
183
+ cleanup .deferCleanup (context );
184
+
185
+ ListAppender <ILoggingEvent > listAppender = getListAppender ();
186
+ listAppender .list .clear ();
187
+
188
+ Span span = testing .getOpenTelemetry ().getTracer ("test" ).spanBuilder ("test" ).startSpan ();
189
+ try (Scope ignore = span .makeCurrent ()) {
190
+ LoggerFactory .getLogger ("test" ).info ("test log message" );
191
+ }
192
+
193
+ assertThat (testing .logRecords ()).isEmpty ();
194
+ assertThat (listAppender .list )
195
+ .satisfiesExactly (
196
+ event ->
197
+ assertThat (event )
198
+ .satisfies (
199
+ e -> assertThat (e .getMessage ()).isEqualTo ("test log message" ),
200
+ e ->
201
+ assertThat (e .getMDCPropertyMap ())
202
+ .containsOnlyKeys ("trace_id" , "span_id" , "trace_flags" )));
203
+ }
204
+
205
+ @ Test
206
+ void shouldNotInitializeMdcAppenderWhenDisabled () {
207
+ Map <String , Object > properties = new HashMap <>();
208
+ properties .put ("logging.config" , "classpath:logback-no-otel-appenders.xml" );
209
+ properties .put ("otel.instrumentation.logback-appender.enabled" , "false" );
210
+ properties .put ("otel.instrumentation.logback-mdc.enabled" , "false" );
211
+
212
+ SpringApplication app =
213
+ new SpringApplication (
214
+ TestingOpenTelemetryConfiguration .class , OpenTelemetryAppenderAutoConfiguration .class );
215
+ app .setDefaultProperties (properties );
216
+ ConfigurableApplicationContext context = app .run ();
217
+ cleanup .deferCleanup (context );
218
+
219
+ ListAppender <ILoggingEvent > listAppender = getListAppender ();
220
+ listAppender .list .clear ();
221
+
222
+ Span span = testing .getOpenTelemetry ().getTracer ("test" ).spanBuilder ("test" ).startSpan ();
223
+ try (Scope ignore = span .makeCurrent ()) {
224
+ LoggerFactory .getLogger ("test" ).info ("test log message" );
225
+ }
226
+
227
+ assertThat (testing .logRecords ()).isEmpty ();
228
+ assertThat (listAppender .list )
229
+ .satisfiesExactly (
230
+ event ->
231
+ assertThat (event )
232
+ .satisfies (
233
+ e -> assertThat (e .getMessage ()).isEqualTo ("test log message" ),
234
+ e -> assertThat (e .getMDCPropertyMap ()).isEmpty ()));
235
+ }
236
+
237
+ @ SuppressWarnings ("unchecked" )
238
+ private static ListAppender <ILoggingEvent > getListAppender () {
239
+ Logger logger = LoggerFactory .getLogger (Logger .ROOT_LOGGER_NAME );
240
+ ch .qos .logback .classic .Logger logbackLogger = (ch .qos .logback .classic .Logger ) logger ;
241
+ ListAppender <ILoggingEvent > listAppender =
242
+ (ListAppender <ILoggingEvent >) logbackLogger .getAppender ("List" );
243
+ if (listAppender != null ) {
244
+ return listAppender ;
245
+ }
246
+ AppenderAttachable <?> mdcAppender =
247
+ (AppenderAttachable <?>) logbackLogger .getAppender ("OpenTelemetryMdc" );
248
+ return (ListAppender <ILoggingEvent >) mdcAppender .getAppender ("List" );
249
+ }
113
250
}
0 commit comments