Skip to content

Commit 77ac82a

Browse files
tkmsaaaampsx95
andauthored
Fix #258 enable mapping when not gcp kubernetes (#340)
* Fix #258 enable mapping when not gcp kubernetes * fix code style * added comment for posterity * Added label's mappings each mr_type * Update to JUnit5 and refactor with ParameterizedTest * Update code style for test * Fix code style --------- Co-authored-by: Pranav Sharma <[email protected]>
1 parent d08c205 commit 77ac82a

File tree

3 files changed

+396
-346
lines changed

3 files changed

+396
-346
lines changed

shared/resourcemapping/build.gradle

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,9 @@ dependencies {
2828
annotationProcessor(libraries.auto_value)
2929
implementation(libraries.opentelemetry_semconv)
3030
implementation platform(libraries.opentelemetry_bom)
31-
testImplementation(testLibraries.junit)
31+
testImplementation(testLibraries.junit5)
32+
testImplementation(testLibraries.junit5_params)
33+
testRuntimeOnly(testLibraries.junit5_runtime)
3234
testImplementation(libraries.opentelemetry_semconv)
3335
}
3436

@@ -57,6 +59,11 @@ publishing {
5759
}
5860
}
5961

62+
test {
63+
// required for discovering JUnit 5 tests
64+
useJUnitPlatform()
65+
}
66+
6067
// This is to fix the explicit dependency error which comes when publishing via the `candidate` task
6168
publishMavenPublicationToMavenRepository.dependsOn jar
6269
signMavenPublication.dependsOn jar

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

Lines changed: 37 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,30 @@ public static AttributeMapping create(
103103
AttributeMapping.create("namespace_name", ResourceAttributes.K8S_NAMESPACE_NAME),
104104
AttributeMapping.create("container_name", ResourceAttributes.K8S_CONTAINER_NAME),
105105
AttributeMapping.create("pod_name", ResourceAttributes.K8S_POD_NAME));
106+
private static List<AttributeMapping> K8S_POD_LABELS =
107+
Arrays.asList(
108+
AttributeMapping.create(
109+
"location",
110+
Arrays.asList(
111+
ResourceAttributes.CLOUD_AVAILABILITY_ZONE, ResourceAttributes.CLOUD_REGION)),
112+
AttributeMapping.create("cluster_name", ResourceAttributes.K8S_CLUSTER_NAME),
113+
AttributeMapping.create("namespace_name", ResourceAttributes.K8S_NAMESPACE_NAME),
114+
AttributeMapping.create("pod_name", ResourceAttributes.K8S_POD_NAME));
115+
private static List<AttributeMapping> K8S_NODE_LABELS =
116+
Arrays.asList(
117+
AttributeMapping.create(
118+
"location",
119+
Arrays.asList(
120+
ResourceAttributes.CLOUD_AVAILABILITY_ZONE, ResourceAttributes.CLOUD_REGION)),
121+
AttributeMapping.create("cluster_name", ResourceAttributes.K8S_CLUSTER_NAME),
122+
AttributeMapping.create("node_name", ResourceAttributes.K8S_NODE_NAME));
123+
private static List<AttributeMapping> K8S_CLUSTER_LABELS =
124+
Arrays.asList(
125+
AttributeMapping.create(
126+
"location",
127+
Arrays.asList(
128+
ResourceAttributes.CLOUD_AVAILABILITY_ZONE, ResourceAttributes.CLOUD_REGION)),
129+
AttributeMapping.create("cluster_name", ResourceAttributes.K8S_CLUSTER_NAME));
106130
private static List<AttributeMapping> AWS_EC2_INSTANCE_LABELS =
107131
Arrays.asList(
108132
AttributeMapping.create("instance_id", ResourceAttributes.HOST_ID),
@@ -153,13 +177,24 @@ public static GcpResource mapResource(Resource resource) {
153177
switch (platform) {
154178
case ResourceAttributes.CloudPlatformValues.GCP_COMPUTE_ENGINE:
155179
return mapBase(resource, "gce_instance", GCE_INSTANCE_LABELS);
156-
case ResourceAttributes.CloudPlatformValues.GCP_KUBERNETES_ENGINE:
157-
return mapBase(resource, "k8s_container", K8S_CONTAINER_LABELS);
158180
case ResourceAttributes.CloudPlatformValues.AWS_EC2:
159181
return mapBase(resource, "aws_ec2_instance", AWS_EC2_INSTANCE_LABELS);
160182
case ResourceAttributes.CloudPlatformValues.GCP_APP_ENGINE:
161183
return mapBase(resource, "gae_instance", GOOGLE_CLOUD_APP_ENGINE_INSTANCE_LABELS);
162184
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+
}
163198
return genericTaskOrNode(resource);
164199
}
165200
}

0 commit comments

Comments
 (0)