From 3b83955bb0f9ab71f76d28451fb0b4d07fff6c6f Mon Sep 17 00:00:00 2001 From: Prithvish Date: Tue, 4 Nov 2025 06:52:58 +0530 Subject: [PATCH] add webhook debugging logs --- src/worker/queues/send-webhook-queue.ts | 35 ++++++++++++-- src/worker/tasks/send-webhook-worker.ts | 64 ++++++++++++++++++++++++- 2 files changed, 93 insertions(+), 6 deletions(-) diff --git a/src/worker/queues/send-webhook-queue.ts b/src/worker/queues/send-webhook-queue.ts index 6259670c..410f6053 100644 --- a/src/worker/queues/send-webhook-queue.ts +++ b/src/worker/queues/send-webhook-queue.ts @@ -13,6 +13,7 @@ import { import { getWebhooksByEventType } from "../../shared/utils/cache/get-webhook"; import { redis } from "../../shared/utils/redis/redis"; import { defaultJobOptions } from "./queues"; +import { logger } from "../../shared/utils/logger"; export type EnqueueContractSubscriptionWebhookData = { type: WebhooksEventTypes.CONTRACT_SUBSCRIPTION; @@ -137,15 +138,41 @@ export class SendWebhookQueue { ...(await getWebhooksByEventType(data.type)), ]; + logger({ + service: "worker", + level: "info", + message: `[Webhook] Enqueuing transaction webhooks to queue for transaction ${data.queueId}`, + queueId: data.queueId, + data: { + eventType: data.type, + webhookCount: webhooks.length, + }, + }); + for (const webhook of webhooks) { const job: WebhookJob = { data, webhook }; const serialized = SuperJSON.stringify(job); + const idempotencyKey = this._getTransactionWebhookIdempotencyKey({ + webhook, + eventType: data.type, + queueId: data.queueId, + }); + await this.q.add(`${data.type}:${webhook.id}`, serialized, { - jobId: this._getTransactionWebhookIdempotencyKey({ - webhook, + jobId: idempotencyKey, + }); + + logger({ + service: "worker", + level: "info", + message: `[Webhook] Transaction webhook added to queue for transaction ${data.queueId} at destination ${webhook.url}`, + queueId: data.queueId, + data: { eventType: data.type, - queueId: data.queueId, - }), + destination: webhook.url, + webhookId: webhook.id, + idempotencyKey, + }, }); } }; diff --git a/src/worker/tasks/send-webhook-worker.ts b/src/worker/tasks/send-webhook-worker.ts index 7b2037c2..ef1eaee3 100644 --- a/src/worker/tasks/send-webhook-worker.ts +++ b/src/worker/tasks/send-webhook-worker.ts @@ -28,6 +28,27 @@ import { env } from "../../shared/utils/env"; const handler: Processor = async (job: Job) => { const { data, webhook } = superjson.parse(job.data); + // Extract transaction ID if available + let transactionId: string | undefined; + if ("queueId" in data) { + transactionId = data.queueId; + } + + // Log webhook attempt with HMAC info + const hmacMode = env.ENABLE_CUSTOM_HMAC_AUTH ? "custom" : "standard"; + logger({ + service: "worker", + level: "info", + message: `[Webhook] Attempting to send webhook for transaction ${transactionId} at destination ${webhook.url}`, + queueId: transactionId, + data: { + eventType: data.type, + destination: webhook.url, + webhookId: webhook.id, + hmacMode, + }, + }); + let resp: WebhookResponse | undefined; switch (data.type) { case WebhooksEventTypes.CONTRACT_SUBSCRIPTION: { @@ -61,6 +82,17 @@ const handler: Processor = async (job: Job) => { const transaction = await TransactionDB.get(data.queueId); if (!transaction) { job.log("Transaction not found."); + logger({ + service: "worker", + level: "warn", + message: `[Webhook] Transaction not found for webhook`, + queueId: data.queueId, + data: { + eventType: data.type, + destination: webhook.url, + webhookId: webhook.id, + }, + }); return; } const webhookBody: Static = @@ -85,6 +117,26 @@ const handler: Processor = async (job: Job) => { } } + // Log the response + if (resp) { + const logLevel = resp.ok ? "info" : resp.status >= 500 ? "error" : "warn"; + logger({ + service: "worker", + level: logLevel, + message: `[Webhook] Webhook response received: ${resp.status} for transaction ${transactionId} at destination ${webhook.url}`, + queueId: transactionId, + data: { + eventType: data.type, + destination: webhook.url, + webhookId: webhook.id, + responseCode: resp.status, + responseOk: resp.ok, + hmacMode, + responseBody: resp.body.substring(0, 200), // Truncate response body to first 200 chars + }, + }); + } + // Throw on 5xx so it remains in the queue to retry later. if (resp && resp.status >= 500) { const error = new Error( @@ -92,9 +144,17 @@ const handler: Processor = async (job: Job) => { ); job.log(error.message); logger({ - level: "debug", - message: error.message, + level: "error", + message: `[Webhook] 5xx error, will retry`, service: "worker", + queueId: transactionId, + data: { + eventType: data.type, + destination: webhook.url, + webhookId: webhook.id, + responseCode: resp.status, + hmacMode, + }, }); throw error; }