9999type MetadataProvider interface {
100100 // AddMetadata adds metadata to the provided labels.Builder for the given PID.
101101 // It returns whether the metadata can be safely cached.
102- AddMetadata (pid libpf.PID , lb * labels.Builder ) bool
102+ AddMetadata (ctx context. Context , pid libpf.PID , lb * labels.Builder ) bool
103103}
104104
105105// containerMetadataProvider does the retrieval of container metadata for a particular pid.
@@ -521,7 +521,7 @@ func matchContainerID(containerIDStr string) (string, error) {
521521}
522522
523523// AddMetadata adds metadata to the provided labels.Builder for the given PID.
524- func (p * containerMetadataProvider ) AddMetadata (pid libpf.PID , lb * labels.Builder ) bool {
524+ func (p * containerMetadataProvider ) AddMetadata (ctx context. Context , pid libpf.PID , lb * labels.Builder ) bool {
525525 // Fast path, check container metadata has been cached
526526 // For kubernetes pods, the shared informer may have updated
527527 // the container id to container metadata cache, so retrieve the container ID for this pid.
@@ -552,7 +552,7 @@ func (p *containerMetadataProvider) AddMetadata(pid libpf.PID, lb *labels.Builde
552552 // client.
553553 switch {
554554 case isContainerEnvironment (env , envKubernetes ) && p .kubeClientSet != nil :
555- metadata , err := p .getKubernetesPodMetadata (pidContainerID )
555+ metadata , err := p .getKubernetesPodMetadata (ctx , pidContainerID )
556556 if err != nil {
557557 log .Debugf ("Failed to get kubernetes pod metadata for container id %v: %v" ,
558558 pidContainerID , err )
@@ -563,7 +563,7 @@ func (p *containerMetadataProvider) AddMetadata(pid libpf.PID, lb *labels.Builde
563563 }
564564 return true
565565 case isContainerEnvironment (env , envDocker ) && p .dockerClient != nil :
566- metadata , err := p .getDockerContainerMetadata (pidContainerID )
566+ metadata , err := p .getDockerContainerMetadata (ctx , pidContainerID )
567567 if err != nil {
568568 log .Warnf ("Failed to get docker container metadata for container id %v: %v" ,
569569 pidContainerID , err )
@@ -574,7 +574,7 @@ func (p *containerMetadataProvider) AddMetadata(pid libpf.PID, lb *labels.Builde
574574 }
575575 return true
576576 case isContainerEnvironment (env , envContainerd ) && p .containerdClient != nil :
577- metadata , err := p .getContainerdContainerMetadata (pidContainerID )
577+ metadata , err := p .getContainerdContainerMetadata (ctx , pidContainerID )
578578 if err != nil {
579579 log .Debugf ("Failed to get containerd container metadata for container id %v: %v" ,
580580 pidContainerID , err )
@@ -596,12 +596,12 @@ func (p *containerMetadataProvider) AddMetadata(pid libpf.PID, lb *labels.Builde
596596 }
597597}
598598
599- func (p * containerMetadataProvider ) getKubernetesPodMetadata (pidContainerID string ) (
599+ func (p * containerMetadataProvider ) getKubernetesPodMetadata (ctx context. Context , pidContainerID string ) (
600600 model.LabelSet , error ) {
601601 log .Debugf ("Get kubernetes pod metadata for container id %v" , pidContainerID )
602602
603603 p .kubernetesClientQueryCount .Add (1 )
604- pods , err := p .kubeClientSet .CoreV1 ().Pods ("" ).List (context . TODO () , v1.ListOptions {
604+ pods , err := p .kubeClientSet .CoreV1 ().Pods ("" ).List (ctx , v1.ListOptions {
605605 FieldSelector : "spec.nodeName=" + p .nodeName ,
606606 })
607607 if err != nil {
@@ -657,12 +657,12 @@ func (p *containerMetadataProvider) getKubernetesPodMetadata(pidContainerID stri
657657 "containerID '%v' in %d pods" , pidContainerID , len (pods .Items ))
658658}
659659
660- func (p * containerMetadataProvider ) getDockerContainerMetadata (pidContainerID string ) (
660+ func (p * containerMetadataProvider ) getDockerContainerMetadata (ctx context. Context , pidContainerID string ) (
661661 model.LabelSet , error ) {
662662 log .Debugf ("Get docker container metadata for container id %v" , pidContainerID )
663663
664664 p .dockerClientQueryCount .Add (1 )
665- containers , err := p .dockerClient .ContainerList (context . Background () ,
665+ containers , err := p .dockerClient .ContainerList (ctx ,
666666 container.ListOptions {})
667667 if err != nil {
668668 return nil , fmt .Errorf ("failed to list docker containers, %v" , err )
@@ -676,6 +676,13 @@ func (p *containerMetadataProvider) getDockerContainerMetadata(pidContainerID st
676676 "__meta_docker_container_id" : lv (containers [i ].ID ),
677677 "__meta_docker_container_name" : lv (containerName ),
678678 }
679+
680+ for k , v := range containers [i ].Labels {
681+ ln := strutil .SanitizeLabelName (k )
682+ metadata [model .LabelName ("__meta_docker_container_label_" + ln )] = lv (v )
683+ metadata [model .LabelName ("__meta_docker_container_labelpresent_" + ln )] = presentValue
684+ }
685+
679686 p .containerMetadataCache .Add (pidContainerID , metadata )
680687 return metadata , nil
681688 }
@@ -686,7 +693,7 @@ func (p *containerMetadataProvider) getDockerContainerMetadata(pidContainerID st
686693 pidContainerID )
687694}
688695
689- func (p * containerMetadataProvider ) getContainerdContainerMetadata (pidContainerID string ) (
696+ func (p * containerMetadataProvider ) getContainerdContainerMetadata (ctx context. Context , pidContainerID string ) (
690697 model.LabelSet , error ) {
691698 log .Debugf ("Get containerd container metadata for container id %v" , pidContainerID )
692699
@@ -701,7 +708,7 @@ func (p *containerMetadataProvider) getContainerdContainerMetadata(pidContainerI
701708 }
702709
703710 p .containerdClientQueryCount .Add (1 )
704- ctx : = namespaces .WithNamespace (context . Background () , fields [1 ])
711+ ctx = namespaces .WithNamespace (ctx , fields [1 ])
705712 containers , err := p .containerdClient .Containers (ctx )
706713 if err != nil {
707714 return nil ,
@@ -718,6 +725,19 @@ func (p *containerMetadataProvider) getContainerdContainerMetadata(pidContainerI
718725 "__meta_containerd_container_name" : lv (fields [2 ]),
719726 "__meta_containerd_pod_name" : lv (fields [1 ]),
720727 }
728+
729+ lbls , err := container .Labels (ctx )
730+ if err != nil {
731+ return nil ,
732+ fmt .Errorf ("failed to get containerd container labels for container id '%s': %v" ,
733+ fields [2 ], err )
734+ }
735+ for k , v := range lbls {
736+ ln := strutil .SanitizeLabelName (k )
737+ metadata [model .LabelName ("__meta_containerd_container_label_" + ln )] = lv (v )
738+ metadata [model .LabelName ("__meta_containerd_container_labelpresent_" + ln )] = presentValue
739+ }
740+
721741 p .containerMetadataCache .Add (pidContainerID , metadata )
722742 return metadata , nil
723743 }
0 commit comments