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

feat(request-id): add a request ID to logs and error templates #11308

Closed
wants to merge 5 commits into from

Conversation

samugi
Copy link
Member

@samugi samugi commented Jul 27, 2023

Closed in favor of #11624

Summary

Add a request ID (which matches the trace_id or correlation_id when available) to error templates and logs.

Description

Error logs are populated with a unique identifier for the request (random generated 16 Bytes hex), using a format similar to the following:

2023/07/31 18:21:23 [error] 1773198#0: *2966 [lua] some_file.lua:58: rewrite(): some log, client: 127.0.0.1, server: kong, request: "GET / HTTP/1.1", host: "localhost:8000", request_id: "c48604ca1518bcb38bb2661a155d5fff"

The ID is stored in the Nginx variable $kong_request_id and can be used in the access_log directive.

Trace ID

When available, the request ID is replaced by the current request's trace ID. Trace IDs in Kong are generated during the rewrite phase, for each request, which means that when tracing is enabled the same request may log different IDs before and after the value is updated during the rewrite phase. In addition to that, in case of distributed tracing (requests incoming to Kong contain headers with tracing information, including the trace ID), the trace ID is updated by the Opentelemetry plugin during the access phase. Whenever the Request ID is updated, a line is printed in the logs to notify about the update event, e.g.:

setting request_id to: [new_id] for the current request, request_id: "[old_id]"

Example:

Request that provides the 4bf92f3577b34da6a3ce929d0e0e4736 trace ID via a tracing header:
http :8000 traceparent:00-4bf92f3577b34da6a3ce929d0e0e4736-00f067aa0ba902b7-01
Logs:
some_file.lua:58: access(): some log (before OTEL), client: 127.0.0.1, server: kong, request: "GET / HTTP/1.1", host: "0.0.0.0:9000", request_id: "c48604ca1518bcb38bb2661a155d5fff"

request_id.lua:83 setting request_id to: '4bf92f3577b34da6a3ce929d0e0e4736' for the current request, client: 127.0.0.1, server: kong, request: "GET / HTTP/1.1", host: "0.0.0.0:9000", request_id: "c48604ca1518bcb38bb2661a155d5fff"

handler.lua:118: access(): test log after Opentelemetry, client: 127.0.0.1, server: kong, request: "GET / HTTP/1.1", host: "0.0.0.0:9000", request_id: "4bf92f3577b34da6a3ce929d0e0e4736"

Error templates always display the updated Request ID.

Testing

Error templates & logs

Start Kong using an error template that includes two placeholders for parameters, the first will be populated with the error message, the second with the request ID. The builtin fixtures can be used to test this, similarly to the following example:

KONG_ERROR_TEMPLATE_JSON=spec/fixtures/error_templates/error_template.json kong start
Request:
http :8000
Response:
{
    "custom_template_message": "no Route matched with those values",
    "request_id": "26751e2f52415e17c847e4bfb764cb66"
}

Access log

Example output:
[4bf92f3577b34da6a3ce929d0e0e4736] 127.0.0.1 - - [31/Jul/2023:18:21:24 +0200] "GET / HTTP/1.1" 200 648 "-" "HTTPie/3.2.1"

Checklist

Full changelog

  • Add a request ID (which matches the trace_id or correlation_id when available) to error templates, access logs and error logs.
  • Bump lua-kong-nginx-module to version 0.7.0 (needed to use the custom error log handler)
  • Include request ID in log serializer output

Issue reference

KAG-2034

FTI-4837

@samugi samugi marked this pull request as draft July 27, 2023 16:27
@samugi samugi force-pushed the feat/correlation-trace-request-id branch from a37e3cc to 5d39f87 Compare July 27, 2023 16:28
@pull-request-size pull-request-size bot added size/M and removed size/L labels Jul 27, 2023
@samugi samugi force-pushed the feat/correlation-trace-request-id branch from 5d39f87 to d466f6b Compare July 28, 2023 08:35
@pull-request-size pull-request-size bot added size/L and removed size/M labels Jul 28, 2023
@samugi samugi force-pushed the feat/correlation-trace-request-id branch 4 times, most recently from ac7b065 to c356dc1 Compare July 31, 2023 16:46
@samugi samugi force-pushed the feat/correlation-trace-request-id branch 3 times, most recently from f0e69e2 to 384ddb6 Compare August 1, 2023 16:12
@samugi samugi changed the title feat(request-id): request trace id feat(request-id): add a request ID to logs and error templates Aug 2, 2023
@samugi samugi force-pushed the feat/correlation-trace-request-id branch 3 times, most recently from 3299bb4 to 34f98cf Compare August 8, 2023 11:25
@pull-request-size pull-request-size bot removed the size/L label Aug 8, 2023
@samugi samugi force-pushed the feat/correlation-trace-request-id branch from d9c2a69 to 3bf7bac Compare September 12, 2023 10:52
@samugi samugi force-pushed the feat/correlation-trace-request-id branch 2 times, most recently from 08e888c to 4f0fd27 Compare September 12, 2023 14:54
@samugi samugi marked this pull request as draft September 12, 2023 14:55
@samugi samugi force-pushed the feat/correlation-trace-request-id branch from 4f0fd27 to 8462d21 Compare September 12, 2023 15:58
@dndx dndx requested a review from chronolaw September 13, 2023 08:26
.requirements Outdated Show resolved Hide resolved
kong/tracing/request_id.lua Outdated Show resolved Hide resolved
* Add a request ID (which matches the trace_id or correlation_id when
  available) to error templates and logs.

* Bump lua-kong-nginx-module to version 0.7.0

* Use the new directive `lua_kong_error_log_request_id`
  introduced in 0.7.0 which adds the request id to the error log output
add unit tests
add integration tests to cover zipkin as well
make access_log include request_id by default
refactoring and log messages
add request id to the default error templates
@samugi samugi force-pushed the feat/correlation-trace-request-id branch 2 times, most recently from 26cd886 to d1a9f29 Compare September 19, 2023 18:43
@samugi samugi force-pushed the feat/correlation-trace-request-id branch from d1a9f29 to e19f787 Compare September 19, 2023 19:31
@samugi samugi force-pushed the feat/correlation-trace-request-id branch from e19f787 to 4b638b9 Compare September 19, 2023 21:25
setting trace id from propagation allows to set the correct format
of trace id depending on the headers format, instead of defauting to hex

bump lua-kong-nginx-module to 0.7.1
@samugi samugi force-pushed the feat/correlation-trace-request-id branch from 4b638b9 to d280b41 Compare September 19, 2023 21:27
@samugi samugi marked this pull request as ready for review September 19, 2023 21:27
@github-actions github-actions bot added the author/community PRs from the open-source community (not Kong Inc) label Sep 20, 2023
@samugi samugi marked this pull request as draft September 20, 2023 09:30
@samugi samugi closed this Sep 20, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants