Skip to content

Commit 0ca3aef

Browse files
committed
Add ability to customize upstream and stream log format
1 parent fb8e2d7 commit 0ca3aef

File tree

4 files changed

+72
-11
lines changed

4 files changed

+72
-11
lines changed

controllers/nginx/pkg/config/config.go

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ import (
2121

2222
"github.com/golang/glog"
2323

24+
"fmt"
2425
"k8s.io/ingress/core/pkg/ingress"
2526
"k8s.io/ingress/core/pkg/ingress/defaults"
2627
)
@@ -46,6 +47,10 @@ const (
4647

4748
gzipTypes = "application/atom+xml application/javascript application/x-javascript application/json application/rss+xml application/vnd.ms-fontobject application/x-font-ttf application/x-web-app-manifest+json application/xhtml+xml application/xml font/opentype image/svg+xml image/x-icon text/css text/plain text/x-component"
4849

50+
logFormatUpstream = "'%v - [$proxy_add_x_forwarded_for] - $remote_user [$time_local] \"$request\" $status $body_bytes_sent \"$http_referer\" \"$http_user_agent\" $request_length $request_time [$proxy_upstream_name] $upstream_addr $upstream_response_length $upstream_response_time $upstream_status'"
51+
52+
logFormatStream = "'$remote_addr [$time_local] $protocol [$ssl_preread_server_name] [$stream_upstream] $status $bytes_sent $bytes_received $session_time'"
53+
4954
// http://nginx.org/en/docs/http/ngx_http_ssl_module.html#ssl_buffer_size
5055
// Sets the size of the buffer used for sending data.
5156
// 4k helps NGINX to improve TLS Time To First Byte (TTTFB)
@@ -143,6 +148,14 @@ type Configuration struct {
143148
// Default: 4 8k
144149
LargeClientHeaderBuffers string `json:"large-client-header-buffers"`
145150

151+
// Customize upstream log_format
152+
// http://nginx.org/en/docs/http/ngx_http_log_module.html#log_format
153+
LogFormatUpstream string `json:"log-format-upstream,omitempty"`
154+
155+
// Customize stream log_format
156+
// http://nginx.org/en/docs/http/ngx_http_log_module.html#log_format
157+
LogFormatStream string `json:"log-format-stream,omitempty"`
158+
146159
// Maximum number of simultaneous connections that can be opened by each worker process
147160
// http://nginx.org/en/docs/ngx_core_module.html#worker_connections
148161
MaxWorkerConnections int `json:"max-worker-connections,omitempty"`
@@ -250,6 +263,8 @@ func NewDefault() Configuration {
250263
GzipTypes: gzipTypes,
251264
KeepAlive: 75,
252265
LargeClientHeaderBuffers: "4 8k",
266+
LogFormatStream: logFormatStream,
267+
LogFormatUpstream: BuildLogFormatUpstream(false),
253268
MaxWorkerConnections: 16384,
254269
MapHashBucketSize: 64,
255270
ProxyRealIPCIDR: defIPCIDR,
@@ -291,6 +306,15 @@ func NewDefault() Configuration {
291306
return cfg
292307
}
293308

309+
// BuildLogFormatUpstream format the log_format upstream based on proxy_protocol
310+
func BuildLogFormatUpstream(useProxyProtocol bool) string {
311+
312+
if useProxyProtocol {
313+
return fmt.Sprintf(logFormatUpstream, "$proxy_protocol_addr")
314+
}
315+
return fmt.Sprintf(logFormatUpstream, "$remote_addr")
316+
}
317+
294318
// TemplateConfig contains the nginx configuration to render the file nginx.conf
295319
type TemplateConfig struct {
296320
ProxySetHeaders map[string]string
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
package config
2+
3+
import (
4+
"fmt"
5+
"testing"
6+
)
7+
8+
func TestBuildLogFormatUpstream(t *testing.T) {
9+
10+
testCases := []struct {
11+
useProxyProtocol bool // use proxy protocol
12+
expected string
13+
}{
14+
{true, fmt.Sprintf(logFormatUpstream, "$proxy_protocol_addr")},
15+
{false, fmt.Sprintf(logFormatUpstream, "$remote_addr")},
16+
}
17+
18+
for _, testCase := range testCases {
19+
20+
result := BuildLogFormatUpstream(testCase.useProxyProtocol)
21+
22+
if result != testCase.expected {
23+
t.Errorf(" expected %v but return %v", testCase.expected, result)
24+
}
25+
26+
}
27+
}

controllers/nginx/pkg/template/template.go

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ import (
3131
"github.com/golang/glog"
3232

3333
"k8s.io/ingress/controllers/nginx/pkg/config"
34+
nginxconfig "k8s.io/ingress/controllers/nginx/pkg/config"
3435
"k8s.io/ingress/core/pkg/ingress"
3536
ing_net "k8s.io/ingress/core/pkg/net"
3637
"k8s.io/ingress/core/pkg/watch"
@@ -134,12 +135,12 @@ var (
134135
"buildSSLPassthroughUpstreams": buildSSLPassthroughUpstreams,
135136
"buildResolvers": buildResolvers,
136137
"isLocationAllowed": isLocationAllowed,
137-
138-
"contains": strings.Contains,
139-
"hasPrefix": strings.HasPrefix,
140-
"hasSuffix": strings.HasSuffix,
141-
"toUpper": strings.ToUpper,
142-
"toLower": strings.ToLower,
138+
"buildLogFormatUpstream": buildLogFormatUpstream,
139+
"contains": strings.Contains,
140+
"hasPrefix": strings.HasPrefix,
141+
"hasSuffix": strings.HasSuffix,
142+
"toUpper": strings.ToUpper,
143+
"toLower": strings.ToLower,
143144
}
144145
)
145146

@@ -227,6 +228,17 @@ func buildAuthLocation(input interface{}) string {
227228
return fmt.Sprintf("/_external-auth-%v", str)
228229
}
229230

231+
func buildLogFormatUpstream(input interface{}) string {
232+
config, ok := input.(config.Configuration)
233+
234+
if !ok {
235+
glog.Errorf("error an ingress.buildLogFormatUpstream type but %T was returned", input)
236+
}
237+
238+
return nginxconfig.BuildLogFormatUpstream(config.UseProxyProtocol)
239+
240+
}
241+
230242
// buildProxyPass produces the proxy pass string, if the ingress has redirects
231243
// (specified through the ingress.kubernetes.io/rewrite-to annotation)
232244
// If the annotation ingress.kubernetes.io/add-base-url:"true" is specified it will

controllers/nginx/rootfs/etc/nginx/template/nginx.tmpl

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -77,11 +77,9 @@ http {
7777
gzip_proxied any;
7878
{{ end }}
7979

80-
server_tokens {{ if $cfg.ShowServerTokens }}on{{ else }}off{{ end }};
80+
server_tokens {{ if $cfg.ShowServerTokens }}on{{ else }}off{{ end }};
8181

82-
log_format upstreaminfo '{{ if $cfg.UseProxyProtocol }}$proxy_protocol_addr{{ else }}$remote_addr{{ end }} - '
83-
'[$proxy_add_x_forwarded_for] - $remote_user [$time_local] "$request" $status $body_bytes_sent "$http_referer" "$http_user_agent" '
84-
'$request_length $request_time [$proxy_upstream_name] $upstream_addr $upstream_response_length $upstream_response_time $upstream_status';
82+
log_format upstreaminfo {{ buildLogFormatUpstream $cfg }};
8583

8684
{{/* map urls that should not appear in access.log */}}
8785
{{/* http://nginx.org/en/docs/http/ngx_http_log_module.html#access_log */}}
@@ -448,7 +446,7 @@ stream {
448446
default nginx-ssl-backend;
449447
}
450448

451-
log_format log_stream '$remote_addr [$time_local] $protocol [$ssl_preread_server_name] [$stream_upstream] $status $bytes_sent $bytes_received $session_time';
449+
log_format log_stream {{ $cfg.LogFormatStream }};
452450

453451
{{ if $cfg.DisableAccessLog }}
454452
access_log off;

0 commit comments

Comments
 (0)