@@ -108,55 +108,79 @@ static void setupSpec() {
108108 backend .start ();
109109 }
110110
111- protected static GenericContainer <?> startTarget (
112- String image , Consumer <GenericContainer <?>> customizeContainer ) {
111+ public static TestTarget testTarget (String image ) {
112+ return new TestTarget (image );
113+ }
113114
114- @ SuppressWarnings ("resource" )
115- GenericContainer <?> target =
116- new GenericContainer <>(image )
117- .withNetwork (network )
118- .withLogConsumer (new Slf4jLogConsumer (logger ))
119- .withCopyFileToContainer (MountableFile .forHostPath (AGENT_PATH ), JAVAAGENT_JAR_PATH );
115+ public static class TestTarget {
116+ private final String image ;
117+ private Consumer <GenericContainer <?>> customizeContainer ;
118+ private boolean baseEnvVariables ;
120119
121- StringBuilder jvmArgs = new StringBuilder ();
120+ public TestTarget (String image ) {
121+ this .image = image ;
122+ }
122123
123- if (JavaExecutable .isDebugging ()) {
124- // when debugging, automatically use verbose debug logging
125- target .withEnv ("OTEL_JAVAAGENT_DEBUG" , "true" );
124+ public TestTarget customize (Consumer <GenericContainer <?>> customizeContainer ) {
125+ this .customizeContainer = customizeContainer ;
126+ return this ;
127+ }
126128
127- if (JavaExecutable .isListeningDebuggerStarted (TARGET_DEBUG_PORT , "target" )) {
128- target = addDockerDebugHost (target );
129- jvmArgs
130- .append (JavaExecutable .jvmDebugArgument ("remote-localhost" , TARGET_DEBUG_PORT ))
131- .append (" " );
132- }
133- // Use very long startup delay when debugging as the remote JVM is likely stopped before the
134- // app has started
135- target .waitingFor (Wait .defaultWaitStrategy ().withStartupTimeout (Duration .ofMinutes (10 )));
129+ public TestTarget withBaseEnvironmentVariables (boolean value ) {
130+ this .baseEnvVariables = value ;
131+ return this ;
136132 }
137133
138- jvmArgs .append (JavaExecutable .jvmAgentArgument (JAVAAGENT_JAR_PATH ));
139- target .withEnv ("JAVA_TOOL_OPTIONS" , jvmArgs .toString ());
134+ public GenericContainer <?> start () {
135+ @ SuppressWarnings ("resource" )
136+ GenericContainer <?> target =
137+ new GenericContainer <>(image )
138+ .withNetwork (network )
139+ .withLogConsumer (new Slf4jLogConsumer (logger ))
140+ .withCopyFileToContainer (MountableFile .forHostPath (AGENT_PATH ), JAVAAGENT_JAR_PATH );
141+
142+ StringBuilder jvmArgs = new StringBuilder ();
143+
144+ if (JavaExecutable .isDebugging ()) {
145+ // when debugging, automatically use verbose debug logging
146+ target .withEnv ("OTEL_JAVAAGENT_DEBUG" , "true" );
147+
148+ if (JavaExecutable .isListeningDebuggerStarted (TARGET_DEBUG_PORT , "target" )) {
149+ target = addDockerDebugHost (target );
150+ jvmArgs
151+ .append (JavaExecutable .jvmDebugArgument ("remote-localhost" , TARGET_DEBUG_PORT ))
152+ .append (" " );
153+ }
154+ // Use very long startup delay when debugging as the remote JVM is likely stopped before the
155+ // app has started
156+ target .waitingFor (Wait .defaultWaitStrategy ().withStartupTimeout (Duration .ofMinutes (10 )));
157+ }
140158
141- customizeContainer .accept (target );
159+ jvmArgs .append (JavaExecutable .jvmAgentArgument (JAVAAGENT_JAR_PATH ));
160+ target .withEnv ("JAVA_TOOL_OPTIONS" , jvmArgs .toString ());
161+
162+ if (baseEnvVariables ) {
163+ target
164+ // batch span processor: very small batch size for testing
165+ .withEnv ("OTEL_BSP_MAX_EXPORT_BATCH" , "1" )
166+ // batch span processor: very short delay for testing
167+ .withEnv ("OTEL_BSP_SCHEDULE_DELAY" , "10" )
168+ // use grpc endpoint as default is now http/protobuf with agent 2.x
169+ .withEnv ("OTEL_EXPORTER_OTLP_PROTOCOL" , "grpc" )
170+ .withEnv ("OTEL_PROPAGATORS" , "tracecontext,baggage" )
171+ .withEnv ("OTEL_EXPORTER_OTLP_ENDPOINT" , BACKEND_ENDPOINT );
172+ }
142173
143- Objects .requireNonNull (target ).start ();
174+ if (customizeContainer != null ) {
175+ customizeContainer .accept (target );
176+ }
144177
145- startedContainers . add (target );
178+ Objects . requireNonNull (target ). start ( );
146179
147- return target ;
148- }
180+ startedContainers .add (target );
149181
150- protected static void setBaseEnvironmentVariables (GenericContainer <?> target ) {
151- target
152- // batch span processor: very small batch size for testing
153- .withEnv ("OTEL_BSP_MAX_EXPORT_BATCH" , "1" )
154- // batch span processor: very short delay for testing
155- .withEnv ("OTEL_BSP_SCHEDULE_DELAY" , "10" )
156- // use grpc endpoint as default is now http/protobuf with agent 2.x
157- .withEnv ("OTEL_EXPORTER_OTLP_PROTOCOL" , "grpc" )
158- .withEnv ("OTEL_PROPAGATORS" , "tracecontext,baggage" )
159- .withEnv ("OTEL_EXPORTER_OTLP_ENDPOINT" , BACKEND_ENDPOINT );
182+ return target ;
183+ }
160184 }
161185
162186 private static GenericContainer <?> addDockerDebugHost (GenericContainer <?> target ) {
0 commit comments