Skip to content

Commit

Permalink
Add container name label
Browse files Browse the repository at this point in the history
  • Loading branch information
marctc committed Nov 22, 2024
1 parent e766187 commit 3eee216
Show file tree
Hide file tree
Showing 11 changed files with 26 additions and 0 deletions.
1 change: 1 addition & 0 deletions pkg/export/attributes/attr_defs.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
1 change: 1 addition & 0 deletions pkg/export/attributes/names/attrs.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down
1 change: 1 addition & 0 deletions pkg/export/prom/prom.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)],
Expand Down
9 changes: 9 additions & 0 deletions pkg/internal/kube/store.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
3 changes: 3 additions & 0 deletions pkg/transform/k8s.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
4 changes: 4 additions & 0 deletions pkg/transform/k8s_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand All @@ -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",
Expand All @@ -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",
Expand Down Expand Up @@ -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",
Expand Down
2 changes: 2 additions & 0 deletions test/integration/k8s/daemonset/k8s_daemonset_traces_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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},
Expand Down Expand Up @@ -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},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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},
Expand Down Expand Up @@ -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},
Expand Down
1 change: 1 addition & 0 deletions test/integration/k8s/otel/k8s_otel_traces_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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},
Expand Down
1 change: 1 addition & 0 deletions test/integration/k8s/owners/k8s_daemonset_metadata_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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},
Expand Down

0 comments on commit 3eee216

Please sign in to comment.