request-id
The request-id
plugin adds a unique ID to each request proxied through APISIX, which can be used to track API requests. If a request carries an ID in the header corresponding to header_name
, the plugin will use the header value as the unique ID and will not overwrite with the automatically generated ID.
If you are using API7 Enterprise, the request ID can also be found in the gateway's access log and error log.
Examples
The examples below demonstrate how you can configure request-id
in different scenarios.
Attach Request ID to Default Response Header
The following example demonstrates how to configure request-id
on a route which attaches a generated request ID to the default X-Request-Id
response header, if the header value is not passed in the request. When the X-Request-Id
header is set in the request, the plugin will take the value in the request header as the request ID.
Create a route with the request-id
plugin using its default configurations (explicitly defined):
curl "http://127.0.0.1:9180/apisix/admin/routes" -X PUT \
-H "X-API-KEY: ${ADMIN_API_KEY}" \
-d '{
"id": "request-id-route",
"uri": "/anything",
"plugins": {
"request-id": {
"header_name": "X-Request-Id",
"include_in_response": true,
"algorithm": "uuid"
}
},
"upstream": {
"type": "roundrobin",
"nodes": {
"httpbin.org:80": 1
}
}
}'
Send a request to the route:
curl -i "http://127.0.0.1:9080/anything"
You should receive an HTTP/1.1 200 OK
response and see the response includes the X-Request-Id
header with a generated ID:
X-Request-Id: b9b2c0d4-d058-46fa-bafc-dd91a0ccf441
Send a request to the route with a custom request ID in the header:
curl -i "http://127.0.0.1:9080/anything" -H 'X-Request-Id: some-custom-request-id'
You should receive an HTTP/1.1 200 OK
response and see the response includes the X-Request-Id
header with the custom request ID:
X-Request-Id: some-custom-request-id
Attach Request ID to Custom Response Header
The following example demonstrates how to configure request-id
on a route which attaches a generated request ID to a specified header.
Create a route with the request-id
plugin as such:
curl "http://127.0.0.1:9180/apisix/admin/routes" -X PUT \
-H "X-API-KEY: ${ADMIN_API_KEY}" \
-d '{
"id": "request-id-route",
"uri": "/anything",
"plugins": {
"request-id": {
"header_name": "X-Req-Identifier",
"include_in_response": true
}
},
"upstream": {
"type": "roundrobin",
"nodes": {
"httpbin.org:80": 1
}
}
}'
❶ Define a custom header that carries the request ID.
❷ Include the request ID in the response header.
Send a request to the route:
curl -i "http://127.0.0.1:9080/anything"
You should receive an HTTP/1.1 200 OK
response and see the response includes the X-Req-Identifier
header with a generated ID:
X-Req-Identifier: 1c42ff59-ee4c-4103-a980-8359f4135b21
Hide Request ID in Response Header
The following example demonstrates how to configure request-id
on a route which attaches a generated request ID to a specified header. The header containing the request ID should be forwarded to the upstream service but not returned in the response header.
Create a route with the request-id
plugin as such:
curl "http://127.0.0.1:9180/apisix/admin/routes" -X PUT \
-H "X-API-KEY: ${ADMIN_API_KEY}" \
-d '{
"id": "request-id-route",
"uri": "/anything",
"plugins": {
"request-id": {
"header_name": "X-Req-Identifier",
"include_in_response": false
}
},
"upstream": {
"type": "roundrobin",
"nodes": {
"httpbin.org:80": 1
}
}
}'
❶ Define a custom header that carries the request ID.
❷ Do not include the request ID in the response header.
Send a request to the route:
curl -i "http://127.0.0.1:9080/anything"
You should receive an HTTP/1.1 200 OK
response not and see X-Req-Identifier
header among the response headers. In the response body, you should see:
{
"args": {},
"data": "",
"files": {},
"form": {},
"headers": {
"Accept": "*/*",
"Host": "127.0.0.1",
"User-Agent": "curl/8.6.0",
"X-Amzn-Trace-Id": "Root=1-6752748c-7d364f48564508db1e8c9ea8",
"X-Forwarded-Host": "127.0.0.1",
"X-Req-Identifier": "268092bc-15e1-4461-b277-bf7775f2856f"
},
...
}
This shows the request ID is forwarded to the upstream service but not returned in the response header.
Use nanoid
Algorithm
The following example demonstrates how to configure request-id
on a route and use the nanoid
algorithm to generate the request ID.
Create a route with the request-id
plugin as such:
curl "http://127.0.0.1:9180/apisix/admin/routes" -X PUT \
-H "X-API-KEY: ${ADMIN_API_KEY}" \
-d '{
"id": "request-id-route",
"uri": "/anything",
"plugins": {
"request-id": {
"algorithm": "nanoid"
}
},
"upstream": {
"type": "roundrobin",
"nodes": {
"httpbin.org:80": 1
}
}
}'
Send a request to the route:
curl -i "http://127.0.0.1:9080/anything"
You should receive an HTTP/1.1 200 OK
response and see the response includes the X-Req-Identifier
header with an ID generated using the nanoid
algorithm:
X-Request-Id: kepgHWCH2ycQ6JknQKrX2
Attach Request ID Globally and on a Route
The following example demonstrates how to configure request-id
as a global plugin and on a route to attach two IDs.
Create a global rule for the request-id
plugin which adds request ID to a custom header:
curl -i "http://127.0.0.1:9180/apisix/admin/global_rules" -X PUT -d '{
"id": "rule-for-request-id",
"plugins": {
"request-id": {
"header_name": "Global-Request-ID"
}
}
}'
Create a route with the request-id
plugin which adds request ID to a different custom header:
curl "http://127.0.0.1:9180/apisix/admin/routes" -X PUT \
-H "X-API-KEY: ${ADMIN_API_KEY}" \
-d '{
"id": "request-id-route",
"uri": "/anything",
"plugins": {
"request-id": {
"header_name": "Route-Request-ID"
}
},
"upstream": {
"type": "roundrobin",
"nodes": {
"httpbin.org:80": 1
}
}
}'
Send a request to the route:
curl -i "http://127.0.0.1:9080/anything"
You should receive an HTTP/1.1 200 OK
response and see the response includes the following headers:
Global-Request-ID: 2e9b99c1-08ed-4a74-b347-49c0891b07ad
Route-Request-ID: d755666b-732c-4f0e-a30e-a7a71ace4e26