-
Notifications
You must be signed in to change notification settings - Fork 3
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
Adding custom attribute support for http middleware #26
base: main
Are you sure you want to change the base?
Conversation
customWriter := &responseWriter{ResponseWriter: w} | ||
// If any fields in CustomAttributes are nil, do not call them to avoid errors | ||
addExtraAttributes := validateCustomAttributes(&l.customAttributeAssigner) | ||
var extraAttributes map[string]interface{} |
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.
this will be initialized to the zero value, correct? For maps it's nil. So this should just be initialized to an empty map
l.LogRequestEnd(ctx, r, "finished call", customWriter.statusCode, latency, updatedAttrs) | ||
} | ||
|
||
func (l *loggingMiddleware) BuildLoggingAttributes(ctx context.Context, r *http.Request, extra ...interface{}) []interface{} { |
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.
any particular reason why extra
changed to an interface? Could you just keep the map[string]interface{} type throughout?
http/server/logging/logging.go
Outdated
type loggingFunc func(w http.ResponseWriter, r *http.Request, attrs map[string]interface{}) map[string]interface{} | ||
type CustomAttributes struct { | ||
AttributeInitializer *initFunc // sets keys for custom attributes at the beginning of ServeHTTP() | ||
AttributeAssigner *loggingFunc // assigns values for custom attributes after request has completed |
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.
Should we provide default Attribute initializers and assigners?
now: time.Now, | ||
logger: *logger, | ||
source: source, | ||
customAttributeAssigner: customAttributeAssigner, |
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.
Do you support the initializer and assigner funcs to be nil? If not then you should assign them to default functions if not provided
|
||
var updatedAttrs map[string]interface{} | ||
if addExtraAttributes { | ||
updatedAttrs = (l.customAttributeAssigner.AttributeAssigner)(w, r, extraAttributes) |
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.
Why do we pass in the original http.ResponseWriter rather than the customWriter here?
} | ||
|
||
// sets default source "ApiRequestLog" | ||
func setSourceIfEmtpy(source *string) { |
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.
typo
type CustomAttributes struct { | ||
AttributeInitializer initFunc // sets keys for custom attributes at the beginning of ServeHTTP() | ||
AttributeAssigner loggingFunc // assigns values for custom attributes after request has completed | ||
} |
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.
Consider renaming the struct to something like "AttributeManager" and the methods to be along the lines of "Initialize" and "Assign"
type CustomAttributes struct { | ||
AttributeInitializer initFunc // sets keys for custom attributes at the beginning of ServeHTTP() | ||
AttributeAssigner loggingFunc // assigns values for custom attributes after request has completed | ||
} |
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.
Why don't we just set the keys and assign the values in the same function after the request has completed?
This PR allows extra attributes to be assigned to the logger.
CustomAttributes struct:
loggingMiddleware takes source as a field. If specified, it is assigned. Otherwise, the default is "ApiRequestLog"