Skip to content

Commit f765922

Browse files
committed
make temp dir more reliable
1 parent 79d7414 commit f765922

File tree

3 files changed

+15
-30
lines changed

3 files changed

+15
-30
lines changed

inferred-spans/src/main/java/io/opentelemetry/contrib/inferredspans/InferredSpansProcessor.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,8 @@ public class InferredSpansProcessor implements SpanProcessor {
4949
boolean startScheduledProfiling,
5050
@Nullable File activationEventsFile,
5151
@Nullable File jfrFile) {
52-
profiler = new SamplingProfiler(config, clock, this::getTracer, activationEventsFile, jfrFile);
52+
profiler =
53+
new SamplingProfiler(config, clock, this::getTracer, activationEventsFile, jfrFile, null);
5354
if (startScheduledProfiling) {
5455
profiler.start();
5556
}

inferred-spans/src/main/java/io/opentelemetry/contrib/inferredspans/internal/SamplingProfiler.java

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -149,6 +149,7 @@ public class SamplingProfiler implements Runnable {
149149
private final ProfilingActivationListener activationListener;
150150

151151
private final Supplier<Tracer> tracerProvider;
152+
@Nullable private final File tempDir;
152153

153154
private final AsyncProfiler profiler;
154155

@@ -168,9 +169,11 @@ public SamplingProfiler(
168169
SpanAnchoredClock nanoClock,
169170
Supplier<Tracer> tracerProvider,
170171
@Nullable File activationEventsFile,
171-
@Nullable File jfrFile) {
172+
@Nullable File jfrFile,
173+
@Nullable File tempDir) {
172174
this.config = config;
173175
this.tracerProvider = tracerProvider;
176+
this.tempDir = tempDir;
174177
this.scheduler =
175178
Executors.newSingleThreadScheduledExecutor(
176179
r -> {
@@ -250,12 +253,13 @@ boolean isProfilingActiveOnThread(Thread thread) {
250253

251254
private synchronized void createFilesIfRequired() throws IOException {
252255
if (jfrFile == null || !jfrFile.exists()) {
253-
jfrFile = File.createTempFile("otel-inferred-traces-", ".jfr");
256+
jfrFile = File.createTempFile("otel-inferred-traces-", ".jfr", tempDir);
254257
jfrFile.deleteOnExit();
255258
canDeleteJfrFile = true;
256259
}
257260
if (activationEventsFile == null || !activationEventsFile.exists()) {
258-
activationEventsFile = File.createTempFile("otel-inferred-activation-events-", ".bin");
261+
activationEventsFile =
262+
File.createTempFile("otel-inferred-activation-events-", ".bin", tempDir);
259263
activationEventsFile.deleteOnExit();
260264
canDeleteActivationEventsFile = true;
261265
}

inferred-spans/src/test/java/io/opentelemetry/contrib/inferredspans/internal/SamplingProfilerTest.java

Lines changed: 6 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -32,12 +32,12 @@
3232
import java.util.stream.Stream;
3333
import org.assertj.core.api.Assertions;
3434
import org.junit.jupiter.api.AfterEach;
35-
import org.junit.jupiter.api.BeforeEach;
3635
import org.junit.jupiter.api.Test;
3736
import org.junit.jupiter.api.condition.DisabledForJreRange;
3837
import org.junit.jupiter.api.condition.DisabledOnOs;
3938
import org.junit.jupiter.api.condition.JRE;
4039
import org.junit.jupiter.api.condition.OS;
40+
import org.junit.jupiter.api.io.TempDir;
4141

4242
// async-profiler doesn't work on Windows
4343
@DisabledOnOs(OS.WINDOWS)
@@ -46,19 +46,14 @@ class SamplingProfilerTest {
4646

4747
private ProfilerTestSetup setup;
4848

49-
@BeforeEach
50-
void setup() {
51-
// avoids any test failure to make other tests to fail
52-
getProfilerTempFiles().forEach(SamplingProfilerTest::silentDeleteFile);
53-
}
49+
@TempDir private Path tempDir;
5450

5551
@AfterEach
5652
void tearDown() {
5753
if (setup != null) {
5854
setup.close();
5955
setup = null;
6056
}
61-
getProfilerTempFiles().forEach(SamplingProfilerTest::silentDeleteFile);
6257
}
6358

6459
@Test
@@ -117,8 +112,8 @@ void shouldNotDeleteProvidedFiles() throws Exception {
117112
defaultConfig = ProfilerTestSetup.extractProfilerImpl(profiler1).getConfig();
118113
}
119114

120-
Path tempFile1 = Files.createTempFile("otel-inferred-provided", "test.bin");
121-
Path tempFile2 = Files.createTempFile("otel-inferred-provided", "test.jfr");
115+
Path tempFile1 = Files.createTempFile(tempDir, "otel-inferred-provided", "test.bin");
116+
Path tempFile2 = Files.createTempFile(tempDir, "otel-inferred-provided", "test.jfr");
122117

123118
try (OpenTelemetrySdk sdk = OpenTelemetrySdk.builder().build()) {
124119

@@ -128,7 +123,8 @@ void shouldNotDeleteProvidedFiles() throws Exception {
128123
new FixedClock(),
129124
() -> sdk.getTracer("my-tracer"),
130125
tempFile1.toFile(),
131-
tempFile2.toFile());
126+
tempFile2.toFile(),
127+
tempDir.toFile());
132128

133129
otherProfiler.start();
134130
awaitProfilerStarted(otherProfiler);
@@ -333,20 +329,4 @@ private void setupProfiler(Consumer<InferredSpansProcessorBuilder> configCustomi
333329
configCustomizer.accept(config);
334330
});
335331
}
336-
337-
private static void awaitProfilerStarted(SamplingProfiler profiler) {
338-
// ensure profiler is initialized
339-
await()
340-
.pollDelay(Duration.ofMillis(10))
341-
.timeout(Duration.ofSeconds(6))
342-
.until(() -> profiler.getProfilingSessions() > 1);
343-
}
344-
345-
private static void silentDeleteFile(Path f) {
346-
try {
347-
Files.delete(f);
348-
} catch (IOException e) {
349-
throw new IllegalStateException(e);
350-
}
351-
}
352332
}

0 commit comments

Comments
 (0)