Skip to content

Add a webhook log success field to allow filtering in the UI #2061

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 2 commits into from
Feb 16, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
1 change: 1 addition & 0 deletions packages/api/src/controllers/webhook-log.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ const requestsFieldsMap: FieldsMap = {
userId: `webhook_log.data->>'userId'`,
event: `webhook_log.data->>'event'`,
statusCode: `webhook_log.data->'response'->>'status'`,
success: `webhook_log.data->>'success'`,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure how simple it would be, but you can consider adding unit test for this case.

resourceId: {
val: `webhook_log.data->'request'->>'body'`,
type: "full-text",
Expand Down
3 changes: 3 additions & 0 deletions packages/api/src/schema/api-schema.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -244,6 +244,9 @@ components:
duration:
type: number
description: The time taken (in seconds) to make the webhook request
success:
type: boolean
description: Whether the webhook request was successful
request:
type: object
properties:
Expand Down
2 changes: 2 additions & 0 deletions packages/api/src/schema/db-schema.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -614,6 +614,8 @@ components:
index: true
createdAt:
index: true
success:
index: true
response:
type: object
properties:
Expand Down
28 changes: 19 additions & 9 deletions packages/api/src/webhooks/cannon.ts
Original file line number Diff line number Diff line change
Expand Up @@ -413,7 +413,7 @@ export default class WebhookCannon {
responseBody = await resp.text();
statusCode = resp.status;

if (resp.status >= 200 && resp.status < 300) {
if (isSuccess(resp)) {
// 2xx requests are cool. all is good
logger.info(`webhook ${webhook.id} fired successfully`);
return true;
Expand Down Expand Up @@ -449,7 +449,7 @@ export default class WebhookCannon {
);
} catch (e) {
console.log(
`Unable to store response of webhook ${webhook.id} url: ${webhook.url}`
`Unable to store response of webhook ${webhook.id} url: ${webhook.url} error: ${e}`
);
}
await this.storeTriggerStatus(
Expand Down Expand Up @@ -585,6 +585,10 @@ export default class WebhookCannon {
}
}

function isSuccess(resp: Response) {
return resp?.status >= 200 && resp?.status < 300;
}

async function storeResponse(
webhook: DBWebhook,
eventId: string,
Expand All @@ -596,9 +600,12 @@ async function storeResponse(
params
): Promise<WebhookLog> {
const hrDuration = process.hrtime(startTime);
const encodedResponseBody = Buffer.from(
responseBody.substring(0, 1024)
).toString("base64");
let encodedResponseBody = "";
if (responseBody) {
encodedResponseBody = Buffer.from(responseBody.substring(0, 1024)).toString(
"base64"
);
}

const webhookLog = {
id: uuid(),
Expand All @@ -608,11 +615,12 @@ async function storeResponse(
userId: webhook.userId,
createdAt: Date.now(),
duration: hrDuration[0] + hrDuration[1] / 1e9,
success: isSuccess(resp),
response: {
body: encodedResponseBody,
redirected: resp.redirected,
status: resp.status,
statusText: resp.statusText,
redirected: resp?.redirected,
status: resp?.status,
statusText: resp?.statusText,
},
request: {
url: webhook.url,
Expand Down Expand Up @@ -694,7 +702,9 @@ export async function storeTriggerStatus(
): Promise<void> {
try {
let status: DBWebhook["status"] = { lastTriggeredAt: triggerTime };
let encodedResponseBody = Buffer.from(responseBody).toString("base64");
let encodedResponseBody = responseBody
? Buffer.from(responseBody).toString("base64")
: "";
if (statusCode >= 300 || !statusCode) {
status = {
...status,
Expand Down