Skip to content
This repository was archived by the owner on Dec 23, 2023. It is now read-only.

Commit 381a98a

Browse files
Bogdan Drutusongy23
authored andcommitted
Refactor resource util to match specs. (#1805)
* Refactor resource util to match specs. * Fix checker issues * Fix isRunningOnGcp and isRunningOnAws. * Fix autodetection test. * ./gradlew goJF * Add todos to auto-detect azure and container image details.
1 parent a419d04 commit 381a98a

File tree

23 files changed

+725
-555
lines changed

23 files changed

+725
-555
lines changed

contrib/monitored_resource_util/src/main/java/io/opencensus/contrib/monitoredresource/util/MonitoredResource.java

Lines changed: 38 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -18,12 +18,8 @@
1818

1919
import static com.google.common.base.MoreObjects.firstNonNull;
2020

21-
import com.google.common.collect.ImmutableList;
22-
import io.opencensus.contrib.resource.util.AwsEc2InstanceResource;
23-
import io.opencensus.contrib.resource.util.GcpGceInstanceResource;
24-
import io.opencensus.contrib.resource.util.K8sContainerResource;
21+
import com.google.common.collect.ImmutableMap;
2522
import io.opencensus.resource.Resource;
26-
import java.util.Collections;
2723
import java.util.Map;
2824
import javax.annotation.concurrent.Immutable;
2925

@@ -75,7 +71,7 @@ public ResourceType getResourceType() {
7571
* @since 0.13
7672
*/
7773
public String getAccount() {
78-
return firstNonNull(labels.get(AwsEc2InstanceResource.ACCOUNT_ID_KEY), "");
74+
return firstNonNull(labels.get(ResourceKeyConstants.AWS_ACCOUNT_KEY), "");
7975
}
8076

8177
/**
@@ -85,7 +81,7 @@ public String getAccount() {
8581
* @since 0.13
8682
*/
8783
public String getInstanceId() {
88-
return firstNonNull(labels.get(AwsEc2InstanceResource.INSTANCE_ID_KEY), "");
84+
return firstNonNull(labels.get(ResourceKeyConstants.AWS_INSTANCE_ID_KEY), "");
8985
}
9086

9187
/**
@@ -95,7 +91,7 @@ public String getInstanceId() {
9591
* @since 0.13
9692
*/
9793
public String getRegion() {
98-
return firstNonNull(labels.get(AwsEc2InstanceResource.REGION_KEY), "");
94+
return firstNonNull(labels.get(ResourceKeyConstants.AWS_REGION_KEY), "");
9995
}
10096

10197
/**
@@ -110,7 +106,13 @@ public String getRegion() {
110106
public static AwsEc2InstanceMonitoredResource create(
111107
String account, String instanceId, String region) {
112108
return new AwsEc2InstanceMonitoredResource(
113-
AwsEc2InstanceResource.create(account, region, instanceId).getLabels());
109+
ImmutableMap.of(
110+
ResourceKeyConstants.AWS_ACCOUNT_KEY,
111+
account,
112+
ResourceKeyConstants.AWS_REGION_KEY,
113+
region,
114+
ResourceKeyConstants.AWS_INSTANCE_ID_KEY,
115+
instanceId));
114116
}
115117

116118
static AwsEc2InstanceMonitoredResource create(Resource resource) {
@@ -145,7 +147,7 @@ public ResourceType getResourceType() {
145147
* @since 0.13
146148
*/
147149
public String getAccount() {
148-
return firstNonNull(labels.get(GcpGceInstanceResource.PROJECT_ID_KEY), "");
150+
return firstNonNull(labels.get(ResourceKeyConstants.GCP_ACCOUNT_ID_KEY), "");
149151
}
150152

151153
/**
@@ -155,7 +157,7 @@ public String getAccount() {
155157
* @since 0.13
156158
*/
157159
public String getInstanceId() {
158-
return firstNonNull(labels.get(GcpGceInstanceResource.INSTANCE_ID_KEY), "");
160+
return firstNonNull(labels.get(ResourceKeyConstants.GCP_INSTANCE_ID_KEY), "");
159161
}
160162

161163
/**
@@ -165,7 +167,7 @@ public String getInstanceId() {
165167
* @since 0.13
166168
*/
167169
public String getZone() {
168-
return firstNonNull(labels.get(GcpGceInstanceResource.ZONE_KEY), "");
170+
return firstNonNull(labels.get(ResourceKeyConstants.GCP_ZONE_KEY), "");
169171
}
170172

171173
/**
@@ -180,7 +182,13 @@ public String getZone() {
180182
public static GcpGceInstanceMonitoredResource create(
181183
String account, String instanceId, String zone) {
182184
return new GcpGceInstanceMonitoredResource(
183-
GcpGceInstanceResource.create(account, zone, instanceId).getLabels());
185+
ImmutableMap.of(
186+
ResourceKeyConstants.GCP_ACCOUNT_ID_KEY,
187+
account,
188+
ResourceKeyConstants.GCP_ZONE_KEY,
189+
zone,
190+
ResourceKeyConstants.GCP_INSTANCE_ID_KEY,
191+
instanceId));
184192
}
185193

186194
static GcpGceInstanceMonitoredResource create(Resource resource) {
@@ -215,7 +223,7 @@ public ResourceType getResourceType() {
215223
* @since 0.13
216224
*/
217225
public String getAccount() {
218-
return firstNonNull(labels.get(GcpGceInstanceResource.PROJECT_ID_KEY), "");
226+
return firstNonNull(labels.get(ResourceKeyConstants.GCP_ACCOUNT_ID_KEY), "");
219227
}
220228

221229
/**
@@ -225,7 +233,7 @@ public String getAccount() {
225233
* @since 0.13
226234
*/
227235
public String getClusterName() {
228-
return firstNonNull(labels.get(K8sContainerResource.CLUSTER_NAME_KEY), "");
236+
return firstNonNull(labels.get(ResourceKeyConstants.K8S_CLUSTER_NAME_KEY), "");
229237
}
230238

231239
/**
@@ -235,7 +243,7 @@ public String getClusterName() {
235243
* @since 0.13
236244
*/
237245
public String getContainerName() {
238-
return firstNonNull(labels.get(K8sContainerResource.CONTAINER_NAME_KEY), "");
246+
return firstNonNull(labels.get(ResourceKeyConstants.K8S_CONTAINER_NAME_KEY), "");
239247
}
240248

241249
/**
@@ -245,7 +253,7 @@ public String getContainerName() {
245253
* @since 0.13
246254
*/
247255
public String getNamespaceId() {
248-
return firstNonNull(labels.get(K8sContainerResource.NAMESPACE_NAME_KEY), "");
256+
return firstNonNull(labels.get(ResourceKeyConstants.K8S_NAMESPACE_NAME_KEY), "");
249257
}
250258

251259
/**
@@ -255,7 +263,7 @@ public String getNamespaceId() {
255263
* @since 0.13
256264
*/
257265
public String getInstanceId() {
258-
return firstNonNull(labels.get(GcpGceInstanceResource.INSTANCE_ID_KEY), "");
266+
return firstNonNull(labels.get(ResourceKeyConstants.GCP_INSTANCE_ID_KEY), "");
259267
}
260268

261269
/**
@@ -265,7 +273,7 @@ public String getInstanceId() {
265273
* @since 0.13
266274
*/
267275
public String getPodId() {
268-
return firstNonNull(labels.get(K8sContainerResource.POD_NAME_KEY), "");
276+
return firstNonNull(labels.get(ResourceKeyConstants.K8S_POD_NAME_KEY), "");
269277
}
270278

271279
/**
@@ -275,7 +283,7 @@ public String getPodId() {
275283
* @since 0.13
276284
*/
277285
public String getZone() {
278-
return firstNonNull(labels.get(GcpGceInstanceResource.ZONE_KEY), "");
286+
return firstNonNull(labels.get(ResourceKeyConstants.GCP_ZONE_KEY), "");
279287
}
280288

281289
/**
@@ -299,15 +307,16 @@ public static GcpGkeContainerMonitoredResource create(
299307
String instanceId,
300308
String podId,
301309
String zone) {
302-
Resource resource =
303-
Resource.mergeResources(
304-
ImmutableList.of(
305-
K8sContainerResource.create(clusterName, namespaceId, podId, containerName),
306-
GcpGceInstanceResource.create(account, zone, instanceId)));
307-
if (resource == null) {
308-
return new GcpGkeContainerMonitoredResource(Collections.<String, String>emptyMap());
309-
}
310-
return new GcpGkeContainerMonitoredResource(resource.getLabels());
310+
return new GcpGkeContainerMonitoredResource(
311+
ImmutableMap.<String, String>builder()
312+
.put(ResourceKeyConstants.GCP_ACCOUNT_ID_KEY, account)
313+
.put(ResourceKeyConstants.K8S_CLUSTER_NAME_KEY, clusterName)
314+
.put(ResourceKeyConstants.K8S_CONTAINER_NAME_KEY, containerName)
315+
.put(ResourceKeyConstants.K8S_NAMESPACE_NAME_KEY, namespaceId)
316+
.put(ResourceKeyConstants.GCP_INSTANCE_ID_KEY, instanceId)
317+
.put(ResourceKeyConstants.K8S_POD_NAME_KEY, podId)
318+
.put(ResourceKeyConstants.GCP_ZONE_KEY, zone)
319+
.build());
311320
}
312321

313322
static GcpGkeContainerMonitoredResource create(Resource resource) {

contrib/monitored_resource_util/src/main/java/io/opencensus/contrib/monitoredresource/util/MonitoredResourceUtils.java

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,9 @@
1616

1717
package io.opencensus.contrib.monitoredresource.util;
1818

19-
import io.opencensus.contrib.resource.util.AwsEc2InstanceResource;
20-
import io.opencensus.contrib.resource.util.GcpGceInstanceResource;
21-
import io.opencensus.contrib.resource.util.K8sContainerResource;
19+
import io.opencensus.contrib.resource.util.CloudResource;
20+
import io.opencensus.contrib.resource.util.ContainerResource;
21+
import io.opencensus.contrib.resource.util.HostResource;
2222
import io.opencensus.contrib.resource.util.ResourceUtils;
2323
import io.opencensus.resource.Resource;
2424
import javax.annotation.Nullable;
@@ -47,17 +47,17 @@ public static MonitoredResource getDefaultResource() {
4747
return null;
4848
}
4949
String resourceType = resource.getType();
50-
if (resourceType == null) {
51-
return null;
52-
}
53-
if (resourceType.equals(K8sContainerResource.TYPE)) {
50+
if (ContainerResource.TYPE.equals(resourceType)) {
5451
return MonitoredResource.GcpGkeContainerMonitoredResource.create(resource);
5552
}
56-
if (resourceType.equals(GcpGceInstanceResource.TYPE)) {
57-
return MonitoredResource.GcpGceInstanceMonitoredResource.create(resource);
58-
}
59-
if (resourceType.equals(AwsEc2InstanceResource.TYPE)) {
60-
return MonitoredResource.AwsEc2InstanceMonitoredResource.create(resource);
53+
if (HostResource.TYPE.equals(resourceType)) {
54+
String provider = resource.getLabels().get(CloudResource.PROVIDER_KEY);
55+
if (CloudResource.PROVIDER_GCP.equals(provider)) {
56+
return MonitoredResource.GcpGceInstanceMonitoredResource.create(resource);
57+
}
58+
if (CloudResource.PROVIDER_AWS.equals(provider)) {
59+
return MonitoredResource.AwsEc2InstanceMonitoredResource.create(resource);
60+
}
6161
}
6262
return null;
6363
}

contrib/monitored_resource_util/src/main/java/io/opencensus/contrib/monitoredresource/util/ResourceKeyConstants.java

Lines changed: 19 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -16,16 +16,17 @@
1616

1717
package io.opencensus.contrib.monitoredresource.util;
1818

19-
import io.opencensus.contrib.resource.util.AwsEc2InstanceResource;
20-
import io.opencensus.contrib.resource.util.GcpGceInstanceResource;
21-
import io.opencensus.contrib.resource.util.K8sContainerResource;
19+
import io.opencensus.contrib.resource.util.CloudResource;
20+
import io.opencensus.contrib.resource.util.ContainerResource;
21+
import io.opencensus.contrib.resource.util.HostResource;
22+
import io.opencensus.contrib.resource.util.K8sResource;
2223

2324
/**
2425
* Constants for collecting resource information.
2526
*
2627
* @since 0.18
27-
* @deprecated use constant values from resource helper {@link AwsEc2InstanceResource}, {@link
28-
* GcpGceInstanceResource} and {@link K8sContainerResource}.
28+
* @deprecated use constant values from resource helper {@link HostResource}, {@link CloudResource}
29+
* and {@link ContainerResource}.
2930
*/
3031
@Deprecated
3132
public final class ResourceKeyConstants {
@@ -35,93 +36,93 @@ public final class ResourceKeyConstants {
3536
*
3637
* @since 0.18
3738
*/
38-
public static final String AWS_EC2_INSTANCE_TYPE = AwsEc2InstanceResource.TYPE;
39+
public static final String AWS_EC2_INSTANCE_TYPE = "aws.com/ec2/instance";
3940

4041
/**
4142
* AWS key that represents a region for the VM.
4243
*
4344
* @since 0.18
4445
*/
45-
public static final String AWS_REGION_KEY = AwsEc2InstanceResource.REGION_KEY;
46+
public static final String AWS_REGION_KEY = CloudResource.REGION_KEY;
4647

4748
/**
4849
* AWS key that represents the AWS account number for the VM.
4950
*
5051
* @since 0.18
5152
*/
52-
public static final String AWS_ACCOUNT_KEY = AwsEc2InstanceResource.ACCOUNT_ID_KEY;
53+
public static final String AWS_ACCOUNT_KEY = CloudResource.ACCOUNT_ID_KEY;
5354

5455
/**
5556
* AWS key that represents the VM instance identifier assigned by AWS.
5657
*
5758
* @since 0.18
5859
*/
59-
public static final String AWS_INSTANCE_ID_KEY = AwsEc2InstanceResource.INSTANCE_ID_KEY;
60+
public static final String AWS_INSTANCE_ID_KEY = HostResource.ID_KEY;
6061

6162
/**
6263
* GCP GCE key that represents a type of the resource.
6364
*
6465
* @since 0.18
6566
*/
66-
public static final String GCP_GCE_INSTANCE_TYPE = GcpGceInstanceResource.TYPE;
67+
public static final String GCP_GCE_INSTANCE_TYPE = "cloud.google.com/gce/instance";
6768

6869
/**
6970
* GCP GCE key that represents the GCP account number for the instance.
7071
*
7172
* @since 0.18
7273
*/
73-
public static final String GCP_ACCOUNT_ID_KEY = GcpGceInstanceResource.PROJECT_ID_KEY;
74+
public static final String GCP_ACCOUNT_ID_KEY = CloudResource.ACCOUNT_ID_KEY;
7475

7576
/**
7677
* GCP GCE key that represents the numeric VM instance identifier assigned by GCE.
7778
*
7879
* @since 0.18
7980
*/
80-
public static final String GCP_INSTANCE_ID_KEY = GcpGceInstanceResource.INSTANCE_ID_KEY;
81+
public static final String GCP_INSTANCE_ID_KEY = HostResource.ID_KEY;
8182

8283
/**
8384
* GCP GCE key that represents the GCE zone in which the VM is running.
8485
*
8586
* @since 0.18
8687
*/
87-
public static final String GCP_ZONE_KEY = GcpGceInstanceResource.ZONE_KEY;
88+
public static final String GCP_ZONE_KEY = CloudResource.ZONE_KEY;
8889

8990
/**
9091
* Kubernetes resources key that represents a type of the resource.
9192
*
9293
* @since 0.18
9394
*/
94-
public static final String K8S_CONTAINER_TYPE = K8sContainerResource.TYPE;
95+
public static final String K8S_CONTAINER_TYPE = "k8s.io/container";
9596

9697
/**
9798
* Kubernetes resources key that represents the name for the cluster the container is running in.
9899
*
99100
* @since 0.18
100101
*/
101-
public static final String K8S_CLUSTER_NAME_KEY = K8sContainerResource.CLUSTER_NAME_KEY;
102+
public static final String K8S_CLUSTER_NAME_KEY = K8sResource.CLUSTER_NAME_KEY;
102103

103104
/**
104105
* Kubernetes resources key that represents the name of the container.
105106
*
106107
* @since 0.18
107108
*/
108-
public static final String K8S_CONTAINER_NAME_KEY = K8sContainerResource.CONTAINER_NAME_KEY;
109+
public static final String K8S_CONTAINER_NAME_KEY = ContainerResource.NAME_KEY;
109110

110111
/**
111112
* Kubernetes resources key that represents the identifier for the GCE instance the container is
112113
* running in.
113114
*
114115
* @since 0.18
115116
*/
116-
public static final String K8S_NAMESPACE_NAME_KEY = K8sContainerResource.NAMESPACE_NAME_KEY;
117+
public static final String K8S_NAMESPACE_NAME_KEY = K8sResource.NAMESPACE_NAME_KEY;
117118

118119
/**
119120
* Kubernetes resources key that represents the identifier for the pod the container is running
120121
* in.
121122
*
122123
* @since 0.18
123124
*/
124-
public static final String K8S_POD_NAME_KEY = K8sContainerResource.POD_NAME_KEY;
125+
public static final String K8S_POD_NAME_KEY = K8sResource.POD_NAME_KEY;
125126

126127
private ResourceKeyConstants() {}
127128
}

contrib/monitored_resource_util/src/test/java/io/opencensus/contrib/monitoredresource/util/MonitoredResourceUtilsTest.java

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,9 @@
1818

1919
import static com.google.common.truth.Truth.assertThat;
2020

21-
import io.opencensus.contrib.resource.util.AwsEc2InstanceResource;
22-
import io.opencensus.contrib.resource.util.GcpGceInstanceResource;
23-
import io.opencensus.contrib.resource.util.K8sContainerResource;
21+
import io.opencensus.contrib.resource.util.CloudResource;
22+
import io.opencensus.contrib.resource.util.ContainerResource;
23+
import io.opencensus.contrib.resource.util.HostResource;
2424
import io.opencensus.contrib.resource.util.ResourceUtils;
2525
import io.opencensus.resource.Resource;
2626
import org.junit.Test;
@@ -40,11 +40,16 @@ public void testGetDefaultResource() {
4040
assertThat(monitoredResource).isNull();
4141
return;
4242
}
43-
if (resourceType.equals(K8sContainerResource.TYPE)) {
43+
assertThat(monitoredResource).isNotNull();
44+
if (resourceType.equals(ContainerResource.TYPE)) {
4445
assertThat(monitoredResource.getResourceType()).isEqualTo(ResourceType.GCP_GKE_CONTAINER);
45-
} else if (resourceType.equals(GcpGceInstanceResource.TYPE)) {
46+
} else if (resourceType.equals(HostResource.TYPE)
47+
&& CloudResource.PROVIDER_GCP.equals(
48+
resource.getLabels().get(CloudResource.PROVIDER_KEY))) {
4649
assertThat(monitoredResource.getResourceType()).isEqualTo(ResourceType.GCP_GCE_INSTANCE);
47-
} else if (resourceType.equals(AwsEc2InstanceResource.TYPE)) {
50+
} else if (resourceType.equals(HostResource.TYPE)
51+
&& CloudResource.PROVIDER_GCP.equals(
52+
resource.getLabels().get(CloudResource.PROVIDER_KEY))) {
4853
assertThat(monitoredResource.getResourceType()).isEqualTo(ResourceType.AWS_EC2_INSTANCE);
4954
} else {
5055
assertThat(monitoredResource).isNull();

0 commit comments

Comments
 (0)