Skip to content

Commit 0ced21f

Browse files
authored
Add server zone metrics (#2360)
Problem: server_zone metrics were not being emitted for NGINX Plus users Solution: Add these metrics to the server blocks when running NGINX Plus
1 parent 6289c6e commit 0ced21f

File tree

9 files changed

+125
-18
lines changed

9 files changed

+125
-18
lines changed

internal/mode/static/nginx/config/generator.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -169,12 +169,12 @@ func (g GeneratorImpl) generateHTTPConfig(
169169
func (g GeneratorImpl) getExecuteFuncs(generator policies.Generator) []executeFunc {
170170
return []executeFunc{
171171
executeBaseHTTPConfig,
172-
newExecuteServersFunc(generator),
172+
g.newExecuteServersFunc(generator),
173173
g.executeUpstreams,
174174
executeSplitClients,
175175
executeMaps,
176176
executeTelemetry,
177-
executeStreamServers,
177+
g.executeStreamServers,
178178
g.executeStreamUpstreams,
179179
executeStreamMaps,
180180
}

internal/mode/static/nginx/config/http/config.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,7 @@ type ProxySSLVerify struct {
111111
type ServerConfig struct {
112112
Servers []Server
113113
IPFamily shared.IPFamily
114+
Plus bool
114115
}
115116

116117
// Include defines a file that's included via the include directive.

internal/mode/static/nginx/config/servers.go

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -59,18 +59,19 @@ var grpcBaseHeaders = []http.Header{
5959
},
6060
}
6161

62-
func newExecuteServersFunc(generator policies.Generator) executeFunc {
62+
func (g GeneratorImpl) newExecuteServersFunc(generator policies.Generator) executeFunc {
6363
return func(configuration dataplane.Configuration) []executeResult {
64-
return executeServers(configuration, generator)
64+
return g.executeServers(configuration, generator)
6565
}
6666
}
6767

68-
func executeServers(conf dataplane.Configuration, generator policies.Generator) []executeResult {
68+
func (g GeneratorImpl) executeServers(conf dataplane.Configuration, generator policies.Generator) []executeResult {
6969
servers, httpMatchPairs := createServers(conf.HTTPServers, conf.SSLServers, conf.TLSPassthroughServers, generator)
7070

7171
serverConfig := http.ServerConfig{
7272
Servers: servers,
7373
IPFamily: getIPFamily(conf.BaseHTTPConfig),
74+
Plus: g.plus,
7475
}
7576

7677
serverResult := executeResult{

internal/mode/static/nginx/config/servers_template.go

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ package config
22

33
const serversTemplateText = `
44
js_preload_object matches from /etc/nginx/conf.d/matches.json;
5-
{{- range $s := .Servers -}}
5+
{{ range $s := .Servers -}}
66
{{ if $s.IsDefaultSSL -}}
77
server {
88
{{- if or ($.IPFamily.IPv4) ($s.IsSocket) }}
@@ -52,9 +52,13 @@ server {
5252
5353
server_name {{ $s.ServerName }};
5454
55+
{{- if $.Plus }}
56+
status_zone {{ $s.ServerName }};
57+
{{- end }}
58+
5559
{{- range $i := $s.Includes }}
5660
include {{ $i.Name }};
57-
{{ end -}}
61+
{{- end }}
5862
5963
{{ range $l := $s.Locations }}
6064
location {{ $l.Path }} {
@@ -85,6 +89,7 @@ server {
8589
include /etc/nginx/grpc-error-pages.conf;
8690
{{- end }}
8791
92+
proxy_http_version 1.1;
8893
{{- if $l.ProxyPass -}}
8994
{{ range $h := $l.ProxySetHeaders }}
9095
{{ $proxyOrGRPC }}_set_header {{ $h.Name }} "{{ $h.Value }}";
@@ -100,7 +105,6 @@ server {
100105
{{ range $h := $l.ResponseHeaders.Remove }}
101106
proxy_hide_header {{ $h }};
102107
{{- end }}
103-
proxy_http_version 1.1;
104108
{{- if $l.ProxySSLVerify }}
105109
{{ $proxyOrGRPC }}_ssl_server_name on;
106110
{{ $proxyOrGRPC }}_ssl_verify on;

internal/mode/static/nginx/config/servers_test.go

Lines changed: 48 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,7 @@ func TestExecuteServers(t *testing.T) {
9797
"ssl_certificate /etc/nginx/secrets/test-keypair.pem;": 2,
9898
"ssl_certificate_key /etc/nginx/secrets/test-keypair.pem;": 2,
9999
"proxy_ssl_server_name on;": 1,
100+
"status_zone": 0,
100101
}
101102

102103
type assertion func(g *WithT, data string)
@@ -131,7 +132,8 @@ func TestExecuteServers(t *testing.T) {
131132
},
132133
})
133134

134-
results := executeServers(conf, fakeGenerator)
135+
gen := GeneratorImpl{}
136+
results := gen.executeServers(conf, fakeGenerator)
135137
g.Expect(results).To(HaveLen(len(expectedResults)))
136138

137139
for _, res := range results {
@@ -142,7 +144,7 @@ func TestExecuteServers(t *testing.T) {
142144
}
143145
}
144146

145-
func TestExecuteServersForIPFamily(t *testing.T) {
147+
func TestExecuteServers_IPFamily(t *testing.T) {
146148
httpServers := []dataplane.VirtualServer{
147149
{
148150
IsDefault: true,
@@ -256,14 +258,17 @@ func TestExecuteServersForIPFamily(t *testing.T) {
256258
"listen [::]:8080;": 1,
257259
"listen [::]:8443 ssl default_server;": 1,
258260
"listen [::]:8443 ssl;": 1,
261+
"status_zone": 0,
259262
},
260263
},
261264
}
262265

263266
for _, test := range tests {
264267
t.Run(test.msg, func(t *testing.T) {
265268
g := NewWithT(t)
266-
results := executeServers(test.config, &policiesfakes.FakeGenerator{})
269+
270+
gen := GeneratorImpl{}
271+
results := gen.executeServers(test.config, &policiesfakes.FakeGenerator{})
267272
g.Expect(results).To(HaveLen(2))
268273
serverConf := string(results[0].data)
269274
httpMatchConf := string(results[1].data)
@@ -276,6 +281,44 @@ func TestExecuteServersForIPFamily(t *testing.T) {
276281
}
277282
}
278283

284+
func TestExecuteServers_Plus(t *testing.T) {
285+
config := dataplane.Configuration{
286+
HTTPServers: []dataplane.VirtualServer{
287+
{
288+
Hostname: "example.com",
289+
},
290+
{
291+
Hostname: "example2.com",
292+
},
293+
},
294+
SSLServers: []dataplane.VirtualServer{
295+
{
296+
Hostname: "example.com",
297+
SSL: &dataplane.SSL{
298+
KeyPairID: "test-keypair",
299+
},
300+
},
301+
},
302+
}
303+
304+
expectedHTTPConfig := map[string]int{
305+
"status_zone example.com;": 2,
306+
"status_zone example2.com;": 1,
307+
}
308+
309+
g := NewWithT(t)
310+
311+
gen := GeneratorImpl{plus: true}
312+
results := gen.executeServers(config, &policiesfakes.FakeGenerator{})
313+
g.Expect(results).To(HaveLen(2))
314+
315+
serverConf := string(results[0].data)
316+
317+
for expSubStr, expCount := range expectedHTTPConfig {
318+
g.Expect(strings.Count(serverConf, expSubStr)).To(Equal(expCount))
319+
}
320+
}
321+
279322
func TestExecuteForDefaultServers(t *testing.T) {
280323
testcases := []struct {
281324
msg string
@@ -347,7 +390,8 @@ func TestExecuteForDefaultServers(t *testing.T) {
347390
t.Run(tc.msg, func(t *testing.T) {
348391
g := NewWithT(t)
349392

350-
serverResults := executeServers(tc.conf, &policiesfakes.FakeGenerator{})
393+
gen := GeneratorImpl{}
394+
serverResults := gen.executeServers(tc.conf, &policiesfakes.FakeGenerator{})
351395
g.Expect(serverResults).To(HaveLen(2))
352396
serverConf := string(serverResults[0].data)
353397
httpMatchConf := string(serverResults[1].data)

internal/mode/static/nginx/config/stream/config.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import "github.com/nginxinc/nginx-gateway-fabric/internal/mode/static/nginx/conf
55
// Server holds all configuration for a stream server.
66
type Server struct {
77
Listen string
8+
StatusZone string
89
ProxyPass string
910
Pass string
1011
SSLPreread bool
@@ -27,4 +28,5 @@ type UpstreamServer struct {
2728
type ServerConfig struct {
2829
Servers []Server
2930
IPFamily shared.IPFamily
31+
Plus bool
3032
}

internal/mode/static/nginx/config/stream_servers.go

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,13 @@ import (
1111

1212
var streamServersTemplate = gotemplate.Must(gotemplate.New("streamServers").Parse(streamServersTemplateText))
1313

14-
func executeStreamServers(conf dataplane.Configuration) []executeResult {
14+
func (g GeneratorImpl) executeStreamServers(conf dataplane.Configuration) []executeResult {
1515
streamServers := createStreamServers(conf)
1616

1717
streamServerConfig := stream.ServerConfig{
1818
Servers: streamServers,
1919
IPFamily: getIPFamily(conf.BaseHTTPConfig),
20+
Plus: g.plus,
2021
}
2122

2223
streamServerResult := executeResult{
@@ -46,9 +47,10 @@ func createStreamServers(conf dataplane.Configuration) []stream.Server {
4647
if u, ok := upstreams[server.UpstreamName]; ok && server.UpstreamName != "" {
4748
if server.Hostname != "" && len(u.Endpoints) > 0 {
4849
streamServers = append(streamServers, stream.Server{
49-
Listen: getSocketNameTLS(server.Port, server.Hostname),
50-
ProxyPass: server.UpstreamName,
51-
IsSocket: true,
50+
Listen: getSocketNameTLS(server.Port, server.Hostname),
51+
StatusZone: server.Hostname,
52+
ProxyPass: server.UpstreamName,
53+
IsSocket: true,
5254
})
5355
}
5456
}
@@ -60,6 +62,7 @@ func createStreamServers(conf dataplane.Configuration) []stream.Server {
6062
portSet[server.Port] = struct{}{}
6163
streamServers = append(streamServers, stream.Server{
6264
Listen: fmt.Sprint(server.Port),
65+
StatusZone: server.Hostname,
6366
Pass: getTLSPassthroughVarName(server.Port),
6467
SSLPreread: true,
6568
})

internal/mode/static/nginx/config/stream_servers_template.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,11 @@ server {
99
{{- if and ($.IPFamily.IPv6) (not $s.IsSocket) }}
1010
listen [::]:{{ $s.Listen }};
1111
{{- end }}
12+
13+
{{- if $.Plus }}
14+
status_zone {{ $s.StatusZone }};
15+
{{- end }}
16+
1217
{{- if $s.ProxyPass }}
1318
proxy_pass {{ $s.ProxyPass }};
1419
{{- end }}

internal/mode/static/nginx/config/stream_servers_test.go

Lines changed: 49 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -58,10 +58,12 @@ func TestExecuteStreamServers(t *testing.T) {
5858
"pass $dest8080;": 1,
5959
"ssl_preread on;": 2,
6060
"proxy_pass": 3,
61+
"status_zone": 0,
6162
}
6263
g := NewWithT(t)
6364

64-
results := executeStreamServers(conf)
65+
gen := GeneratorImpl{}
66+
results := gen.executeStreamServers(conf)
6567
g.Expect(results).To(HaveLen(1))
6668
result := results[0]
6769

@@ -71,6 +73,44 @@ func TestExecuteStreamServers(t *testing.T) {
7173
}
7274
}
7375

76+
func TestExecuteStreamServers_Plus(t *testing.T) {
77+
config := dataplane.Configuration{
78+
TLSPassthroughServers: []dataplane.Layer4VirtualServer{
79+
{
80+
Hostname: "example.com",
81+
Port: 8081,
82+
UpstreamName: "backend1",
83+
},
84+
{
85+
Hostname: "example.com",
86+
Port: 8080,
87+
UpstreamName: "backend1",
88+
},
89+
{
90+
Hostname: "cafe.example.com",
91+
Port: 8082,
92+
UpstreamName: "backend2",
93+
},
94+
},
95+
}
96+
expectedHTTPConfig := map[string]int{
97+
"status_zone example.com;": 2,
98+
"status_zone cafe.example.com;": 1,
99+
}
100+
101+
g := NewWithT(t)
102+
103+
gen := GeneratorImpl{plus: true}
104+
results := gen.executeStreamServers(config)
105+
g.Expect(results).To(HaveLen(1))
106+
107+
serverConf := string(results[0].data)
108+
109+
for expSubStr, expCount := range expectedHTTPConfig {
110+
g.Expect(strings.Count(serverConf, expSubStr)).To(Equal(expCount))
111+
}
112+
}
113+
74114
func TestCreateStreamServers(t *testing.T) {
75115
conf := dataplane.Configuration{
76116
TLSPassthroughServers: []dataplane.Layer4VirtualServer{
@@ -139,29 +179,34 @@ func TestCreateStreamServers(t *testing.T) {
139179
{
140180
Listen: getSocketNameTLS(conf.TLSPassthroughServers[0].Port, conf.TLSPassthroughServers[0].Hostname),
141181
ProxyPass: conf.TLSPassthroughServers[0].UpstreamName,
182+
StatusZone: conf.TLSPassthroughServers[0].Hostname,
142183
SSLPreread: false,
143184
IsSocket: true,
144185
},
145186
{
146187
Listen: getSocketNameTLS(conf.TLSPassthroughServers[1].Port, conf.TLSPassthroughServers[1].Hostname),
147188
ProxyPass: conf.TLSPassthroughServers[1].UpstreamName,
189+
StatusZone: conf.TLSPassthroughServers[1].Hostname,
148190
SSLPreread: false,
149191
IsSocket: true,
150192
},
151193
{
152194
Listen: getSocketNameTLS(conf.TLSPassthroughServers[2].Port, conf.TLSPassthroughServers[2].Hostname),
153195
ProxyPass: conf.TLSPassthroughServers[2].UpstreamName,
196+
StatusZone: conf.TLSPassthroughServers[2].Hostname,
154197
SSLPreread: false,
155198
IsSocket: true,
156199
},
157200
{
158201
Listen: fmt.Sprint(8081),
159202
Pass: getTLSPassthroughVarName(8081),
203+
StatusZone: "example.com",
160204
SSLPreread: true,
161205
},
162206
{
163207
Listen: fmt.Sprint(8080),
164208
Pass: getTLSPassthroughVarName(8080),
209+
StatusZone: "example.com",
165210
SSLPreread: true,
166211
},
167212
}
@@ -239,7 +284,9 @@ func TestExecuteStreamServersForIPFamily(t *testing.T) {
239284
for _, test := range tests {
240285
t.Run(test.msg, func(t *testing.T) {
241286
g := NewWithT(t)
242-
results := executeStreamServers(test.config)
287+
288+
gen := GeneratorImpl{}
289+
results := gen.executeStreamServers(test.config)
243290
g.Expect(results).To(HaveLen(1))
244291
serverConf := string(results[0].data)
245292

0 commit comments

Comments
 (0)