Skip to content

Commit 33fa4c5

Browse files
authored
Merge pull request #951 from ahoppen/ahoppen/signpost
Emit signposts for request handling
2 parents cc40dfb + 47e7a13 commit 33fa4c5

File tree

4 files changed

+58
-1
lines changed

4 files changed

+58
-1
lines changed

Sources/LSPLogging/Logging.swift

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,9 +27,17 @@ import os // os_log
2727

2828
public typealias LogLevel = os.OSLogType
2929
public typealias Logger = os.Logger
30+
public typealias Signposter = OSSignposter
31+
32+
extension os.Logger {
33+
public func makeSignposter() -> Signposter {
34+
return OSSignposter(logger: self)
35+
}
36+
}
3037
#else
3138
public typealias LogLevel = NonDarwinLogLevel
3239
public typealias Logger = NonDarwinLogger
40+
public typealias Signposter = NonDarwinSignposter
3341
#endif
3442

3543
/// The logger that is used to log any messages.

Sources/LSPLogging/NonDarwinLogging.swift

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -340,4 +340,35 @@ public struct NonDarwinLogger {
340340
public static func flush() {
341341
loggingQueue.sync {}
342342
}
343+
344+
public func makeSignposter() -> NonDarwinSignposter {
345+
return NonDarwinSignposter()
346+
}
347+
}
348+
349+
// MARK: - Signposter
350+
351+
public struct NonDarwinSignpostID {}
352+
353+
public struct NonDarwinSignpostIntervalState {}
354+
355+
/// A type that is API-compatible to `OSLogMessage` for all uses within sourcekit-lsp.
356+
///
357+
/// Since non-Darwin platforms don't have signposts, the type just has no-op operations.
358+
public struct NonDarwinSignposter {
359+
public func makeSignpostID() -> NonDarwinSignpostID {
360+
return NonDarwinSignpostID()
361+
}
362+
363+
public func beginInterval(
364+
_ name: StaticString,
365+
id: NonDarwinSignpostID,
366+
_ message: NonDarwinLogMessage
367+
) -> NonDarwinSignpostIntervalState {
368+
return NonDarwinSignpostIntervalState()
369+
}
370+
371+
public func emitEvent(_ name: StaticString, id: NonDarwinSignpostID, _ message: NonDarwinLogMessage = "") {}
372+
373+
public func endInterval(_ name: StaticString, _ state: NonDarwinSignpostIntervalState, _ message: StaticString) {}
343374
}

Sources/SourceKitD/SourceKitD.swift

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,10 @@ extension SourceKitD {
6868
public func send(_ req: SKDRequestDictionary) async throws -> SKDResponseDictionary {
6969
logRequest(req)
7070

71+
let signposter = logger.makeSignposter()
72+
let signpostID = signposter.makeSignpostID()
73+
let signposterState = signposter.beginInterval("sourcekitd-request", id: signpostID, "Start")
74+
7175
let sourcekitdResponse: SKDResponse = try await withCancellableCheckedThrowingContinuation { continuation in
7276
var handle: sourcekitd_request_handle_t? = nil
7377
api.send_request(req.dict, &handle) { _resp in
@@ -81,9 +85,11 @@ extension SourceKitD {
8185
logResponse(sourcekitdResponse)
8286

8387
guard let dict = sourcekitdResponse.value else {
88+
signposter.endInterval("sourcekitd-request", signposterState, "Error")
8489
throw sourcekitdResponse.error!
8590
}
8691

92+
signposter.endInterval("sourcekitd-request", signposterState, "Done")
8793
return dict
8894
}
8995
}

Sources/SourceKitLSP/SourceKitServer.swift

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -650,11 +650,17 @@ extension SourceKitServer: MessageHandler {
650650
self.cancelRequest(params)
651651
}
652652

653+
let notificationID = getNextNotificationIDForLogging()
654+
655+
let signposter = Logger(subsystem: subsystem, category: "notification-\(notificationID)").makeSignposter()
656+
let signpostID = signposter.makeSignpostID()
657+
let state = signposter.beginInterval("Notification", id: signpostID, "\(type(of: params))")
653658
messageHandlingQueue.async(metadata: TaskMetadata(params)) {
654-
let notificationID = getNextNotificationIDForLogging()
659+
signposter.emitEvent("Start handling", id: signpostID)
655660

656661
await withLoggingScope("notification-\(notificationID)") {
657662
await self.handleImpl(params, from: clientID)
663+
signposter.endInterval("Notification", state, "Done")
658664
}
659665
}
660666
}
@@ -698,9 +704,15 @@ extension SourceKitServer: MessageHandler {
698704
from clientID: ObjectIdentifier,
699705
reply: @escaping (LSPResult<R.Response>) -> Void
700706
) {
707+
let signposter = Logger(subsystem: subsystem, category: "request-\(id)").makeSignposter()
708+
let signpostID = signposter.makeSignpostID()
709+
let state = signposter.beginInterval("Request", id: signpostID, "\(R.self)")
710+
701711
let task = messageHandlingQueue.async(metadata: TaskMetadata(params)) {
712+
signposter.emitEvent("Start handling", id: signpostID)
702713
await withLoggingScope("request-\(id)") {
703714
await self.handleImpl(params, id: id, from: clientID, reply: reply)
715+
signposter.endInterval("Request", state, "Done")
704716
}
705717
// We have handled the request and can't cancel it anymore.
706718
// Stop keeping track of it to free the memory.

0 commit comments

Comments
 (0)