Skip to content

Commit 191708f

Browse files
authored
add webhook debugging logs (#924)
1 parent b9a20f8 commit 191708f

File tree

2 files changed

+93
-6
lines changed

2 files changed

+93
-6
lines changed

src/worker/queues/send-webhook-queue.ts

Lines changed: 31 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import {
1313
import { getWebhooksByEventType } from "../../shared/utils/cache/get-webhook";
1414
import { redis } from "../../shared/utils/redis/redis";
1515
import { defaultJobOptions } from "./queues";
16+
import { logger } from "../../shared/utils/logger";
1617

1718
export type EnqueueContractSubscriptionWebhookData = {
1819
type: WebhooksEventTypes.CONTRACT_SUBSCRIPTION;
@@ -137,15 +138,41 @@ export class SendWebhookQueue {
137138
...(await getWebhooksByEventType(data.type)),
138139
];
139140

141+
logger({
142+
service: "worker",
143+
level: "info",
144+
message: `[Webhook] Enqueuing transaction webhooks to queue for transaction ${data.queueId}`,
145+
queueId: data.queueId,
146+
data: {
147+
eventType: data.type,
148+
webhookCount: webhooks.length,
149+
},
150+
});
151+
140152
for (const webhook of webhooks) {
141153
const job: WebhookJob = { data, webhook };
142154
const serialized = SuperJSON.stringify(job);
155+
const idempotencyKey = this._getTransactionWebhookIdempotencyKey({
156+
webhook,
157+
eventType: data.type,
158+
queueId: data.queueId,
159+
});
160+
143161
await this.q.add(`${data.type}:${webhook.id}`, serialized, {
144-
jobId: this._getTransactionWebhookIdempotencyKey({
145-
webhook,
162+
jobId: idempotencyKey,
163+
});
164+
165+
logger({
166+
service: "worker",
167+
level: "info",
168+
message: `[Webhook] Transaction webhook added to queue for transaction ${data.queueId} at destination ${webhook.url}`,
169+
queueId: data.queueId,
170+
data: {
146171
eventType: data.type,
147-
queueId: data.queueId,
148-
}),
172+
destination: webhook.url,
173+
webhookId: webhook.id,
174+
idempotencyKey,
175+
},
149176
});
150177
}
151178
};

src/worker/tasks/send-webhook-worker.ts

Lines changed: 62 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,27 @@ import { env } from "../../shared/utils/env";
2828
const handler: Processor<string, void, string> = async (job: Job<string>) => {
2929
const { data, webhook } = superjson.parse<WebhookJob>(job.data);
3030

31+
// Extract transaction ID if available
32+
let transactionId: string | undefined;
33+
if ("queueId" in data) {
34+
transactionId = data.queueId;
35+
}
36+
37+
// Log webhook attempt with HMAC info
38+
const hmacMode = env.ENABLE_CUSTOM_HMAC_AUTH ? "custom" : "standard";
39+
logger({
40+
service: "worker",
41+
level: "info",
42+
message: `[Webhook] Attempting to send webhook for transaction ${transactionId} at destination ${webhook.url}`,
43+
queueId: transactionId,
44+
data: {
45+
eventType: data.type,
46+
destination: webhook.url,
47+
webhookId: webhook.id,
48+
hmacMode,
49+
},
50+
});
51+
3152
let resp: WebhookResponse | undefined;
3253
switch (data.type) {
3354
case WebhooksEventTypes.CONTRACT_SUBSCRIPTION: {
@@ -61,6 +82,17 @@ const handler: Processor<string, void, string> = async (job: Job<string>) => {
6182
const transaction = await TransactionDB.get(data.queueId);
6283
if (!transaction) {
6384
job.log("Transaction not found.");
85+
logger({
86+
service: "worker",
87+
level: "warn",
88+
message: `[Webhook] Transaction not found for webhook`,
89+
queueId: data.queueId,
90+
data: {
91+
eventType: data.type,
92+
destination: webhook.url,
93+
webhookId: webhook.id,
94+
},
95+
});
6496
return;
6597
}
6698
const webhookBody: Static<typeof TransactionSchema> =
@@ -85,16 +117,44 @@ const handler: Processor<string, void, string> = async (job: Job<string>) => {
85117
}
86118
}
87119

120+
// Log the response
121+
if (resp) {
122+
const logLevel = resp.ok ? "info" : resp.status >= 500 ? "error" : "warn";
123+
logger({
124+
service: "worker",
125+
level: logLevel,
126+
message: `[Webhook] Webhook response received: ${resp.status} for transaction ${transactionId} at destination ${webhook.url}`,
127+
queueId: transactionId,
128+
data: {
129+
eventType: data.type,
130+
destination: webhook.url,
131+
webhookId: webhook.id,
132+
responseCode: resp.status,
133+
responseOk: resp.ok,
134+
hmacMode,
135+
responseBody: resp.body.substring(0, 200), // Truncate response body to first 200 chars
136+
},
137+
});
138+
}
139+
88140
// Throw on 5xx so it remains in the queue to retry later.
89141
if (resp && resp.status >= 500) {
90142
const error = new Error(
91143
`Received status ${resp.status} from webhook ${webhook.url}.`,
92144
);
93145
job.log(error.message);
94146
logger({
95-
level: "debug",
96-
message: error.message,
147+
level: "error",
148+
message: `[Webhook] 5xx error, will retry`,
97149
service: "worker",
150+
queueId: transactionId,
151+
data: {
152+
eventType: data.type,
153+
destination: webhook.url,
154+
webhookId: webhook.id,
155+
responseCode: resp.status,
156+
hmacMode,
157+
},
98158
});
99159
throw error;
100160
}

0 commit comments

Comments
 (0)