Skip to content

Commit

Permalink
discovery: Remove processInfo (#29014)
Browse files Browse the repository at this point in the history
  • Loading branch information
vitkyrka authored Sep 4, 2024
1 parent 2172bc9 commit b5758df
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 58 deletions.
18 changes: 9 additions & 9 deletions pkg/collector/corechecks/servicediscovery/events.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,14 +67,14 @@ func (ts *telemetrySender) newEvent(t eventType, svc serviceInfo) *event {
Env: env,
ServiceLanguage: svc.meta.Language,
ServiceType: svc.meta.Type,
StartTime: int64(svc.process.Stat.StartTime),
StartTime: int64(svc.service.StartTimeSecs),
LastSeen: svc.LastHeartbeat.Unix(),
APMInstrumentation: svc.meta.APMInstrumentation,
ServiceNameSource: svc.meta.NameSource,
Ports: svc.process.Ports,
PID: svc.process.PID,
CommandLine: svc.process.CmdLine,
RSSMemory: svc.process.Stat.RSS,
Ports: svc.service.Ports,
PID: svc.service.PID,
CommandLine: svc.service.CommandLine,
RSSMemory: svc.service.RSS,
},
}
}
Expand All @@ -88,9 +88,9 @@ func newTelemetrySender(sender sender.Sender) *telemetrySender {

func (ts *telemetrySender) sendStartServiceEvent(svc serviceInfo) {
log.Debugf("[pid: %d | name: %s | ports: %v] start-service",
svc.process.PID,
svc.service.PID,
svc.meta.Name,
svc.process.Ports,
svc.service.Ports,
)

e := ts.newEvent(eventTypeStartService, svc)
Expand All @@ -105,7 +105,7 @@ func (ts *telemetrySender) sendStartServiceEvent(svc serviceInfo) {

func (ts *telemetrySender) sendHeartbeatServiceEvent(svc serviceInfo) {
log.Debugf("[pid: %d | name: %s] heartbeat-service",
svc.process.PID,
svc.service.PID,
svc.meta.Name,
)

Expand All @@ -121,7 +121,7 @@ func (ts *telemetrySender) sendHeartbeatServiceEvent(svc serviceInfo) {

func (ts *telemetrySender) sendEndServiceEvent(svc serviceInfo) {
log.Debugf("[pid: %d | name: %s] end-service",
svc.process.PID,
svc.service.PID,
svc.meta.Name,
)

Expand Down
28 changes: 11 additions & 17 deletions pkg/collector/corechecks/servicediscovery/events_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import (

"github.com/DataDog/datadog-agent/comp/core/hostname/hostnameinterface"
"github.com/DataDog/datadog-agent/pkg/aggregator/mocksender"
"github.com/DataDog/datadog-agent/pkg/collector/corechecks/servicediscovery/model"
)

func mockSenderEvents(t *testing.T, m *mocksender.MockSender) []*event {
Expand Down Expand Up @@ -54,15 +55,12 @@ func Test_telemetrySender(t *testing.T) {
ts.hostname = mHostname

svc := serviceInfo{
process: processInfo{
PID: 99,
CmdLine: []string{"test-service", "--args"},
Env: nil,
Stat: procStat{
StartTime: uint64(now.Add(-20 * time.Minute).Unix()),
RSS: 500 * 1024 * 1024,
},
Ports: []uint16{80, 8080},
service: model.Service{
PID: 99,
CommandLine: []string{"test-service", "--args"},
Ports: []uint16{80, 8080},
StartTimeSecs: uint64(now.Add(-20 * time.Minute).Unix()),
RSS: 500 * 1024 * 1024,
},
meta: ServiceMetadata{
Name: "test-service",
Expand Down Expand Up @@ -166,14 +164,10 @@ func Test_telemetrySender_name_provided(t *testing.T) {
ts.hostname = mHostname

svc := serviceInfo{
process: processInfo{
PID: 55,
CmdLine: []string{"foo", "--option"},
Env: nil,
Stat: procStat{
StartTime: uint64(now.Add(-20 * time.Minute).Unix()),
},
Ports: nil,
service: model.Service{
PID: 55,
CommandLine: []string{"foo", "--option"},
StartTimeSecs: uint64(now.Add(-20 * time.Minute).Unix()),
},
meta: ServiceMetadata{
Name: "test-service",
Expand Down
19 changes: 5 additions & 14 deletions pkg/collector/corechecks/servicediscovery/impl_linux.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ func (li *linuxImpl) DiscoverServices() (*discoveredServices, error) {
for pid, svc := range li.potentialServices {
if service, ok := serviceMap[pid]; ok {
svc.LastHeartbeat = now
svc.process.Stat.RSS = service.RSS
svc.service.RSS = service.RSS
li.aliveServices[pid] = svc
events.start = append(events.start, *svc)
}
Expand All @@ -93,7 +93,7 @@ func (li *linuxImpl) DiscoverServices() (*discoveredServices, error) {
if _, ok := li.aliveServices[pid]; !ok {
log.Debugf("[pid: %d] found new process with open ports", pid)

svc := li.getServiceInfo(pid, service)
svc := li.getServiceInfo(service)
if li.ignoreCfg[svc.meta.Name] {
log.Debugf("[pid: %d] process ignored from config: %s", pid, svc.meta.Name)
li.ignoreProcs[pid] = true
Expand All @@ -111,7 +111,7 @@ func (li *linuxImpl) DiscoverServices() (*discoveredServices, error) {
events.stop = append(events.stop, *svc)
} else if now.Sub(svc.LastHeartbeat).Truncate(time.Minute) >= heartbeatTime {
svc.LastHeartbeat = now
svc.process.Stat.RSS = service.RSS
svc.service.RSS = service.RSS
events.heartbeat = append(events.heartbeat, *svc)
}
}
Expand All @@ -131,21 +131,12 @@ func (li *linuxImpl) DiscoverServices() (*discoveredServices, error) {
}, nil
}

func (li *linuxImpl) getServiceInfo(pid int, service model.Service) serviceInfo {
func (li *linuxImpl) getServiceInfo(service model.Service) serviceInfo {
// if the process name is docker-proxy, we should talk to docker to get the process command line and env vars
// have to see how far this can go but not for the initial release

// for now, docker-proxy is going on the ignore list

pInfo := processInfo{
PID: pid,
Stat: procStat{
StartTime: service.StartTimeSecs,
},
Ports: service.Ports,
CmdLine: service.CommandLine,
}

serviceType := servicetype.Detect(service.Name, service.Ports)

meta := ServiceMetadata{
Expand All @@ -157,8 +148,8 @@ func (li *linuxImpl) getServiceInfo(pid int, service model.Service) serviceInfo
}

return serviceInfo{
process: pInfo,
meta: meta,
service: service,
LastHeartbeat: li.time.Now(),
}
}
Expand Down
24 changes: 6 additions & 18 deletions pkg/collector/corechecks/servicediscovery/servicediscovery.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import (
"github.com/DataDog/datadog-agent/pkg/aggregator/sender"
"github.com/DataDog/datadog-agent/pkg/collector/check"
"github.com/DataDog/datadog-agent/pkg/collector/corechecks"
"github.com/DataDog/datadog-agent/pkg/collector/corechecks/servicediscovery/model"
pkgconfig "github.com/DataDog/datadog-agent/pkg/config"
"github.com/DataDog/datadog-agent/pkg/util/log"
"github.com/DataDog/datadog-agent/pkg/util/optional"
Expand All @@ -34,24 +35,11 @@ const (
)

type serviceInfo struct {
process processInfo
meta ServiceMetadata
service model.Service
LastHeartbeat time.Time
}

type procStat struct {
StartTime uint64
RSS uint64
}

type processInfo struct {
PID int
CmdLine []string
Env map[string]string
Stat procStat
Ports []uint16
}

type serviceEvents struct {
start []serviceInfo
stop []serviceInfo
Expand Down Expand Up @@ -177,7 +165,7 @@ func (c *Check) Run() error {
continue
}
for _, svc := range svcs {
if c.sentRepeatedEventPIDs[svc.process.PID] {
if c.sentRepeatedEventPIDs[svc.service.PID] {
continue
}
err := fmt.Errorf("found repeated service name: %s", svc.meta.Name)
Expand All @@ -187,7 +175,7 @@ func (c *Check) Run() error {
svc: &svc.meta,
})
// track the PID, so we don't increase this counter in every run of the check.
c.sentRepeatedEventPIDs[svc.process.PID] = true
c.sentRepeatedEventPIDs[svc.service.PID] = true
}
}

Expand All @@ -211,9 +199,9 @@ func (c *Check) Run() error {
continue
}
eventsByName.addStop(p)
if c.sentRepeatedEventPIDs[p.process.PID] {
if c.sentRepeatedEventPIDs[p.service.PID] {
// delete this process from the map, so we track it if the PID gets reused
delete(c.sentRepeatedEventPIDs, p.process.PID)
delete(c.sentRepeatedEventPIDs, p.service.PID)
}
}

Expand Down

0 comments on commit b5758df

Please sign in to comment.