Skip to content

Commit

Permalink
feat(api): support custom Nginx log format + log additional headers a…
Browse files Browse the repository at this point in the history
…nd fields (#120)
  • Loading branch information
nettoclaudio authored Jul 19, 2022
1 parent 57d477e commit ec13a90
Show file tree
Hide file tree
Showing 7 changed files with 143 additions and 6 deletions.
6 changes: 6 additions & 0 deletions api/v1alpha1/rpaasplan_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,12 @@ type NginxConfig struct {
CacheSnapshotStorage CacheSnapshotStorage `json:"cacheSnapshotStorage,omitempty"`
CacheSnapshotSync CacheSnapshotSyncSpec `json:"cacheSnapshotSync,omitempty"`

LogFormat string `json:"logFormat,omitempty"`
LogFormatEscape string `json:"logFormatEscape,omitempty"`
LogFormatName string `json:"logFormatName,omitempty"`
LogAdditionalHeaders []string `json:"logAdditionalHeaders,omitempty"`
LogAdditionalFields map[string]string `json:"logAdditionalFields,omitempty"`

HTTPListenOptions string `json:"httpListenOptions,omitempty"`
HTTPSListenOptions string `json:"httpsListenOptions,omitempty"`

Expand Down
12 changes: 12 additions & 0 deletions api/v1alpha1/zz_generated.deepcopy.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

14 changes: 14 additions & 0 deletions config/crd/bases/extensions.tsuru.io_rpaasflavors.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -446,6 +446,20 @@ spec:
type: string
httpsListenOptions:
type: string
logAdditionalFields:
additionalProperties:
type: string
type: object
logAdditionalHeaders:
items:
type: string
type: array
logFormat:
type: string
logFormatEscape:
type: string
logFormatName:
type: string
syslogEnabled:
type: boolean
syslogFacility:
Expand Down
14 changes: 14 additions & 0 deletions config/crd/bases/extensions.tsuru.io_rpaasinstances.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -423,6 +423,20 @@ spec:
type: string
httpsListenOptions:
type: string
logAdditionalFields:
additionalProperties:
type: string
type: object
logAdditionalHeaders:
items:
type: string
type: array
logFormat:
type: string
logFormatEscape:
type: string
logFormatName:
type: string
syslogEnabled:
type: boolean
syslogFacility:
Expand Down
14 changes: 14 additions & 0 deletions config/crd/bases/extensions.tsuru.io_rpaasplans.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,20 @@ spec:
type: string
httpsListenOptions:
type: string
logAdditionalFields:
additionalProperties:
type: string
type: object
logAdditionalHeaders:
items:
type: string
type: array
logFormat:
type: string
logFormatEscape:
type: string
logFormatName:
type: string
syslogEnabled:
type: boolean
syslogFacility:
Expand Down
31 changes: 28 additions & 3 deletions internal/pkg/rpaas/nginx/configuration_render.go
Original file line number Diff line number Diff line change
Expand Up @@ -289,14 +289,40 @@ http {
include mime.types;
default_type application/octet-stream;
{{- $logFormatName := default "rpaasv2" $config.LogFormatName }}
{{- if $config.LogFormat }}
log_format {{ $config.LogFormatName }} {{ with $config.LogFormatEscape}}escape={{ . }}{{ end }} {{ $config.LogFormat }};
{{- else }}
log_format {{ $logFormatName }} escape=json
'{'
{{- range $key, $value := $config.LogAdditionalFields }}
'"{{ $key }}":"{{ $value }}",'
{{- end }}
'"remote_addr":"${remote_addr}",'
'"remote_user":"${remote_user}",'
'"time_local":"${time_local}",'
'"request":"${request}",'
'"status":"${status}",'
'"body_bytes_sent":"${body_bytes_sent}",'
'"referer":"${http_referer}",'
'"user_agent":"${http_user_agent}"'
{{- range $index, $header := $config.LogAdditionalHeaders }}
{{- if not $index }}{{ "\n" }}','{{ end }}
{{- $h := lower (replace "-" "_" $header) }}
'"header_{{ $h }}":"${http_{{ $h }}}" {{- if lt (add1 $index) (len $config.LogAdditionalHeaders) }},{{ end }}'
{{- end }}
'}';
{{- end }}
{{- if not (boolValue $config.SyslogEnabled) }}
access_log /dev/stdout combined;
access_log /dev/stdout {{ $logFormatName }};
error_log /dev/stderr;
{{- else }}
access_log syslog:server={{ $config.SyslogServerAddress }}
{{- with $config.SyslogFacility }},facility={{ . }}{{ end }}
{{- with $config.SyslogTag }},tag={{ . }}{{ end}}
combined;
{{ $logFormatName }};
error_log syslog:server={{ $config.SyslogServerAddress }}
{{- with $config.SyslogFacility }},facility={{ . }}{{ end }}
Expand Down Expand Up @@ -338,7 +364,6 @@ http {
{{- end }}
{{- range $index, $bind := $instance.Spec.Binds }}
{{- if eq $index 0 }}
upstream rpaas_default_upstream {
server {{ $bind.Host }};
Expand Down
58 changes: 55 additions & 3 deletions internal/pkg/rpaas/nginx/configuration_render_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ func TestRpaasConfigurationRenderer_Render(t *testing.T) {
assert.NotRegexp(t, `worker_processes(.+);`, result)
assert.NotRegexp(t, `worker_connections(.+);`, result)
assert.Regexp(t, `include modules/\*\.conf;`, result)
assert.Regexp(t, `access_log /dev/stdout combined;`, result)
assert.Regexp(t, `access_log /dev/stdout rpaasv2;`, result)
assert.Regexp(t, `error_log /dev/stderr;`, result)
assert.Regexp(t, `server {\n\s+listen 8800;\n\s+}\n+`, result)
assert.Regexp(t, `server {
Expand Down Expand Up @@ -127,7 +127,7 @@ func TestRpaasConfigurationRenderer_Render(t *testing.T) {
Instance: &v1alpha1.RpaasInstance{},
},
assertion: func(t *testing.T, result string) {
assert.Regexp(t, `access_log syslog:server=syslog.server.example.com\n\s+combined;`, result)
assert.Regexp(t, `access_log syslog:server=syslog.server.example.com\n\s+rpaasv2;`, result)
assert.Regexp(t, `error_log syslog:server=syslog.server.example.com;`, result)
},
},
Expand All @@ -143,7 +143,7 @@ func TestRpaasConfigurationRenderer_Render(t *testing.T) {
Instance: &v1alpha1.RpaasInstance{},
},
assertion: func(t *testing.T, result string) {
assert.Regexp(t, `access_log syslog:server=syslog.server.example.com,facility=local1,tag=my-tag\n\s+combined;`, result)
assert.Regexp(t, `access_log syslog:server=syslog.server.example.com,facility=local1,tag=my-tag\n\s+rpaasv2;`, result)
assert.Regexp(t, `error_log syslog:server=syslog.server.example.com,facility=local1,tag=my-tag;`, result)
},
},
Expand Down Expand Up @@ -531,6 +531,58 @@ func TestRpaasConfigurationRenderer_Render(t *testing.T) {
assert.Regexp(t, `load_module "modules/mod2.so";`, result)
},
},
{
name: "with custom log format",
data: ConfigurationData{
Config: &v1alpha1.NginxConfig{
LogFormatName: "custom",
LogFormatEscape: "default",
LogFormat: `'status=${status} foo_bar=${http_x_foo_bar}'`,
},
Instance: &v1alpha1.RpaasInstance{},
},
assertion: func(t *testing.T, result string) {
assert.Regexp(t, `log_format custom escape=default 'status=\$\{status\} foo_bar=\$\{http_x_foo_bar\}';`, result)
assert.Regexp(t, `access_log /dev/stdout custom;`, result)
},
},
{
name: "with default log format and additional headers",
data: ConfigurationData{
Config: &v1alpha1.NginxConfig{
LogAdditionalHeaders: []string{"X-Foo-Bar", "X-App-Version", "X-App-Vendor", "X-App-User"},
},
Instance: &v1alpha1.RpaasInstance{},
},
assertion: func(t *testing.T, result string) {
assert.Regexp(t, `\s+','
\s+'"header_x_foo_bar":"\$\{http_x_foo_bar\}",'
\s+'"header_x_app_version":"\$\{http_x_app_version\}",'
\s+'"header_x_app_vendor":"\$\{http_x_app_vendor\}",'
\s+'"header_x_app_user":"\$\{http_x_app_user\}"'
\s+'}';`, result)
},
},
{
name: "with log additional fields",
data: ConfigurationData{
Config: &v1alpha1.NginxConfig{
LogAdditionalFields: map[string]string{
"key1": "Some custom var: ${http_x_foo_bar}",
"key2": "Another custom var: ${host}",
"custom_key": "${custom_var}",
},
},
Instance: &v1alpha1.RpaasInstance{},
},
assertion: func(t *testing.T, result string) {
assert.Regexp(t, `\s+'\{'
\s+'"custom_key":"\$\{custom_var\}",'
\s+'"key1":"Some custom var: \$\{http_x_foo_bar\}",'
\s+'"key2":"Another custom var: \$\{host\}",'
`, result)
},
},
}

for _, tt := range tests {
Expand Down

0 comments on commit ec13a90

Please sign in to comment.