Skip to content
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

Add endpoint-specific fields to API request log #3528

Merged
merged 2 commits into from
Nov 8, 2022

Conversation

dnephin
Copy link
Contributor

@dnephin dnephin commented Oct 31, 2022

Summary

This PR introduces a hook for adding fields to the API request log. It also uses the new hook in list grants to add a lastUpdatedIndex and numGrants fields. I think we may also want to use this with SCIM endpoints, so that we can track which provider is using which fields. I'm sure we'll find other use cases for adding fields to the request log as we add new endpoints.

I think maybe we can find a better way to include these fields, but this seemed to work for now. If anyone has a suggestion for a better interface, I'd be happy to incorporate it.

Comment on lines 31 to 33
rCtx.AddLogFields = append(rCtx.AddLogFields, func(event *zerolog.Event) {
event.Int64("lastUpdateIndex", r.LastUpdateIndex)
})
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I add this separately from numGrants so that if the request errors and exits early we still get this context added to the log entry.

Copy link
Collaborator

@BruceMacD BruceMacD left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What log level do these get output at?

@dnephin
Copy link
Contributor Author

dnephin commented Oct 31, 2022

@dnephin dnephin force-pushed the dnephin/log-some-query-parameters branch from d7a039f to 2a48700 Compare November 2, 2022 21:39
@dnephin
Copy link
Contributor Author

dnephin commented Nov 2, 2022

I made a small change to the implementation. Instead of storing the slice directly on RequestContext, I moved it to a new Response field. I think that helps keep it separate from other immutable fields that are inputs to the handlers.


// Response is a mutable field. It can be modified by API handlers to add
// new response metadata.
Response *ResponseMetadata
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

consider making this a non-pointer

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I believe this will need to be a pointer so that we can mutate it without needing to call c.Set() to update the value stored in the gin.Context.

A pointer receiver may work, but I was hoping that making this a pointer make it more obvious that this was a mutable value.

I'll think about it some more .

@dnephin dnephin force-pushed the dnephin/log-some-query-parameters branch from 2a48700 to 8be8f16 Compare November 8, 2022 16:57
@dnephin
Copy link
Contributor Author

dnephin commented Nov 8, 2022

I pushed a commit to fix the lint failure on main.

I don't think we've found an alternative without adding methods to RequestContext which I'd really like to avoid. By keeping the methods off the top level it should be move obvious how the methods are supposed to be used (to read data from the request, or to add metadata associated with the response).

I'm going to merge it this way. It should be super easy to change at any time. I don't see us adding all that many fields to our logs anytime soon.

@dnephin dnephin merged commit e31a20a into main Nov 8, 2022
@dnephin dnephin deleted the dnephin/log-some-query-parameters branch November 8, 2022 17:46
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants