diff --git a/pkg/collector/corechecks/servicediscovery/events.go b/pkg/collector/corechecks/servicediscovery/events.go index 35ecd4aa2a6db..f1d040dacefcb 100644 --- a/pkg/collector/corechecks/servicediscovery/events.go +++ b/pkg/collector/corechecks/servicediscovery/events.go @@ -31,6 +31,7 @@ type eventPayload struct { GeneratedServiceName string `json:"generated_service_name"` GeneratedServiceNameSource string `json:"generated_service_name_source,omitempty"` ContainerServiceName string `json:"container_service_name,omitempty"` + ContainerServiceNameSource string `json:"container_service_name_source,omitempty"` DDService string `json:"dd_service,omitempty"` HostName string `json:"host_name"` Env string `json:"env"` @@ -81,6 +82,7 @@ func (ts *telemetrySender) newEvent(t eventType, svc serviceInfo) *event { GeneratedServiceName: svc.service.GeneratedName, GeneratedServiceNameSource: svc.service.GeneratedNameSource, ContainerServiceName: svc.service.ContainerServiceName, + ContainerServiceNameSource: svc.service.ContainerServiceNameSource, DDService: svc.service.DDService, HostName: host, Env: env, diff --git a/pkg/collector/corechecks/servicediscovery/events_test.go b/pkg/collector/corechecks/servicediscovery/events_test.go index 63f33b8483a70..b38dc5ddf084f 100644 --- a/pkg/collector/corechecks/servicediscovery/events_test.go +++ b/pkg/collector/corechecks/servicediscovery/events_test.go @@ -56,18 +56,19 @@ func Test_telemetrySender(t *testing.T) { svc := serviceInfo{ service: model.Service{ - PID: 99, - CommandLine: []string{"test-service", "--args"}, - Ports: []uint16{80, 8080}, - StartTimeMilli: uint64(now.Add(-20 * time.Minute).UnixMilli()), - RSS: 500 * 1024 * 1024, - GeneratedName: "generated-name", - GeneratedNameSource: "generated-name-source", - ContainerServiceName: "container-service-name", - DDService: "dd-service", - DDServiceInjected: true, - CPUCores: 1.5, - ContainerID: "abcd", + PID: 99, + CommandLine: []string{"test-service", "--args"}, + Ports: []uint16{80, 8080}, + StartTimeMilli: uint64(now.Add(-20 * time.Minute).UnixMilli()), + RSS: 500 * 1024 * 1024, + GeneratedName: "generated-name", + GeneratedNameSource: "generated-name-source", + ContainerServiceName: "container-service-name", + ContainerServiceNameSource: "service", + DDService: "dd-service", + DDServiceInjected: true, + CPUCores: 1.5, + ContainerID: "abcd", }, meta: ServiceMetadata{ Name: "test-service", @@ -92,6 +93,7 @@ func Test_telemetrySender(t *testing.T) { GeneratedServiceName: "generated-name", GeneratedServiceNameSource: "generated-name-source", ContainerServiceName: "container-service-name", + ContainerServiceNameSource: "service", DDService: "dd-service", ServiceNameSource: "injected", HostName: "test-host", @@ -119,6 +121,7 @@ func Test_telemetrySender(t *testing.T) { GeneratedServiceName: "generated-name", GeneratedServiceNameSource: "generated-name-source", ContainerServiceName: "container-service-name", + ContainerServiceNameSource: "service", DDService: "dd-service", ServiceNameSource: "injected", HostName: "test-host", @@ -146,6 +149,7 @@ func Test_telemetrySender(t *testing.T) { GeneratedServiceName: "generated-name", GeneratedServiceNameSource: "generated-name-source", ContainerServiceName: "container-service-name", + ContainerServiceNameSource: "service", DDService: "dd-service", ServiceNameSource: "injected", HostName: "test-host", @@ -192,14 +196,15 @@ func Test_telemetrySender_name_provided(t *testing.T) { svc := serviceInfo{ service: model.Service{ - PID: 55, - CommandLine: []string{"foo", "--option"}, - StartTimeMilli: uint64(now.Add(-20 * time.Minute).UnixMilli()), - GeneratedName: "generated-name2", - GeneratedNameSource: "generated-name-source2", - ContainerServiceName: "container-service-name2", - DDService: "dd-service-provided", - ContainerID: "abcd", + PID: 55, + CommandLine: []string{"foo", "--option"}, + StartTimeMilli: uint64(now.Add(-20 * time.Minute).UnixMilli()), + GeneratedName: "generated-name2", + GeneratedNameSource: "generated-name-source2", + ContainerServiceName: "container-service-name2", + ContainerServiceNameSource: "service", + DDService: "dd-service-provided", + ContainerID: "abcd", }, meta: ServiceMetadata{ Name: "test-service", @@ -224,6 +229,7 @@ func Test_telemetrySender_name_provided(t *testing.T) { GeneratedServiceName: "generated-name2", GeneratedServiceNameSource: "generated-name-source2", ContainerServiceName: "container-service-name2", + ContainerServiceNameSource: "service", DDService: "dd-service-provided", ServiceNameSource: "provided", HostName: "test-host", @@ -248,6 +254,7 @@ func Test_telemetrySender_name_provided(t *testing.T) { GeneratedServiceName: "generated-name2", GeneratedServiceNameSource: "generated-name-source2", ContainerServiceName: "container-service-name2", + ContainerServiceNameSource: "service", DDService: "dd-service-provided", ServiceNameSource: "provided", HostName: "test-host", @@ -272,6 +279,7 @@ func Test_telemetrySender_name_provided(t *testing.T) { GeneratedServiceName: "generated-name2", GeneratedServiceNameSource: "generated-name-source2", ContainerServiceName: "container-service-name2", + ContainerServiceNameSource: "service", DDService: "dd-service-provided", ServiceNameSource: "provided", HostName: "test-host", diff --git a/pkg/collector/corechecks/servicediscovery/impl_linux.go b/pkg/collector/corechecks/servicediscovery/impl_linux.go index 6df763167209d..2f0b3df61d63f 100644 --- a/pkg/collector/corechecks/servicediscovery/impl_linux.go +++ b/pkg/collector/corechecks/servicediscovery/impl_linux.go @@ -129,6 +129,7 @@ func (li *linuxImpl) DiscoverServices() (*discoveredServices, error) { svc.service.ContainerID = service.ContainerID svc.service.GeneratedName = service.GeneratedName svc.service.ContainerServiceName = service.ContainerServiceName + svc.service.ContainerServiceNameSource = service.ContainerServiceNameSource svc.service.Name = service.Name svc.meta.Name = service.Name events.heartbeat = append(events.heartbeat, *svc) @@ -169,6 +170,7 @@ func (li *linuxImpl) handlePotentialServices(events *serviceEvents, now time.Tim svc.service.ContainerID = service.ContainerID svc.service.GeneratedName = service.GeneratedName svc.service.ContainerServiceName = service.ContainerServiceName + svc.service.ContainerServiceNameSource = service.ContainerServiceNameSource svc.service.Name = service.Name svc.meta.Name = service.Name diff --git a/pkg/collector/corechecks/servicediscovery/impl_linux_test.go b/pkg/collector/corechecks/servicediscovery/impl_linux_test.go index 28c87e6837f38..ba5e0e5a1caad 100644 --- a/pkg/collector/corechecks/servicediscovery/impl_linux_test.go +++ b/pkg/collector/corechecks/servicediscovery/impl_linux_test.go @@ -71,50 +71,53 @@ var ( var ( portTCP8080 = model.Service{ - PID: procTestService1.pid, - Name: "test-service-1", - GeneratedName: "test-service-1-generated", - GeneratedNameSource: "test-service-1-generated-source", - ContainerServiceName: "test-service-1-container", - DDService: "test-service-1", - DDServiceInjected: true, - Ports: []uint16{8080}, - APMInstrumentation: string(apm.None), - RSS: 100 * 1024 * 1024, - CPUCores: 1.5, - CommandLine: []string{"test-service-1"}, - StartTimeMilli: procLaunchedMilli, - ContainerID: dummyContainerID, + PID: procTestService1.pid, + Name: "test-service-1", + GeneratedName: "test-service-1-generated", + GeneratedNameSource: "test-service-1-generated-source", + ContainerServiceName: "test-service-1-container", + ContainerServiceNameSource: "service", + DDService: "test-service-1", + DDServiceInjected: true, + Ports: []uint16{8080}, + APMInstrumentation: string(apm.None), + RSS: 100 * 1024 * 1024, + CPUCores: 1.5, + CommandLine: []string{"test-service-1"}, + StartTimeMilli: procLaunchedMilli, + ContainerID: dummyContainerID, } portTCP8080UpdatedRSS = model.Service{ - PID: procTestService1.pid, - Name: "test-service-1", - GeneratedName: "test-service-1-generated", - GeneratedNameSource: "test-service-1-generated-source", - ContainerServiceName: "test-service-1-container", - DDService: "test-service-1", - DDServiceInjected: true, - Ports: []uint16{8080}, - APMInstrumentation: string(apm.None), - RSS: 200 * 1024 * 1024, - CPUCores: 1.5, - CommandLine: []string{"test-service-1"}, - StartTimeMilli: procLaunchedMilli, - ContainerID: dummyContainerID, + PID: procTestService1.pid, + Name: "test-service-1", + GeneratedName: "test-service-1-generated", + GeneratedNameSource: "test-service-1-generated-source", + ContainerServiceName: "test-service-1-container", + ContainerServiceNameSource: "service", + DDService: "test-service-1", + DDServiceInjected: true, + Ports: []uint16{8080}, + APMInstrumentation: string(apm.None), + RSS: 200 * 1024 * 1024, + CPUCores: 1.5, + CommandLine: []string{"test-service-1"}, + StartTimeMilli: procLaunchedMilli, + ContainerID: dummyContainerID, } portTCP8080DifferentPID = model.Service{ - PID: procTestService1DifferentPID.pid, - Name: "test-service-1", - GeneratedName: "test-service-1-generated", - GeneratedNameSource: "test-service-1-generated-source", - ContainerServiceName: "test-service-1-container", - DDService: "test-service-1", - DDServiceInjected: true, - Ports: []uint16{8080}, - APMInstrumentation: string(apm.Injected), - CommandLine: []string{"test-service-1"}, - StartTimeMilli: procLaunchedMilli, - ContainerID: dummyContainerID, + PID: procTestService1DifferentPID.pid, + Name: "test-service-1", + GeneratedName: "test-service-1-generated", + GeneratedNameSource: "test-service-1-generated-source", + ContainerServiceName: "test-service-1-container", + ContainerServiceNameSource: "service", + DDService: "test-service-1", + DDServiceInjected: true, + Ports: []uint16{8080}, + APMInstrumentation: string(apm.Injected), + CommandLine: []string{"test-service-1"}, + StartTimeMilli: procLaunchedMilli, + ContainerID: dummyContainerID, } portTCP8081 = model.Service{ PID: procIgnoreService1.pid, @@ -125,27 +128,29 @@ var ( ContainerID: dummyContainerID, } portTCP5000 = model.Service{ - PID: procPythonService.pid, - Name: "python-service", - GeneratedName: "python-service", - GeneratedNameSource: "python-service-source", - ContainerServiceName: "test-service-1-container", - Language: "python", - Ports: []uint16{5000}, - CommandLine: pythonCommandLine, - StartTimeMilli: procLaunchedMilli, - ContainerID: dummyContainerID, + PID: procPythonService.pid, + Name: "python-service", + GeneratedName: "python-service", + GeneratedNameSource: "python-service-source", + ContainerServiceName: "test-service-1-container", + ContainerServiceNameSource: "app", + Language: "python", + Ports: []uint16{5000}, + CommandLine: pythonCommandLine, + StartTimeMilli: procLaunchedMilli, + ContainerID: dummyContainerID, } portTCP5432 = model.Service{ - PID: procTestService1Repeat.pid, - Name: "test-service-1", - GeneratedName: "test-service-1", - GeneratedNameSource: "test-service-1-generated-source", - ContainerServiceName: "test-service-1-container", - Ports: []uint16{5432}, - CommandLine: []string{"test-service-1"}, - StartTimeMilli: procLaunchedMilli, - ContainerID: dummyContainerID, + PID: procTestService1Repeat.pid, + Name: "test-service-1", + GeneratedName: "test-service-1", + GeneratedNameSource: "test-service-1-generated-source", + ContainerServiceName: "test-service-1-container", + ContainerServiceNameSource: "service", + Ports: []uint16{5432}, + CommandLine: []string{"test-service-1"}, + StartTimeMilli: procLaunchedMilli, + ContainerID: dummyContainerID, } ) @@ -241,6 +246,7 @@ func Test_linuxImpl(t *testing.T) { GeneratedServiceName: "test-service-1-generated", GeneratedServiceNameSource: "test-service-1-generated-source", ContainerServiceName: "test-service-1-container", + ContainerServiceNameSource: "service", DDService: "test-service-1", ServiceNameSource: "injected", ServiceType: "web_service", @@ -267,6 +273,7 @@ func Test_linuxImpl(t *testing.T) { GeneratedServiceName: "test-service-1-generated", GeneratedServiceNameSource: "test-service-1-generated-source", ContainerServiceName: "test-service-1-container", + ContainerServiceNameSource: "service", DDService: "test-service-1", ServiceNameSource: "injected", ServiceType: "web_service", @@ -293,6 +300,7 @@ func Test_linuxImpl(t *testing.T) { GeneratedServiceName: "test-service-1-generated", GeneratedServiceNameSource: "test-service-1-generated-source", ContainerServiceName: "test-service-1-container", + ContainerServiceNameSource: "service", DDService: "test-service-1", ServiceNameSource: "injected", ServiceType: "web_service", @@ -319,6 +327,7 @@ func Test_linuxImpl(t *testing.T) { GeneratedServiceName: "python-service", GeneratedServiceNameSource: "python-service-source", ContainerServiceName: "test-service-1-container", + ContainerServiceNameSource: "app", ServiceType: "web_service", HostName: host, Env: "", @@ -341,6 +350,7 @@ func Test_linuxImpl(t *testing.T) { GeneratedServiceName: "python-service", GeneratedServiceNameSource: "python-service-source", ContainerServiceName: "test-service-1-container", + ContainerServiceNameSource: "app", ServiceType: "web_service", HostName: host, Env: "", @@ -400,6 +410,7 @@ func Test_linuxImpl(t *testing.T) { GeneratedServiceName: "test-service-1", GeneratedServiceNameSource: "test-service-1-generated-source", ContainerServiceName: "test-service-1-container", + ContainerServiceNameSource: "service", ServiceType: "db", HostName: host, Env: "", @@ -421,6 +432,7 @@ func Test_linuxImpl(t *testing.T) { GeneratedServiceName: "test-service-1-generated", GeneratedServiceNameSource: "test-service-1-generated-source", ContainerServiceName: "test-service-1-container", + ContainerServiceNameSource: "service", DDService: "test-service-1", ServiceNameSource: "injected", ServiceType: "web_service", @@ -447,6 +459,7 @@ func Test_linuxImpl(t *testing.T) { GeneratedServiceName: "test-service-1", GeneratedServiceNameSource: "test-service-1-generated-source", ContainerServiceName: "test-service-1-container", + ContainerServiceNameSource: "service", ServiceType: "db", HostName: host, Env: "", @@ -468,6 +481,7 @@ func Test_linuxImpl(t *testing.T) { GeneratedServiceName: "test-service-1", GeneratedServiceNameSource: "test-service-1-generated-source", ContainerServiceName: "test-service-1-container", + ContainerServiceNameSource: "service", ServiceType: "db", HostName: host, Env: "", @@ -489,6 +503,7 @@ func Test_linuxImpl(t *testing.T) { GeneratedServiceName: "test-service-1-generated", GeneratedServiceNameSource: "test-service-1-generated-source", ContainerServiceName: "test-service-1-container", + ContainerServiceNameSource: "service", DDService: "test-service-1", ServiceNameSource: "injected", ServiceType: "web_service", @@ -550,6 +565,7 @@ func Test_linuxImpl(t *testing.T) { GeneratedServiceName: "test-service-1-generated", GeneratedServiceNameSource: "test-service-1-generated-source", ContainerServiceName: "test-service-1-container", + ContainerServiceNameSource: "service", DDService: "test-service-1", ServiceNameSource: "injected", ServiceType: "web_service", @@ -576,6 +592,7 @@ func Test_linuxImpl(t *testing.T) { GeneratedServiceName: "test-service-1-generated", GeneratedServiceNameSource: "test-service-1-generated-source", ContainerServiceName: "test-service-1-container", + ContainerServiceNameSource: "service", DDService: "test-service-1", ServiceNameSource: "injected", ServiceType: "web_service", diff --git a/pkg/collector/corechecks/servicediscovery/model/model.go b/pkg/collector/corechecks/servicediscovery/model/model.go index b816cfcbd5ea2..9a99681fe6cfc 100644 --- a/pkg/collector/corechecks/servicediscovery/model/model.go +++ b/pkg/collector/corechecks/servicediscovery/model/model.go @@ -8,22 +8,23 @@ package model // Service represents a listening process. type Service struct { - PID int `json:"pid"` - Name string `json:"name"` - GeneratedName string `json:"generated_name"` - GeneratedNameSource string `json:"generated_name_source"` - ContainerServiceName string `json:"container_service_name"` - DDService string `json:"dd_service"` - DDServiceInjected bool `json:"dd_service_injected"` - CheckedContainerData bool `json:"checked_container_data"` - Ports []uint16 `json:"ports"` - APMInstrumentation string `json:"apm_instrumentation"` - Language string `json:"language"` - RSS uint64 `json:"rss"` - CommandLine []string `json:"cmdline"` - StartTimeMilli uint64 `json:"start_time"` - CPUCores float64 `json:"cpu_cores"` - ContainerID string `json:"container_id"` + PID int `json:"pid"` + Name string `json:"name"` + GeneratedName string `json:"generated_name"` + GeneratedNameSource string `json:"generated_name_source"` + ContainerServiceName string `json:"container_service_name"` + ContainerServiceNameSource string `json:"container_service_name_source"` + DDService string `json:"dd_service"` + DDServiceInjected bool `json:"dd_service_injected"` + CheckedContainerData bool `json:"checked_container_data"` + Ports []uint16 `json:"ports"` + APMInstrumentation string `json:"apm_instrumentation"` + Language string `json:"language"` + RSS uint64 `json:"rss"` + CommandLine []string `json:"cmdline"` + StartTimeMilli uint64 `json:"start_time"` + CPUCores float64 `json:"cpu_cores"` + ContainerID string `json:"container_id"` } // ServicesResponse is the response for the system-probe /discovery/services endpoint. diff --git a/pkg/collector/corechecks/servicediscovery/module/impl_linux.go b/pkg/collector/corechecks/servicediscovery/module/impl_linux.go index 67406b0e505c5..18220974cb5dc 100644 --- a/pkg/collector/corechecks/servicediscovery/module/impl_linux.go +++ b/pkg/collector/corechecks/servicediscovery/module/impl_linux.go @@ -619,7 +619,7 @@ func (s *discovery) updateServicesCPUStats(services []model.Service) error { return nil } -func getServiceNameFromContainerTags(tags []string) string { +func getServiceNameFromContainerTags(tags []string) (string, string) { // 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 { @@ -660,10 +660,10 @@ func getServiceNameFromContainerTags(tags []string) string { } log.Debugf("Using %v:%v tag for service name", tag.tagName, *tag.tagValue) - return *tag.tagValue + return tag.tagName, *tag.tagValue } - return "" + return "", "" } func (s *discovery) enrichContainerData(service *model.Service, containers map[string]*agentPayload.Container, pidToCid map[int]string) { @@ -684,8 +684,9 @@ func (s *discovery) enrichContainerData(service *model.Service, containers map[s return } - serviceName := getServiceNameFromContainerTags(container.Tags) + tagName, serviceName := getServiceNameFromContainerTags(container.Tags) service.ContainerServiceName = serviceName + service.ContainerServiceNameSource = tagName service.CheckedContainerData = true s.mux.Lock() diff --git a/pkg/collector/corechecks/servicediscovery/module/impl_linux_test.go b/pkg/collector/corechecks/servicediscovery/module/impl_linux_test.go index 91f516b4d4a12..cdda8f3cc754a 100644 --- a/pkg/collector/corechecks/servicediscovery/module/impl_linux_test.go +++ b/pkg/collector/corechecks/servicediscovery/module/impl_linux_test.go @@ -851,6 +851,7 @@ func TestDocker(t *testing.T) { require.Contains(t, portMap[pid1111].GeneratedName, "http.server") require.Contains(t, portMap[pid1111].GeneratedNameSource, string(usm.CommandLine)) require.Contains(t, portMap[pid1111].ContainerServiceName, "foo_from_app_tag") + require.Contains(t, portMap[pid1111].ContainerServiceNameSource, "app") } // Check that the cache is cleaned when procceses die. @@ -923,56 +924,67 @@ func TestTagsPriority(t *testing.T) { cases := []struct { name string tags []string + expectedTagName string expectedServiceName string }{ { "nil tag list", nil, "", + "", }, { "empty tag list", []string{}, "", + "", }, { "no useful tags", []string{"foo:bar"}, "", + "", }, { "malformed tag", []string{"foobar"}, "", + "", }, { "service tag", []string{"service:foo"}, + "service", "foo", }, { "app tag", []string{"app:foo"}, + "app", "foo", }, { "short_image tag", []string{"short_image:foo"}, + "short_image", "foo", }, { "kube_container_name tag", []string{"kube_container_name:foo"}, + "kube_container_name", "foo", }, { "kube_deployment tag", []string{"kube_deployment:foo"}, + "kube_deployment", "foo", }, { "kube_service tag", []string{"kube_service:foo"}, + "kube_service", "foo", }, { @@ -983,6 +995,7 @@ func TestTagsPriority(t *testing.T) { "service:my_service", "malformed", }, + "service", "my_service", }, { @@ -991,6 +1004,7 @@ func TestTagsPriority(t *testing.T) { "service:", "app:foo", }, + "app", "foo", }, { @@ -1002,6 +1016,7 @@ func TestTagsPriority(t *testing.T) { "service:my_service", "malformed", }, + "service", "my_service", }, { @@ -1014,14 +1029,16 @@ func TestTagsPriority(t *testing.T) { "app:my_app", "service:my_service", }, + "service", "my_service", }, } for _, c := range cases { t.Run(c.name, func(t *testing.T) { - name := getServiceNameFromContainerTags(c.tags) + tagName, name := getServiceNameFromContainerTags(c.tags) require.Equalf(t, c.expectedServiceName, name, "got wrong service name from container tags") + require.Equalf(t, c.expectedTagName, tagName, "got wrong tag name for service naming") }) } }