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

Expose Vapor Request's request-id value, so that it can be passed into logging from other libraries #2964

Merged
merged 4 commits into from Feb 24, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
6 changes: 5 additions & 1 deletion Sources/Vapor/Request/Request.swift
Expand Up @@ -27,6 +27,9 @@ public final class Request: CustomStringConvertible {

internal var isKeepAlive: Bool

/// A uniquely generated ID for each request
public let id: String

// MARK: Metadata

/// Route object we found for this request.
Expand Down Expand Up @@ -230,6 +233,7 @@ public final class Request: CustomStringConvertible {
byteBufferAllocator: ByteBufferAllocator = ByteBufferAllocator(),
on eventLoop: EventLoop
) {
self.id = UUID().uuidString
self.application = application
self.method = method
self.url = url
Expand All @@ -246,7 +250,7 @@ public final class Request: CustomStringConvertible {
self.storage = .init()
self.isKeepAlive = true
self.logger = logger
self.logger[metadataKey: "request-id"] = .string(UUID().uuidString)
self.logger[metadataKey: "request-id"] = .string(id)
self.byteBufferAllocator = byteBufferAllocator
}
}
10 changes: 10 additions & 0 deletions Tests/VaporTests/RequestTests.swift
Expand Up @@ -18,6 +18,16 @@ final class RequestTests: XCTestCase {
XCTAssertEqual(res.body.string, ipV4Hostname)
}
}

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

let request1 = Request(application: app, on: app.eventLoopGroup.next())
let request2 = Request(application: app, on: app.eventLoopGroup.next())

XCTAssertNotEqual(request1.id, request2.id)
}

func testRequestPeerAddressForwarded() throws {
let app = Application(.testing)
Expand Down