Skip to content

Commit 935cadf

Browse files
committed
Avoids autoconfigure tests failing if a temporary directory cannot be created
1 parent 52d9133 commit 935cadf

File tree

6 files changed

+87
-18
lines changed

6 files changed

+87
-18
lines changed

sdk/monitor/azure-monitor-opentelemetry-autoconfigure/src/main/java/com/azure/monitor/opentelemetry/autoconfigure/implementation/utils/TempDirs.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@ public class TempDirs {
1717
private static final List<String> CANDIDATE_USERNAME_ENVIRONMENT_VARIABLES
1818
= Collections.unmodifiableList(Arrays.asList("USER", "LOGNAME", "USERNAME"));
1919

20+
public static final String UNABLE_TO_CREATE_DIRECTORY = "Unable to create directory: ";
21+
2022
@Nullable
2123
public static File getApplicationInsightsTempDir(ClientLogger logger, String message) {
2224
File tempDir = new File(System.getProperty("java.io.tmpdir"));
@@ -51,7 +53,7 @@ public static File getSubDir(File parent, String name) {
5153
File dir = new File(parent, name);
5254

5355
if (!dir.exists() && !dir.mkdirs()) {
54-
throw new IllegalArgumentException("Unable to create directory: " + dir);
56+
throw new IllegalArgumentException(UNABLE_TO_CREATE_DIRECTORY + dir);
5557
}
5658
if (!dir.canRead()) {
5759
throw new IllegalArgumentException("Missing read permission to subdirectory: " + dir);

sdk/monitor/azure-monitor-opentelemetry-autoconfigure/src/test/java/com/azure/monitor/opentelemetry/autoconfigure/AppConfigurationExporterIntegrationTest.java

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
import io.opentelemetry.api.trace.Span;
2323
import io.opentelemetry.api.trace.Tracer;
2424
import io.opentelemetry.context.Scope;
25+
import io.opentelemetry.sdk.OpenTelemetrySdk;
2526
import org.junit.jupiter.api.Disabled;
2627
import org.junit.jupiter.api.Test;
2728
import reactor.core.publisher.Mono;
@@ -30,6 +31,7 @@
3031
import java.io.ByteArrayOutputStream;
3132
import java.io.IOException;
3233
import java.nio.charset.StandardCharsets;
34+
import java.util.Optional;
3335
import java.util.concurrent.CountDownLatch;
3436
import java.util.concurrent.TimeUnit;
3537
import java.util.zip.GZIPInputStream;
@@ -53,7 +55,12 @@ public void setConfigurationTest() throws InterruptedException {
5355
CountDownLatch exporterCountDown = new CountDownLatch(1);
5456

5557
ValidationPolicy validationPolicy = new ValidationPolicy(exporterCountDown, "set-config-exporter-testing");
56-
OpenTelemetry openTelemetry = TestUtils.createOpenTelemetrySdk(getHttpPipeline(validationPolicy));
58+
Optional<OpenTelemetrySdk> optionalOpenTelemetry
59+
= TestUtils.createOpenTelemetrySdk(getHttpPipeline(validationPolicy));
60+
if (!optionalOpenTelemetry.isPresent()) {
61+
return;
62+
}
63+
OpenTelemetrySdk openTelemetry = optionalOpenTelemetry.get();
5764

5865
Tracer tracer = openTelemetry.getTracer("Sample");
5966

@@ -78,7 +85,12 @@ public void testDisableTracing() throws InterruptedException {
7885
CountDownLatch exporterCountDown = new CountDownLatch(1);
7986

8087
ValidationPolicy validationPolicy = new ValidationPolicy(exporterCountDown, "disable-config-exporter-testing");
81-
OpenTelemetry openTelemetry = TestUtils.createOpenTelemetrySdk(getHttpPipeline(validationPolicy));
88+
Optional<OpenTelemetrySdk> optionalOpenTelemetry
89+
= TestUtils.createOpenTelemetrySdk(getHttpPipeline(validationPolicy));
90+
if (!optionalOpenTelemetry.isPresent()) {
91+
return;
92+
}
93+
OpenTelemetrySdk openTelemetry = optionalOpenTelemetry.get();
8294

8395
Tracer tracer = openTelemetry.getTracer("Sample");
8496

sdk/monitor/azure-monitor-opentelemetry-autoconfigure/src/test/java/com/azure/monitor/opentelemetry/autoconfigure/AzureMonitorExportersEndToEndTest.java

Lines changed: 27 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
import java.util.Collections;
3333
import java.util.List;
3434
import java.util.Map;
35+
import java.util.Optional;
3536
import java.util.concurrent.CountDownLatch;
3637

3738
import static java.util.concurrent.TimeUnit.SECONDS;
@@ -53,7 +54,12 @@ public void testBuildTraceExporter() throws Exception {
5354
CountDownLatch countDownLatch = new CountDownLatch(numberOfSpans);
5455
CustomValidationPolicy customValidationPolicy = new CustomValidationPolicy(countDownLatch);
5556
HttpPipeline httpPipeline = getHttpPipeline(customValidationPolicy);
56-
OpenTelemetry openTelemetry = TestUtils.createOpenTelemetrySdk(httpPipeline, getConfiguration());
57+
Optional<OpenTelemetrySdk> optionalOpenTelemetry
58+
= TestUtils.createOpenTelemetrySdk(httpPipeline, getConfiguration());
59+
if (!optionalOpenTelemetry.isPresent()) {
60+
return;
61+
}
62+
OpenTelemetrySdk openTelemetry = optionalOpenTelemetry.get();
5763

5864
// generate spans
5965
for (int i = 0; i < numberOfSpans; i++) {
@@ -96,8 +102,12 @@ public void testBuildMetricExporter() throws Exception {
96102
// create the OpenTelemetry SDK
97103
CountDownLatch countDownLatch = new CountDownLatch(1);
98104
CustomValidationPolicy customValidationPolicy = new CustomValidationPolicy(countDownLatch);
99-
OpenTelemetrySdk openTelemetry
105+
Optional<OpenTelemetrySdk> optionalOpenTelemetry
100106
= TestUtils.createOpenTelemetrySdk(getHttpPipeline(customValidationPolicy), getConfiguration());
107+
if (!optionalOpenTelemetry.isPresent()) {
108+
return;
109+
}
110+
OpenTelemetrySdk openTelemetry = optionalOpenTelemetry.get();
101111

102112
// generate a metric
103113
generateMetric(openTelemetry);
@@ -118,8 +128,12 @@ public void testBuildLogExporter() throws Exception {
118128
// create the OpenTelemetry SDK
119129
CountDownLatch countDownLatch = new CountDownLatch(1);
120130
CustomValidationPolicy customValidationPolicy = new CustomValidationPolicy(countDownLatch);
121-
OpenTelemetry openTelemetry
131+
Optional<OpenTelemetrySdk> optionalOpenTelemetry
122132
= TestUtils.createOpenTelemetrySdk(getHttpPipeline(customValidationPolicy), getConfiguration());
133+
if (!optionalOpenTelemetry.isPresent()) {
134+
return;
135+
}
136+
OpenTelemetrySdk openTelemetry = optionalOpenTelemetry.get();
123137

124138
// generate a log
125139
generateLog(openTelemetry);
@@ -145,8 +159,12 @@ public void testBuildLogExporterWithCustomEvent() throws Exception {
145159
// create the OpenTelemetry SDK
146160
CountDownLatch countDownLatch = new CountDownLatch(1);
147161
CustomValidationPolicy customValidationPolicy = new CustomValidationPolicy(countDownLatch);
148-
OpenTelemetry openTelemetry
162+
Optional<OpenTelemetrySdk> optionalOpenTelemetry
149163
= TestUtils.createOpenTelemetrySdk(getHttpPipeline(customValidationPolicy), getConfiguration());
164+
if (!optionalOpenTelemetry.isPresent()) {
165+
return;
166+
}
167+
OpenTelemetrySdk openTelemetry = optionalOpenTelemetry.get();
150168

151169
// generate a log
152170
generateEvent(openTelemetry);
@@ -172,8 +190,12 @@ public void testBuildTraceMetricLogExportersConsecutively() throws Exception {
172190
// create the OpenTelemetry SDK
173191
CountDownLatch countDownLatch = new CountDownLatch(3);
174192
CustomValidationPolicy customValidationPolicy = new CustomValidationPolicy(countDownLatch);
175-
OpenTelemetrySdk openTelemetry
193+
Optional<OpenTelemetrySdk> optionalOpenTelemetry
176194
= TestUtils.createOpenTelemetrySdk(getHttpPipeline(customValidationPolicy), getConfiguration());
195+
if (!optionalOpenTelemetry.isPresent()) {
196+
return;
197+
}
198+
OpenTelemetrySdk openTelemetry = optionalOpenTelemetry.get();
177199

178200
// generate telemetry
179201
generateSpan(openTelemetry);

sdk/monitor/azure-monitor-opentelemetry-autoconfigure/src/test/java/com/azure/monitor/opentelemetry/autoconfigure/AzureMonitorStatsbeatTest.java

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
import java.net.URL;
3232
import java.util.HashMap;
3333
import java.util.Map;
34+
import java.util.Optional;
3435
import java.util.concurrent.CountDownLatch;
3536
import java.util.concurrent.atomic.AtomicInteger;
3637
import java.util.function.Function;
@@ -51,9 +52,13 @@ public void testStatsbeat() throws Exception {
5152
// create the OpenTelemetry SDK
5253
CountDownLatch countDownLatch = new CountDownLatch(1);
5354
CustomValidationPolicy customValidationPolicy = new CustomValidationPolicy(countDownLatch);
54-
OpenTelemetrySdk openTelemetry
55+
Optional<OpenTelemetrySdk> optionalOpenTelemetry
5556
= TestUtils.createOpenTelemetrySdk(getHttpPipeline(customValidationPolicy, HttpClient.createDefault()),
5657
getStatsbeatConfiguration(), STATSBEAT_CONNECTION_STRING);
58+
if (!optionalOpenTelemetry.isPresent()) {
59+
return;
60+
}
61+
OpenTelemetrySdk openTelemetry = optionalOpenTelemetry.get();
5762

5863
// generate a metric
5964
generateMetric(openTelemetry);
@@ -93,10 +98,13 @@ private void verifyStatsbeatShutdownOrnNot(String fakeBody, boolean shutdown) th
9398
// create OpenTelemetrySdk
9499
CountDownLatch countDownLatch = new CountDownLatch(1);
95100
CustomValidationPolicy customValidationPolicy = new CustomValidationPolicy(countDownLatch);
96-
OpenTelemetrySdk openTelemetrySdk
101+
Optional<OpenTelemetrySdk> optionalOpenTelemetry
97102
= TestUtils.createOpenTelemetrySdk(getHttpPipeline(customValidationPolicy, mockedHttpClient),
98103
getStatsbeatConfiguration(), STATSBEAT_CONNECTION_STRING);
99-
104+
if (!optionalOpenTelemetry.isPresent()) {
105+
return;
106+
}
107+
OpenTelemetrySdk openTelemetrySdk = optionalOpenTelemetry.get();
100108
generateMetric(openTelemetrySdk);
101109

102110
// close to flush

sdk/monitor/azure-monitor-opentelemetry-autoconfigure/src/test/java/com/azure/monitor/opentelemetry/autoconfigure/EventHubsExporterIntegrationTest.java

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@
3434
import io.opentelemetry.api.trace.Span;
3535
import io.opentelemetry.api.trace.Tracer;
3636
import io.opentelemetry.context.Scope;
37+
import io.opentelemetry.sdk.OpenTelemetrySdk;
3738
import org.junit.jupiter.api.Disabled;
3839
import org.junit.jupiter.api.Test;
3940
import reactor.core.publisher.Mono;
@@ -42,6 +43,7 @@
4243
import java.nio.charset.StandardCharsets;
4344
import java.time.Duration;
4445
import java.util.List;
46+
import java.util.Optional;
4547
import java.util.concurrent.CountDownLatch;
4648
import java.util.concurrent.TimeUnit;
4749

@@ -107,7 +109,13 @@ private void checkTelemetry(HttpPipelineCallContext context) {
107109
}
108110
};
109111

110-
OpenTelemetry otel = TestUtils.createOpenTelemetrySdk(getHttpPipeline(validationPolicy));
112+
Optional<OpenTelemetrySdk> optionalOpenTelemetry
113+
= TestUtils.createOpenTelemetrySdk(getHttpPipeline(validationPolicy));
114+
if (!optionalOpenTelemetry.isPresent()) {
115+
return;
116+
}
117+
OpenTelemetrySdk otel = optionalOpenTelemetry.get();
118+
111119
Tracer tracer = otel.getTracer("Sample");
112120

113121
try (EventHubProducerAsyncClient producer = new EventHubClientBuilder().credential(credential)
@@ -156,7 +164,13 @@ public void processorTest() throws InterruptedException {
156164
});
157165
return next.process();
158166
};
159-
Tracer tracer = TestUtils.createOpenTelemetrySdk(getHttpPipeline(validationPolicy)).getTracer("Sample");
167+
Optional<OpenTelemetrySdk> optionalOpenTelemetry
168+
= TestUtils.createOpenTelemetrySdk(getHttpPipeline(validationPolicy));
169+
if (!optionalOpenTelemetry.isPresent()) {
170+
return;
171+
}
172+
OpenTelemetrySdk openTelemetrySdk = optionalOpenTelemetry.get();
173+
Tracer tracer = openTelemetrySdk.getTracer("Sample");
160174

161175
CountDownLatch partitionOwned = new CountDownLatch(1);
162176
CountDownLatch eventCountDown = new CountDownLatch(1);

sdk/monitor/azure-monitor-opentelemetry-autoconfigure/src/test/java/com/azure/monitor/opentelemetry/autoconfigure/implementation/utils/TestUtils.java

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
import io.opentelemetry.sdk.OpenTelemetrySdk;
2121
import io.opentelemetry.sdk.autoconfigure.AutoConfiguredOpenTelemetrySdk;
2222
import io.opentelemetry.sdk.autoconfigure.AutoConfiguredOpenTelemetrySdkBuilder;
23+
import io.opentelemetry.sdk.autoconfigure.spi.ConfigurationException;
2324
import io.opentelemetry.sdk.resources.Resource;
2425

2526
import java.io.IOException;
@@ -28,6 +29,7 @@
2829
import java.util.HashMap;
2930
import java.util.List;
3031
import java.util.Map;
32+
import java.util.Optional;
3133

3234
public final class TestUtils {
3335

@@ -76,25 +78,34 @@ public static TelemetryItem createMetricTelemetry(String name, int value, String
7678
return telemetry;
7779
}
7880

79-
public static OpenTelemetrySdk createOpenTelemetrySdk(HttpPipeline httpPipeline) {
81+
public static Optional<OpenTelemetrySdk> createOpenTelemetrySdk(HttpPipeline httpPipeline) {
8082
return createOpenTelemetrySdk(httpPipeline, Collections.emptyMap());
8183
}
8284

83-
public static OpenTelemetrySdk createOpenTelemetrySdk(HttpPipeline httpPipeline,
85+
public static Optional<OpenTelemetrySdk> createOpenTelemetrySdk(HttpPipeline httpPipeline,
8486
Map<String, String> configuration) {
8587
return createOpenTelemetrySdk(httpPipeline, configuration, TRACE_CONNECTION_STRING);
8688

8789
}
8890

89-
public static OpenTelemetrySdk createOpenTelemetrySdk(HttpPipeline httpPipeline, Map<String, String> configuration,
90-
String connectionString) {
91+
public static Optional<OpenTelemetrySdk> createOpenTelemetrySdk(HttpPipeline httpPipeline,
92+
Map<String, String> configuration, String connectionString) {
9193
AutoConfiguredOpenTelemetrySdkBuilder sdkBuilder = AutoConfiguredOpenTelemetrySdk.builder();
9294

9395
AzureMonitorAutoConfigureOptions exporterOptions
9496
= new AzureMonitorAutoConfigureOptions().connectionString(connectionString).pipeline(httpPipeline);
9597
AzureMonitorAutoConfigure.customize(sdkBuilder, exporterOptions);
9698

97-
return sdkBuilder.addPropertiesSupplier(() -> configuration).build().getOpenTelemetrySdk();
99+
try {
100+
return Optional.of(sdkBuilder.addPropertiesSupplier(() -> configuration).build().getOpenTelemetrySdk());
101+
} catch (ConfigurationException e) {
102+
if (e.getCause() instanceof IllegalArgumentException
103+
&& e.getCause().getMessage() != null
104+
&& e.getCause().getMessage().startsWith(TempDirs.UNABLE_TO_CREATE_DIRECTORY)) {
105+
return Optional.empty();
106+
}
107+
throw e;
108+
}
98109
}
99110

100111
// azure-json doesn't deserialize subtypes yet, so need to convert the abstract MonitorDomain to RemoteDependencyData

0 commit comments

Comments
 (0)