Skip to content

Commit dab3381

Browse files
authored
Convert logging instrumentation tests to Java (#7631)
currently based on #7632
1 parent f847f35 commit dab3381

File tree

11 files changed

+851
-649
lines changed

11 files changed

+851
-649
lines changed

instrumentation/java-util-logging/javaagent/src/test/groovy/JavaUtilLoggingTest.groovy

-103
This file was deleted.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,165 @@
1+
/*
2+
* Copyright The OpenTelemetry Authors
3+
* SPDX-License-Identifier: Apache-2.0
4+
*/
5+
6+
package io.opentelemetry.javaagent.instrumentation.jul;
7+
8+
import static io.opentelemetry.sdk.testing.assertj.LogAssertions.assertThat;
9+
import static io.opentelemetry.sdk.testing.assertj.OpenTelemetryAssertions.equalTo;
10+
import static io.opentelemetry.sdk.testing.assertj.OpenTelemetryAssertions.satisfies;
11+
12+
import io.opentelemetry.api.logs.Severity;
13+
import io.opentelemetry.instrumentation.testing.junit.AgentInstrumentationExtension;
14+
import io.opentelemetry.instrumentation.testing.junit.InstrumentationExtension;
15+
import io.opentelemetry.sdk.common.InstrumentationScopeInfo;
16+
import io.opentelemetry.sdk.logs.data.LogRecordData;
17+
import io.opentelemetry.semconv.trace.attributes.SemanticAttributes;
18+
import java.util.logging.Level;
19+
import java.util.logging.Logger;
20+
import java.util.stream.Stream;
21+
import org.junit.jupiter.api.extension.RegisterExtension;
22+
import org.junit.jupiter.params.ParameterizedTest;
23+
import org.junit.jupiter.params.provider.Arguments;
24+
import org.junit.jupiter.params.provider.MethodSource;
25+
26+
class JavaUtilLoggingTest {
27+
28+
private static final Logger logger = Logger.getLogger("abc");
29+
30+
@RegisterExtension
31+
static final InstrumentationExtension testing = AgentInstrumentationExtension.create();
32+
33+
private static Stream<Arguments> provideParameters() {
34+
return Stream.of(
35+
Arguments.of(false, false, false),
36+
Arguments.of(false, false, true),
37+
Arguments.of(false, true, false),
38+
Arguments.of(false, true, true),
39+
Arguments.of(true, false, false),
40+
Arguments.of(true, false, true),
41+
Arguments.of(true, true, false),
42+
Arguments.of(true, true, true));
43+
}
44+
45+
@ParameterizedTest
46+
@MethodSource("provideParameters")
47+
public void test(boolean withParam, boolean logException, boolean withParent)
48+
throws InterruptedException {
49+
test(Level.FINE, Logger::fine, withParam, logException, withParent, null, null, null);
50+
testing.clearData();
51+
test(
52+
Level.INFO,
53+
Logger::info,
54+
withParam,
55+
logException,
56+
withParent,
57+
"abc",
58+
Severity.INFO,
59+
"INFO");
60+
testing.clearData();
61+
test(
62+
Level.WARNING,
63+
Logger::warning,
64+
withParam,
65+
logException,
66+
withParent,
67+
"abc",
68+
Severity.WARN,
69+
"WARNING");
70+
testing.clearData();
71+
test(
72+
Level.SEVERE,
73+
Logger::severe,
74+
withParam,
75+
logException,
76+
withParent,
77+
"abc",
78+
Severity.ERROR,
79+
"SEVERE");
80+
testing.clearData();
81+
}
82+
83+
private static void test(
84+
Level level,
85+
LoggerMethod loggerMethod,
86+
boolean withParam,
87+
boolean logException,
88+
boolean withParent,
89+
String expectedLoggerName,
90+
Severity expectedSeverity,
91+
String expectedSeverityText)
92+
throws InterruptedException {
93+
94+
// when
95+
if (withParent) {
96+
testing.runWithSpan(
97+
"parent", () -> performLogging(level, loggerMethod, withParam, logException));
98+
} else {
99+
performLogging(level, loggerMethod, withParam, logException);
100+
}
101+
102+
// then
103+
if (withParent) {
104+
testing.waitForTraces(1);
105+
}
106+
107+
if (expectedSeverity != null) {
108+
LogRecordData log = testing.waitForLogRecords(1).get(0);
109+
assertThat(log)
110+
.hasBody(withParam ? "xyz: 123" : "xyz")
111+
.hasInstrumentationScope(InstrumentationScopeInfo.builder(expectedLoggerName).build())
112+
.hasSeverity(expectedSeverity)
113+
.hasSeverityText(expectedSeverityText);
114+
if (logException) {
115+
assertThat(log)
116+
.hasAttributesSatisfyingExactly(
117+
equalTo(SemanticAttributes.THREAD_NAME, Thread.currentThread().getName()),
118+
equalTo(SemanticAttributes.THREAD_ID, Thread.currentThread().getId()),
119+
equalTo(SemanticAttributes.EXCEPTION_TYPE, IllegalStateException.class.getName()),
120+
equalTo(SemanticAttributes.EXCEPTION_MESSAGE, "hello"),
121+
satisfies(
122+
SemanticAttributes.EXCEPTION_STACKTRACE,
123+
v -> v.contains(JavaUtilLoggingTest.class.getName())));
124+
} else {
125+
assertThat(log)
126+
.hasAttributesSatisfyingExactly(
127+
equalTo(SemanticAttributes.THREAD_NAME, Thread.currentThread().getName()),
128+
equalTo(SemanticAttributes.THREAD_ID, Thread.currentThread().getId()));
129+
}
130+
131+
if (withParent) {
132+
assertThat(log).hasSpanContext(testing.spans().get(0).getSpanContext());
133+
} else {
134+
assertThat(log.getSpanContext().isValid()).isFalse();
135+
}
136+
137+
} else {
138+
Thread.sleep(500); // sleep a bit just to make sure no log is captured
139+
assertThat(testing.logRecords()).isEmpty();
140+
}
141+
}
142+
143+
private static void performLogging(
144+
Level level, LoggerMethod loggerMethod, boolean withParam, boolean logException) {
145+
if (logException) {
146+
if (withParam) {
147+
// this is the best j.u.l. can do
148+
logger.log(level, new IllegalStateException("hello"), () -> "xyz: 123");
149+
} else {
150+
logger.log(level, "xyz", new IllegalStateException("hello"));
151+
}
152+
} else {
153+
if (withParam) {
154+
logger.log(level, "xyz: {0}", 123);
155+
} else {
156+
loggerMethod.call(logger, "xyz");
157+
}
158+
}
159+
}
160+
161+
@FunctionalInterface
162+
interface LoggerMethod {
163+
void call(Logger logger, String msg);
164+
}
165+
}

instrumentation/jboss-logmanager/jboss-logmanager-appender-1.1/javaagent/build.gradle.kts

+1
Original file line numberDiff line numberDiff line change
@@ -27,4 +27,5 @@ tasks.withType<Test>().configureEach {
2727
// TODO run tests both with and without experimental log attributes
2828
jvmArgs("-Dotel.instrumentation.jboss-logmanager.experimental.capture-mdc-attributes=*")
2929
jvmArgs("-Dotel.instrumentation.jboss-logmanager.experimental-log-attributes=true")
30+
jvmArgs("-Dotel.instrumentation.java-util-logging.experimental-log-attributes=true")
3031
}

0 commit comments

Comments
 (0)