Skip to content

Commit 941b36f

Browse files
committed
more
1 parent cf7643f commit 941b36f

File tree

3 files changed

+34
-83
lines changed

3 files changed

+34
-83
lines changed

instrumentation/logback/logback-appender-1.0/library/src/test/java/io/opentelemetry/instrumentation/logback/appender/v1_0/AbstractOpenTelemetryAppenderTest.java

Lines changed: 2 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -9,14 +9,10 @@
99
import static io.opentelemetry.sdk.testing.assertj.OpenTelemetryAssertions.equalTo;
1010

1111
import ch.qos.logback.classic.LoggerContext;
12-
import ch.qos.logback.core.ContextBase;
1312
import io.opentelemetry.api.common.AttributeKey;
1413
import io.opentelemetry.instrumentation.testing.junit.InstrumentationExtension;
1514
import io.opentelemetry.sdk.common.InstrumentationScopeInfo;
1615
import io.opentelemetry.sdk.resources.Resource;
17-
import java.lang.reflect.Field;
18-
import java.lang.reflect.Method;
19-
import java.util.Map;
2016
import org.junit.jupiter.api.BeforeAll;
2117
import org.junit.jupiter.api.Test;
2218
import org.slf4j.Logger;
@@ -36,28 +32,7 @@ static void setupAll() {
3632
resource = Resource.getDefault();
3733
instrumentationScopeInfo = InstrumentationScopeInfo.create("TestLogger");
3834
// by default LoggerContext contains HOSTNAME property we clear it to start with empty context
39-
resetLoggerContext();
40-
}
41-
42-
static void resetLoggerContext() {
43-
try {
44-
LoggerContext loggerContext = (LoggerContext) LoggerFactory.getILoggerFactory();
45-
Field field = ContextBase.class.getDeclaredField("propertyMap");
46-
field.setAccessible(true);
47-
Map<?, ?> map = (Map<?, ?>) field.get(loggerContext);
48-
map.clear();
49-
50-
Method method;
51-
try {
52-
method = LoggerContext.class.getDeclaredMethod("syncRemoteView");
53-
} catch (NoSuchMethodException noSuchMethodException) {
54-
method = LoggerContext.class.getDeclaredMethod("updateLoggerContextVO");
55-
}
56-
method.setAccessible(true);
57-
method.invoke(loggerContext);
58-
} catch (Exception exception) {
59-
throw new IllegalStateException("Failed to reset logger context", exception);
60-
}
35+
AbstractLogbackTest.resetLoggerContext();
6136
}
6237

6338
protected abstract InstrumentationExtension getTesting();
@@ -70,7 +45,7 @@ void logLoggerContext() {
7045
logger.info("log message 1");
7146
executeAfterLogsExecution();
7247
} finally {
73-
resetLoggerContext();
48+
AbstractLogbackTest.resetLoggerContext();
7449
}
7550

7651
getTesting()

instrumentation/logback/logback-appender-1.0/library/src/test/java/io/opentelemetry/instrumentation/logback/appender/v1_0/LogReplayOpenTelemetryAppenderTest.java

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ private static void reloadLoggerConfiguration() throws Exception {
5252
URL url = LogReplayOpenTelemetryAppenderTest.class.getResource("/logback-test.xml");
5353
ContextInitializer.class.getMethod("configureByResource", URL.class).invoke(ci, url);
5454
// by default LoggerContext contains HOSTNAME property we clear it to start with empty context
55-
resetLoggerContext();
55+
AbstractLogbackTest.resetLoggerContext();
5656
}
5757
}
5858

@@ -64,9 +64,8 @@ void executeAfterLogsExecution() {
6464
@Test
6565
void twoLogs() {
6666
logger.info("log message 1");
67-
logger.info(
68-
"log message 2"); // Won't be instrumented because cache size is 1 (see logback-test.xml
69-
// file)
67+
// The second call won't be captured because cache size is 1 (see logback-test.xml file)
68+
logger.info("log message 2");
7069

7170
OpenTelemetryAppender.install(testing.getOpenTelemetry());
7271

instrumentation/logback/logback-appender-1.0/testing/src/main/java/io/opentelemetry/instrumentation/logback/appender/v1_0/AbstractLogbackTest.java

Lines changed: 29 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -48,19 +48,13 @@ public abstract class AbstractLogbackTest {
4848

4949
protected abstract InstrumentationExtension testing();
5050

51-
/**
52-
* Override to install specific instrumentation before each test.
53-
*/
51+
/** Override to install specific instrumentation before each test. */
5452
protected void beforeEachTest() {}
5553

56-
/**
57-
* Override to clean up instrumentation after each test.
58-
*/
54+
/** Override to clean up instrumentation after each test. */
5955
protected void afterEachTest() {}
6056

61-
/**
62-
* Override to add instrumentation-specific thread attribute assertions.
63-
*/
57+
/** Override to add instrumentation-specific thread attribute assertions. */
6458
protected List<AttributeAssertion> threadAssertions() {
6559
return Collections.emptyList();
6660
}
@@ -94,22 +88,16 @@ private static Stream<Arguments> parameters() {
9488
private List<AttributeAssertion> baseAssertions(String methodName) {
9589
List<AttributeAssertion> assertions = new ArrayList<>(threadAssertions());
9690
assertions.addAll(codeFunctionAssertions(AbstractLogbackTest.class, methodName));
97-
assertions.addAll(codeFileAndLineAssertions(AbstractLogbackTest.class.getSimpleName() + ".java"));
91+
assertions.addAll(
92+
codeFileAndLineAssertions(AbstractLogbackTest.class.getSimpleName() + ".java"));
9893
return assertions;
9994
}
10095

10196
@ParameterizedTest
10297
@MethodSource("parameters")
10398
void logLevels(boolean logException, boolean withParent) throws InterruptedException {
10499
logAndAssert(
105-
ABC_LOGGER,
106-
Logger::debug,
107-
Logger::debug,
108-
logException,
109-
withParent,
110-
null,
111-
null,
112-
null);
100+
ABC_LOGGER, Logger::debug, Logger::debug, logException, withParent, null, null, null);
113101
testing().clearData();
114102
logAndAssert(
115103
ABC_LOGGER,
@@ -142,24 +130,10 @@ void logLevels(boolean logException, boolean withParent) throws InterruptedExcep
142130
"ERROR");
143131
testing().clearData();
144132
logAndAssert(
145-
DEF_LOGGER,
146-
Logger::debug,
147-
Logger::debug,
148-
logException,
149-
withParent,
150-
null,
151-
null,
152-
null);
133+
DEF_LOGGER, Logger::debug, Logger::debug, logException, withParent, null, null, null);
153134
testing().clearData();
154135
logAndAssert(
155-
DEF_LOGGER,
156-
Logger::info,
157-
Logger::info,
158-
logException,
159-
withParent,
160-
null,
161-
null,
162-
null);
136+
DEF_LOGGER, Logger::info, Logger::info, logException, withParent, null, null, null);
163137
testing().clearData();
164138
logAndAssert(
165139
DEF_LOGGER,
@@ -224,10 +198,13 @@ private void logAndAssert(
224198

225199
List<AttributeAssertion> attributeAsserts = baseAssertions("performLogging");
226200
if (logException) {
227-
attributeAsserts.add(equalTo(EXCEPTION_TYPE, IllegalStateException.class.getName()));
201+
attributeAsserts.add(
202+
equalTo(EXCEPTION_TYPE, IllegalStateException.class.getName()));
228203
attributeAsserts.add(equalTo(EXCEPTION_MESSAGE, "hello"));
229204
attributeAsserts.add(
230-
satisfies(EXCEPTION_STACKTRACE, value -> value.contains(AbstractLogbackTest.class.getName())));
205+
satisfies(
206+
EXCEPTION_STACKTRACE,
207+
value -> value.contains(AbstractLogbackTest.class.getName())));
231208
}
232209

233210
logRecord.hasAttributesSatisfyingExactly(attributeAsserts);
@@ -256,19 +233,19 @@ void logMdc() {
256233
assertions.add(equalTo(AttributeKey.stringKey("event.name"), "MyEventName"));
257234
}
258235

259-
testing()
260-
.waitAndAssertLogRecords(
261-
logRecord -> {
262-
logRecord
263-
.hasBody("xyz: 123")
264-
.hasInstrumentationScope(InstrumentationScopeInfo.builder("abc").build())
265-
.hasSeverity(Severity.INFO)
266-
.hasSeverityText("INFO")
267-
.hasAttributesSatisfyingExactly(assertions);
268-
if (expectEventName()) {
269-
logRecord.hasEventName("MyEventName");
270-
}
271-
});
236+
testing()
237+
.waitAndAssertLogRecords(
238+
logRecord -> {
239+
logRecord
240+
.hasBody("xyz: 123")
241+
.hasInstrumentationScope(InstrumentationScopeInfo.builder("abc").build())
242+
.hasSeverity(Severity.INFO)
243+
.hasSeverityText("INFO")
244+
.hasAttributesSatisfyingExactly(assertions);
245+
if (expectEventName()) {
246+
logRecord.hasEventName("MyEventName");
247+
}
248+
});
272249
}
273250

274251
@Test
@@ -281,10 +258,10 @@ void logMarker() {
281258
List<AttributeAssertion> assertions = baseAssertions("logMarker");
282259
assertions.add(
283260
equalTo(
284-
AttributeKey.stringArrayKey("logback.marker"),
285-
Collections.singletonList(markerName)));
261+
AttributeKey.stringArrayKey("logback.marker"), Collections.singletonList(markerName)));
286262

287-
testing().waitAndAssertLogRecords(logRecord -> logRecord.hasAttributesSatisfyingExactly(assertions));
263+
testing()
264+
.waitAndAssertLogRecords(logRecord -> logRecord.hasAttributesSatisfyingExactly(assertions));
288265
}
289266

290267
private static void performLogging(

0 commit comments

Comments
 (0)