Skip to content

Commit f3bcbca

Browse files
committed
chore: add logging to inbox
1 parent d7c650b commit f3bcbca

File tree

1 file changed

+48
-1
lines changed

1 file changed

+48
-1
lines changed

src/inbox.ts

+48-1
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import * as vscode from "vscode"
55
import { WebSocket } from "ws"
66
import { errToStr } from "./api-helper"
77
import { type Storage } from "./storage"
8+
import { getMemoryLogger } from "./memoryLogger"
89

910
// These are the template IDs of our notifications.
1011
// Maybe in the future we should avoid hardcoding
@@ -16,9 +17,16 @@ export class Inbox implements vscode.Disposable {
1617
readonly #storage: Storage
1718
#disposed = false
1819
#socket: WebSocket
20+
#messageCount = 0
21+
#workspaceId: string
1922

2023
constructor(workspace: Workspace, httpAgent: ProxyAgent, restClient: Api, storage: Storage) {
24+
const logger = getMemoryLogger()
2125
this.#storage = storage
26+
this.#workspaceId = workspace.id
27+
28+
logger.trackResourceCreated("InboxWebSocket", workspace.id)
29+
logger.info(`Creating inbox for workspace: ${workspace.owner_name}/${workspace.name} (${workspace.id})`)
2230

2331
const baseUrlRaw = restClient.getAxiosInstance().defaults.baseURL
2432
if (!baseUrlRaw) {
@@ -37,6 +45,8 @@ export class Inbox implements vscode.Disposable {
3745
const socketProto = baseUrl.protocol === "https:" ? "wss:" : "ws:"
3846
const socketUrl = `${socketProto}//${baseUrl.host}/api/v2/notifications/inbox/watch?format=plaintext&templates=${watchTemplatesParam}&targets=${watchTargetsParam}`
3947

48+
logger.debug(`Connecting to inbox WebSocket at: ${socketUrl}`)
49+
4050
const coderSessionTokenHeader = "Coder-Session-Token"
4151
this.#socket = new WebSocket(new URL(socketUrl), {
4252
followRedirects: true,
@@ -49,35 +59,72 @@ export class Inbox implements vscode.Disposable {
4959
})
5060

5161
this.#socket.on("open", () => {
62+
logger.info(`Inbox WebSocket connection opened for workspace: ${workspace.id}`)
5263
this.#storage.writeToCoderOutputChannel("Listening to Coder Inbox")
5364
})
5465

5566
this.#socket.on("error", (error) => {
67+
logger.error(`Inbox WebSocket error for workspace: ${workspace.id}`, error)
5668
this.notifyError(error)
5769
this.dispose()
5870
})
5971

72+
this.#socket.on("close", (code, reason) => {
73+
logger.info(`Inbox WebSocket closed for workspace: ${workspace.id}, code: ${code}, reason: ${reason || "none"}`)
74+
if (!this.#disposed) {
75+
this.dispose()
76+
}
77+
})
78+
6079
this.#socket.on("message", (data) => {
80+
this.#messageCount++
81+
82+
// Log periodic message stats
83+
if (this.#messageCount % 10 === 0) {
84+
logger.info(`Inbox received ${this.#messageCount} messages for workspace: ${workspace.id}`)
85+
logger.logMemoryUsage("INBOX_WEBSOCKET")
86+
}
87+
6188
try {
6289
const inboxMessage = JSON.parse(data.toString()) as GetInboxNotificationResponse
63-
90+
logger.debug(`Inbox notification received: ${inboxMessage.notification.title}`)
6491
vscode.window.showInformationMessage(inboxMessage.notification.title)
6592
} catch (error) {
93+
logger.error(`Error processing inbox message for workspace: ${workspace.id}`, error)
6694
this.notifyError(error)
6795
}
6896
})
97+
98+
// Log memory stats periodically
99+
const memoryInterval = setInterval(
100+
() => {
101+
if (!this.#disposed) {
102+
logger.logMemoryUsage("INBOX_PERIODIC")
103+
} else {
104+
clearInterval(memoryInterval)
105+
}
106+
},
107+
5 * 60 * 1000,
108+
) // Every 5 minutes
69109
}
70110

71111
dispose() {
112+
const logger = getMemoryLogger()
113+
72114
if (!this.#disposed) {
115+
logger.info(`Disposing inbox for workspace: ${this.#workspaceId} after ${this.#messageCount} messages`)
73116
this.#storage.writeToCoderOutputChannel("No longer listening to Coder Inbox")
74117
this.#socket.close()
75118
this.#disposed = true
119+
logger.trackResourceDisposed("InboxWebSocket", this.#workspaceId)
76120
}
77121
}
78122

79123
private notifyError(error: unknown) {
124+
const logger = getMemoryLogger()
80125
const message = errToStr(error, "Got empty error while monitoring Coder Inbox")
126+
127+
logger.error(`Inbox error for workspace: ${this.#workspaceId}`, error)
81128
this.#storage.writeToCoderOutputChannel(message)
82129
}
83130
}

0 commit comments

Comments
 (0)