Skip to content

Commit

Permalink
discovery: generate service name from container tag
Browse files Browse the repository at this point in the history
  • Loading branch information
Yumasi committed Nov 27, 2024
1 parent c7ab2a3 commit 4adaf8c
Show file tree
Hide file tree
Showing 2 changed files with 79 additions and 8 deletions.
67 changes: 62 additions & 5 deletions pkg/collector/corechecks/servicediscovery/module/impl_linux.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import (
"sync"
"time"

agentPayload "github.com/DataDog/agent-payload/v5/process"
"github.com/shirou/gopsutil/v3/process"

"github.com/DataDog/datadog-agent/cmd/system-probe/api/module"
Expand Down Expand Up @@ -613,6 +614,65 @@ func (s *discovery) updateServicesCPUStats(services []model.Service) error {
return nil
}

func (s *discovery) enrichContainerData(service *model.Service, containers []*agentPayload.Container, pidToCid map[int]string) {
id, ok := pidToCid[service.PID]
if !ok {
return
}

// The tags we look for service name generation, in their priority order.
// The map entries will be filled as we go through the containers tags.
tagsPriority := []struct {
tagName string
tagValue *string
}{
{"service", nil},
{"app", nil},
{"short_image", nil},
{"kube_container_name", nil},
{"kube_deployment", nil},
{"kube_service", nil},
}

service.ContainerID = id
log.Debugf("Found container id for %v: %v", service.Name, id)
for _, c := range containers {
if c.Id != id {
continue
}

for _, tag := range c.Tags {
// Get index of separator between name and value
sepIndex := strings.IndexRune(tag, ':')
if sepIndex < 0 || sepIndex >= len(tag)-1 {
// Malformed tag; we skip it
continue
}

for i := range tagsPriority {
if tag[:sepIndex] != tagsPriority[i].tagName {
// Not a tag we care about; we skip it
continue
}

value := tag[sepIndex+1:]
tagsPriority[i].tagValue = &value
break
}
}

for _, tag := range tagsPriority {
if tag.tagValue != nil {
service.GeneratedName = *tag.tagValue
log.Debugf("Using %v:%v tag for service name", tag.tagName, *tag.tagValue)
return
}
}

return
}
}

// getStatus returns the list of currently running services.
func (s *discovery) getServices() (*[]model.Service, error) {
procRoot := kernel.ProcFSRoot()
Expand All @@ -628,7 +688,7 @@ func (s *discovery) getServices() (*[]model.Service, error) {

var services []model.Service
alivePids := make(map[int32]struct{}, len(pids))
_, _, pidToCid, err := s.containerProvider.GetContainers(1*time.Minute, nil)
containers, _, pidToCid, err := s.containerProvider.GetContainers(1*time.Minute, nil)
if err != nil {
log.Errorf("could not get containers: %s", err)
}
Expand All @@ -640,10 +700,7 @@ func (s *discovery) getServices() (*[]model.Service, error) {
if service == nil {
continue
}

if id, ok := pidToCid[service.PID]; ok {
service.ContainerID = id
}
s.enrichContainerData(service, containers, pidToCid)

services = append(services, *service)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ import (
"testing"
"time"

agentPayload "github.com/DataDog/agent-payload/v5/process"
"github.com/golang/mock/gomock"
gorillamux "github.com/gorilla/mux"
"github.com/prometheus/procfs"
Expand Down Expand Up @@ -812,9 +813,21 @@ func TestDocker(t *testing.T) {
}
if comm == "python-1111" {
pid1111 = process.PID
mockContainerProvider.EXPECT().GetContainers(1*time.Minute, nil).Return(nil, nil, map[int]string{
pid1111: "dummyCID",
}, nil)
mockContainerProvider.
EXPECT().
GetContainers(1*time.Minute, nil).
Return(
[]*agentPayload.Container{
{Id: "dummyCID", Tags: []string{
"sometag:somevalue",
"kube_service:kube_foo", // Should not have priority compared to app tag, for service naming
"app:foo_from_app_tag",
}},
},
nil,
map[int]string{
pid1111: "dummyCID",
}, nil)

break
}
Expand All @@ -827,6 +840,7 @@ func TestDocker(t *testing.T) {
require.Contains(t, portMap, pid1111)
require.Contains(t, portMap[pid1111].Ports, uint16(1234))
require.Contains(t, portMap[pid1111].ContainerID, "dummyCID")
require.Contains(t, portMap[pid1111].GeneratedName, "foo_from_app_tag")
}

// Check that the cache is cleaned when procceses die.
Expand Down

0 comments on commit 4adaf8c

Please sign in to comment.