Skip to content

Commit a60155b

Browse files
committed
Only log process details if processes have changed
1 parent 6adcc0a commit a60155b

File tree

3 files changed

+28
-20
lines changed

3 files changed

+28
-20
lines changed

internal/watcher/instance/instance_watcher_service.go

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ package instance
77

88
import (
99
"context"
10+
"encoding/json"
1011
"log/slog"
1112
"slices"
1213
"sync"
@@ -43,13 +44,14 @@ type (
4344
processOperator process.ProcessOperatorInterface
4445
nginxConfigParser parser.ConfigParser
4546
executer exec.ExecInterface
47+
nginxParser processParser
4648
enabled *atomic.Bool
4749
agentConfig *config.Config
4850
instanceCache map[string]*mpi.Instance
4951
nginxConfigCache map[string]*model.NginxConfigContext
5052
instancesChannel chan<- InstanceUpdatesMessage
5153
nginxConfigContextChannel chan<- NginxConfigContextMessage
52-
nginxParser processParser
54+
processCache []*nginxprocess.Process
5355
cacheMutex sync.Mutex
5456
}
5557

@@ -84,6 +86,7 @@ func NewInstanceWatcherService(agentConfig *config.Config) *InstanceWatcherServi
8486
nginxConfigCache: make(map[string]*model.NginxConfigContext),
8587
executer: &exec.Exec{},
8688
enabled: enabled,
89+
processCache: []*nginxprocess.Process{},
8790
}
8891
}
8992

@@ -270,6 +273,18 @@ func (iw *InstanceWatcherService) instanceUpdates(ctx context.Context) (
270273
return instanceUpdates, err
271274
}
272275

276+
if !slices.EqualFunc(iw.processCache, nginxProcesses, func(a, b *nginxprocess.Process) bool {
277+
return a.Equal(b)
278+
}) {
279+
processesJSON, marshalErr := json.Marshal(nginxProcesses)
280+
if marshalErr != nil {
281+
slog.DebugContext(ctx, "Unable to marshal NGINX processes", "error", marshalErr)
282+
} else {
283+
slog.DebugContext(ctx, "NGINX processes changed", "processes", processesJSON)
284+
iw.processCache = nginxProcesses
285+
}
286+
}
287+
273288
// NGINX Agent is always the first instance in the list
274289
instancesFound := make(map[string]*mpi.Instance)
275290
agentInstance := iw.agentInstance(ctx)

internal/watcher/instance/nginx_process_parser.go

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -48,24 +48,12 @@ func NewNginxProcessParser() *NginxProcessParser {
4848
//
4949
//nolint:revive,gocognit // cognitive complexity of 20 because of the if statements in the for loop
5050
func (npp *NginxProcessParser) Parse(ctx context.Context, processes []*nginxprocess.Process) map[string]*mpi.Instance {
51-
slog.DebugContext(ctx, "Parsing NGINX processes", "number_of_processes", len(processes))
52-
5351
instanceMap := make(map[string]*mpi.Instance) // key is instanceID
5452
workers := make(map[int32][]*mpi.InstanceChild) // key is ppid of process
5553

5654
processesByPID := convertToMap(processes)
5755

5856
for _, proc := range processesByPID {
59-
slog.DebugContext(ctx, "NGINX process details",
60-
"ppid", proc.PPID,
61-
"pid", proc.PID,
62-
"name", proc.Name,
63-
"created", proc.Created,
64-
"status", proc.Status,
65-
"cmd", proc.Cmd,
66-
"exe", proc.Exe,
67-
)
68-
6957
if proc.IsWorker() {
7058
// Here we are determining if the worker process has a master
7159
if masterProcess, ok := processesByPID[proc.PPID]; ok {

pkg/nginxprocess/process.go

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -18,13 +18,13 @@ import (
1818
type Process struct {
1919
// Created is when this process was created, precision varies by platform and is at best to the millisecond. On
2020
// linux there can be significant skew compared to [time.Now], ± 1s.
21-
Created time.Time
22-
Name string
23-
Cmd string
24-
Exe string // path to the executable
25-
Status string // process status, only present if this process was created using [WithStatus]
26-
PID int32
27-
PPID int32 // parent PID
21+
Created time.Time `json:"created"`
22+
Name string `json:"name"`
23+
Cmd string `json:"cmd"`
24+
Exe string `json:"exe"` // path to the executable
25+
Status string `json:"status"` // process status, only present if this process was created using [WithStatus]
26+
PID int32 `json:"pid"`
27+
PPID int32 `json:"ppid"` // parent PID
2828
}
2929

3030
// IsWorker returns true if the process is a NGINX worker process.
@@ -50,6 +50,11 @@ func (p *Process) IsHealthy() bool {
5050
return p.Status != "" && !strings.Contains(p.Status, process.Zombie)
5151
}
5252

53+
func (p *Process) Equal(b *Process) bool {
54+
return p.PID == b.PID && p.Name == b.Name && p.PPID == b.PPID && p.Cmd == b.Cmd &&
55+
p.Exe == b.Exe && p.Created.Equal(b.Created) && p.Status == b.Status
56+
}
57+
5358
type options struct {
5459
loadStatus bool
5560
}

0 commit comments

Comments
 (0)