Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Rename GCPResource -> GCPResourceDetector #271

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
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 @@ -29,19 +29,19 @@
* Google Compute Engine (GCE), Google Kubernetes Engine (GKE), Google Cloud Functions (GCF), Google
* App Engine (GAE) and Google Cloud Run (GCR).
*/
public class GCPResource implements ResourceProvider {
public class GCPResourceDetector implements ResourceProvider {
private final GCPMetadataConfig metadata;
private final EnvVars envVars;

private static final Logger LOGGER = Logger.getLogger(GCPResource.class.getSimpleName());
private static final Logger LOGGER = Logger.getLogger(GCPResourceDetector.class.getSimpleName());

public GCPResource() {
public GCPResourceDetector() {
this.metadata = GCPMetadataConfig.DEFAULT_INSTANCE;
this.envVars = EnvVars.DEFAULT_INSTANCE;
}

// for testing only
GCPResource(GCPMetadataConfig metadata, EnvVars envVars) {
GCPResourceDetector(GCPMetadataConfig metadata, EnvVars envVars) {
this.metadata = metadata;
this.envVars = envVars;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1 +1 @@
com.google.cloud.opentelemetry.detectors.GCPResource
com.google.cloud.opentelemetry.detectors.GCPResourceDetector
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@
import org.mockito.Mockito;

@RunWith(JUnit4.class)
public class GCPResourceTest {
public class GCPResourceDetectorTest {
@Rule public final WireMockRule wireMockRule = new WireMockRule(8089);
private final GCPMetadataConfig metadataConfig = new GCPMetadataConfig("http://localhost:8089/");
private static final Map<String, String> envVars = new HashMap<>();
Expand All @@ -52,15 +52,16 @@ public void findsWithServiceLoader() {
ServiceLoader.load(ResourceProvider.class, getClass().getClassLoader());
assertTrue(
"Could not load GCP Resource detector using serviceloader, found: " + services,
services.stream().anyMatch(provider -> provider.type().equals(GCPResource.class)));
services.stream().anyMatch(provider -> provider.type().equals(GCPResourceDetector.class)));
}

@Test
public void testGCPComputeResourceNotGCP() {
GCPMetadataConfig mockMetadataConfig = Mockito.mock(GCPMetadataConfig.class);
Mockito.when(mockMetadataConfig.isRunningOnGcp()).thenReturn(false);

GCPResource testResource = new GCPResource(mockMetadataConfig, EnvVars.DEFAULT_INSTANCE);
GCPResourceDetector testResource =
new GCPResourceDetector(mockMetadataConfig, EnvVars.DEFAULT_INSTANCE);
// If GCPMetadataConfig determines that its not running on GCP, then attributes should be empty
assertThat(testResource.getAttributes()).isEmpty();
}
Expand All @@ -72,7 +73,8 @@ public void testGCPComputeResourceNonGCPEndpoint() {
stubFor(
get(urlEqualTo("/project/project-id"))
.willReturn(aResponse().withBody("nonGCPendpointTest")));
GCPResource testResource = new GCPResource(metadataConfig, new EnvVarMock(envVars));
GCPResourceDetector testResource =
new GCPResourceDetector(metadataConfig, new EnvVarMock(envVars));
assertThat(testResource.getAttributes()).isEmpty();
}

Expand All @@ -85,7 +87,8 @@ public void testGCEResourceWithGCEAttributesSucceeds() {
stubEndpoint("/instance/name", "GCE-instance-name");
stubEndpoint("/instance/machine-type", "GCE-instance-type");

final GCPResource testResource = new GCPResource(metadataConfig, new EnvVarMock(envVars));
final GCPResourceDetector testResource =
new GCPResourceDetector(metadataConfig, new EnvVarMock(envVars));
assertThat(testResource.getAttributes())
.hasSize(8)
.containsEntry(
Expand Down Expand Up @@ -118,7 +121,8 @@ public void testGKEResourceWithGKEAttributesSucceedsLocationZone() {
stubEndpoint("/instance/attributes/cluster-name", "GKE-cluster-name");
stubEndpoint("/instance/attributes/cluster-location", "country-region-zone");

GCPResource testResource = new GCPResource(metadataConfig, new EnvVarMock(envVars));
GCPResourceDetector testResource =
new GCPResourceDetector(metadataConfig, new EnvVarMock(envVars));
assertThat(testResource.getAttributes())
.hasSize(8)
.containsEntry(ResourceAttributes.CLOUD_PROVIDER, "gcp")
Expand Down Expand Up @@ -147,7 +151,8 @@ public void testGKEResourceWithGKEAttributesSucceedsLocationRegion() {
stubEndpoint("/instance/attributes/cluster-name", "GKE-cluster-name");
stubEndpoint("/instance/attributes/cluster-location", "country-region");

GCPResource testResource = new GCPResource(metadataConfig, new EnvVarMock(envVars));
GCPResourceDetector testResource =
new GCPResourceDetector(metadataConfig, new EnvVarMock(envVars));
assertThat(testResource.getAttributes())
.hasSize(8)
.containsEntry(ResourceAttributes.CLOUD_PROVIDER, "gcp")
Expand All @@ -172,7 +177,8 @@ public void testGCFResourceWithCloudFunctionAttributesSucceeds() {
stubEndpoint("/instance/zone", "country-region-zone");
stubEndpoint("/instance/id", "GCF-instance-id");

GCPResource testResource = new GCPResource(metadataConfig, new EnvVarMock(envVars));
GCPResourceDetector testResource =
new GCPResourceDetector(metadataConfig, new EnvVarMock(envVars));
assertThat(testResource.getAttributes())
.hasSize(7)
.containsEntry(
Expand Down Expand Up @@ -200,7 +206,8 @@ public void testGAEResourceWithAppEngineAttributesSucceedsInFlex() {
stubEndpoint("/instance/region", "country-region1");
stubEndpoint("/instance/id", "GAE-instance-id");

GCPResource testResource = new GCPResource(metadataConfig, new EnvVarMock(envVars));
GCPResourceDetector testResource =
new GCPResourceDetector(metadataConfig, new EnvVarMock(envVars));
assertThat(testResource.getAttributes())
.hasSize(7)
.containsEntry(
Expand Down Expand Up @@ -229,7 +236,8 @@ public void testGAEResourceWithAppEngineAttributesSucceedsInStandard() {

Map<String, String> updatedEnvVars = new HashMap<>(envVars);
updatedEnvVars.put("GAE_ENV", "standard");
GCPResource testResource = new GCPResource(metadataConfig, new EnvVarMock(updatedEnvVars));
GCPResourceDetector testResource =
new GCPResourceDetector(metadataConfig, new EnvVarMock(updatedEnvVars));
assertThat(testResource.getAttributes())
.hasSize(7)
.containsEntry(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
*/
package com.google.cloud.opentelemetry.endtoend;

import com.google.cloud.opentelemetry.detectors.GCPResource;
import com.google.cloud.opentelemetry.detectors.GCPResourceDetector;
import com.google.cloud.opentelemetry.propagators.XCloudTraceContextPropagator;
import com.google.cloud.opentelemetry.trace.TraceConfiguration;
import com.google.cloud.opentelemetry.trace.TraceExporter;
Expand Down Expand Up @@ -89,7 +89,7 @@ private Response basicTrace(Request request) {
private Response detectResource(Request request) {
LOGGER.info("Running detectResource test, request: " + request.toString());
Resource gcpResource =
new GCPResource()
new GCPResourceDetector()
.createResource(
DefaultConfigProperties.create(
Map.of("otel.traces.exporter", "none", "otel.metrics.exporter", "none")));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
*/
package com.google.cloud.opentelemetry.example.resource;

import com.google.cloud.opentelemetry.detectors.GCPResource;
import com.google.cloud.opentelemetry.detectors.GCPResourceDetector;
import io.opentelemetry.sdk.autoconfigure.ResourceConfiguration;
import io.opentelemetry.sdk.resources.Resource;

Expand All @@ -25,7 +25,7 @@ public static void main(String[] args) {
Resource autoResource = ResourceConfiguration.createEnvironmentResource();
System.out.println(autoResource.getAttributes());
System.out.println("Detecting resource: hardcoded");
GCPResource resource = new GCPResource();
GCPResourceDetector resource = new GCPResourceDetector();
System.out.println(resource.getAttributes());
}
}
Loading