Skip to content

Commit

Permalink
Consistently use the value from X-Request-Id as the request's ID wh…
Browse files Browse the repository at this point in the history
…en present (#3117)

* Consistently use the value from X-Request-Id as the request's ID

* Use faster X-Request-Id header lookup

Co-authored-by: Mahdi Bahrami <github@mahdibm.com>

---------

Co-authored-by: Mahdi Bahrami <github@mahdibm.com>
  • Loading branch information
baarde and MahdiBM committed Dec 13, 2023
1 parent 00c902c commit 9c830d4
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 8 deletions.
14 changes: 7 additions & 7 deletions Sources/Vapor/Request/Request.swift
Expand Up @@ -54,7 +54,10 @@ public final class Request: CustomStringConvertible, Sendable {
}
}

/// A uniquely generated ID for each request
/// A unique ID for the request.
///
/// The request identifier is set to value of the `X-Request-Id` header when present, or to a
/// uniquelly generated value otherwise.
public let id: String

// MARK: Metadata
Expand Down Expand Up @@ -309,6 +312,7 @@ public final class Request: CustomStringConvertible, Sendable {
byteBufferAllocator: ByteBufferAllocator = ByteBufferAllocator(),
on eventLoop: EventLoop
) {
let requestId = headers.first(name: .xRequestId) ?? UUID().uuidString
let bodyStorage: BodyStorage
if let body = collectedBody {
bodyStorage = .collected(body)
Expand All @@ -317,11 +321,7 @@ public final class Request: CustomStringConvertible, Sendable {
}

var logger = logger
if let requestId = headers[.xRequestId].first {
logger[metadataKey: "request-id"] = .string(requestId)
} else {
logger[metadataKey: "request-id"] = .string(UUID().uuidString)
}
logger[metadataKey: "request-id"] = .string(requestId)
self._logger = .init(logger)

let storageBox = RequestBox(
Expand All @@ -335,7 +335,7 @@ public final class Request: CustomStringConvertible, Sendable {
byteBufferAllocator: byteBufferAllocator
)
self.requestBox = .init(storageBox)
self.id = UUID().uuidString
self.id = requestId
self.application = application

self.remoteAddress = remoteAddress
Expand Down
14 changes: 13 additions & 1 deletion Tests/VaporTests/RequestTests.swift
Expand Up @@ -29,6 +29,18 @@ final class RequestTests: XCTestCase {
XCTAssertNotEqual(request1.id, request2.id)
}

func testRequestIdInLoggerMetadata() throws {
let app = Application(.testing)
defer { app.shutdown() }

let request = Request(application: app, on: app.eventLoopGroup.next())
guard case .string(let string) = request.logger[metadataKey: "request-id"] else {
XCTFail("Did not find request-id key in logger metadata.")
return
}
XCTAssertEqual(string, request.id)
}

func testRequestPeerAddressForwarded() throws {
let app = Application(.testing)
defer { app.shutdown() }
Expand Down Expand Up @@ -104,7 +116,7 @@ final class RequestTests: XCTestCase {
defer { app.shutdown() }

app.get("remote") {
if case .string(let string) = $0.logger[metadataKey: "request-id"] {
if case .string(let string) = $0.logger[metadataKey: "request-id"], string == $0.id {
return string
} else {
throw Abort(.notFound)
Expand Down

0 comments on commit 9c830d4

Please sign in to comment.