Skip to content

Commit 2634880

Browse files
committed
builder
1 parent 74e6360 commit 2634880

File tree

11 files changed

+268
-130
lines changed

11 files changed

+268
-130
lines changed

smoke-tests/src/test/java/io/opentelemetry/smoketest/AgentDebugLoggingTest.java

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -12,17 +12,18 @@
1212

1313
@DisabledIf("io.opentelemetry.smoketest.TestContainerManager#useWindowsContainers")
1414
class AgentDebugLoggingTest extends JavaSmokeTest {
15-
@Override
16-
protected String getTargetImage(String jdk) {
17-
return "ghcr.io/open-telemetry/opentelemetry-java-instrumentation/smoke-test-spring-boot:jdk"
18-
+ jdk
19-
+ "-20250915.17728045097";
20-
}
2115

22-
@Override
23-
protected TargetWaitStrategy getWaitStrategy() {
24-
return new TargetWaitStrategy.Log(
25-
Duration.ofMinutes(1), ".*DEBUG io.opentelemetry.javaagent.tooling.VersionLogger.*");
16+
public AgentDebugLoggingTest() {
17+
super(
18+
SmokeTestTarget.builder(
19+
jdk ->
20+
"ghcr.io/open-telemetry/opentelemetry-java-instrumentation/smoke-test-spring-boot:jdk"
21+
+ jdk
22+
+ "-20250915.17728045097")
23+
.waitStrategy(
24+
new TargetWaitStrategy.Log(
25+
Duration.ofMinutes(1),
26+
".*DEBUG io.opentelemetry.javaagent.tooling.VersionLogger.*")));
2627
}
2728

2829
@DisplayName("verifies that debug logging is working by checking for a debug log on startup")

smoke-tests/src/test/java/io/opentelemetry/smoketest/DeclarativeConfigurationSmokeTest.java

Lines changed: 11 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -21,29 +21,24 @@
2121

2222
@DisabledIf("io.opentelemetry.smoketest.TestContainerManager#useWindowsContainers")
2323
class DeclarativeConfigurationSmokeTest extends JavaSmokeTest {
24-
@Override
25-
protected String getTargetImage(String jdk) {
26-
return "ghcr.io/open-telemetry/opentelemetry-java-instrumentation/smoke-test-spring-boot:jdk"
27-
+ jdk
28-
+ "-20241021.11448062567";
24+
25+
public DeclarativeConfigurationSmokeTest() {
26+
super(
27+
SmokeTestTarget.builder(
28+
jdk ->
29+
"ghcr.io/open-telemetry/opentelemetry-java-instrumentation/smoke-test-spring-boot:jdk"
30+
+ jdk
31+
+ "-20241021.11448062567")
32+
.waitStrategy(new TargetWaitStrategy.Log(
33+
Duration.ofMinutes(1), ".*Started SpringbootApplication in.*"))
34+
.env("OTEL_EXPERIMENTAL_CONFIG_FILE", "declarative-config.yaml"));
2935
}
3036

3137
@Override
3238
protected List<ResourceMapping> getExtraResources() {
3339
return List.of(ResourceMapping.of("declarative-config.yaml", "/declarative-config.yaml"));
3440
}
3541

36-
@Override
37-
protected Map<String, String> getExtraEnv() {
38-
return Map.of("OTEL_EXPERIMENTAL_CONFIG_FILE", "declarative-config.yaml");
39-
}
40-
41-
@Override
42-
protected TargetWaitStrategy getWaitStrategy() {
43-
return new TargetWaitStrategy.Log(
44-
Duration.ofMinutes(1), ".*Started SpringbootApplication in.*");
45-
}
46-
4742
@ParameterizedTest
4843
@ValueSource(ints = {8, 11, 17})
4944
void springBootSmokeTest(int jdk) {

smoke-tests/src/test/java/io/opentelemetry/smoketest/JavaSmokeTest.java

Lines changed: 16 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -33,43 +33,25 @@ public abstract class JavaSmokeTest implements TelemetryRetrieverProvider {
3333
private static final Pattern TRACE_ID_PATTERN =
3434
Pattern.compile(".*trace_id=(?<traceId>[a-zA-Z0-9]+).*");
3535
protected static final TestContainerManager containerManager = createContainerManager();
36+
37+
private final SmokeTestTarget target;
3638
private static TelemetryRetriever telemetryRetriever;
3739

3840
protected String agentPath =
3941
System.getProperty("io.opentelemetry.smoketest.agent.shadowJar.path");
4042

4143
@RegisterExtension static final AutoCleanupExtension autoCleanup = AutoCleanupExtension.create();
4244

43-
protected WebClient client() {
44-
return WebClient.of("h1c://localhost:" + containerManager.getTargetMappedPort(8080));
45-
}
46-
47-
/** Subclasses can override this method to pass jvm arguments in another environment variable */
48-
protected String getJvmArgsEnvVarName() {
49-
return "JAVA_TOOL_OPTIONS";
50-
}
51-
52-
/** Subclasses can override this method to customise target application's environment */
53-
protected Map<String, String> getExtraEnv() {
54-
return emptyMap();
55-
}
56-
57-
/** Subclasses can override this method to disable setting default service name */
58-
protected boolean getSetServiceName() {
59-
return true;
45+
public JavaSmokeTest(SmokeTestTarget.Builder builder) {
46+
this.target = customize(builder).build();
6047
}
6148

62-
/** Subclasses can override this method to provide additional files to copy to target container */
63-
protected List<ResourceMapping> getExtraResources() {
64-
return emptyList();
49+
protected SmokeTestTarget.Builder customize(SmokeTestTarget.Builder builder) {
50+
return builder;
6551
}
6652

67-
/**
68-
* Subclasses can override this method to provide additional ports that should be exposed from the
69-
* target container
70-
*/
71-
protected List<Integer> getExtraPorts() {
72-
return emptyList();
53+
public WebClient client() {
54+
return WebClient.of("h1c://localhost:" + containerManager.getTargetMappedPort(8080));
7355
}
7456

7557
@BeforeAll
@@ -83,33 +65,19 @@ protected Consumer<OutputFrame> startTarget(int jdk) {
8365
}
8466

8567
protected Consumer<OutputFrame> startTarget(String jdk, String serverVersion, boolean windows) {
86-
String targetImage = getTargetImage(jdk, serverVersion, windows);
68+
String targetImage = target.getGetTargetImage(jdk, serverVersion, windows);
8769
autoCleanup.deferCleanup(() -> containerManager.stopTarget());
8870

8971
return containerManager.startTarget(
9072
targetImage,
9173
agentPath,
92-
getJvmArgsEnvVarName(),
93-
getExtraEnv(),
94-
getSetServiceName(),
95-
getExtraResources(),
96-
getExtraPorts(),
97-
getWaitStrategy(),
98-
getCommand());
99-
}
100-
101-
protected abstract String getTargetImage(String jdk);
102-
103-
protected String getTargetImage(String jdk, String serverVersion, boolean windows) {
104-
return getTargetImage(jdk);
105-
}
106-
107-
protected TargetWaitStrategy getWaitStrategy() {
108-
return null;
109-
}
110-
111-
protected String[] getCommand() {
112-
return null;
74+
target.getJvmArgsEnvVarName(),
75+
target.getExtraEnv(),
76+
target.getSetServiceName(),
77+
target.getExtraResources(),
78+
target.getExtraPorts(),
79+
target.getWaitStrategy(),
80+
target.getCommand());
11381
}
11482

11583
protected static void assertVersionLogged(Consumer<OutputFrame> output, String version) {

smoke-tests/src/test/java/io/opentelemetry/smoketest/LogsSmokeTest.java

Lines changed: 15 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -5,28 +5,29 @@
55

66
package io.opentelemetry.smoketest;
77

8-
import static org.assertj.core.api.Assertions.assertThat;
9-
108
import io.opentelemetry.sdk.logs.data.LogRecordData;
11-
import java.time.Duration;
12-
import java.util.Collection;
139
import org.junit.jupiter.api.condition.DisabledIf;
1410
import org.junit.jupiter.params.ParameterizedTest;
1511
import org.junit.jupiter.params.provider.ValueSource;
1612

13+
import java.time.Duration;
14+
import java.util.Collection;
15+
16+
import static org.assertj.core.api.Assertions.assertThat;
17+
1718
@DisabledIf("io.opentelemetry.smoketest.TestContainerManager#useWindowsContainers")
1819
class LogsSmokeTest extends JavaSmokeTest {
19-
@Override
20-
protected String getTargetImage(String jdk) {
21-
return "ghcr.io/open-telemetry/opentelemetry-java-instrumentation/smoke-test-spring-boot:jdk"
22-
+ jdk
23-
+ "-20211213.1570880324";
24-
}
2520

26-
@Override
27-
protected TargetWaitStrategy getWaitStrategy() {
28-
return new TargetWaitStrategy.Log(
29-
Duration.ofMinutes(1), ".*Started SpringbootApplication in.*");
21+
public LogsSmokeTest() {
22+
super(
23+
SmokeTestTarget.builder(
24+
jdk ->
25+
"ghcr.io/open-telemetry/opentelemetry-java-instrumentation/smoke-test-spring-boot:jdk"
26+
+ jdk
27+
+ "-20211213.1570880324")
28+
.waitStrategy(
29+
new TargetWaitStrategy.Log(
30+
Duration.ofMinutes(1), ".*Started SpringbootApplication in.*")));
3031
}
3132

3233
@ParameterizedTest
Lines changed: 172 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,172 @@
1+
/*
2+
* Copyright The OpenTelemetry Authors
3+
* SPDX-License-Identifier: Apache-2.0
4+
*/
5+
6+
package io.opentelemetry.smoketest;
7+
8+
import java.time.Duration;
9+
import java.util.List;
10+
import java.util.Map;
11+
import java.util.function.Function;
12+
13+
import static java.util.Collections.emptyMap;
14+
15+
public class SmokeTestTarget {
16+
17+
@FunctionalInterface
18+
interface GetTargetImage {
19+
String getTargetImage(String jdk, String serverVersion, boolean windows);
20+
}
21+
22+
private final GetTargetImage getTargetImage;
23+
24+
private final String[] command;
25+
private final String jvmArgsEnvVarName;
26+
private final boolean setServiceName;
27+
private final Map<String, String> extraEnv;
28+
private final List<ResourceMapping> extraResources;
29+
private final TargetWaitStrategy waitStrategy;
30+
private final List<Integer> extraPorts;
31+
32+
public SmokeTestTarget(
33+
GetTargetImage getTargetImage,
34+
String[] command,
35+
String jvmArgsEnvVarName,
36+
boolean setServiceName,
37+
Map<String, String> extraEnv,
38+
List<ResourceMapping> extraResources,
39+
TargetWaitStrategy waitStrategy,
40+
List<Integer> extraPorts) {
41+
this.getTargetImage = getTargetImage;
42+
this.command = command;
43+
this.jvmArgsEnvVarName = jvmArgsEnvVarName;
44+
this.setServiceName = setServiceName;
45+
this.extraEnv = extraEnv;
46+
this.extraResources = extraResources;
47+
this.waitStrategy = waitStrategy;
48+
this.extraPorts = extraPorts;
49+
}
50+
51+
public GetTargetImage getGetTargetImage() {
52+
return getTargetImage;
53+
}
54+
55+
public String[] getCommand() {
56+
return command;
57+
}
58+
59+
/** Subclasses can override this method to pass jvm arguments in another environment variable */
60+
public String getJvmArgsEnvVarName() {
61+
return jvmArgsEnvVarName;
62+
}
63+
64+
/** Subclasses can override this method to customise target application's environment */
65+
public Map<String, String> getExtraEnv() {
66+
return extraEnv;
67+
}
68+
69+
/** Subclasses can override this method to disable setting default service name */
70+
public boolean getSetServiceName() {
71+
return setServiceName;
72+
}
73+
74+
/** Subclasses can override this method to provide additional files to copy to target container */
75+
public List<ResourceMapping> getExtraResources() {
76+
return extraResources;
77+
}
78+
79+
/**
80+
* Subclasses can override this method to provide additional ports that should be exposed from the
81+
* target container
82+
*/
83+
public List<Integer> getExtraPorts() {
84+
return extraPorts;
85+
}
86+
87+
public TargetWaitStrategy getWaitStrategy() {
88+
return waitStrategy;
89+
}
90+
91+
public static Builder builder(Function<String, String> getTargetImage) {
92+
return builder((jdk, serverVersion, windows) -> getTargetImage.apply(jdk));
93+
}
94+
95+
public static Builder builder(GetTargetImage getTargetImage) {
96+
return new Builder(getTargetImage);
97+
}
98+
99+
public static Builder springBoot() {
100+
return builder(
101+
jdk ->
102+
"ghcr.io/open-telemetry/opentelemetry-java-instrumentation/smoke-test-spring-boot:jdk"
103+
+ jdk
104+
+ "-20211213.1570880324")
105+
.waitStrategy(
106+
new TargetWaitStrategy.Log(
107+
Duration.ofMinutes(1), ".*Started SpringbootApplication in.*"));
108+
}
109+
110+
public static class Builder {
111+
private final GetTargetImage getTargetImage;
112+
private String[] command;
113+
private String jvmArgsEnvVarName = "JAVA_TOOL_OPTIONS";
114+
private boolean setServiceName = true;
115+
private Map<String, String> extraEnv;
116+
private List<ResourceMapping> extraResources;
117+
private TargetWaitStrategy waitStrategy;
118+
private List<Integer> extraPorts;
119+
120+
private Builder(GetTargetImage getTargetImage) {
121+
this.getTargetImage = getTargetImage;
122+
this.extraEnv = emptyMap();
123+
}
124+
125+
public Builder command(String... command) {
126+
this.command = command;
127+
return this;
128+
}
129+
130+
public Builder jvmArgsEnvVarName(String jvmArgsEnvVarName) {
131+
this.jvmArgsEnvVarName = jvmArgsEnvVarName;
132+
return this;
133+
}
134+
135+
public Builder setServiceName(boolean setServiceName) {
136+
this.setServiceName = setServiceName;
137+
return this;
138+
}
139+
140+
public Builder env(String key, String value) {
141+
this.extraEnv.put(key, value);
142+
return this;
143+
}
144+
145+
public Builder extraResources(ResourceMapping... resources) {
146+
this.extraResources = List.of(resources);
147+
return this;
148+
}
149+
150+
public Builder waitStrategy(TargetWaitStrategy waitStrategy) {
151+
this.waitStrategy = waitStrategy;
152+
return this;
153+
}
154+
155+
public Builder extraPorts(Integer... ports) {
156+
this.extraPorts = List.of(ports);
157+
return this;
158+
}
159+
160+
public SmokeTestTarget build() {
161+
return new SmokeTestTarget(
162+
getTargetImage,
163+
command,
164+
jvmArgsEnvVarName,
165+
setServiceName,
166+
extraEnv,
167+
extraResources,
168+
waitStrategy,
169+
extraPorts);
170+
}
171+
}
172+
}

smoke-tests/src/test/java/io/opentelemetry/smoketest/propagation/B3MultiPropagationTest.java

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,14 @@
55

66
package io.opentelemetry.smoketest.propagation;
77

8-
import java.util.Map;
8+
import io.opentelemetry.smoketest.SmokeTestTarget;
99
import org.junit.jupiter.api.condition.DisabledIf;
1010

1111
@DisabledIf("io.opentelemetry.smoketest.TestContainerManager#useWindowsContainers")
1212
class B3MultiPropagationTest extends PropagationTest {
13+
1314
@Override
14-
protected Map<String, String> getExtraEnv() {
15-
return Map.of("otel.propagators", "b3multi");
15+
protected SmokeTestTarget.Builder customize(SmokeTestTarget.Builder builder) {
16+
return builder.env("otel.propagators", "b3multi");
1617
}
1718
}

0 commit comments

Comments
 (0)