Skip to content

Commit 78c5cd4

Browse files
authored
Fix resource mapping for null platform values (#342)
* Fix handling of null platform values according to spec. * Use appropriate cloud provider values in tests * Adds missing test cases * Fixed the order about test cases & added comments
1 parent 77ac82a commit 78c5cd4

File tree

2 files changed

+153
-102
lines changed

2 files changed

+153
-102
lines changed

shared/resourcemapping/src/main/java/com/google/cloud/opentelemetry/resource/ResourceTranslator.java

Lines changed: 19 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -172,7 +172,7 @@ public static AttributeMapping create(
172172
public static GcpResource mapResource(Resource resource) {
173173
String platform = resource.getAttribute(ResourceAttributes.CLOUD_PLATFORM);
174174
if (platform == null) {
175-
return genericTaskOrNode(resource);
175+
return mapK8sResourceOrGenericTaskOrNode(resource);
176176
}
177177
switch (platform) {
178178
case ResourceAttributes.CloudPlatformValues.GCP_COMPUTE_ENGINE:
@@ -182,21 +182,25 @@ public static GcpResource mapResource(Resource resource) {
182182
case ResourceAttributes.CloudPlatformValues.GCP_APP_ENGINE:
183183
return mapBase(resource, "gae_instance", GOOGLE_CLOUD_APP_ENGINE_INSTANCE_LABELS);
184184
default:
185-
// if k8s.cluster.name is set, pattern match for various k8s resources.
186-
// this will also match non-cloud k8s platforms like minikube.
187-
if (resource.getAttribute(ResourceAttributes.K8S_CLUSTER_NAME) != null) {
188-
if (resource.getAttribute(ResourceAttributes.K8S_CONTAINER_NAME) != null) {
189-
return mapBase(resource, "k8s_container", K8S_CONTAINER_LABELS);
190-
} else if (resource.getAttribute(ResourceAttributes.K8S_POD_NAME) != null) {
191-
return mapBase(resource, "k8s_pod", K8S_POD_LABELS);
192-
} else if (resource.getAttribute(ResourceAttributes.K8S_NODE_NAME) != null) {
193-
return mapBase(resource, "k8s_node", K8S_NODE_LABELS);
194-
} else {
195-
return mapBase(resource, "k8s_cluster", K8S_CLUSTER_LABELS);
196-
}
197-
}
198-
return genericTaskOrNode(resource);
185+
return mapK8sResourceOrGenericTaskOrNode(resource);
186+
}
187+
}
188+
189+
private static GcpResource mapK8sResourceOrGenericTaskOrNode(Resource resource) {
190+
// if k8s.cluster.name is set, pattern match for various k8s resources.
191+
// this will also match non-cloud k8s platforms like minikube.
192+
if (resource.getAttribute(ResourceAttributes.K8S_CLUSTER_NAME) != null) {
193+
if (resource.getAttribute(ResourceAttributes.K8S_CONTAINER_NAME) != null) {
194+
return mapBase(resource, "k8s_container", K8S_CONTAINER_LABELS);
195+
} else if (resource.getAttribute(ResourceAttributes.K8S_POD_NAME) != null) {
196+
return mapBase(resource, "k8s_pod", K8S_POD_LABELS);
197+
} else if (resource.getAttribute(ResourceAttributes.K8S_NODE_NAME) != null) {
198+
return mapBase(resource, "k8s_node", K8S_NODE_LABELS);
199+
} else {
200+
return mapBase(resource, "k8s_cluster", K8S_CLUSTER_LABELS);
201+
}
199202
}
203+
return genericTaskOrNode(resource);
200204
}
201205

202206
private static GcpResource genericTaskOrNode(Resource resource) {

0 commit comments

Comments
 (0)