Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

service discovery: Report additional names separately #33817

Merged
merged 1 commit into from
Feb 10, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions pkg/collector/corechecks/servicediscovery/events.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ type eventPayload struct {
ServiceName string `json:"service_name"`
GeneratedServiceName string `json:"generated_service_name"`
GeneratedServiceNameSource string `json:"generated_service_name_source,omitempty"`
AdditionalGeneratedNames []string `json:"additional_generated_names,omitempty"`
ContainerServiceName string `json:"container_service_name,omitempty"`
ContainerServiceNameSource string `json:"container_service_name_source,omitempty"`
DDService string `json:"dd_service,omitempty"`
Expand Down Expand Up @@ -82,6 +83,7 @@ func (ts *telemetrySender) newEvent(t eventType, service model.Service) *event {
ServiceName: service.Name,
GeneratedServiceName: service.GeneratedName,
GeneratedServiceNameSource: service.GeneratedNameSource,
AdditionalGeneratedNames: service.AdditionalGeneratedNames,
ContainerServiceName: service.ContainerServiceName,
ContainerServiceNameSource: service.ContainerServiceNameSource,
DDService: service.DDService,
Expand Down
4 changes: 4 additions & 0 deletions pkg/collector/corechecks/servicediscovery/events_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ func Test_telemetrySender(t *testing.T) {
Name: "test-service",
GeneratedName: "generated-name",
GeneratedNameSource: "generated-name-source",
AdditionalGeneratedNames: []string{"additional0", "additional1"},
ContainerServiceName: "container-service-name",
ContainerServiceNameSource: "service",
DDService: "dd-service",
Expand Down Expand Up @@ -86,6 +87,7 @@ func Test_telemetrySender(t *testing.T) {
ServiceName: "test-service",
GeneratedServiceName: "generated-name",
GeneratedServiceNameSource: "generated-name-source",
AdditionalGeneratedNames: []string{"additional0", "additional1"},
ContainerServiceName: "container-service-name",
ContainerServiceNameSource: "service",
DDService: "dd-service",
Expand Down Expand Up @@ -114,6 +116,7 @@ func Test_telemetrySender(t *testing.T) {
ServiceName: "test-service",
GeneratedServiceName: "generated-name",
GeneratedServiceNameSource: "generated-name-source",
AdditionalGeneratedNames: []string{"additional0", "additional1"},
ContainerServiceName: "container-service-name",
ContainerServiceNameSource: "service",
DDService: "dd-service",
Expand Down Expand Up @@ -142,6 +145,7 @@ func Test_telemetrySender(t *testing.T) {
ServiceName: "test-service",
GeneratedServiceName: "generated-name",
GeneratedServiceNameSource: "generated-name-source",
AdditionalGeneratedNames: []string{"additional0", "additional1"},
ContainerServiceName: "container-service-name",
ContainerServiceNameSource: "service",
DDService: "dd-service",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,7 @@ var (
Name: "python-service",
GeneratedName: "python-service",
GeneratedNameSource: "python-service-source",
AdditionalGeneratedNames: []string{"bar", "foo"},
ContainerServiceName: "test-service-1-container",
ContainerServiceNameSource: "app",
Language: "python",
Expand Down Expand Up @@ -268,6 +269,7 @@ func Test_linuxImpl(t *testing.T) {
ServiceName: "python-service",
GeneratedServiceName: "python-service",
GeneratedServiceNameSource: "python-service-source",
AdditionalGeneratedNames: []string{"bar", "foo"},
ContainerServiceName: "test-service-1-container",
ContainerServiceNameSource: "app",
ServiceType: "web_service",
Expand All @@ -291,6 +293,7 @@ func Test_linuxImpl(t *testing.T) {
ServiceName: "python-service",
GeneratedServiceName: "python-service",
GeneratedServiceNameSource: "python-service-source",
AdditionalGeneratedNames: []string{"bar", "foo"},
ContainerServiceName: "test-service-1-container",
ContainerServiceNameSource: "app",
ServiceType: "web_service",
Expand Down
1 change: 1 addition & 0 deletions pkg/collector/corechecks/servicediscovery/model/model.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ type Service struct {
Name string `json:"name"`
GeneratedName string `json:"generated_name"`
GeneratedNameSource string `json:"generated_name_source"`
AdditionalGeneratedNames []string `json:"additional_generated_names"`
ContainerServiceName string `json:"container_service_name"`
ContainerServiceNameSource string `json:"container_service_name_source"`
DDService string `json:"dd_service"`
Expand Down
21 changes: 12 additions & 9 deletions pkg/collector/corechecks/servicediscovery/module/impl_linux.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ type serviceInfo struct {
name string
generatedName string
generatedNameSource string
additionalGeneratedNames []string
containerServiceName string
containerServiceNameSource string
ddServiceName string
Expand Down Expand Up @@ -87,6 +88,7 @@ func (i *serviceInfo) toModelService(pid int32, out *model.Service) *model.Servi
out.Name = i.name
out.GeneratedName = i.generatedName
out.GeneratedNameSource = i.generatedNameSource
out.AdditionalGeneratedNames = i.additionalGeneratedNames
out.ContainerServiceName = i.containerServiceName
out.ContainerServiceNameSource = i.containerServiceNameSource
out.DDService = i.ddServiceName
Expand Down Expand Up @@ -538,15 +540,16 @@ func (s *discovery) getServiceInfo(pid int32) (*serviceInfo, error) {
}

return &serviceInfo{
name: name,
generatedName: nameMeta.Name,
generatedNameSource: string(nameMeta.Source),
ddServiceName: nameMeta.DDService,
language: lang,
apmInstrumentation: apmInstrumentation,
ddServiceInjected: nameMeta.DDServiceInjected,
cmdLine: sanitizeCmdLine(s.scrubber, cmdline),
startTimeMilli: uint64(createTime),
name: name,
generatedName: nameMeta.Name,
generatedNameSource: string(nameMeta.Source),
additionalGeneratedNames: nameMeta.AdditionalNames,
ddServiceName: nameMeta.DDService,
language: lang,
apmInstrumentation: apmInstrumentation,
ddServiceInjected: nameMeta.DDServiceInjected,
cmdLine: sanitizeCmdLine(s.scrubber, cmdline),
startTimeMilli: uint64(createTime),
}, nil
}

Expand Down
26 changes: 14 additions & 12 deletions pkg/collector/corechecks/servicediscovery/service_detector.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,29 +23,31 @@ type ServiceMetadata struct {
}

func fixAdditionalNames(additionalNames []string) []string {
if len(additionalNames) == 0 {
return additionalNames
}

out := make([]string, 0, len(additionalNames))
for _, v := range additionalNames {
if len(strings.TrimSpace(v)) > 0 {
out = append(out, v)
if len(strings.TrimSpace(v)) == 0 {
continue
}

// lang is only used for fallback names, which we don't use since we
// check for errors.
norm, err := traceutil.NormalizeService(v, "")
if err == nil {
out = append(out, norm)
}
}
slices.Sort(out)
return out
}

func makeFinalName(meta usm.ServiceMetadata) string {
name := meta.Name
if len(meta.AdditionalNames) > 0 {
name = name + "-" + strings.Join(fixAdditionalNames(meta.AdditionalNames), "-")
}

return name
}

// fixupMetadata performs additional adjustments on the meta data returned from
// the meta data extraction library.
func fixupMetadata(meta usm.ServiceMetadata, lang language.Language) usm.ServiceMetadata {
meta.Name = makeFinalName(meta)
meta.AdditionalNames = fixAdditionalNames(meta.AdditionalNames)

langName := ""
if lang != language.Unknown {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ func TestFixup(t *testing.T) {
assert.Equal(t, meta.Name, "unnamed-service")
assert.Equal(t, meta.DDService, "")

meta = fixupMetadata(usm.ServiceMetadata{Name: "foo", AdditionalNames: []string{"bar", "baz"}}, language.Go)
assert.Equal(t, meta.Name, "foo-bar-baz")
meta = fixupMetadata(usm.ServiceMetadata{Name: "foo", AdditionalNames: []string{"bAr", " ", "*", "baz", "a"}}, language.Go)
assert.Equal(t, "foo", meta.Name)
assert.Equal(t, []string{"a", "bar", "baz"}, meta.AdditionalNames)
}
Loading