Skip to content

Commit

Permalink
fix(plugin-server): add more fields to rusty-hook payload (#19288)
Browse files Browse the repository at this point in the history
  • Loading branch information
bretthoerner authored Dec 12, 2023
1 parent 150e62b commit e3b2c9f
Showing 1 changed file with 20 additions and 7 deletions.
27 changes: 20 additions & 7 deletions plugin-server/src/worker/plugins/run.ts
Original file line number Diff line number Diff line change
Expand Up @@ -76,16 +76,29 @@ export async function runOnEvent(hub: Hub, event: ProcessedPluginEvent): Promise
const RUSTY_HOOK_BASE_DELAY_MS = 100
const MAX_RUSTY_HOOK_DELAY_MS = 30_000

async function enqueueInRustyHook(hub: Hub, webhook: Webhook, pluginIdStr: string) {
interface RustyWebhookPayload extends Webhook {
team_id: number
plugin_id: number
plugin_config_id: number
}

async function enqueueInRustyHook(hub: Hub, webhook: Webhook, pluginConfig: PluginConfig) {
webhook.method ??= 'POST'
webhook.headers ??= {}
const body = JSON.stringify(webhook, undefined, 4)
let attempt = 0

const rustyWebhookPayload: RustyWebhookPayload = {
...webhook,
team_id: pluginConfig.team_id,
plugin_id: pluginConfig.plugin_id,
plugin_config_id: pluginConfig.id,
}
const body = JSON.stringify(rustyWebhookPayload, undefined, 4)

// We attempt to enqueue into the rusty-hook service until we succeed. This is deliberatly
// designed to block up the consumer if rusty-hook is down or if we deploy code that
// sends malformed requests. The entire purpose of rusty-hook is to reliably deliver webhooks,
// so we'd rather leave items in the Kafka topic until we manage to get them into rusty-hook.
let attempt = 0
while (true) {
const timer = new Date()
try {
Expand All @@ -103,7 +116,7 @@ async function enqueueInRustyHook(hub: Hub, webhook: Webhook, pluginIdStr: strin
if (response.ok) {
// Success, exit the loop.
pluginActionMsSummary
.labels(pluginIdStr, 'enqueueRustyHook', 'success')
.labels(pluginConfig.plugin_id.toString(), 'enqueueRustyHook', 'success')
.observe(new Date().getTime() - timer.getTime())

break
Expand All @@ -113,10 +126,10 @@ async function enqueueInRustyHook(hub: Hub, webhook: Webhook, pluginIdStr: strin
throw new Error(`rusty-hook returned ${response.status} ${response.statusText}: ${await response.text()}`)
} catch (error) {
pluginActionMsSummary
.labels(pluginIdStr, 'enqueueRustyHook', 'error')
.labels(pluginConfig.plugin_id.toString(), 'enqueueRustyHook', 'error')
.observe(new Date().getTime() - timer.getTime())

const redactedWebhook = { ...webhook, body: '<redacted>' }
const redactedWebhook = { ...rustyWebhookPayload, body: '<redacted>' }
status.error('🔴', 'Webhook enqueue to rusty-hook failed', { error, redactedWebhook, attempt })
Sentry.captureException(error, { extra: { redactedWebhook } })
}
Expand Down Expand Up @@ -155,7 +168,7 @@ async function runSingleTeamPluginComposeWebhook(
}

if (hub.rustyHookForTeams?.(event.team_id)) {
return await enqueueInRustyHook(hub, webhook, pluginConfig.plugin?.id.toString() ?? '?')
return await enqueueInRustyHook(hub, webhook, pluginConfig)
}

const request = await trackedFetch(webhook.url, {
Expand Down

0 comments on commit e3b2c9f

Please sign in to comment.