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 context to http server log middleware #500

Merged
merged 1 commit into from
Nov 12, 2023

Conversation

Crevil
Copy link
Member

@Crevil Crevil commented Nov 12, 2023

Currently, the HTTP logging middleware adds the request ID to its context fields twice and ignores any other context values in the logger.

info	http/server.go:2122	[200] GET /status	{"requestId": "73efeeb4-af33-46b5-b31b-6dbd21efa245", "req": {"id":"73efeeb4-af33-46b5-b31b-6dbd21efa245","url":"/status?service=test-service","method":"GET","path":"/status","headers":{"Accept-Encoding":"gzip","Authorization":"Bearer test","User-Agent":"Go-http-client/1.1","X-Cli-Version":"7fce8c29f7bbc1e38d683713aca5fa42c43215a8","X-Request-Id":"73efeeb4-af33-46b5-b31b-6dbd21efa245"}}, "res": {"statusCode":200}, "responseTime": 3}

This change removes the excessive request ID fields and uses a context logger, which automatically has the request ID available. This simplifies the middleware a bit but also allows for other context values to be propagated in the logging context, eg. subject when OAuth2.0 support is added.

info	http/server.go:2122	[404] GET /policies	{"requestId": "b8ac36e3-f72c-4146-b43b-d61b02fb92f0", "req": {"url":"/policies?service=test","method":"GET","path":"/policies","headers":{"Accept-Encoding":"gzip","Authorization":"Bearer test","User-Agent":"Go-http-client/1.1","X-Caller-Email":"bso@lunar.app","X-Cli-Version":"96e2ef7d006e6b0e44aca10515cecd2821cc8123","X-Request-Id":"b8ac36e3-f72c-4146-b43b-d61b02fb92f0"}}, "res": {"statusCode":404}, "responseTime": 0}

The AddContext function is also expanded to merge existing values in the logging context to avoid overriding any values.

This change was extracted from #498 where the subject from JWT tokens will be added as context values. Here it became clear that it wasn't working as intended.

Currently the HTTP logging middleware adds the requst ID to its context fields
twice and further ignores any other context values in the logger.

    info	http/server.go:2122	[200] GET /status	{"requestId": "73efeeb4-af33-46b5-b31b-6dbd21efa245", "req": {"id":"73efeeb4-af33-46b5-b31b-6dbd21efa245","url":"/status?service=test-service","method":"GET","path":"/status","headers":{"Accept-Encoding":"gzip","Authorization":"Bearer test","User-Agent":"Go-http-client/1.1","X-Cli-Version":"7fce8c29f7bbc1e38d683713aca5fa42c43215a8","X-Request-Id":"73efeeb4-af33-46b5-b31b-6dbd21efa245"}}, "res": {"statusCode":200}, "responseTime": 3}

This change removes the excessive request ID fields and uses a context logger,
which automatically has the request ID available. This simplifies the middleware
a bit but also allows for other context values to be propagated in the logging
context, eg. subject when OAuth2.0 support is added.

    info	http/server.go:2122	[404] GET /policies	{"requestId": "b8ac36e3-f72c-4146-b43b-d61b02fb92f0", "req": {"url":"/policies?service=test","method":"GET","path":"/policies","headers":{"Accept-Encoding":"gzip","Authorization":"Bearer test","User-Agent":"Go-http-client/1.1","X-Caller-Email":"bso@lunar.app","X-Cli-Version":"96e2ef7d006e6b0e44aca10515cecd2821cc8123","X-Request-Id":"b8ac36e3-f72c-4146-b43b-d61b02fb92f0"}}, "res": {"statusCode":404}, "responseTime": 0}
@Crevil Crevil requested a review from a team as a code owner November 12, 2023 20:19
@github-actions github-actions bot added the minor label Nov 12, 2023
Copy link
Contributor

@kjuulh kjuulh left a comment

Choose a reason for hiding this comment

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

LGTM

@Crevil Crevil added patch and removed minor labels Nov 12, 2023
@Crevil Crevil merged commit d63510b into master Nov 12, 2023
4 checks passed
@Crevil Crevil deleted the feature/context-http-middleware branch November 12, 2023 21:04
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants