Skip to content

Commit 03f92bc

Browse files
committed
Update NGINX process parser to handle worker processes that look like master processes
1 parent 5f54404 commit 03f92bc

File tree

4 files changed

+69
-15
lines changed

4 files changed

+69
-15
lines changed

api/grpc/mpi/v1/command_grpc.pb.go

Lines changed: 6 additions & 6 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

api/grpc/mpi/v1/files_grpc.pb.go

Lines changed: 8 additions & 8 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

internal/watcher/instance/nginx_process_parser.go

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ func NewNginxProcessParser() *NginxProcessParser {
4646
// cognitive complexity of 16 because of the if statements in the for loop
4747
// don't think can be avoided due to the need for continue
4848
//
49-
//nolint:revive // cognitive complexity of 20 because of the if statements in the for loop
49+
//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 {
5151
slog.DebugContext(ctx, "Parsing NGINX processes", "number_of_processes", len(processes))
5252

@@ -102,6 +102,15 @@ func (npp *NginxProcessParser) Parse(ctx context.Context, processes []*nginxproc
102102

103103
// check if proc is a master process, process is not a worker but could be cache manager etc
104104
if proc.IsMaster() {
105+
// sometimes a master process can have another master as parent
106+
// which means that it is actually a worker process and not a master process
107+
if masterProcess, ok := processesByPID[proc.PPID]; ok {
108+
workers[masterProcess.PID] = append(workers[masterProcess.PID],
109+
&mpi.InstanceChild{ProcessId: proc.PID})
110+
111+
continue
112+
}
113+
105114
nginxInfo, err := npp.info(ctx, proc)
106115
if err != nil {
107116
slog.DebugContext(ctx, "Unable to get NGINX info", "pid", proc.PID, "error", err)

internal/watcher/instance/nginx_process_parser_test.go

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -228,6 +228,17 @@ func TestNginxProcessParser_Parse_Processes(t *testing.T) {
228228
instancesList[1].GetInstanceMeta().GetInstanceId(): instancesList[1],
229229
}
230230

231+
process6 := protos.NginxOssInstance(nil)
232+
process6.GetInstanceRuntime().InstanceChildren = []*mpi.InstanceChild{
233+
{ProcessId: 567},
234+
{ProcessId: 789},
235+
{ProcessId: 5678},
236+
}
237+
238+
instancesTest6 := map[string]*mpi.Instance{
239+
process6.GetInstanceMeta().GetInstanceId(): process6,
240+
}
241+
231242
tests := []struct {
232243
expected map[string]*mpi.Instance
233244
name string
@@ -368,6 +379,40 @@ func TestNginxProcessParser_Parse_Processes(t *testing.T) {
368379
},
369380
expected: make(map[string]*mpi.Instance),
370381
},
382+
{
383+
name: "Test 6: 1 master process each with 2 workers and 1 master process",
384+
processes: []*nginxprocess.Process{
385+
{
386+
PID: 1234,
387+
PPID: 1,
388+
Name: "nginx",
389+
Cmd: "nginx: master process /usr/local/opt/nginx/bin/nginx -g daemon off;",
390+
Exe: exePath,
391+
},
392+
{
393+
PID: 789,
394+
PPID: 1234,
395+
Name: "nginx",
396+
Cmd: "nginx: worker process",
397+
Exe: exePath,
398+
},
399+
{
400+
PID: 567,
401+
PPID: 1234,
402+
Name: "nginx",
403+
Cmd: "nginx: worker process",
404+
Exe: exePath,
405+
},
406+
{
407+
PID: 5678,
408+
PPID: 1234,
409+
Name: "nginx",
410+
Cmd: "nginx: master process /usr/local/opt/nginx/bin/nginx -g daemon off;",
411+
Exe: exePath,
412+
},
413+
},
414+
expected: instancesTest6,
415+
},
371416
}
372417

373418
for _, test := range tests {

0 commit comments

Comments
 (0)