-
Notifications
You must be signed in to change notification settings - Fork 141
Adding nginx_proxy access_log format ability #4102
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Conversation
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## main #4102 +/- ##
==========================================
+ Coverage 85.99% 86.01% +0.01%
==========================================
Files 131 131
Lines 14063 14123 +60
Branches 35 35
==========================================
+ Hits 12094 12148 +54
- Misses 1769 1773 +4
- Partials 200 202 +2 ☔ View full report in Codecov by Sentry. 🚀 New features to boost your workflow:
|
apis/v1alpha2/nginxproxy_types.go
Outdated
| Format string `json:"format"` | ||
| Buffer string `json:"buffer,omitempty"` | ||
| Condition string `json:"condition,omitempty"` | ||
| Gzip bool `json:"gzip,omitempty"` |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm really not sure if we need those Buffer, Condition and Gzip or they are part of format itself
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm not convinced we should support writing logs to a file right now, especially due to our read-only filesystem structure. But I'll defer to others who can spend a bit more time reviewing and researching than I can right now :)
sidenote: if you add any optional fields to a CRD spec, they should be pointer types (except for slices and maps, those are pointers already). The reason for this is that if it's not a pointer type, then if we see that field as its zero value, we have no idea if a user set it to that, or if they just omitted it. By having it be a pointer, it's more obvious because if it's a zero value, we know that a user set it to that, otherwise it would be nil if a user didn't set it.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'll do a full review next week as the release work is taking up a lot of my time today, but I'll address your questions as best I can now.
I agree with Saylor, we should not support writing to a file. It would require a volume mount, which is not easily configurable, and anyway we don't support log rotation so it would fill up quickly and be an operational nightmare.
The scope of the ticket is just to support configuring the format, however we could also support syslog as a destination, but the default should be /dev/stdout.
Following on from that, we don't need to support the Buffer, and Gzip fields as they only really make sense when writing to a file. Condition requires map directives, we don't need it right now.
We should support the ability to disable the access logs completely too.
Here's what NIC supports for reference: https://docs.nginx.com/nginx-ingress-controller/configuration/global-configuration/configmap-resource/#logging
| Buffer string `json:"buffer,omitempty"` | ||
| Condition string `json:"condition,omitempty"` | ||
| Gzip bool `json:"gzip,omitempty"` | ||
| } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Also not sure if combined option should be included:
map $status $loggable {
~^[23] 0;
default 1;
}
access_log /path/to/access.log combined if=$loggable;
| if len(logFormats) == 0 { | ||
| logFormats = append(logFormats, ngfAPIv1alpha2.LogFormat{ | ||
| Name: "default", | ||
| Format: "$remote_addr - [$time_local] \"$request\" $status $body_bytes_sent", |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
if we have a default format, then what it should be?
| // +optional | ||
| LogFormats []LogFormat `json:"logFormats,omitempty"` | ||
|
|
||
| // AccessLogs defines the access log settings, including the log file path, format, and optional parameters. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
While log_format can only be specified in the http context, access_logcan be specified at pretty much any context. This can be useful for users who want to add logs for a specificlocation, for example, instead of the entire http` block.
Has there been any discussion or thought around how we could let a user specify at that level to be more granular? I think it would probably end up being a policy or filter to do that, which would obviously be a lot more work than this. Since this work is just targeting logging at the http context, let's be clear in this comment that this only applies to that context. If we document this feature, we should also be clear about that.
apis/v1alpha2/nginxproxy_types.go
Outdated
| Format string `json:"format"` | ||
| Buffer string `json:"buffer,omitempty"` | ||
| Condition string `json:"condition,omitempty"` | ||
| Gzip bool `json:"gzip,omitempty"` |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm not convinced we should support writing logs to a file right now, especially due to our read-only filesystem structure. But I'll defer to others who can spend a bit more time reviewing and researching than I can right now :)
sidenote: if you add any optional fields to a CRD spec, they should be pointer types (except for slices and maps, those are pointers already). The reason for this is that if it's not a pointer type, then if we see that field as its zero value, we have no idea if a user set it to that, or if they just omitted it. By having it be a pointer, it's more obvious because if it's a zero value, we know that a user set it to that, otherwise it would be nil if a user didn't set it.
0923130 to
3d18022
Compare
|
|
||
| // Logging defines logging related settings for NGINX. | ||
| type Logging struct { | ||
| // ErrorLevel defines the error log level. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
again lost comments
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, it's an annoying bug in the linter tool when it automatically optimizes structs for the compiler, so comments have to be manually added back in.
| Name *string `json:"name"` | ||
| Format *string `json:"format"` |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Be sure to add omitempty for all pointer fields.
| {{- if .LoggingSettings }} | ||
| {{- /* Define custom log format */ -}} | ||
| {{- if .LoggingSettings.LogFormat.Name }} | ||
| log_format {{ .LoggingSettings.LogFormat.Name }} '{{ .LoggingSettings.LogFormat.Format }}'; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We don't necessarily want to indent the actual directives that get rendered in the conf, otherwise it'll look strange with a random indent. The templating statements can be indented, but the directives should match the indentation of the context of the conf that they're in (in this case, no indent at all).
Same comment for the access_log directives below.
| // buildLogging converts the API logging spec (currently singular LogFormat / AccessLog fields | ||
| // in v1alpha2) into internal slice-based representation used by templates. | ||
| func buildLogging(gateway *graph.Gateway) Logging { | ||
| logSettings := Logging{ErrorLevel: defaultErrorLogLevel} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can we not just use this Logging struct instead of defining a new LoggingSettings struct that contains similar information?
Proposed changes
Problem: As a user of NGF
I want to have the ability to configure the log format of NGINX's access and error logs
So that I can easily collect logs from NGINX in my logging platform.
Solution: Adding log_format and access_log fields to nginx-proxy CRD, also adding default values for them
Testing: TODO
Closes #1200
Checklist
Before creating a PR, run through this checklist and mark each as complete.
Release notes
If this PR introduces a change that affects users and needs to be mentioned in the release notes,
please add a brief note that summarizes the change.