diff --git a/pkg/collector/corechecks/servicediscovery/events.go b/pkg/collector/corechecks/servicediscovery/events.go index 7a1bd49dcc04d..fe22df5e15326 100644 --- a/pkg/collector/corechecks/servicediscovery/events.go +++ b/pkg/collector/corechecks/servicediscovery/events.go @@ -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, }, } } @@ -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) @@ -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, ) @@ -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, ) diff --git a/pkg/collector/corechecks/servicediscovery/events_test.go b/pkg/collector/corechecks/servicediscovery/events_test.go index c3803fb1ce9ad..747d49e5dcbf2 100644 --- a/pkg/collector/corechecks/servicediscovery/events_test.go +++ b/pkg/collector/corechecks/servicediscovery/events_test.go @@ -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 { @@ -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", @@ -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", diff --git a/pkg/collector/corechecks/servicediscovery/impl_linux.go b/pkg/collector/corechecks/servicediscovery/impl_linux.go index acf02f9fbb4ed..db48fbfe6de20 100644 --- a/pkg/collector/corechecks/servicediscovery/impl_linux.go +++ b/pkg/collector/corechecks/servicediscovery/impl_linux.go @@ -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) } @@ -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 @@ -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) } } @@ -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{ @@ -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(), } } diff --git a/pkg/collector/corechecks/servicediscovery/servicediscovery.go b/pkg/collector/corechecks/servicediscovery/servicediscovery.go index e1a6b43635cbf..3e8bf7eb7dab6 100644 --- a/pkg/collector/corechecks/servicediscovery/servicediscovery.go +++ b/pkg/collector/corechecks/servicediscovery/servicediscovery.go @@ -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" @@ -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 @@ -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) @@ -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 } } @@ -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) } }