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

Commit 0fa5564

Browse files
authored
Resource: Add deployment label for k8s resource. (#1943)
* Resource: Add deployment label for k8s resource. * Add a missing annotation. * Parse deployment name correctly.
1 parent b04ec36 commit 0fa5564

File tree

2 files changed

+86
-4
lines changed

2 files changed

+86
-4
lines changed

contrib/resource_util/src/main/java/io/opencensus/contrib/resource/util/K8sResource.java

Lines changed: 53 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,11 @@
1919
import static com.google.common.base.MoreObjects.firstNonNull;
2020
import static com.google.common.base.Preconditions.checkNotNull;
2121

22+
import com.google.common.annotations.VisibleForTesting;
23+
import com.google.common.base.Splitter;
2224
import io.opencensus.resource.Resource;
2325
import java.util.LinkedHashMap;
26+
import java.util.List;
2427
import java.util.Map;
2528

2629
/**
@@ -57,6 +60,15 @@ public class K8sResource {
5760
*/
5861
public static final String POD_NAME_KEY = "k8s.pod.name";
5962

63+
/**
64+
* Key for the name of the deployment.
65+
*
66+
* @since 0.24
67+
*/
68+
public static final String DEPLOYMENT_NAME_KEY = "k8s.deployment.name";
69+
70+
private static final Splitter splitter = Splitter.on('-');
71+
6072
/**
6173
* Returns a {@link Resource} that describes Kubernetes deployment service.
6274
*
@@ -65,20 +77,60 @@ public class K8sResource {
6577
* @param podName the k8s pod name.
6678
* @return a {@link Resource} that describes a k8s container.
6779
* @since 0.20
80+
* @deprecated in favor of {@link #create(String, String, String, String)}.
6881
*/
82+
@Deprecated
6983
public static Resource create(String clusterName, String namespace, String podName) {
84+
return create(clusterName, namespace, podName, "");
85+
}
86+
87+
/**
88+
* Returns a {@link Resource} that describes Kubernetes deployment service.
89+
*
90+
* @param clusterName the k8s cluster name.
91+
* @param namespace the k8s namespace.
92+
* @param podName the k8s pod name.
93+
* @param deploymentName the k8s deployment name.
94+
* @return a {@link Resource} that describes a k8s container.
95+
* @since 0.24
96+
*/
97+
public static Resource create(
98+
String clusterName, String namespace, String podName, String deploymentName) {
7099
Map<String, String> labels = new LinkedHashMap<String, String>();
71100
labels.put(CLUSTER_NAME_KEY, checkNotNull(clusterName, "clusterName"));
72101
labels.put(NAMESPACE_NAME_KEY, checkNotNull(namespace, "namespace"));
73102
labels.put(POD_NAME_KEY, checkNotNull(podName, "podName"));
103+
labels.put(DEPLOYMENT_NAME_KEY, checkNotNull(deploymentName, "deploymentName"));
74104
return Resource.create(TYPE, labels);
75105
}
76106

77107
static Resource detect() {
108+
String podName = firstNonNull(System.getenv("HOSTNAME"), "");
109+
String deploymentName = getDeploymentNameFromPodName(podName);
78110
return create(
79111
GcpMetadataConfig.getClusterName(),
80112
firstNonNull(System.getenv("NAMESPACE"), ""),
81-
firstNonNull(System.getenv("HOSTNAME"), ""));
113+
podName,
114+
deploymentName);
115+
}
116+
117+
@VisibleForTesting
118+
static String getDeploymentNameFromPodName(String podName) {
119+
StringBuilder deploymentName = new StringBuilder();
120+
// Extract deployment name from the pod name. Pod name is created using
121+
// format: [deployment-name]-[Random-String-For-ReplicaSet]-[Random-String-For-Pod]
122+
List<String> parts = splitter.splitToList(podName);
123+
if (parts.size() == 3) {
124+
deploymentName.append(parts.get(0));
125+
} else if (parts.size() > 3) { // Deployment name could also contain '-'
126+
for (int i = 0; i < parts.size() - 2; i++) {
127+
if (deploymentName.length() > 0) {
128+
deploymentName.append('-');
129+
}
130+
deploymentName.append(parts.get(i));
131+
}
132+
}
133+
return deploymentName.toString();
82134
}
83135

84136
private K8sResource() {}

contrib/resource_util/src/test/java/io/opencensus/contrib/resource/util/K8sResourceTest.java

Lines changed: 33 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,10 +28,11 @@
2828
public class K8sResourceTest {
2929
private static final String K8S_CLUSTER_NAME = "cluster";
3030
private static final String K8S_NAMESPACE_NAME = "namespace";
31-
private static final String K8S_POD_NAME = "pod-id";
31+
private static final String K8S_POD_NAME = "deployment-replica-pod";
32+
private static final String K8S_DEPLOYMENT_NAME = "deployment";
3233

3334
@Test
34-
public void create_K8sContainerResourceTest() {
35+
public void create_K8sContainerResourceTest_Deprecated() {
3536
Resource resource = K8sResource.create(K8S_CLUSTER_NAME, K8S_NAMESPACE_NAME, K8S_POD_NAME);
3637
assertThat(resource.getType()).isEqualTo(K8sResource.TYPE);
3738
assertThat(resource.getLabels())
@@ -41,6 +42,35 @@ public void create_K8sContainerResourceTest() {
4142
K8sResource.NAMESPACE_NAME_KEY,
4243
K8S_NAMESPACE_NAME,
4344
K8sResource.POD_NAME_KEY,
44-
K8S_POD_NAME);
45+
K8S_POD_NAME,
46+
K8sResource.DEPLOYMENT_NAME_KEY,
47+
"");
48+
}
49+
50+
@Test
51+
public void create_K8sContainerResourceTest() {
52+
Resource resource =
53+
K8sResource.create(K8S_CLUSTER_NAME, K8S_NAMESPACE_NAME, K8S_POD_NAME, K8S_DEPLOYMENT_NAME);
54+
assertThat(resource.getType()).isEqualTo(K8sResource.TYPE);
55+
assertThat(resource.getLabels())
56+
.containsExactly(
57+
K8sResource.CLUSTER_NAME_KEY,
58+
K8S_CLUSTER_NAME,
59+
K8sResource.NAMESPACE_NAME_KEY,
60+
K8S_NAMESPACE_NAME,
61+
K8sResource.POD_NAME_KEY,
62+
K8S_POD_NAME,
63+
K8sResource.DEPLOYMENT_NAME_KEY,
64+
K8S_DEPLOYMENT_NAME);
65+
}
66+
67+
@Test
68+
public void getDeploymentNameFromPodName() {
69+
assertThat(K8sResource.getDeploymentNameFromPodName(K8S_POD_NAME))
70+
.isEqualTo(K8S_DEPLOYMENT_NAME);
71+
assertThat(K8sResource.getDeploymentNameFromPodName("")).isEqualTo("");
72+
assertThat(K8sResource.getDeploymentNameFromPodName("simple-name")).isEqualTo("");
73+
assertThat(K8sResource.getDeploymentNameFromPodName("deployment-name-replica-pod"))
74+
.isEqualTo("deployment-name");
4575
}
4676
}

0 commit comments

Comments
 (0)