1919import static com .google .common .base .MoreObjects .firstNonNull ;
2020import static com .google .common .base .Preconditions .checkNotNull ;
2121
22+ import com .google .common .annotations .VisibleForTesting ;
23+ import com .google .common .base .Splitter ;
2224import io .opencensus .resource .Resource ;
2325import java .util .LinkedHashMap ;
26+ import java .util .List ;
2427import 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 () {}
0 commit comments