diff --git a/benchmark-overhead/src/test/java/io/opentelemetry/agents/LatestAgentSnapshotResolver.java b/benchmark-overhead/src/test/java/io/opentelemetry/agents/LatestAgentSnapshotResolver.java index 4685390ab037..58aaa1aa5049 100644 --- a/benchmark-overhead/src/test/java/io/opentelemetry/agents/LatestAgentSnapshotResolver.java +++ b/benchmark-overhead/src/test/java/io/opentelemetry/agents/LatestAgentSnapshotResolver.java @@ -13,6 +13,7 @@ import java.nio.file.Paths; import java.nio.file.StandardOpenOption; import java.util.Optional; +import java.time.Duration; import okhttp3.OkHttpClient; import okhttp3.Request; import okhttp3.Response; @@ -30,6 +31,11 @@ public class LatestAgentSnapshotResolver { "https://oss.sonatype.org/content/repositories/snapshots/io/opentelemetry/javaagent/opentelemetry-javaagent"; static final String LATEST_SNAPSHOT_META = BASE_URL + "/maven-metadata.xml"; + private static final OkHttpClient client = new OkHttpClient.Builder() + .connectTimeout(Duration.ofMinutes(1)) + .readTimeout(Duration.ofMinutes(1)) + .build(); + Optional resolve() throws IOException { String version = fetchLatestSnapshotVersion(); logger.info("Latest snapshot version is {}", version); @@ -81,11 +87,33 @@ private byte[] fetchBodyBytesFrom(String url) throws IOException { return fetchBodyFrom(url).bytes(); } + // The sonatype repository can be very unreliable, so we retry a few times private ResponseBody fetchBodyFrom(String url) throws IOException { Request request = new Request.Builder().url(url).build(); - OkHttpClient client = new OkHttpClient(); - Response response = client.newCall(request).execute(); - ResponseBody body = response.body(); - return body; + IOException lastException = null; + + for (int attempt = 0; attempt < 3; attempt++) { + try { + try (Response response = client.newCall(request).execute()) { + if (!response.isSuccessful()) { + throw new IOException("Unexpected HTTP code " + response.code() + " for " + url); + } + ResponseBody body = response.body(); + if (body != null) { + byte[] data = body.bytes(); + return ResponseBody.create(data, body.contentType()); + } else { + throw new IOException("Response body is null"); + } + } + } catch (IOException e) { + lastException = e; + if (attempt < 2) { + logger.warn("Attempt {} to fetch {} failed: {}. Retrying...", attempt + 1, url, e.getMessage()); + } + } + } + throw lastException; } } +