|
10 | 10 | import io.opentelemetry.api.trace.Span; |
11 | 11 | import io.opentelemetry.instrumentation.testing.junit.AgentInstrumentationExtension; |
12 | 12 | import io.opentelemetry.instrumentation.testing.junit.InstrumentationExtension; |
13 | | -import java.lang.reflect.InvocationTargetException; |
14 | 13 | import java.lang.reflect.Method; |
15 | 14 | import java.util.LinkedList; |
16 | 15 | import java.util.List; |
@@ -50,88 +49,98 @@ public void close() {} |
50 | 49 |
|
51 | 50 | @Test |
52 | 51 | void noIdsGeneratedWhenNoSpanProvided() { |
53 | | - Logger logger = LogContext.getLogContext().getLogger("TestLogger"); |
| 52 | + Logger logger = LogContext.getLogContext().getLogger("noIdsGeneratedWhenNoSpanProvided"); |
54 | 53 | LinkedList<ExtLogRecord> logRecords = new LinkedList<>(); |
| 54 | + LogHandler handler = new LogHandler(logRecords); |
55 | 55 |
|
56 | 56 | logger.setLevel(Level.INFO); |
57 | | - logger.addHandler(new LogHandler(logRecords)); |
58 | | - |
59 | | - logger.info("log message 1"); |
60 | | - |
61 | | - assertThat(logRecords).hasSize(1); |
62 | | - assertThat(logRecords.get(0).getMessage()).isEqualTo("log message 1"); |
63 | | - assertThat(logRecords.get(0).getMdc("trace_id")).isNull(); |
64 | | - assertThat(logRecords.get(0).getMdc("span_id")).isNull(); |
65 | | - assertThat(logRecords.get(0).getMdc("trace_flags")).isNull(); |
| 57 | + logger.addHandler(handler); |
| 58 | + try { |
| 59 | + logger.info("log message 1"); |
| 60 | + |
| 61 | + assertThat(logRecords).hasSize(1); |
| 62 | + assertThat(logRecords.get(0).getMessage()).isEqualTo("log message 1"); |
| 63 | + assertThat(logRecords.get(0).getMdc("trace_id")).isNull(); |
| 64 | + assertThat(logRecords.get(0).getMdc("span_id")).isNull(); |
| 65 | + assertThat(logRecords.get(0).getMdc("trace_flags")).isNull(); |
| 66 | + } finally { |
| 67 | + logger.removeHandler(handler); |
| 68 | + } |
66 | 69 | } |
67 | 70 |
|
68 | 71 | @Test |
69 | | - void idsGeneratedWhenSpanProvided() throws InvocationTargetException, IllegalAccessException { |
70 | | - Logger logger = LogContext.getLogContext().getLogger("TestLogger"); |
| 72 | + void idsGeneratedWhenSpanProvided() throws ReflectiveOperationException { |
| 73 | + Logger logger = LogContext.getLogContext().getLogger("idsGeneratedWhenSpanProvided"); |
71 | 74 | logger.setLevel(Level.DEBUG); |
72 | 75 | LinkedList<ExtLogRecord> logRecords = new LinkedList<>(); |
73 | | - logger.addHandler(new LogHandler(logRecords)); |
74 | | - |
75 | | - Span span1 = |
76 | | - testing.runWithSpan( |
77 | | - "test 1", |
78 | | - () -> { |
79 | | - logger.info("log message 1"); |
80 | | - return Span.current(); |
81 | | - }); |
82 | | - |
83 | | - logger.info("log message 2"); |
84 | | - |
85 | | - Span span2 = |
86 | | - testing.runWithSpan( |
87 | | - "test 2", |
88 | | - () -> { |
89 | | - logger.info("log message 3"); |
90 | | - return Span.current(); |
91 | | - }); |
92 | | - |
93 | | - assertThat(logRecords.size()).isEqualTo(3); |
94 | | - |
95 | | - Method getMdcCopy = null; |
| 76 | + LogHandler handler = new LogHandler(logRecords); |
| 77 | + logger.addHandler(handler); |
96 | 78 | try { |
97 | | - getMdcCopy = logRecords.get(0).getClass().getMethod("getMdcCopy"); |
98 | | - } catch (NoSuchMethodException ignored) { |
99 | | - // ignored |
100 | | - } |
101 | | - |
102 | | - assertThat(logRecords.get(0).getMessage()).isEqualTo("log message 1"); |
103 | | - assertThat(logRecords.get(0).getMdc("trace_id")).isEqualTo(span1.getSpanContext().getTraceId()); |
104 | | - assertThat(logRecords.get(0).getMdc("span_id")).isEqualTo(span1.getSpanContext().getSpanId()); |
105 | | - assertThat(logRecords.get(0).getMdc("trace_flags")) |
106 | | - .isEqualTo(span1.getSpanContext().getTraceFlags().asHex()); |
107 | | - |
108 | | - if (getMdcCopy != null) { |
109 | | - @SuppressWarnings("unchecked") |
110 | | - Map<String, String> copiedMdc = (Map<String, String>) getMdcCopy.invoke(logRecords.get(0)); |
111 | | - assertThat(copiedMdc.get("trace_id")).isEqualTo(span1.getSpanContext().getTraceId()); |
112 | | - assertThat(copiedMdc.get("span_id")).isEqualTo(span1.getSpanContext().getSpanId()); |
113 | | - assertThat(copiedMdc.get("trace_flags")) |
| 79 | + Span span1 = |
| 80 | + testing.runWithSpan( |
| 81 | + "test 1", |
| 82 | + () -> { |
| 83 | + logger.info("log message 1"); |
| 84 | + return Span.current(); |
| 85 | + }); |
| 86 | + |
| 87 | + logger.info("log message 2"); |
| 88 | + |
| 89 | + Span span2 = |
| 90 | + testing.runWithSpan( |
| 91 | + "test 2", |
| 92 | + () -> { |
| 93 | + logger.info("log message 3"); |
| 94 | + return Span.current(); |
| 95 | + }); |
| 96 | + |
| 97 | + assertThat(logRecords).hasSize(3); |
| 98 | + |
| 99 | + Method getMdcCopy = null; |
| 100 | + try { |
| 101 | + getMdcCopy = logRecords.get(0).getClass().getMethod("getMdcCopy"); |
| 102 | + } catch (NoSuchMethodException ignored) { |
| 103 | + // ignored |
| 104 | + } |
| 105 | + |
| 106 | + assertThat(logRecords.get(0).getMessage()).isEqualTo("log message 1"); |
| 107 | + assertThat(logRecords.get(0).getMdc("trace_id")) |
| 108 | + .isEqualTo(span1.getSpanContext().getTraceId()); |
| 109 | + assertThat(logRecords.get(0).getMdc("span_id")).isEqualTo(span1.getSpanContext().getSpanId()); |
| 110 | + assertThat(logRecords.get(0).getMdc("trace_flags")) |
114 | 111 | .isEqualTo(span1.getSpanContext().getTraceFlags().asHex()); |
115 | | - } |
116 | 112 |
|
117 | | - assertThat(logRecords.get(1).getMessage()).isEqualTo("log message 2"); |
118 | | - assertThat(logRecords.get(1).getMdc("trace_id")).isNull(); |
119 | | - assertThat(logRecords.get(1).getMdc("span_id")).isNull(); |
120 | | - assertThat(logRecords.get(1).getMdc("trace_flags")).isNull(); |
121 | | - |
122 | | - assertThat(logRecords.get(2).getMessage()).isEqualTo("log message 3"); |
123 | | - assertThat(logRecords.get(2).getMdc("trace_id")).isEqualTo(span2.getSpanContext().getTraceId()); |
124 | | - assertThat(logRecords.get(2).getMdc("span_id")).isEqualTo(span2.getSpanContext().getSpanId()); |
125 | | - assertThat(logRecords.get(2).getMdc("trace_flags")) |
126 | | - .isEqualTo(span2.getSpanContext().getTraceFlags().asHex()); |
127 | | - |
128 | | - if (getMdcCopy != null) { |
129 | | - @SuppressWarnings("unchecked") |
130 | | - Map<String, String> copiedMdc = (Map<String, String>) getMdcCopy.invoke(logRecords.get(2)); |
131 | | - assertThat(copiedMdc.get("trace_id")).isEqualTo(span2.getSpanContext().getTraceId()); |
132 | | - assertThat(copiedMdc.get("span_id")).isEqualTo(span2.getSpanContext().getSpanId()); |
133 | | - assertThat(copiedMdc.get("trace_flags")) |
| 113 | + if (getMdcCopy != null) { |
| 114 | + @SuppressWarnings("unchecked") |
| 115 | + Map<String, String> copiedMdc = (Map<String, String>) getMdcCopy.invoke(logRecords.get(0)); |
| 116 | + assertThat(copiedMdc.get("trace_id")).isEqualTo(span1.getSpanContext().getTraceId()); |
| 117 | + assertThat(copiedMdc.get("span_id")).isEqualTo(span1.getSpanContext().getSpanId()); |
| 118 | + assertThat(copiedMdc.get("trace_flags")) |
| 119 | + .isEqualTo(span1.getSpanContext().getTraceFlags().asHex()); |
| 120 | + } |
| 121 | + |
| 122 | + assertThat(logRecords.get(1).getMessage()).isEqualTo("log message 2"); |
| 123 | + assertThat(logRecords.get(1).getMdc("trace_id")).isNull(); |
| 124 | + assertThat(logRecords.get(1).getMdc("span_id")).isNull(); |
| 125 | + assertThat(logRecords.get(1).getMdc("trace_flags")).isNull(); |
| 126 | + |
| 127 | + assertThat(logRecords.get(2).getMessage()).isEqualTo("log message 3"); |
| 128 | + assertThat(logRecords.get(2).getMdc("trace_id")) |
| 129 | + .isEqualTo(span2.getSpanContext().getTraceId()); |
| 130 | + assertThat(logRecords.get(2).getMdc("span_id")).isEqualTo(span2.getSpanContext().getSpanId()); |
| 131 | + assertThat(logRecords.get(2).getMdc("trace_flags")) |
134 | 132 | .isEqualTo(span2.getSpanContext().getTraceFlags().asHex()); |
| 133 | + |
| 134 | + if (getMdcCopy != null) { |
| 135 | + @SuppressWarnings("unchecked") |
| 136 | + Map<String, String> copiedMdc = (Map<String, String>) getMdcCopy.invoke(logRecords.get(2)); |
| 137 | + assertThat(copiedMdc.get("trace_id")).isEqualTo(span2.getSpanContext().getTraceId()); |
| 138 | + assertThat(copiedMdc.get("span_id")).isEqualTo(span2.getSpanContext().getSpanId()); |
| 139 | + assertThat(copiedMdc.get("trace_flags")) |
| 140 | + .isEqualTo(span2.getSpanContext().getTraceFlags().asHex()); |
| 141 | + } |
| 142 | + } finally { |
| 143 | + logger.removeHandler(handler); |
135 | 144 | } |
136 | 145 | } |
137 | 146 | } |
0 commit comments