0
0
mirror of https://github.com/PostHog/posthog.git synced 2024-12-01 12:21:02 +01:00

Add teamId tag for plugin metrics (#8474)

* Add teamId tag for plugin metrics

* handle optional better

* Catch errors on topic creation if topic exists
This commit is contained in:
James Greenhill 2022-02-07 17:26:09 -08:00 committed by GitHub
parent 0faec5b7c1
commit abc1de3972
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 43 additions and 14 deletions

View File

@ -23,9 +23,13 @@ export async function runOnEvent(server: Hub, event: PluginEvent): Promise<void>
await onEvent(event)
} catch (error) {
await processError(server, pluginConfig, error, event)
server.statsd?.increment(`plugin.${pluginConfig.plugin?.name}.on_event.ERROR`)
server.statsd?.increment(`plugin.${pluginConfig.plugin?.name}.on_event.ERROR`, {
teamId: event.team_id.toString(),
})
}
server.statsd?.timing(`plugin.${pluginConfig.plugin?.name}.on_event`, timer)
server.statsd?.timing(`plugin.${pluginConfig.plugin?.name}.on_event`, timer, {
teamId: event.team_id.toString(),
})
captureTimeSpentRunning(event.team_id, timer, 'onEvent')
}
})
@ -44,9 +48,13 @@ export async function runOnAction(server: Hub, action: Action, event: PluginEven
await onAction(action, event)
} catch (error) {
await processError(server, pluginConfig, error, event)
server.statsd?.increment(`plugin.${pluginConfig.plugin?.name}.on_action.ERROR`)
server.statsd?.increment(`plugin.${pluginConfig.plugin?.name}.on_action.ERROR`, {
teamId: event.team_id.toString(),
})
}
server.statsd?.timing(`plugin.${pluginConfig.plugin?.name}.on_action`, timer)
server.statsd?.timing(`plugin.${pluginConfig.plugin?.name}.on_action`, timer, {
teamId: event.team_id.toString(),
})
captureTimeSpentRunning(event.team_id, timer, 'onAction')
}
})
@ -65,9 +73,13 @@ export async function runOnSnapshot(server: Hub, event: PluginEvent): Promise<vo
await onSnapshot(event)
} catch (error) {
await processError(server, pluginConfig, error, event)
server.statsd?.increment(`plugin.${pluginConfig.plugin?.name}.on_snapshot.ERROR`)
server.statsd?.increment(`plugin.${pluginConfig.plugin?.name}.on_snapshot.ERROR`, {
teamId: event.team_id.toString(),
})
}
server.statsd?.timing(`plugin.${pluginConfig.plugin?.name}.on_snapshot`, timer)
server.statsd?.timing(`plugin.${pluginConfig.plugin?.name}.on_snapshot`, timer, {
teamId: event.team_id.toString(),
})
captureTimeSpentRunning(event.team_id, timer, 'onSnapshot')
}
})
@ -97,7 +109,9 @@ export async function runProcessEvent(server: Hub, event: PluginEvent): Promise<
pluginsSucceeded.push(`${pluginConfig.plugin?.name} (${pluginConfig.id})`)
} catch (error) {
await processError(server, pluginConfig, error, returnedEvent)
server.statsd?.increment(`plugin.${pluginConfig.plugin?.name}.process_event.ERROR`)
server.statsd?.increment(`plugin.${pluginConfig.plugin?.name}.process_event.ERROR`, {
teamId: String(event.team_id),
})
pluginsFailed.push(`${pluginConfig.plugin?.name} (${pluginConfig.id})`)
}
server.statsd?.timing(`plugin.process_event`, timer, {
@ -150,7 +164,14 @@ export async function runPluginTask(
response = await (payload ? task?.exec(payload) : task?.exec())
} catch (error) {
await processError(server, pluginConfig || null, error)
server.statsd?.increment(`plugin.task.${taskType}.${taskName}.${pluginConfigId}.ERROR`)
let teamIdStr = '?'
if (pluginConfig != null) {
teamIdStr = pluginConfig.team_id.toString()
}
server.statsd?.increment(`plugin.task.${taskType}.${taskName}.${pluginConfigId}.ERROR`, {
teamId: teamIdStr,
})
}
captureTimeSpentRunning(pluginConfig?.team_id || 0, timer, 'pluginTask')
return response
@ -169,9 +190,13 @@ export async function runHandleAlert(server: Hub, alert: Alert): Promise<void> {
await handleAlert(alert)
} catch (error) {
await processError(server, pluginConfig, error)
server.statsd?.increment(`plugin.${pluginConfig.plugin?.name}.handle_alert.ERROR`)
server.statsd?.increment(`plugin.${pluginConfig.plugin?.name}.handle_alert.ERROR`, {
teamId: pluginConfig.team_id.toString(),
})
}
server.statsd?.timing(`plugin.${pluginConfig.plugin?.name}.handle_alert`, timer)
server.statsd?.timing(`plugin.${pluginConfig.plugin?.name}.handle_alert`, timer, {
teamId: pluginConfig.team_id.toString(),
})
captureTimeSpentRunning(pluginConfig.team_id, timer, 'handleAlert')
}
})

View File

@ -88,9 +88,13 @@ export async function resetKafka(extraServerConfig: Partial<PluginsServerConfig>
async function createTopics(kafka: Kafka, topics: string[]) {
const admin = kafka.admin()
await admin.connect()
await admin.createTopics({
waitForLeaders: true,
topics: topics.map((topic) => ({ topic })),
})
try {
await admin.createTopics({
waitForLeaders: true,
topics: topics.map((topic) => ({ topic })),
})
} catch (error) {
console.log("Error creating topics. Probably they're already created.\n", error)
}
await admin.disconnect()
}