-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
NH-37575: download agent from GitHub
- Loading branch information
1 parent
9ff38e6
commit 8a50dc0
Showing
9 changed files
with
196 additions
and
90 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
142 changes: 105 additions & 37 deletions
142
benchmark/src/test/java/io/opentelemetry/agents/LatestSolarwindsAgentResolver.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,52 +1,120 @@ | ||
package io.opentelemetry.agents; | ||
|
||
import com.fasterxml.jackson.annotation.JsonIgnoreProperties; | ||
import okhttp3.OkHttpClient; | ||
import okhttp3.Request; | ||
import okhttp3.Response; | ||
|
||
import java.nio.file.Files; | ||
import java.nio.file.Path; | ||
import java.nio.file.Paths; | ||
import java.nio.file.StandardOpenOption; | ||
import java.util.List; | ||
import java.util.Optional; | ||
import okhttp3.OkHttpClient; | ||
import okhttp3.Request; | ||
import okhttp3.Response; | ||
import org.testcontainers.shaded.com.fasterxml.jackson.core.type.TypeReference; | ||
import org.testcontainers.shaded.com.fasterxml.jackson.databind.ObjectMapper; | ||
|
||
public class LatestSolarwindsAgentResolver { | ||
private static final String NH_URL = "https://agent-binaries.global.st-ssp.solarwinds.com/apm/java/latest/solarwinds-apm-agent.jar"; | ||
private static final String AO_URL = "https://files.appoptics.com/java/latest/appoptics-agent.jar"; | ||
private static final String NH_AGENT_JAR_NAME = "solarwinds-apm-agent.jar"; | ||
private static final String AO_AGENT_JAR_NAME = "appoptics-agent.jar"; | ||
public static boolean useAOAgent = "AO".equals(System.getenv("AGENT_TYPE")); | ||
|
||
Optional<Path> resolve() throws Exception { | ||
return Optional.of(downloadAgent()); | ||
} | ||
|
||
private Path downloadAgent() throws Exception { | ||
String assetURL; | ||
if (useAOAgent) { | ||
assetURL = AO_URL; | ||
} else { | ||
assetURL = NH_URL; | ||
} | ||
private static final String NH_URL = " https://api.github.com/repos/solarwinds/apm-java/releases"; | ||
private static final String AO_URL = | ||
"https://files.appoptics.com/java/latest/appoptics-agent.jar"; | ||
private static final String NH_AGENT_JAR_NAME = "solarwinds-apm-agent.jar"; | ||
private static final String AO_AGENT_JAR_NAME = "appoptics-agent.jar"; | ||
public static boolean useAOAgent = "AO".equals(System.getenv("AGENT_TYPE")); | ||
|
||
OkHttpClient client = new OkHttpClient(); | ||
Request request = new Request.Builder().url(assetURL) | ||
.header("Authorization", "token " + System.getenv("GITHUB_TOKEN")) | ||
.header("Accept", "application/octet-stream").build(); | ||
|
||
Path path = Paths.get(".", useAOAgent ? AO_AGENT_JAR_NAME : NH_AGENT_JAR_NAME); | ||
try (Response response = client.newCall(request).execute()) { | ||
assert response.body() != null; | ||
byte[] fileRaw = response.body().bytes(); | ||
Files.write( | ||
path, | ||
fileRaw, | ||
StandardOpenOption.CREATE, | ||
StandardOpenOption.WRITE, | ||
StandardOpenOption.TRUNCATE_EXISTING); | ||
Optional<Path> resolve() throws Exception { | ||
return Optional.of(downloadAgent()); | ||
} | ||
|
||
private Path downloadAgent() throws Exception { | ||
String assetURL; | ||
OkHttpClient client = new OkHttpClient(); | ||
if (useAOAgent) { | ||
assetURL = AO_URL; | ||
} else { | ||
assetURL = NH_URL; | ||
Request request = | ||
new Request.Builder() | ||
.url(NH_URL) | ||
.header("Authorization", "token " + System.getenv("GITHUB_TOKEN")) | ||
.header("Accept", "application/vnd.github.v3+json") | ||
.build(); | ||
|
||
try (Response response = client.newCall(request).execute()) { | ||
assert response.body() != null; | ||
byte[] raw = response.body().bytes(); | ||
|
||
ObjectMapper mapper = new ObjectMapper(); | ||
List<GithubRelease> releases = | ||
mapper.readValue(raw, new TypeReference<List<GithubRelease>>() {}); | ||
|
||
outerLoop: | ||
for (GithubRelease release : releases) { | ||
for (Asset asset : release.assets) { | ||
if (asset.name.equals(NH_AGENT_JAR_NAME)) { | ||
assetURL = asset.url; | ||
break outerLoop; | ||
} | ||
} | ||
} | ||
return path; | ||
if (assetURL == null) { | ||
throw new RuntimeException("Asset url not found for the NH agent."); | ||
} | ||
} | ||
} | ||
|
||
Request request = | ||
new Request.Builder() | ||
.url(assetURL) | ||
.header("Authorization", "token " + System.getenv("GITHUB_TOKEN")) | ||
.header("Accept", "application/octet-stream") | ||
.build(); | ||
|
||
Path path = Paths.get(".", useAOAgent ? AO_AGENT_JAR_NAME : NH_AGENT_JAR_NAME); | ||
try (Response response = client.newCall(request).execute()) { | ||
assert response.body() != null; | ||
byte[] fileRaw = response.body().bytes(); | ||
Files.write( | ||
path, | ||
fileRaw, | ||
StandardOpenOption.CREATE, | ||
StandardOpenOption.WRITE, | ||
StandardOpenOption.TRUNCATE_EXISTING); | ||
} | ||
return path; | ||
} | ||
|
||
@JsonIgnoreProperties(ignoreUnknown = true) | ||
public static class GithubRelease { | ||
private List<Asset> assets; | ||
|
||
public List<Asset> getAssets() { | ||
return assets; | ||
} | ||
|
||
public void setAssets(List<Asset> assets) { | ||
this.assets = assets; | ||
} | ||
} | ||
|
||
@JsonIgnoreProperties(ignoreUnknown = true) | ||
public static class Asset { | ||
private String url; | ||
private String name; | ||
|
||
public String getUrl() { | ||
return url; | ||
} | ||
|
||
public String getName() { | ||
return name; | ||
} | ||
|
||
public void setUrl(String url) { | ||
this.url = url; | ||
} | ||
|
||
public void setName(String name) { | ||
this.name = name; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters