Skip to content

Commit

Permalink
Migrate AwsResource to ResourceProvider. (#1582)
Browse files Browse the repository at this point in the history
  • Loading branch information
Anuraag Agrawal authored Aug 25, 2020
1 parent 7e87a69 commit 07af1d1
Show file tree
Hide file tree
Showing 9 changed files with 76 additions and 130 deletions.

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,17 @@
import com.google.common.annotations.VisibleForTesting;
import io.opentelemetry.common.Attributes;
import io.opentelemetry.sdk.resources.ResourceConstants;
import io.opentelemetry.sdk.resources.ResourceProvider;
import java.io.File;
import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;

class BeanstalkResource extends AwsResource {
/**
* A {@link ResourceProvider} which provides information about the current EC2 instance if running
* on AWS Elastic Beanstalk.
*/
public class BeanstalkResource extends ResourceProvider {

private static final Logger logger = Logger.getLogger(BeanstalkResource.class.getName());

Expand All @@ -39,7 +44,11 @@ class BeanstalkResource extends AwsResource {

private final String configPath;

BeanstalkResource() {
/**
* Returns a {@link BeanstalkResource} which attempts to compute information about the Beanstalk
* environment if available.
*/
public BeanstalkResource() {
this(BEANSTALK_CONF_PATH);
}

Expand All @@ -49,7 +58,7 @@ class BeanstalkResource extends AwsResource {
}

@Override
Attributes createAttributes() {
public Attributes getAttributes() {
File configFile = new File(configPath);
if (!configFile.exists()) {
return Attributes.empty();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import com.google.common.io.ByteStreams;
import io.opentelemetry.common.Attributes;
import io.opentelemetry.sdk.resources.ResourceConstants;
import io.opentelemetry.sdk.resources.ResourceProvider;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
Expand All @@ -35,7 +36,11 @@
import java.util.logging.Level;
import java.util.logging.Logger;

class Ec2Resource extends AwsResource {
/**
* A {@link ResourceProvider} which provides information about the current EC2 instance if running
* on AWS EC2.
*/
public class Ec2Resource extends ResourceProvider {

private static final Logger logger = Logger.getLogger(Ec2Resource.class.getName());

Expand All @@ -49,7 +54,11 @@ class Ec2Resource extends AwsResource {
private final URL hostnameUrl;
private final URL tokenUrl;

Ec2Resource() {
/**
* Returns a {@link Ec2Resource} which attempts to compute information about this instance if
* available.
*/
public Ec2Resource() {
// This is only for testing e.g., with a mock IMDS server and never in production so we just
// read from a system property. This is similar to the AWS SDK.
this(System.getProperty("otel.aws.imds.endpointOverride", DEFAULT_IMDS_ENDPOINT));
Expand Down Expand Up @@ -137,7 +146,7 @@ private static String readResponseString(HttpURLConnection connection) {
}

@Override
Attributes createAttributes() {
public Attributes getAttributes() {
String token = fetchToken();

// If token is empty, either IMDSv2 isn't enabled or an unexpected failure happened. We can
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,18 @@
import com.google.common.base.Strings;
import io.opentelemetry.common.Attributes;
import io.opentelemetry.sdk.resources.ResourceConstants;
import io.opentelemetry.sdk.resources.ResourceProvider;
import java.net.InetAddress;
import java.net.UnknownHostException;
import java.util.Map;
import java.util.logging.Level;
import java.util.logging.Logger;

class EcsResource extends AwsResource {
/**
* A {@link ResourceProvider} which provides information about the current ECS container if running
* on AWS ECS.
*/
public class EcsResource extends ResourceProvider {

private static final Logger logger = Logger.getLogger(EcsResource.class.getName());

Expand All @@ -36,7 +41,11 @@ class EcsResource extends AwsResource {
private final Map<String, String> sysEnv;
private final DockerHelper dockerHelper;

EcsResource() {
/**
* Returns a {@link Ec2Resource} which attempts to compute information about this ECS container if
* available.
*/
public EcsResource() {
this(System.getenv(), new DockerHelper());
}

Expand All @@ -47,7 +56,7 @@ class EcsResource extends AwsResource {
}

@Override
Attributes createAttributes() {
public Attributes getAttributes() {
if (!isOnEcs()) {
return Attributes.empty();
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
io.opentelemetry.sdk.extensions.trace.aws.resource.BeanstalkResource
io.opentelemetry.sdk.extensions.trace.aws.resource.Ec2Resource
io.opentelemetry.sdk.extensions.trace.aws.resource.EcsResource

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,10 @@
import com.google.common.io.Files;
import io.opentelemetry.common.Attributes;
import io.opentelemetry.sdk.resources.ResourceConstants;
import io.opentelemetry.sdk.resources.ResourceProvider;
import java.io.File;
import java.io.IOException;
import java.util.ServiceLoader;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.io.TempDir;

Expand All @@ -38,7 +40,7 @@ void testCreateAttributes(@TempDir File tempFolder) throws IOException {
+ "version_label\":\"2\",\"environment_name\":\"HttpSubscriber-env\"}";
Files.write(content.getBytes(Charsets.UTF_8), file);
BeanstalkResource populator = new BeanstalkResource(file.getPath());
Attributes attributes = populator.createAttributes();
Attributes attributes = populator.getAttributes();
assertThat(attributes)
.isEqualTo(
Attributes.of(
Expand All @@ -50,7 +52,7 @@ ResourceConstants.SERVICE_VERSION, stringAttributeValue("2"),
@Test
void testConfigFileMissing() throws IOException {
BeanstalkResource populator = new BeanstalkResource("a_file_never_existing");
Attributes attributes = populator.createAttributes();
Attributes attributes = populator.getAttributes();
assertThat(attributes.isEmpty()).isTrue();
}

Expand All @@ -62,7 +64,15 @@ void testBadConfigFile(@TempDir File tempFolder) throws IOException {
+ "environment_name\":\"HttpSubscriber-env\"}";
Files.write(content.getBytes(Charsets.UTF_8), file);
BeanstalkResource populator = new BeanstalkResource(file.getPath());
Attributes attributes = populator.createAttributes();
Attributes attributes = populator.getAttributes();
assertThat(attributes.isEmpty()).isTrue();
}

@Test
void inServiceLoader() {
// No practical way to test the attributes themselves so at least check the service loader picks
// it up.
assertThat(ServiceLoader.load(ResourceProvider.class))
.anyMatch(BeanstalkResource.class::isInstance);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@
import com.github.tomakehurst.wiremock.junit.WireMockClassRule;
import io.opentelemetry.common.Attributes;
import io.opentelemetry.sdk.resources.ResourceConstants;
import io.opentelemetry.sdk.resources.ResourceProvider;
import java.util.ServiceLoader;
import org.junit.Before;
import org.junit.ClassRule;
import org.junit.Test;
Expand Down Expand Up @@ -77,7 +79,7 @@ public void imdsv2() {
.willReturn(okJson(IDENTITY_DOCUMENT)));
stubFor(any(urlPathEqualTo("/latest/meta-data/hostname")).willReturn(ok("ec2-1-2-3-4")));

Attributes attributes = populator.createAttributes();
Attributes attributes = populator.getAttributes();
Attributes.Builder expectedAttrBuilders = Attributes.newBuilder();
expectedAttrBuilders.setAttribute(ResourceConstants.HOST_ID, "i-1234567890abcdef0");
expectedAttrBuilders.setAttribute(ResourceConstants.CLOUD_ZONE, "us-west-2b");
Expand Down Expand Up @@ -108,7 +110,7 @@ public void imdsv1() {
.willReturn(okJson(IDENTITY_DOCUMENT)));
stubFor(any(urlPathEqualTo("/latest/meta-data/hostname")).willReturn(ok("ec2-1-2-3-4")));

Attributes attributes = populator.createAttributes();
Attributes attributes = populator.getAttributes();
Attributes.Builder expectedAttrBuilders = Attributes.newBuilder();
expectedAttrBuilders.setAttribute(ResourceConstants.HOST_ID, "i-1234567890abcdef0");
expectedAttrBuilders.setAttribute(ResourceConstants.CLOUD_ZONE, "us-west-2b");
Expand All @@ -135,7 +137,7 @@ public void badJson() {
any(urlPathEqualTo("/latest/dynamic/instance-identity/document"))
.willReturn(okJson("I'm not JSON")));

Attributes attributes = populator.createAttributes();
Attributes attributes = populator.getAttributes();
assertThat(attributes.isEmpty()).isTrue();

verify(
Expand All @@ -145,4 +147,11 @@ public void badJson() {
getRequestedFor(urlEqualTo("/latest/dynamic/instance-identity/document"))
.withoutHeader("X-aws-ec2-metadata-token"));
}

@Test
public void inServiceLoader() {
// No practical way to test the attributes themselves so at least check the service loader picks
// it up.
assertThat(ServiceLoader.load(ResourceProvider.class)).anyMatch(Ec2Resource.class::isInstance);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,12 @@

import io.opentelemetry.common.Attributes;
import io.opentelemetry.sdk.resources.ResourceConstants;
import io.opentelemetry.sdk.resources.ResourceProvider;
import java.net.InetAddress;
import java.net.UnknownHostException;
import java.util.HashMap;
import java.util.Map;
import java.util.ServiceLoader;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.Mock;
Expand All @@ -44,7 +46,7 @@ void testCreateAttributes() throws UnknownHostException {
Map<String, String> mockSysEnv = new HashMap<>();
mockSysEnv.put(ECS_METADATA_KEY_V3, "ecs_metadata_v3_uri");
EcsResource populator = new EcsResource(mockSysEnv, mockDockerHelper);
Attributes attributes = populator.createAttributes();
Attributes attributes = populator.getAttributes();
assertThat(attributes)
.isEqualTo(
Attributes.of(
Expand All @@ -60,7 +62,7 @@ void testNotOnEcs() {
mockSysEnv.put(ECS_METADATA_KEY_V3, "");
mockSysEnv.put(ECS_METADATA_KEY_V4, "");
EcsResource populator = new EcsResource(mockSysEnv, mockDockerHelper);
Attributes attributes = populator.createAttributes();
Attributes attributes = populator.getAttributes();
assertThat(attributes.isEmpty()).isTrue();
}

Expand All @@ -70,11 +72,18 @@ void testContainerIdMissing() throws UnknownHostException {
Map<String, String> mockSysEnv = new HashMap<>();
mockSysEnv.put(ECS_METADATA_KEY_V4, "ecs_metadata_v4_uri");
EcsResource populator = new EcsResource(mockSysEnv, mockDockerHelper);
Attributes attributes = populator.createAttributes();
Attributes attributes = populator.getAttributes();
assertThat(attributes)
.isEqualTo(
Attributes.of(
ResourceConstants.CONTAINER_NAME,
stringAttributeValue(InetAddress.getLocalHost().getHostName())));
}

@Test
void inServiceLoader() {
// No practical way to test the attributes themselves so at least check the service loader picks
// it up.
assertThat(ServiceLoader.load(ResourceProvider.class)).anyMatch(EcsResource.class::isInstance);
}
}

0 comments on commit 07af1d1

Please sign in to comment.