Skip to content

Commit e936422

Browse files
committed
fix: deprecate resource detector (googleapis#13844)
1 parent 041450d commit e936422

4 files changed

Lines changed: 81 additions & 84 deletions

File tree

  • java-bigtable

java-bigtable/google-cloud-bigtable/pom.xml

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -247,10 +247,6 @@
247247
<groupId>io.opentelemetry</groupId>
248248
<artifactId>opentelemetry-sdk-common</artifactId>
249249
</dependency>
250-
<dependency>
251-
<groupId>com.google.cloud.opentelemetry</groupId>
252-
<artifactId>detector-resources-support</artifactId>
253-
</dependency>
254250
<dependency>
255251
<groupId>io.opentelemetry</groupId>
256252
<artifactId>opentelemetry-sdk-testing</artifactId>
@@ -264,6 +260,14 @@
264260
<groupId>com.google.api.grpc</groupId>
265261
<artifactId>proto-google-cloud-monitoring-v3</artifactId>
266262
</dependency>
263+
<dependency>
264+
<groupId>io.opentelemetry.contrib</groupId>
265+
<artifactId>opentelemetry-gcp-resources</artifactId>
266+
</dependency>
267+
<dependency>
268+
<groupId>io.opentelemetry</groupId>
269+
<artifactId>opentelemetry-sdk-extension-autoconfigure-spi</artifactId>
270+
</dependency>
267271

268272
<!-- export custom metrics to cloud console -->
269273
<dependency>

java-bigtable/google-cloud-bigtable/src/main/java/com/google/cloud/bigtable/data/v2/internal/csm/attributes/EnvInfo.java

Lines changed: 20 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -17,19 +17,15 @@
1717
package com.google.cloud.bigtable.data.v2.internal.csm.attributes;
1818

1919
import com.google.auto.value.AutoValue;
20-
import com.google.cloud.opentelemetry.detection.AttributeKeys;
21-
import com.google.cloud.opentelemetry.detection.DetectedPlatform;
22-
import com.google.cloud.opentelemetry.detection.GCPPlatformDetector;
2320
import com.google.common.base.Function;
2421
import com.google.common.base.Splitter;
2522
import com.google.common.base.Supplier;
26-
import com.google.common.collect.ImmutableList;
27-
import com.google.common.collect.ImmutableMap;
23+
import io.opentelemetry.api.common.AttributeKey;
24+
import io.opentelemetry.api.common.Attributes;
25+
import io.opentelemetry.contrib.gcp.resource.GCPResourceProvider;
2826
import java.lang.management.ManagementFactory;
2927
import java.net.InetAddress;
3028
import java.net.UnknownHostException;
31-
import java.util.Map;
32-
import java.util.Objects;
3329
import java.util.Optional;
3430
import java.util.UUID;
3531
import java.util.concurrent.atomic.AtomicLong;
@@ -47,11 +43,6 @@
4743
public abstract class EnvInfo {
4844
private static final Logger logger = Logger.getLogger(EnvInfo.class.getName());
4945

50-
private static final Map<GCPPlatformDetector.SupportedPlatform, String> SUPPORTED_PLATFORM_MAP =
51-
ImmutableMap.of(
52-
GCPPlatformDetector.SupportedPlatform.GOOGLE_COMPUTE_ENGINE, "gcp_compute_engine",
53-
GCPPlatformDetector.SupportedPlatform.GOOGLE_KUBERNETES_ENGINE, "gcp_kubernetes_engine");
54-
5546
private static final AtomicLong uidSuffix = new AtomicLong(0);
5647

5748
public abstract String getUid();
@@ -110,7 +101,7 @@ private static String computeUid() {
110101

111102
public static EnvInfo detect() {
112103
return detect(
113-
GCPPlatformDetector.DEFAULT_INSTANCE.detectPlatform(),
104+
new GCPResourceProvider().getAttributes(),
114105
System::getenv,
115106
() -> {
116107
try {
@@ -123,11 +114,11 @@ public static EnvInfo detect() {
123114

124115
@Nullable
125116
static EnvInfo detect(
126-
DetectedPlatform detectedPlatform,
117+
Attributes detectedAttributes,
127118
Function<String, String> envGetter,
128119
Supplier<String> hostnameSupplier) {
129120
@Nullable
130-
String cloud_platform = SUPPORTED_PLATFORM_MAP.get(detectedPlatform.getSupportedPlatform());
121+
String cloud_platform = detectedAttributes.get(AttributeKey.stringKey("cloud.platform"));
131122
if (cloud_platform == null) {
132123
return EnvInfo.builder()
133124
.setPlatform("unknown")
@@ -138,32 +129,33 @@ static EnvInfo detect(
138129
.build();
139130
}
140131

141-
Map<String, String> attrs = detectedPlatform.getAttributes();
142-
ImmutableList<String> locationKeys =
143-
ImmutableList.of(
144-
AttributeKeys.GCE_CLOUD_REGION,
145-
AttributeKeys.GCE_AVAILABILITY_ZONE,
146-
AttributeKeys.GKE_LOCATION_TYPE_REGION,
147-
AttributeKeys.GKE_CLUSTER_LOCATION);
148-
149-
String region =
150-
locationKeys.stream().map(attrs::get).filter(Objects::nonNull).findFirst().orElse("global");
132+
// All platform except GKE uses "cloud.region" for region attribute.
133+
// GKE could either use "cloud.region" or "cloud.availability_zone" for region attribute.
134+
String region = detectedAttributes.get(AttributeKey.stringKey("cloud.region"));
135+
String gkeZonalClusterLocation =
136+
detectedAttributes.get(AttributeKey.stringKey("cloud.availability_zone"));
137+
if (region == null && gkeZonalClusterLocation != null) {
138+
region = gkeZonalClusterLocation;
139+
}
140+
region = region == null ? "global" : region;
151141

152142
// Deal with possibility of a zone. Zones are of the form us-east1-c, but we want a region
153143
// which, which is us-east1.
154144
region = Splitter.on('-').splitToStream(region).limit(2).collect(Collectors.joining("-"));
155145

156-
String hostname = attrs.get(AttributeKeys.GCE_INSTANCE_NAME);
146+
String hostname = detectedAttributes.get(AttributeKey.stringKey("host.name"));
147+
157148
// TODO: add support for cloud run & gae by looking at SERVERLESS_COMPUTE_NAME & GAE_MODULE_NAME
158149
if (hostname == null) {
159150
hostname = detectHostname(envGetter, hostnameSupplier);
160151
}
161152

162-
String hostId = Optional.ofNullable(attrs.get(AttributeKeys.GCE_INSTANCE_ID)).orElse("");
153+
String hostId =
154+
Optional.ofNullable(detectedAttributes.get(AttributeKey.stringKey("host.id"))).orElse("");
163155

164156
return builder()
165157
.setPlatform(cloud_platform)
166-
.setProject(detectedPlatform.getProjectId())
158+
.setProject(detectedAttributes.get(AttributeKey.stringKey("cloud.account.id")))
167159
.setRegion(region)
168160
.setHostId(hostId)
169161
.setHostName(hostname)

java-bigtable/google-cloud-bigtable/src/test/java/com/google/cloud/bigtable/data/v2/internal/csm/attributes/EnvInfoTest.java

Lines changed: 52 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -17,34 +17,24 @@
1717
package com.google.cloud.bigtable.data.v2.internal.csm.attributes;
1818

1919
import static com.google.common.truth.Truth.assertThat;
20-
import static org.mockito.Mockito.when;
2120

22-
import com.google.cloud.opentelemetry.detection.DetectedPlatform;
23-
import com.google.cloud.opentelemetry.detection.GCPPlatformDetector.SupportedPlatform;
2421
import com.google.common.base.Function;
2522
import com.google.common.base.Supplier;
2623
import com.google.common.collect.ImmutableMap;
24+
import io.opentelemetry.api.common.Attributes;
2725
import java.util.Map;
2826
import org.junit.jupiter.api.Test;
29-
import org.junit.jupiter.api.extension.ExtendWith;
30-
import org.mockito.Mock;
31-
import org.mockito.junit.jupiter.MockitoExtension;
3227

33-
@ExtendWith(MockitoExtension.class)
3428
class EnvInfoTest {
3529
private static final Supplier<String> NULL_HOST = () -> null;
3630

3731
@SuppressWarnings("UnnecessaryLambda")
3832
private static final Function<String, String> NULL_ENV = (ignored) -> null;
3933

40-
@Mock private DetectedPlatform detectedPlatform;
41-
4234
@Test
4335
void testUid() {
44-
when(detectedPlatform.getSupportedPlatform()).thenReturn(SupportedPlatform.UNKNOWN_PLATFORM);
45-
46-
EnvInfo info1 = EnvInfo.detect(detectedPlatform, NULL_ENV, NULL_HOST);
47-
EnvInfo info2 = EnvInfo.detect(detectedPlatform, NULL_ENV, NULL_HOST);
36+
EnvInfo info1 = EnvInfo.detect(Attributes.empty(), NULL_ENV, NULL_HOST);
37+
EnvInfo info2 = EnvInfo.detect(Attributes.empty(), NULL_ENV, NULL_HOST);
4838

4939
assertThat(info1.getUid()).isNotEmpty();
5040
assertThat(info2.getUid()).isNotEmpty();
@@ -53,8 +43,7 @@ void testUid() {
5343

5444
@Test
5545
void testUnknown() {
56-
when(detectedPlatform.getSupportedPlatform()).thenReturn(SupportedPlatform.UNKNOWN_PLATFORM);
57-
EnvInfo envInfo = EnvInfo.detect(detectedPlatform, NULL_ENV, NULL_HOST);
46+
EnvInfo envInfo = EnvInfo.detect(Attributes.empty(), NULL_ENV, NULL_HOST);
5847
assertThat(envInfo.getHostName()).isEmpty();
5948
assertThat(envInfo.getHostId()).isEmpty();
6049
assertThat(envInfo.getPlatform()).isEqualTo("unknown");
@@ -63,19 +52,16 @@ void testUnknown() {
6352

6453
@Test
6554
void testGce() {
66-
when(detectedPlatform.getSupportedPlatform())
67-
.thenReturn(SupportedPlatform.GOOGLE_COMPUTE_ENGINE);
68-
when(detectedPlatform.getProjectId()).thenReturn("my-project");
69-
when(detectedPlatform.getAttributes())
70-
.thenReturn(
71-
ImmutableMap.of(
72-
"machine_type", "n2-standard-8",
73-
"availability_zone", "us-central1-c",
74-
"instance_id", "1234567890",
75-
"instance_name", "my-vm-name",
76-
"cloud_region", "us-central1",
77-
"instance_hostname", "my-vm-name.us-central1-c.c.my-project.google.com.internal"));
78-
EnvInfo envInfo = EnvInfo.detect(detectedPlatform, NULL_ENV, NULL_HOST);
55+
Attributes attributes =
56+
Attributes.builder()
57+
.put("cloud.platform", "gcp_compute_engine")
58+
.put("cloud.account.id", "my-project")
59+
.put("cloud.region", "us-central1")
60+
.put("cloud.availability_zone", "us-central1-c")
61+
.put("host.id", "1234567890")
62+
.put("host.name", "my-vm-name")
63+
.build();
64+
EnvInfo envInfo = EnvInfo.detect(attributes, NULL_ENV, NULL_HOST);
7965
assertThat(envInfo.getPlatform()).isEqualTo("gcp_compute_engine");
8066
assertThat(envInfo.getProject()).isEqualTo("my-project");
8167
assertThat(envInfo.getRegion()).isEqualTo("us-central1");
@@ -84,20 +70,37 @@ void testGce() {
8470
}
8571

8672
@Test
87-
void testGke() {
88-
when(detectedPlatform.getSupportedPlatform())
89-
.thenReturn(SupportedPlatform.GOOGLE_KUBERNETES_ENGINE);
90-
when(detectedPlatform.getProjectId()).thenReturn("my-project");
91-
when(detectedPlatform.getAttributes())
92-
.thenReturn(
93-
ImmutableMap.of(
94-
"gke_cluster_name", "my-cluster",
95-
"gke_cluster_location", "us-central1",
96-
"gke_cluster_location_type", "country-region",
97-
"instance_id", "1234567890"));
73+
void testGkeRegionalCluster() {
74+
Attributes attributes =
75+
Attributes.builder()
76+
.put("cloud.platform", "gcp_kubernetes_engine")
77+
.put("cloud.account.id", "my-project")
78+
.put("cloud.region", "us-central1")
79+
.put("host.id", "1234567890")
80+
.build();
81+
Map<String, String> env = ImmutableMap.of("HOSTNAME", "my-hostname");
82+
83+
EnvInfo envInfo = EnvInfo.detect(attributes, env::get, NULL_HOST);
84+
assertThat(envInfo.getPlatform()).isEqualTo("gcp_kubernetes_engine");
85+
assertThat(envInfo.getProject()).isEqualTo("my-project");
86+
assertThat(envInfo.getRegion()).isEqualTo("us-central1");
87+
assertThat(envInfo.getHostId()).isEqualTo("1234567890");
88+
assertThat(envInfo.getHostName()).isEqualTo("my-hostname");
89+
}
90+
91+
@Test
92+
void testGkeZonalCluster() {
93+
// Zonal GKE clusters report their location via cloud.availability_zone instead of cloud.region.
94+
Attributes attributes =
95+
Attributes.builder()
96+
.put("cloud.platform", "gcp_kubernetes_engine")
97+
.put("cloud.account.id", "my-project")
98+
.put("cloud.availability_zone", "us-central1-c")
99+
.put("host.id", "1234567890")
100+
.build();
98101
Map<String, String> env = ImmutableMap.of("HOSTNAME", "my-hostname");
99102

100-
EnvInfo envInfo = EnvInfo.detect(detectedPlatform, env::get, NULL_HOST);
103+
EnvInfo envInfo = EnvInfo.detect(attributes, env::get, NULL_HOST);
101104
assertThat(envInfo.getPlatform()).isEqualTo("gcp_kubernetes_engine");
102105
assertThat(envInfo.getProject()).isEqualTo("my-project");
103106
assertThat(envInfo.getRegion()).isEqualTo("us-central1");
@@ -106,18 +109,15 @@ void testGke() {
106109
}
107110

108111
@Test
109-
void testGkeHostanmeFallback() {
110-
when(detectedPlatform.getSupportedPlatform())
111-
.thenReturn(SupportedPlatform.GOOGLE_KUBERNETES_ENGINE);
112-
when(detectedPlatform.getProjectId()).thenReturn("my-project");
113-
when(detectedPlatform.getAttributes())
114-
.thenReturn(
115-
ImmutableMap.of(
116-
"gke_cluster_name", "my-cluster",
117-
"gke_cluster_location", "us-central1",
118-
"gke_cluster_location_type", "country-region",
119-
"instance_id", "1234567890"));
120-
EnvInfo envInfo = EnvInfo.detect(detectedPlatform, NULL_ENV, () -> "my-hostname");
112+
void testGkeHostnameFallback() {
113+
Attributes attributes =
114+
Attributes.builder()
115+
.put("cloud.platform", "gcp_kubernetes_engine")
116+
.put("cloud.account.id", "my-project")
117+
.put("cloud.region", "us-central1")
118+
.put("host.id", "1234567890")
119+
.build();
120+
EnvInfo envInfo = EnvInfo.detect(attributes, NULL_ENV, () -> "my-hostname");
121121
assertThat(envInfo.getPlatform()).isEqualTo("gcp_kubernetes_engine");
122122
assertThat(envInfo.getProject()).isEqualTo("my-project");
123123
assertThat(envInfo.getRegion()).isEqualTo("us-central1");

java-bigtable/pom.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -292,6 +292,7 @@
292292
<artifactId>maven-dependency-plugin</artifactId>
293293
<configuration>
294294
<ignoreNonCompile>true</ignoreNonCompile>
295+
<ignoredDependencies>io.opentelemetry:opentelemetry-sdk-extension-autoconfigure-spi</ignoredDependencies>
295296
</configuration>
296297
</plugin>
297298
</plugins>

0 commit comments

Comments
 (0)