Skip to content
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ public class TempDirs {
private static final List<String> CANDIDATE_USERNAME_ENVIRONMENT_VARIABLES
= Collections.unmodifiableList(Arrays.asList("USER", "LOGNAME", "USERNAME"));

public static final String UNABLE_TO_CREATE_DIRECTORY = "Unable to create directory: ";

@Nullable
public static File getApplicationInsightsTempDir(ClientLogger logger, String message) {
File tempDir = new File(System.getProperty("java.io.tmpdir"));
Expand Down Expand Up @@ -51,7 +53,7 @@ public static File getSubDir(File parent, String name) {
File dir = new File(parent, name);

if (!dir.exists() && !dir.mkdirs()) {
throw new IllegalArgumentException("Unable to create directory: " + dir);
throw new IllegalArgumentException(UNABLE_TO_CREATE_DIRECTORY + dir);
}
if (!dir.canRead()) {
throw new IllegalArgumentException("Missing read permission to subdirectory: " + dir);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import io.opentelemetry.api.trace.Span;
import io.opentelemetry.api.trace.Tracer;
import io.opentelemetry.context.Scope;
import io.opentelemetry.sdk.OpenTelemetrySdk;
import org.junit.jupiter.api.Disabled;
import org.junit.jupiter.api.Test;
import reactor.core.publisher.Mono;
Expand All @@ -30,6 +31,7 @@
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.util.Optional;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.TimeUnit;
import java.util.zip.GZIPInputStream;
Expand All @@ -53,7 +55,12 @@ public void setConfigurationTest() throws InterruptedException {
CountDownLatch exporterCountDown = new CountDownLatch(1);

ValidationPolicy validationPolicy = new ValidationPolicy(exporterCountDown, "set-config-exporter-testing");
OpenTelemetry openTelemetry = TestUtils.createOpenTelemetrySdk(getHttpPipeline(validationPolicy));
Optional<OpenTelemetrySdk> optionalOpenTelemetry
= TestUtils.createOpenTelemetrySdk(getHttpPipeline(validationPolicy));
if (!optionalOpenTelemetry.isPresent()) {
return;
}
OpenTelemetrySdk openTelemetry = optionalOpenTelemetry.get();

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

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

ValidationPolicy validationPolicy = new ValidationPolicy(exporterCountDown, "disable-config-exporter-testing");
OpenTelemetry openTelemetry = TestUtils.createOpenTelemetrySdk(getHttpPipeline(validationPolicy));
Optional<OpenTelemetrySdk> optionalOpenTelemetry
= TestUtils.createOpenTelemetrySdk(getHttpPipeline(validationPolicy));
if (!optionalOpenTelemetry.isPresent()) {
return;
}
OpenTelemetrySdk openTelemetry = optionalOpenTelemetry.get();

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

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.concurrent.CountDownLatch;

import static java.util.concurrent.TimeUnit.SECONDS;
Expand All @@ -53,7 +54,12 @@ public void testBuildTraceExporter() throws Exception {
CountDownLatch countDownLatch = new CountDownLatch(numberOfSpans);
CustomValidationPolicy customValidationPolicy = new CustomValidationPolicy(countDownLatch);
HttpPipeline httpPipeline = getHttpPipeline(customValidationPolicy);
OpenTelemetry openTelemetry = TestUtils.createOpenTelemetrySdk(httpPipeline, getConfiguration());
Optional<OpenTelemetrySdk> optionalOpenTelemetry
= TestUtils.createOpenTelemetrySdk(httpPipeline, getConfiguration());
if (!optionalOpenTelemetry.isPresent()) {
return;
}
OpenTelemetrySdk openTelemetry = optionalOpenTelemetry.get();

// generate spans
for (int i = 0; i < numberOfSpans; i++) {
Expand Down Expand Up @@ -96,8 +102,12 @@ public void testBuildMetricExporter() throws Exception {
// create the OpenTelemetry SDK
CountDownLatch countDownLatch = new CountDownLatch(1);
CustomValidationPolicy customValidationPolicy = new CustomValidationPolicy(countDownLatch);
OpenTelemetrySdk openTelemetry
Optional<OpenTelemetrySdk> optionalOpenTelemetry
= TestUtils.createOpenTelemetrySdk(getHttpPipeline(customValidationPolicy), getConfiguration());
if (!optionalOpenTelemetry.isPresent()) {
return;
}
OpenTelemetrySdk openTelemetry = optionalOpenTelemetry.get();

// generate a metric
generateMetric(openTelemetry);
Expand All @@ -118,8 +128,12 @@ public void testBuildLogExporter() throws Exception {
// create the OpenTelemetry SDK
CountDownLatch countDownLatch = new CountDownLatch(1);
CustomValidationPolicy customValidationPolicy = new CustomValidationPolicy(countDownLatch);
OpenTelemetry openTelemetry
Optional<OpenTelemetrySdk> optionalOpenTelemetry
= TestUtils.createOpenTelemetrySdk(getHttpPipeline(customValidationPolicy), getConfiguration());
if (!optionalOpenTelemetry.isPresent()) {
return;
}
OpenTelemetrySdk openTelemetry = optionalOpenTelemetry.get();

// generate a log
generateLog(openTelemetry);
Expand All @@ -145,8 +159,12 @@ public void testBuildLogExporterWithCustomEvent() throws Exception {
// create the OpenTelemetry SDK
CountDownLatch countDownLatch = new CountDownLatch(1);
CustomValidationPolicy customValidationPolicy = new CustomValidationPolicy(countDownLatch);
OpenTelemetry openTelemetry
Optional<OpenTelemetrySdk> optionalOpenTelemetry
= TestUtils.createOpenTelemetrySdk(getHttpPipeline(customValidationPolicy), getConfiguration());
if (!optionalOpenTelemetry.isPresent()) {
return;
}
OpenTelemetrySdk openTelemetry = optionalOpenTelemetry.get();

// generate a log
generateEvent(openTelemetry);
Expand All @@ -172,8 +190,12 @@ public void testBuildTraceMetricLogExportersConsecutively() throws Exception {
// create the OpenTelemetry SDK
CountDownLatch countDownLatch = new CountDownLatch(3);
CustomValidationPolicy customValidationPolicy = new CustomValidationPolicy(countDownLatch);
OpenTelemetrySdk openTelemetry
Optional<OpenTelemetrySdk> optionalOpenTelemetry
= TestUtils.createOpenTelemetrySdk(getHttpPipeline(customValidationPolicy), getConfiguration());
if (!optionalOpenTelemetry.isPresent()) {
return;
}
OpenTelemetrySdk openTelemetry = optionalOpenTelemetry.get();

// generate telemetry
generateSpan(openTelemetry);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
import java.net.URL;
import java.util.HashMap;
import java.util.Map;
import java.util.Optional;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.function.Function;
Expand All @@ -51,9 +52,13 @@ public void testStatsbeat() throws Exception {
// create the OpenTelemetry SDK
CountDownLatch countDownLatch = new CountDownLatch(1);
CustomValidationPolicy customValidationPolicy = new CustomValidationPolicy(countDownLatch);
OpenTelemetrySdk openTelemetry
Optional<OpenTelemetrySdk> optionalOpenTelemetry
= TestUtils.createOpenTelemetrySdk(getHttpPipeline(customValidationPolicy, HttpClient.createDefault()),
getStatsbeatConfiguration(), STATSBEAT_CONNECTION_STRING);
if (!optionalOpenTelemetry.isPresent()) {
return;
}
OpenTelemetrySdk openTelemetry = optionalOpenTelemetry.get();

// generate a metric
generateMetric(openTelemetry);
Expand Down Expand Up @@ -93,10 +98,13 @@ private void verifyStatsbeatShutdownOrnNot(String fakeBody, boolean shutdown) th
// create OpenTelemetrySdk
CountDownLatch countDownLatch = new CountDownLatch(1);
CustomValidationPolicy customValidationPolicy = new CustomValidationPolicy(countDownLatch);
OpenTelemetrySdk openTelemetrySdk
Optional<OpenTelemetrySdk> optionalOpenTelemetry
= TestUtils.createOpenTelemetrySdk(getHttpPipeline(customValidationPolicy, mockedHttpClient),
getStatsbeatConfiguration(), STATSBEAT_CONNECTION_STRING);

if (!optionalOpenTelemetry.isPresent()) {
return;
}
OpenTelemetrySdk openTelemetrySdk = optionalOpenTelemetry.get();
generateMetric(openTelemetrySdk);

// close to flush
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
import io.opentelemetry.api.trace.Span;
import io.opentelemetry.api.trace.Tracer;
import io.opentelemetry.context.Scope;
import io.opentelemetry.sdk.OpenTelemetrySdk;
import org.junit.jupiter.api.Disabled;
import org.junit.jupiter.api.Test;
import reactor.core.publisher.Mono;
Expand All @@ -42,6 +43,7 @@
import java.nio.charset.StandardCharsets;
import java.time.Duration;
import java.util.List;
import java.util.Optional;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.TimeUnit;

Expand Down Expand Up @@ -107,7 +109,13 @@ private void checkTelemetry(HttpPipelineCallContext context) {
}
};

OpenTelemetry otel = TestUtils.createOpenTelemetrySdk(getHttpPipeline(validationPolicy));
Optional<OpenTelemetrySdk> optionalOpenTelemetry
= TestUtils.createOpenTelemetrySdk(getHttpPipeline(validationPolicy));
if (!optionalOpenTelemetry.isPresent()) {
return;
}
OpenTelemetrySdk otel = optionalOpenTelemetry.get();

Tracer tracer = otel.getTracer("Sample");

try (EventHubProducerAsyncClient producer = new EventHubClientBuilder().credential(credential)
Expand Down Expand Up @@ -156,7 +164,13 @@ public void processorTest() throws InterruptedException {
});
return next.process();
};
Tracer tracer = TestUtils.createOpenTelemetrySdk(getHttpPipeline(validationPolicy)).getTracer("Sample");
Optional<OpenTelemetrySdk> optionalOpenTelemetry
= TestUtils.createOpenTelemetrySdk(getHttpPipeline(validationPolicy));
if (!optionalOpenTelemetry.isPresent()) {
return;
}
OpenTelemetrySdk openTelemetrySdk = optionalOpenTelemetry.get();
Tracer tracer = openTelemetrySdk.getTracer("Sample");

CountDownLatch partitionOwned = new CountDownLatch(1);
CountDownLatch eventCountDown = new CountDownLatch(1);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import io.opentelemetry.sdk.OpenTelemetrySdk;
import io.opentelemetry.sdk.autoconfigure.AutoConfiguredOpenTelemetrySdk;
import io.opentelemetry.sdk.autoconfigure.AutoConfiguredOpenTelemetrySdkBuilder;
import io.opentelemetry.sdk.autoconfigure.spi.ConfigurationException;
import io.opentelemetry.sdk.resources.Resource;

import java.io.IOException;
Expand All @@ -28,6 +29,7 @@
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Optional;

public final class TestUtils {

Expand Down Expand Up @@ -76,25 +78,34 @@ public static TelemetryItem createMetricTelemetry(String name, int value, String
return telemetry;
}

public static OpenTelemetrySdk createOpenTelemetrySdk(HttpPipeline httpPipeline) {
public static Optional<OpenTelemetrySdk> createOpenTelemetrySdk(HttpPipeline httpPipeline) {
return createOpenTelemetrySdk(httpPipeline, Collections.emptyMap());
}

public static OpenTelemetrySdk createOpenTelemetrySdk(HttpPipeline httpPipeline,
public static Optional<OpenTelemetrySdk> createOpenTelemetrySdk(HttpPipeline httpPipeline,
Map<String, String> configuration) {
return createOpenTelemetrySdk(httpPipeline, configuration, TRACE_CONNECTION_STRING);

}

public static OpenTelemetrySdk createOpenTelemetrySdk(HttpPipeline httpPipeline, Map<String, String> configuration,
String connectionString) {
public static Optional<OpenTelemetrySdk> createOpenTelemetrySdk(HttpPipeline httpPipeline,
Map<String, String> configuration, String connectionString) {
AutoConfiguredOpenTelemetrySdkBuilder sdkBuilder = AutoConfiguredOpenTelemetrySdk.builder();

AzureMonitorAutoConfigureOptions exporterOptions
= new AzureMonitorAutoConfigureOptions().connectionString(connectionString).pipeline(httpPipeline);
AzureMonitorAutoConfigure.customize(sdkBuilder, exporterOptions);

return sdkBuilder.addPropertiesSupplier(() -> configuration).build().getOpenTelemetrySdk();
try {
return Optional.of(sdkBuilder.addPropertiesSupplier(() -> configuration).build().getOpenTelemetrySdk());
} catch (ConfigurationException e) {
if (e.getCause() instanceof IllegalArgumentException
&& e.getCause().getMessage() != null
&& e.getCause().getMessage().startsWith(TempDirs.UNABLE_TO_CREATE_DIRECTORY)) {
return Optional.empty();
}
throw e;
}
}

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