Skip to content

Set the logging scope for notification handling one level higher so it’s also set for cancel notifications #1406

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

Merged
merged 1 commit into from
Jun 3, 2024
Merged
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
35 changes: 17 additions & 18 deletions Sources/SourceKitLSP/SourceKitLSPServer.swift
Original file line number Diff line number Diff line change
Expand Up @@ -532,26 +532,25 @@ private nonisolated(unsafe) var notificationIDForLogging = AtomicUInt32(initialV

extension SourceKitLSPServer: MessageHandler {
public nonisolated func handle(_ params: some NotificationType) {
if let params = params as? CancelRequestNotification {
// Request cancellation needs to be able to overtake any other message we
// are currently handling. Ordering is not important here. We thus don't
// need to execute it on `messageHandlingQueue`.
self.cancelRequest(params)
}

let notificationID = notificationIDForLogging.fetchAndIncrement()
withLoggingScope("notification-\(notificationID % 100)") {
if let params = params as? CancelRequestNotification {
// Request cancellation needs to be able to overtake any other message we
// are currently handling. Ordering is not important here. We thus don't
// need to execute it on `messageHandlingQueue`.
self.cancelRequest(params)
}

let signposter = Logger(subsystem: LoggingScope.subsystem, category: "message-handling")
.makeSignposter()
let signpostID = signposter.makeSignpostID()
let state = signposter.beginInterval("Notification", id: signpostID, "\(type(of: params))")
messageHandlingQueue.async(metadata: MessageHandlingDependencyTracker(params)) {
signposter.emitEvent("Start handling", id: signpostID)
let signposter = Logger(subsystem: LoggingScope.subsystem, category: "message-handling")
.makeSignposter()
let signpostID = signposter.makeSignpostID()
let state = signposter.beginInterval("Notification", id: signpostID, "\(type(of: params))")
messageHandlingQueue.async(metadata: MessageHandlingDependencyTracker(params)) {
signposter.emitEvent("Start handling", id: signpostID)

// Only use the last two digits of the notification ID for the logging scope to avoid creating too many scopes.
// See comment in `withLoggingScope`.
// The last 2 digits should be sufficient to differentiate between multiple concurrently running notifications.
await withLoggingScope("notification-\(notificationID % 100)") {
// Only use the last two digits of the notification ID for the logging scope to avoid creating too many scopes.
// See comment in `withLoggingScope`.
// The last 2 digits should be sufficient to differentiate between multiple concurrently running notifications.
await self.handleImpl(params)
signposter.endInterval("Notification", state, "Done")
}
Expand Down Expand Up @@ -2406,7 +2405,7 @@ fileprivate extension RequestID {
var numericValue: Int {
switch self {
case .number(let number): return number
case .string(let string): return Int(string) ?? string.hashValue
case .string(let string): return Int(string) ?? abs(string.hashValue)
}
}
}
Expand Down