|
6 | 6 | package io.opentelemetry.instrumentation.logback.appender.v1_0; |
7 | 7 |
|
8 | 8 | import static io.opentelemetry.instrumentation.testing.junit.code.SemconvCodeStabilityUtil.codeAttributesLogCount; |
9 | | -import static io.opentelemetry.instrumentation.testing.junit.code.SemconvCodeStabilityUtil.codeFileAndLineAssertions; |
10 | | -import static io.opentelemetry.instrumentation.testing.junit.code.SemconvCodeStabilityUtil.codeFunctionAssertions; |
11 | | -import static io.opentelemetry.sdk.testing.assertj.OpenTelemetryAssertions.assertThat; |
12 | 9 | import static io.opentelemetry.sdk.testing.assertj.OpenTelemetryAssertions.equalTo; |
13 | | -import static io.opentelemetry.sdk.testing.assertj.OpenTelemetryAssertions.satisfies; |
14 | | -import static io.opentelemetry.semconv.ExceptionAttributes.EXCEPTION_MESSAGE; |
15 | | -import static io.opentelemetry.semconv.ExceptionAttributes.EXCEPTION_STACKTRACE; |
16 | | -import static io.opentelemetry.semconv.ExceptionAttributes.EXCEPTION_TYPE; |
17 | 10 |
|
18 | 11 | import ch.qos.logback.classic.LoggerContext; |
19 | 12 | import ch.qos.logback.core.ContextBase; |
20 | 13 | import io.opentelemetry.api.common.AttributeKey; |
21 | | -import io.opentelemetry.api.logs.Severity; |
22 | 14 | import io.opentelemetry.instrumentation.testing.junit.InstrumentationExtension; |
23 | 15 | import io.opentelemetry.sdk.common.InstrumentationScopeInfo; |
24 | | -import io.opentelemetry.sdk.logs.data.LogRecordData; |
25 | 16 | import io.opentelemetry.sdk.resources.Resource; |
26 | | -import io.opentelemetry.sdk.testing.assertj.AttributeAssertion; |
27 | 17 | import java.lang.reflect.Field; |
28 | 18 | import java.lang.reflect.Method; |
29 | | -import java.time.Instant; |
30 | | -import java.util.Collections; |
31 | | -import java.util.List; |
32 | 19 | import java.util.Map; |
33 | | -import java.util.concurrent.TimeUnit; |
34 | | -import org.assertj.core.api.AssertAccess; |
35 | 20 | import org.junit.jupiter.api.BeforeAll; |
36 | 21 | import org.junit.jupiter.api.Test; |
37 | 22 | import org.slf4j.Logger; |
38 | 23 | import org.slf4j.LoggerFactory; |
39 | | -import org.slf4j.MDC; |
40 | | -import org.slf4j.Marker; |
41 | | -import org.slf4j.MarkerFactory; |
42 | 24 |
|
43 | 25 | abstract class AbstractOpenTelemetryAppenderTest { |
44 | 26 |
|
@@ -80,91 +62,6 @@ static void resetLoggerContext() { |
80 | 62 |
|
81 | 63 | protected abstract InstrumentationExtension getTesting(); |
82 | 64 |
|
83 | | - @Test |
84 | | - void logNoSpan() { |
85 | | - logger.info("log message 1"); |
86 | | - |
87 | | - executeAfterLogsExecution(); |
88 | | - |
89 | | - getTesting() |
90 | | - .waitAndAssertLogRecords( |
91 | | - logRecord -> |
92 | | - logRecord |
93 | | - .hasResource(resource) |
94 | | - .hasInstrumentationScope(instrumentationScopeInfo) |
95 | | - .hasBody("log message 1") |
96 | | - .hasTotalAttributeCount(codeAttributesLogCount())); |
97 | | - } |
98 | | - |
99 | | - @Test |
100 | | - void logWithExtras() { |
101 | | - Instant start = Instant.now(); |
102 | | - String markerName = "aMarker"; |
103 | | - Marker marker = MarkerFactory.getMarker(markerName); |
104 | | - logger.info(marker, "log message 1", new IllegalStateException("Error!")); |
105 | | - |
106 | | - executeAfterLogsExecution(); |
107 | | - |
108 | | - List<AttributeAssertion> assertions = |
109 | | - codeFunctionAssertions(AbstractOpenTelemetryAppenderTest.class, "logWithExtras"); |
110 | | - assertions.addAll( |
111 | | - codeFileAndLineAssertions( |
112 | | - AbstractOpenTelemetryAppenderTest.class.getSimpleName() + ".java")); |
113 | | - assertions.add(equalTo(EXCEPTION_TYPE, IllegalStateException.class.getName())); |
114 | | - assertions.add(equalTo(EXCEPTION_MESSAGE, "Error!")); |
115 | | - assertions.add( |
116 | | - satisfies(EXCEPTION_STACKTRACE, stackTrace -> stackTrace.contains("logWithExtras"))); |
117 | | - assertions.add( |
118 | | - equalTo( |
119 | | - AttributeKey.stringArrayKey("logback.marker"), Collections.singletonList(markerName))); |
120 | | - |
121 | | - Instant now = Instant.now(); |
122 | | - getTesting() |
123 | | - .waitAndAssertLogRecords( |
124 | | - logRecord -> { |
125 | | - logRecord |
126 | | - .hasResource(resource) |
127 | | - .hasInstrumentationScope(instrumentationScopeInfo) |
128 | | - .hasBody("log message 1") |
129 | | - .hasSeverity(Severity.INFO) |
130 | | - .hasSeverityText("INFO") |
131 | | - .hasAttributesSatisfyingExactly(assertions); |
132 | | - |
133 | | - LogRecordData logRecordData = AssertAccess.getActual(logRecord); |
134 | | - assertThat(logRecordData.getTimestampEpochNanos()) |
135 | | - .isGreaterThanOrEqualTo(TimeUnit.MILLISECONDS.toNanos(start.toEpochMilli())) |
136 | | - .isLessThanOrEqualTo( |
137 | | - TimeUnit.SECONDS.toNanos(now.getEpochSecond()) + now.getNano()); |
138 | | - }); |
139 | | - } |
140 | | - |
141 | | - @Test |
142 | | - void logContextData() { |
143 | | - MDC.put("key1", "val1"); |
144 | | - MDC.put("key2", "val2"); |
145 | | - MDC.put("event.name", "MyEventName"); |
146 | | - try { |
147 | | - logger.info("log message 1"); |
148 | | - } finally { |
149 | | - MDC.clear(); |
150 | | - } |
151 | | - |
152 | | - executeAfterLogsExecution(); |
153 | | - |
154 | | - getTesting() |
155 | | - .waitAndAssertLogRecords( |
156 | | - logRecord -> |
157 | | - logRecord |
158 | | - .hasResource(resource) |
159 | | - .hasInstrumentationScope(instrumentationScopeInfo) |
160 | | - .hasBody("log message 1") |
161 | | - .hasTotalAttributeCount(2 + codeAttributesLogCount()) // code attributes |
162 | | - .hasEventName("MyEventName") |
163 | | - .hasAttributesSatisfying( |
164 | | - equalTo(AttributeKey.stringKey("key1"), "val1"), |
165 | | - equalTo(AttributeKey.stringKey("key2"), "val2"))); |
166 | | - } |
167 | | - |
168 | 65 | @Test |
169 | 66 | void logLoggerContext() { |
170 | 67 | LoggerContext loggerContext = (LoggerContext) LoggerFactory.getILoggerFactory(); |
|
0 commit comments