Skip to content

Commit

Permalink
Use OkHttp for AWS resource detectors
Browse files Browse the repository at this point in the history
  • Loading branch information
anuraaga committed Dec 14, 2021
1 parent cbecf1f commit ddebd09
Show file tree
Hide file tree
Showing 7 changed files with 172 additions and 225 deletions.
1 change: 1 addition & 0 deletions sdk-extensions/aws/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ dependencies {
implementation(project(":semconv"))

implementation("com.fasterxml.jackson.core:jackson-core")
implementation("com.squareup.okhttp3:okhttp")

testImplementation(project(":sdk:testing"))
testImplementation(project(":sdk-extensions:autoconfigure"))
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
import com.fasterxml.jackson.core.JsonToken;
import io.opentelemetry.api.common.Attributes;
import io.opentelemetry.api.common.AttributesBuilder;
import io.opentelemetry.sdk.extension.aws.internal.JdkHttpClient;
import io.opentelemetry.sdk.resources.Resource;
import io.opentelemetry.semconv.resource.attributes.ResourceAttributes;
import java.io.IOException;
Expand Down Expand Up @@ -139,7 +138,7 @@ private static String fetchHostname(URL hostnameUrl, String token) {

// Generic HTTP fetch function for IMDS.
private static String fetchString(String httpMethod, URL url, String token, boolean includeTtl) {
JdkHttpClient client = new JdkHttpClient();
SimpleHttpClient client = new SimpleHttpClient();
Map<String, String> headers = new HashMap<>();

if (includeTtl) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
import com.fasterxml.jackson.core.JsonToken;
import io.opentelemetry.api.common.Attributes;
import io.opentelemetry.api.common.AttributesBuilder;
import io.opentelemetry.sdk.extension.aws.internal.JdkHttpClient;
import io.opentelemetry.sdk.resources.Resource;
import io.opentelemetry.semconv.resource.attributes.ResourceAttributes;
import java.io.File;
Expand Down Expand Up @@ -52,16 +51,16 @@ public static Resource get() {
}

private static Resource buildResource() {
return buildResource(new JdkHttpClient(), new DockerHelper(), K8S_TOKEN_PATH, K8S_CERT_PATH);
return buildResource(new SimpleHttpClient(), new DockerHelper(), K8S_TOKEN_PATH, K8S_CERT_PATH);
}

// Visible for testing
static Resource buildResource(
JdkHttpClient jdkHttpClient,
SimpleHttpClient httpClient,
DockerHelper dockerHelper,
String k8sTokenPath,
String k8sKeystorePath) {
if (!isEks(k8sTokenPath, k8sKeystorePath, jdkHttpClient)) {
if (!isEks(k8sTokenPath, k8sKeystorePath, httpClient)) {
return Resource.empty();
}

Expand All @@ -70,7 +69,7 @@ static Resource buildResource(
attrBuilders.put(
ResourceAttributes.CLOUD_PLATFORM, ResourceAttributes.CloudPlatformValues.AWS_EKS);

String clusterName = getClusterName(jdkHttpClient);
String clusterName = getClusterName(httpClient);
if (clusterName != null && !clusterName.isEmpty()) {
attrBuilders.put(ResourceAttributes.K8S_CLUSTER_NAME, clusterName);
}
Expand All @@ -84,7 +83,7 @@ static Resource buildResource(
}

private static boolean isEks(
String k8sTokenPath, String k8sKeystorePath, JdkHttpClient jdkHttpClient) {
String k8sTokenPath, String k8sKeystorePath, SimpleHttpClient httpClient) {
if (!isK8s(k8sTokenPath, k8sKeystorePath)) {
logger.log(Level.FINE, "Not running on k8s.");
return false;
Expand All @@ -93,7 +92,7 @@ private static boolean isEks(
Map<String, String> requestProperties = new HashMap<>();
requestProperties.put("Authorization", getK8sCredHeader());
String awsAuth =
jdkHttpClient.fetchString(
httpClient.fetchString(
"GET", K8S_SVC_URL + AUTH_CONFIGMAP_PATH, requestProperties, K8S_CERT_PATH);

return awsAuth != null && !awsAuth.isEmpty();
Expand All @@ -105,11 +104,11 @@ private static boolean isK8s(String k8sTokenPath, String k8sKeystorePath) {
return k8sTokeyFile.exists() && k8sKeystoreFile.exists();
}

private static String getClusterName(JdkHttpClient jdkHttpClient) {
private static String getClusterName(SimpleHttpClient httpClient) {
Map<String, String> requestProperties = new HashMap<>();
requestProperties.put("Authorization", getK8sCredHeader());
String json =
jdkHttpClient.fetchString(
httpClient.fetchString(
"GET", K8S_SVC_URL + CW_CONFIGMAP_PATH, requestProperties, K8S_CERT_PATH);

try (JsonParser parser = JSON_FACTORY.createParser(json)) {
Expand Down
Loading

0 comments on commit ddebd09

Please sign in to comment.