Skip to content

Commit b4183ea

Browse files
authored
Bug fix for UpstreamServer Fields Logs Displayed as Memory Addresses (#6635)
1 parent ec22bcf commit b4183ea

File tree

2 files changed

+137
-1
lines changed

2 files changed

+137
-1
lines changed

internal/nginx/manager.go

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -409,11 +409,39 @@ func (lm *LocalManager) UpdateServersInPlus(upstream string, servers []string, c
409409
return fmt.Errorf("error updating servers of %v upstream: %w", upstream, err)
410410
}
411411

412-
nl.Debugf(lm.logger, "Updated servers of %v; Added: %v, Removed: %v, Updated: %v", upstream, added, removed, updated)
412+
logAdded := formatUpdateServersInPlusLog(added)
413+
logRemoved := formatUpdateServersInPlusLog(removed)
414+
logUpdated := formatUpdateServersInPlusLog(updated)
415+
416+
nl.Debugf(lm.logger, "Updated servers of %v; Added: %+v, Removed: %+v, Updated: %+v", upstream, logAdded, logRemoved, logUpdated)
413417

414418
return nil
415419
}
416420

421+
func formatUpdateServersInPlusLog(us []client.UpstreamServer) string {
422+
var logEntries []string
423+
for _, server := range us {
424+
logEntry := fmt.Sprintf("{MaxConns:%v MaxFails:%v Backup:%v Down:%v Weight:%v Server:%v FailTimeout:%v SlowStart:%v Route:%v Service:%v ID:%v Drain:%v}",
425+
derefInt(server.MaxConns), derefInt(server.MaxFails), derefBool(server.Backup), derefBool(server.Down), derefInt(server.Weight), server.Server, server.FailTimeout, server.SlowStart, server.Route, server.Service, server.ID, server.Drain)
426+
logEntries = append(logEntries, logEntry)
427+
}
428+
return fmt.Sprintf("[%s]", strings.Join(logEntries, " "))
429+
}
430+
431+
func derefBool(p *bool) bool {
432+
if p != nil {
433+
return *p
434+
}
435+
return false
436+
}
437+
438+
func derefInt(p *int) int {
439+
if p != nil {
440+
return *p
441+
}
442+
return 0
443+
}
444+
417445
// UpdateStreamServersInPlus updates NGINX Plus stream servers of the given upstream.
418446
func (lm *LocalManager) UpdateStreamServersInPlus(upstream string, servers []string) error {
419447
err := verifyConfigVersion(lm.plusConfigVersionCheckClient, lm.configVersion, lm.verifyClient.timeout)

internal/nginx/manager_test.go

Lines changed: 108 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,108 @@
1+
package nginx
2+
3+
import (
4+
"testing"
5+
6+
"github.com/nginxinc/nginx-plus-go-client/client"
7+
)
8+
9+
// Helper functions to create pointers
10+
func ptrInt(i int) *int { return &i }
11+
func ptrBool(b bool) *bool { return &b }
12+
13+
func TestFormatUpdateServersInPlusLog(t *testing.T) {
14+
tests := []struct {
15+
name string
16+
input []client.UpstreamServer
17+
expected string
18+
}{
19+
{
20+
name: "Empty input",
21+
input: []client.UpstreamServer{},
22+
expected: "[]",
23+
},
24+
{
25+
name: "Single server with all fields set",
26+
input: []client.UpstreamServer{
27+
{
28+
MaxConns: ptrInt(100),
29+
MaxFails: ptrInt(3),
30+
Backup: ptrBool(true),
31+
Down: ptrBool(false),
32+
Weight: ptrInt(10),
33+
Server: "192.168.1.1:8080",
34+
FailTimeout: "30s",
35+
SlowStart: "10s",
36+
Route: "route1",
37+
Service: "serviceA",
38+
ID: 0,
39+
Drain: true,
40+
},
41+
},
42+
expected: "[{MaxConns:100 MaxFails:3 Backup:true Down:false Weight:10 Server:192.168.1.1:8080 FailTimeout:30s SlowStart:10s Route:route1 Service:serviceA ID:0 Drain:true}]",
43+
},
44+
{
45+
name: "Multiple servers",
46+
input: []client.UpstreamServer{
47+
{
48+
MaxConns: ptrInt(50),
49+
MaxFails: ptrInt(2),
50+
Backup: ptrBool(false),
51+
Down: ptrBool(true),
52+
Weight: ptrInt(5),
53+
Server: "192.168.1.2:8080",
54+
FailTimeout: "20s",
55+
SlowStart: "5s",
56+
Route: "route2",
57+
Service: "serviceB",
58+
ID: 1,
59+
Drain: false,
60+
},
61+
{
62+
MaxConns: ptrInt(150),
63+
MaxFails: ptrInt(5),
64+
Backup: ptrBool(true),
65+
Down: ptrBool(false),
66+
Weight: ptrInt(15),
67+
Server: "192.168.1.3:8080",
68+
FailTimeout: "40s",
69+
SlowStart: "20s",
70+
Route: "route3",
71+
Service: "serviceC",
72+
ID: 2,
73+
Drain: true,
74+
},
75+
},
76+
expected: "[{MaxConns:50 MaxFails:2 Backup:false Down:true Weight:5 Server:192.168.1.2:8080 FailTimeout:20s SlowStart:5s Route:route2 Service:serviceB ID:1 Drain:false} {MaxConns:150 MaxFails:5 Backup:true Down:false Weight:15 Server:192.168.1.3:8080 FailTimeout:40s SlowStart:20s Route:route3 Service:serviceC ID:2 Drain:true}]",
77+
},
78+
{
79+
name: "Servers with nil pointer fields",
80+
input: []client.UpstreamServer{
81+
{
82+
MaxConns: nil, // Should default to 0
83+
MaxFails: ptrInt(4),
84+
Backup: nil, // Should default to false
85+
Down: ptrBool(true),
86+
Weight: nil, // Should default to 0
87+
Server: "192.168.1.4:8080",
88+
FailTimeout: "",
89+
SlowStart: "",
90+
Route: "",
91+
Service: "",
92+
ID: 0,
93+
Drain: false,
94+
},
95+
},
96+
expected: "[{MaxConns:0 MaxFails:4 Backup:false Down:true Weight:0 Server:192.168.1.4:8080 FailTimeout: SlowStart: Route: Service: ID:0 Drain:false}]",
97+
},
98+
}
99+
100+
for _, tc := range tests {
101+
t.Run(tc.name, func(t *testing.T) {
102+
actual := formatUpdateServersInPlusLog(tc.input)
103+
if actual != tc.expected {
104+
t.Errorf("FormatUpdateServersInPlusLog() = %v, want %v", actual, tc.expected)
105+
}
106+
})
107+
}
108+
}

0 commit comments

Comments
 (0)