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

2.x.x - RequestID from main branch #328

Merged
merged 1 commit into from
Jan 3, 2024
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 1 addition & 6 deletions Sources/Hummingbird/Application.swift
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
//
//===----------------------------------------------------------------------===//

import Atomics
import HummingbirdCore
import Logging
import NIOCore
Expand Down Expand Up @@ -145,8 +144,7 @@ extension HBApplicationProtocol {
}

public func loggerWithRequestId(_ logger: Logger) -> Logger {
let requestId = globalRequestID.loadThenWrappingIncrement(by: 1, ordering: .relaxed)
return logger.with(metadataKey: "hb_id", value: .stringConvertible(requestId))
return logger.with(metadataKey: "hb_id", value: .stringConvertible(RequestID()))
}

/// Application class. Brings together all the components of Hummingbird together
Expand Down Expand Up @@ -238,6 +236,3 @@ extension Logger {
return logger
}
}

/// Current global request ID
private let globalRequestID = ManagedAtomic(0)
40 changes: 40 additions & 0 deletions Sources/Hummingbird/Server/RequestID.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
//===----------------------------------------------------------------------===//
//
// This source file is part of the Hummingbird server framework project
//
// Copyright (c) 2023-2024 the Hummingbird authors
// Licensed under Apache License v2.0
//
// See LICENSE.txt for license information
// See hummingbird/CONTRIBUTORS.txt for the list of Hummingbird authors
//
// SPDX-License-Identifier: Apache-2.0
//
//===----------------------------------------------------------------------===//

import Atomics

/// Generate Unique ID for each request
struct RequestID: CustomStringConvertible {
let low: UInt64

init() {
self.low = Self.globalRequestID.loadThenWrappingIncrement(by: 1, ordering: .relaxed)
}

var description: String {
Self.high + self.formatAsHexWithLeadingZeros(self.low)
}

func formatAsHexWithLeadingZeros(_ value: UInt64) -> String {
let string = String(value, radix: 16)
if string.count < 16 {
return String(repeating: "0", count: 16 - string.count) + string

Check warning on line 32 in Sources/Hummingbird/Server/RequestID.swift

View check run for this annotation

Codecov / codecov/patch

Sources/Hummingbird/Server/RequestID.swift#L32

Added line #L32 was not covered by tests
} else {
return string
}
}

private static let high = String(UInt64.random(in: .min ... .max), radix: 16)
private static let globalRequestID = ManagedAtomic<UInt64>(UInt64.random(in: .min ... .max))
}
7 changes: 3 additions & 4 deletions Tests/HummingbirdRouterTests/RouterTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -399,15 +399,14 @@ final class RouterTests: XCTestCase {
}
let app = HBApplication(responder: router)
try await app.test(.router) { client in
let idString = try await client.XCTExecute(uri: "/id", method: .get) { response -> String in
let id = try await client.XCTExecute(uri: "/id", method: .get) { response -> String in
let body = try XCTUnwrap(response.body)
return String(buffer: body)
}
let id = try XCTUnwrap(Int(idString))
try await client.XCTExecute(uri: "/id", method: .get) { response in
let body = try XCTUnwrap(response.body)
let id2 = Int(String(buffer: body))
XCTAssertEqual(id2, id + 1)
let id2 = String(buffer: body)
XCTAssertNotEqual(id2, id)
}
}
}
Expand Down
7 changes: 3 additions & 4 deletions Tests/HummingbirdTests/RouterTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -341,15 +341,14 @@ final class RouterTests: XCTestCase {
}
let app = HBApplication(responder: router.buildResponder())
try await app.test(.router) { client in
let idString = try await client.XCTExecute(uri: "/id", method: .get) { response -> String in
let id = try await client.XCTExecute(uri: "/id", method: .get) { response -> String in
let body = try XCTUnwrap(response.body)
return String(buffer: body)
}
let id = try XCTUnwrap(Int(idString))
try await client.XCTExecute(uri: "/id", method: .get) { response in
let body = try XCTUnwrap(response.body)
let id2 = Int(String(buffer: body))
XCTAssertEqual(id2, id + 1)
let id2 = String(buffer: body)
XCTAssertNotEqual(id, id2)
}
}
}
Expand Down
Loading