Skip to content

Commit

Permalink
Rname GCPResource -> GCPResourceDetector
Browse files Browse the repository at this point in the history
  • Loading branch information
psx95 committed Dec 3, 2023
1 parent 5425525 commit c9235af
Show file tree
Hide file tree
Showing 7 changed files with 67 additions and 29 deletions.
25 changes: 20 additions & 5 deletions detectors/resources-support/build.gradle
Original file line number Diff line number Diff line change
@@ -1,18 +1,33 @@
/*
* Copyright 2023 Google LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
plugins {
id 'java'
id 'java'
}

group 'org.example'

repositories {
mavenCentral()
mavenCentral()
}

dependencies {
testImplementation 'org.junit.jupiter:junit-jupiter-api:5.8.1'
testRuntimeOnly 'org.junit.jupiter:junit-jupiter-engine:5.8.1'
testImplementation 'org.junit.jupiter:junit-jupiter-api:5.8.1'
testRuntimeOnly 'org.junit.jupiter:junit-jupiter-engine:5.8.1'
}

test {
useJUnitPlatform()
useJUnitPlatform()
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,22 @@
/*
* Copyright 2023 Google LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.google.cloud.opentelemetry.detectors;

public class Main {
public static void main(String[] args) {
System.out.println("Hello world!");
}
}
public static void main(String[] args) {
System.out.println("Hello world!");
}
}
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();
System.out.println(resource.getAttributes());
GCPResourceDetector resourceDetector = new GCPResourceDetector();
System.out.println(resourceDetector.getAttributes());
}
}

0 comments on commit c9235af

Please sign in to comment.