Merge pull request #1239 from aledbf/conditional-log

[nginx-ingress-controller]: Add support for conditional log of urls
This commit is contained in:
Prashanth B 2016-06-22 10:47:41 -07:00 committed by GitHub
commit b728a0cbd5
3 changed files with 24 additions and 2 deletions

View file

@ -76,7 +76,15 @@ http {
'[$proxy_add_x_forwarded_for] - $remote_user [$time_local] "$request" $status $body_bytes_sent "$http_referer" "$http_user_agent" ' '[$proxy_add_x_forwarded_for] - $remote_user [$time_local] "$request" $status $body_bytes_sent "$http_referer" "$http_user_agent" '
'$request_length $request_time $upstream_addr $upstream_response_length $upstream_response_time $upstream_status'; '$request_length $request_time $upstream_addr $upstream_response_length $upstream_response_time $upstream_status';
access_log /var/log/nginx/access.log upstreaminfo; {{/* map urls that should not appear in access.log */}}
{{/* http://nginx.org/en/docs/http/ngx_http_log_module.html#access_log */}}
map $request $loggable {
{{- range $reqUri := $cfg.skipAccessLogUrls }}
{{ $reqUri }} 0;{{ end }}
default 1;
}
access_log /var/log/nginx/access.log upstreaminfo if=$loggable;
error_log /var/log/nginx/error.log {{ $cfg.errorLogLevel }}; error_log /var/log/nginx/error.log {{ $cfg.errorLogLevel }};
{{ if not (empty .defResolver) }}# Custom dns resolver. {{ if not (empty .defResolver) }}# Custom dns resolver.

View file

@ -162,6 +162,11 @@ type Configuration struct {
// http://nginx.org/en/docs/http/ngx_http_core_module.html#server_names_hash_bucket_size // http://nginx.org/en/docs/http/ngx_http_core_module.html#server_names_hash_bucket_size
ServerNameHashBucketSize int `structs:"server-name-hash-bucket-size,omitempty"` ServerNameHashBucketSize int `structs:"server-name-hash-bucket-size,omitempty"`
// SkipAccessLogURLs sets a list of URLs that should not appear in the NGINX access log
// This is useful with urls like `/health` or `health-check` that make "complex" reading the logs
// By default this list is empty
SkipAccessLogURLs []string `structs:"skip-access-log-urls,-"`
// Enables or disables the redirect (301) to the HTTPS port // Enables or disables the redirect (301) to the HTTPS port
SSLRedirect bool `structs:"ssl-redirect,omitempty"` SSLRedirect bool `structs:"ssl-redirect,omitempty"`
@ -275,6 +280,7 @@ func NewDefault() Configuration {
UseHTTP2: true, UseHTTP2: true,
CustomHTTPErrors: make([]int, 0), CustomHTTPErrors: make([]int, 0),
WhitelistSourceRange: make([]string, 0), WhitelistSourceRange: make([]string, 0),
SkipAccessLogURLs: make([]string, 0),
} }
if glog.V(5) { if glog.V(5) {

View file

@ -33,7 +33,8 @@ import (
) )
const ( const (
customHTTPErrors = "custom-http-errors" customHTTPErrors = "custom-http-errors"
skipAccessLogUrls = "skip-access-log-urls"
) )
// getDNSServers returns the list of nameservers located in the file /etc/resolv.conf // getDNSServers returns the list of nameservers located in the file /etc/resolv.conf
@ -110,6 +111,12 @@ func (ngx *Manager) ReadConfig(conf *api.ConfigMap) config.Configuration {
} }
} }
cSkipUrls := make([]string, 0)
if val, ok := conf.Data[skipAccessLogUrls]; ok {
delete(conf.Data, skipAccessLogUrls)
cSkipUrls = strings.Split(val, ",")
}
err = decoder.Decode(conf.Data) err = decoder.Decode(conf.Data)
if err != nil { if err != nil {
glog.Infof("%v", err) glog.Infof("%v", err)
@ -135,6 +142,7 @@ func (ngx *Manager) ReadConfig(conf *api.ConfigMap) config.Configuration {
} }
cfgDefault.CustomHTTPErrors = ngx.filterErrors(cErrors) cfgDefault.CustomHTTPErrors = ngx.filterErrors(cErrors)
cfgDefault.SkipAccessLogURLs = cSkipUrls
return cfgDefault return cfgDefault
} }