Skip to content

Commit db3777d

Browse files
authored
[FLINK-39107][metrics][test] Run otel container from a prebuilt image (#27637)
We are hitting docker build issues when multiple tests are building otel from a dockerfile in parallel. While it could be addressed by a resource lock, we realized we could actually use the prebuilt image with a copied output directory instead of building from dockerfile.
1 parent 4d88ecf commit db3777d

File tree

2 files changed

+13
-23
lines changed

2 files changed

+13
-23
lines changed

flink-metrics/flink-metrics-otel/src/test/java/org/apache/flink/metrics/otel/OpenTelemetryTestBase.java

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@
3434
import org.junit.jupiter.api.Order;
3535
import org.junit.jupiter.api.extension.ExtendWith;
3636
import org.junit.jupiter.api.extension.RegisterExtension;
37+
import org.junit.jupiter.api.io.TempDir;
3738
import org.slf4j.Logger;
3839
import org.slf4j.LoggerFactory;
3940
import org.testcontainers.containers.output.BaseConsumer;
@@ -42,6 +43,7 @@
4243

4344
import java.io.BufferedReader;
4445
import java.io.InputStreamReader;
46+
import java.nio.file.Path;
4547
import java.time.Duration;
4648
import java.util.ArrayList;
4749
import java.util.List;
@@ -54,12 +56,14 @@ public class OpenTelemetryTestBase {
5456

5557
private static final Duration TIME_OUT = Duration.ofMinutes(2);
5658

59+
@TempDir static Path outputDir;
60+
5761
@RegisterExtension
5862
@Order(1)
5963
private static final AllCallbackWrapper<TestContainerExtension<OtelTestContainer>>
6064
OTEL_EXTENSION =
6165
new AllCallbackWrapper<>(
62-
new TestContainerExtension<>(() -> new OtelTestContainer()));
66+
new TestContainerExtension<>(() -> new OtelTestContainer(outputDir)));
6367

6468
@BeforeEach
6569
public void setup() {

flink-metrics/flink-metrics-otel/src/test/java/org/apache/flink/metrics/otel/OtelTestContainer.java

Lines changed: 8 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -20,12 +20,11 @@
2020

2121
import com.github.dockerjava.api.command.InspectContainerResponse;
2222
import org.testcontainers.containers.GenericContainer;
23-
import org.testcontainers.images.builder.ImageFromDockerfile;
24-
import org.testcontainers.images.builder.dockerfile.DockerfileBuilder;
25-
import org.testcontainers.images.builder.dockerfile.statement.MultiArgsStatement;
2623
import org.testcontainers.utility.Base58;
24+
import org.testcontainers.utility.DockerImageName;
2725
import org.testcontainers.utility.MountableFile;
2826

27+
import java.io.File;
2928
import java.nio.file.Path;
3029
import java.nio.file.Paths;
3130
import java.util.Locale;
@@ -42,33 +41,20 @@ class OtelTestContainer extends GenericContainer<OtelTestContainer> {
4241
private static final Path CONFIG_PATH =
4342
Paths.get("src/test/resources/").resolve("otel-config.yaml");
4443

45-
public OtelTestContainer() {
46-
super(
47-
new ImageFromDockerfile("flink/opentelemetry-collector")
48-
.withDockerfileFromBuilder(
49-
OtelTestContainer::buildOpentelemetryCollectorImage));
44+
public OtelTestContainer(Path outputDir) {
45+
super(DockerImageName.parse("otel/opentelemetry-collector:0.111.0"));
5046
withNetworkAliases(randomString("otel-collector", 6));
5147
addExposedPort(DEFAULT_HTTP_PORT);
5248
addExposedPort(DEFAULT_GRPC_PORT);
5349
withCopyFileToContainer(
5450
MountableFile.forHostPath(CONFIG_PATH.toString()), "otel-config.yaml");
51+
withCopyFileToContainer(
52+
MountableFile.forHostPath(
53+
new File(outputDir.toFile(), LOG_FILE).getAbsolutePath(), 755),
54+
getOutputLogPath().toString());
5555
withCommand("--config", "otel-config.yaml");
5656
}
5757

58-
private static void buildOpentelemetryCollectorImage(DockerfileBuilder builder) {
59-
builder
60-
// otel image doesn't have mkdir - use alpine instead.
61-
.from("alpine:3.20")
62-
// Create the output data directory - otel image doesn't have any directory to write
63-
// to on its own.
64-
.run("mkdir -p " + DATA_DIR)
65-
.from("otel/opentelemetry-collector:0.111.0")
66-
// Copy the output data directory from alpine. It has to be owned by the otel user.
67-
.withStatement(
68-
new MultiArgsStatement("COPY --from=0 --chown=10001", DATA_DIR, DATA_DIR))
69-
.build();
70-
}
71-
7258
public Path getOutputLogPath() {
7359
return Path.of(DATA_DIR, LOG_FILE);
7460
}

0 commit comments

Comments
 (0)