From 3eee2167e0f1311b27f3b7d1d3185125a9a352cd Mon Sep 17 00:00:00 2001 From: Marc Tuduri Date: Fri, 22 Nov 2024 16:10:33 +0100 Subject: [PATCH] Add container name label --- pkg/export/attributes/attr_defs.go | 1 + pkg/export/attributes/names/attrs.go | 1 + pkg/export/prom/prom.go | 1 + pkg/internal/kube/store.go | 9 +++++++++ pkg/transform/k8s.go | 3 +++ pkg/transform/k8s_test.go | 4 ++++ .../k8s/daemonset/k8s_daemonset_traces_test.go | 2 ++ .../k8s/daemonset_python/k8s_daemonset_traces_test.go | 2 ++ test/integration/k8s/otel/k8s_otel_traces_test.go | 1 + .../k8s/owners/k8s_daemonset_metadata_test.go | 1 + .../k8s/owners/k8s_statefulset_metadata_test.go | 1 + 11 files changed, 26 insertions(+) diff --git a/pkg/export/attributes/attr_defs.go b/pkg/export/attributes/attr_defs.go index 87f5946c0..8ceb8b91b 100644 --- a/pkg/export/attributes/attr_defs.go +++ b/pkg/export/attributes/attr_defs.go @@ -99,6 +99,7 @@ func getDefinitions(groups AttrGroups) map[Section]AttrReportGroup { Attributes: map[attr.Name]Default{ attr.K8sNamespaceName: true, attr.K8sPodName: true, + attr.K8sContainerName: true, attr.K8sDeploymentName: true, attr.K8sReplicaSetName: true, attr.K8sDaemonSetName: true, diff --git a/pkg/export/attributes/names/attrs.go b/pkg/export/attributes/names/attrs.go index 80346c4b0..7ea0823ae 100644 --- a/pkg/export/attributes/names/attrs.go +++ b/pkg/export/attributes/names/attrs.go @@ -56,6 +56,7 @@ const ( K8sNamespaceName = Name("k8s.namespace.name") K8sPodName = Name("k8s.pod.name") + K8sContainerName = Name("k8s.container.name") K8sDeploymentName = Name("k8s.deployment.name") K8sReplicaSetName = Name("k8s.replicaset.name") K8sDaemonSetName = Name("k8s.daemonset.name") diff --git a/pkg/export/prom/prom.go b/pkg/export/prom/prom.go index 0e78ae0a3..fc48dea8f 100644 --- a/pkg/export/prom/prom.go +++ b/pkg/export/prom/prom.go @@ -687,6 +687,7 @@ func appendK8sLabelValuesService(values []string, service svc.ID) []string { values = append(values, service.Metadata[(attr.K8sNamespaceName)], service.Metadata[(attr.K8sPodName)], + service.Metadata[(attr.K8sContainerName)], service.Metadata[(attr.K8sNodeName)], service.Metadata[(attr.K8sPodUID)], service.Metadata[(attr.K8sPodStartTime)], diff --git a/pkg/internal/kube/store.go b/pkg/internal/kube/store.go index 885f54135..86ada1458 100644 --- a/pkg/internal/kube/store.go +++ b/pkg/internal/kube/store.go @@ -276,6 +276,15 @@ func (s *Store) PodByPIDNs(pidns uint32) *informer.ObjectMeta { return nil } +func (s *Store) ContainerByPIDNs(pidns uint32) *container.Info { + s.access.RLock() + defer s.access.RUnlock() + if info, ok := s.namespaces[pidns]; ok { + return info + } + return nil +} + func (s *Store) ObjectMetaByIP(ip string) *informer.ObjectMeta { s.access.RLock() defer s.access.RUnlock() diff --git a/pkg/transform/k8s.go b/pkg/transform/k8s.go index 124d88c8e..819434b42 100644 --- a/pkg/transform/k8s.go +++ b/pkg/transform/k8s.go @@ -106,6 +106,9 @@ func (md *metadataDecorator) do(span *request.Span) { if name, _ := md.db.ServiceNameNamespaceForIP(span.Peer); name != "" { span.PeerName = name } + if info := md.db.ContainerByPIDNs(span.Pid.Namespace); info.ContainerID != "" { + span.ServiceID.Metadata[attr.K8sContainerName] = info.ContainerID + } } func (md *metadataDecorator) appendMetadata(span *request.Span, meta *informer.ObjectMeta) { diff --git a/pkg/transform/k8s_test.go b/pkg/transform/k8s_test.go index dbb0fdd3e..1033d0929 100644 --- a/pkg/transform/k8s_test.go +++ b/pkg/transform/k8s_test.go @@ -83,6 +83,7 @@ func TestDecoration(t *testing.T) { "k8s.node.name": "the-node", "k8s.namespace.name": "the-ns", "k8s.pod.name": "pod-12", + "k8s.container.name": "container-12", "k8s.pod.uid": "uid-12", "k8s.deployment.name": "deployment-12", "k8s.owner.name": "deployment-12", @@ -104,6 +105,7 @@ func TestDecoration(t *testing.T) { "k8s.replicaset.name": "rs", "k8s.owner.name": "rs", "k8s.pod.name": "pod-34", + "k8s.container.name": "container-34", "k8s.pod.uid": "uid-34", "k8s.pod.start_time": "2020-01-02 12:34:56", "k8s.cluster.name": "the-cluster", @@ -121,6 +123,7 @@ func TestDecoration(t *testing.T) { "k8s.node.name": "the-node", "k8s.namespace.name": "the-ns", "k8s.pod.name": "the-pod", + "k8s.container.name": "container-56", "k8s.pod.uid": "uid-56", "k8s.pod.start_time": "2020-01-02 12:56:56", "k8s.cluster.name": "the-cluster", @@ -150,6 +153,7 @@ func TestDecoration(t *testing.T) { "k8s.node.name": "the-node", "k8s.namespace.name": "the-ns", "k8s.pod.name": "pod-12", + "k8s.container.name": "container-12", "k8s.pod.uid": "uid-12", "k8s.deployment.name": "deployment-12", "k8s.owner.name": "deployment-12", diff --git a/test/integration/k8s/daemonset/k8s_daemonset_traces_test.go b/test/integration/k8s/daemonset/k8s_daemonset_traces_test.go index 81803c185..fc8a39739 100644 --- a/test/integration/k8s/daemonset/k8s_daemonset_traces_test.go +++ b/test/integration/k8s/daemonset/k8s_daemonset_traces_test.go @@ -67,6 +67,7 @@ func TestBasicTracing(t *testing.T) { parent := res[0] sd := jaeger.DiffAsRegexp([]jaeger.Tag{ {Key: "k8s.pod.name", Type: "string", Value: "^otherinstance-.*"}, + {Key: "k8s.container.name", Type: "string", Value: "^otherinstance-.*"}, {Key: "k8s.node.name", Type: "string", Value: ".+-control-plane$"}, {Key: "k8s.pod.uid", Type: "string", Value: k8s.UUIDRegex}, {Key: "k8s.pod.start_time", Type: "string", Value: k8s.TimeRegex}, @@ -141,6 +142,7 @@ func TestBasicTracing(t *testing.T) { parent := res[0] sd := jaeger.DiffAsRegexp([]jaeger.Tag{ {Key: "k8s.pod.name", Type: "string", Value: "^otherinstance-.*"}, + {Key: "k8s.container.name", Type: "string", Value: "^otherinstance-.*"}, {Key: "k8s.node.name", Type: "string", Value: ".+-control-plane$"}, {Key: "k8s.pod.uid", Type: "string", Value: k8s.UUIDRegex}, {Key: "k8s.pod.start_time", Type: "string", Value: k8s.TimeRegex}, diff --git a/test/integration/k8s/daemonset_python/k8s_daemonset_traces_test.go b/test/integration/k8s/daemonset_python/k8s_daemonset_traces_test.go index 325b9c48d..c9710f747 100644 --- a/test/integration/k8s/daemonset_python/k8s_daemonset_traces_test.go +++ b/test/integration/k8s/daemonset_python/k8s_daemonset_traces_test.go @@ -60,6 +60,7 @@ func TestPythonBasicTracing(t *testing.T) { // check the process information sd = jaeger.DiffAsRegexp([]jaeger.Tag{ {Key: "k8s.pod.name", Type: "string", Value: "^pytestserver-.*"}, + {Key: "k8s.container.name", Type: "string", Value: "^pytestserver-.*"}, {Key: "k8s.node.name", Type: "string", Value: ".+-control-plane$"}, {Key: "k8s.pod.uid", Type: "string", Value: k8s.UUIDRegex}, {Key: "k8s.pod.start_time", Type: "string", Value: k8s.TimeRegex}, @@ -115,6 +116,7 @@ func TestPythonBasicTracing(t *testing.T) { // check the process information sd = jaeger.DiffAsRegexp([]jaeger.Tag{ {Key: "k8s.pod.name", Type: "string", Value: "^pytestserver-.*"}, + {Key: "k8s.container.name", Type: "string", Value: "^pytestserver-.*"}, {Key: "k8s.node.name", Type: "string", Value: ".+-control-plane$"}, {Key: "k8s.pod.uid", Type: "string", Value: k8s.UUIDRegex}, {Key: "k8s.pod.start_time", Type: "string", Value: k8s.TimeRegex}, diff --git a/test/integration/k8s/otel/k8s_otel_traces_test.go b/test/integration/k8s/otel/k8s_otel_traces_test.go index 0356b016a..a206d23de 100644 --- a/test/integration/k8s/otel/k8s_otel_traces_test.go +++ b/test/integration/k8s/otel/k8s_otel_traces_test.go @@ -57,6 +57,7 @@ func TestTracesDecoration(t *testing.T) { require.NotEmpty(t, trace.Spans) sd := jaeger.DiffAsRegexp([]jaeger.Tag{ {Key: "k8s.pod.name", Type: "string", Value: "^testserver-.*"}, + {Key: "k8s.container.name", Type: "string", Value: "^testserver-.*"}, {Key: "k8s.node.name", Type: "string", Value: ".+-control-plane$"}, {Key: "k8s.pod.uid", Type: "string", Value: k8s.UUIDRegex}, {Key: "k8s.pod.start_time", Type: "string", Value: k8s.TimeRegex}, diff --git a/test/integration/k8s/owners/k8s_daemonset_metadata_test.go b/test/integration/k8s/owners/k8s_daemonset_metadata_test.go index a5c8a2336..9eec7f198 100644 --- a/test/integration/k8s/owners/k8s_daemonset_metadata_test.go +++ b/test/integration/k8s/owners/k8s_daemonset_metadata_test.go @@ -61,6 +61,7 @@ func TestDaemonSetMetadata(t *testing.T) { parent := res[0] sd := jaeger.DiffAsRegexp([]jaeger.Tag{ {Key: "k8s.pod.name", Type: "string", Value: "^dsservice-.*"}, + {Key: "k8s.container.name", Type: "string", Value: "^dsservice-.*"}, {Key: "k8s.node.name", Type: "string", Value: ".+-control-plane$"}, {Key: "k8s.pod.uid", Type: "string", Value: k8s.UUIDRegex}, {Key: "k8s.pod.start_time", Type: "string", Value: k8s.TimeRegex}, diff --git a/test/integration/k8s/owners/k8s_statefulset_metadata_test.go b/test/integration/k8s/owners/k8s_statefulset_metadata_test.go index 047ea1800..878f78fcc 100644 --- a/test/integration/k8s/owners/k8s_statefulset_metadata_test.go +++ b/test/integration/k8s/owners/k8s_statefulset_metadata_test.go @@ -61,6 +61,7 @@ func TestStatefulSetMetadata(t *testing.T) { parent := res[0] sd := jaeger.DiffAsRegexp([]jaeger.Tag{ {Key: "k8s.pod.name", Type: "string", Value: "^statefulservice-.*"}, + {Key: "k8s.container.name", Type: "string", Value: "^statefulservice-.*"}, {Key: "k8s.node.name", Type: "string", Value: ".+-control-plane$"}, {Key: "k8s.pod.uid", Type: "string", Value: k8s.UUIDRegex}, {Key: "k8s.pod.start_time", Type: "string", Value: k8s.TimeRegex},