Skip to content

Commit

Permalink
refactor: Sample error to reduce redundent log
Browse files Browse the repository at this point in the history
  • Loading branch information
ci010 committed Apr 22, 2024
1 parent 8bd7b46 commit bd926f3
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 33 deletions.
54 changes: 22 additions & 32 deletions xmcl-runtime/telemetry/pluginTelemetry.ts
Expand Up @@ -90,6 +90,10 @@ export const pluginTelemetry: LauncherAppPlugin = async (app) => {
const e = contextObjects?.error
if (e instanceof Error) {
handleException(exception, e)
if (e.name === 'NodeInternalError') {
// Only log 1/3 of the internal error
envelope.sampleRate = 33
}
}
}
return true
Expand All @@ -108,20 +112,21 @@ export const pluginTelemetry: LauncherAppPlugin = async (app) => {
})

app.on('service-call-end', (serviceName, serviceMethod, duration, success) => {
const shouldTrack = () => {
if (serviceName === 'LaunchService' && serviceMethod === 'launch') return true
if (serviceName === 'UserSerivce' && serviceMethod === 'refreshUser') return true
return false
}
if (shouldTrack()) {
client.trackRequest({
name: `${serviceName}.${serviceMethod}`,
url: `/${serviceName}/${serviceMethod}`,
resultCode: success ? 200 : 500,
duration,
success,
})
}
// Disable request to reduce cost
// const shouldTrack = () => {
// if (serviceName === 'LaunchService' && serviceMethod === 'launch') return true
// if (serviceName === 'UserSerivce' && serviceMethod === 'refreshUser') return true
// return false
// }
// if (shouldTrack()) {
// client.trackRequest({
// name: `${serviceName}.${serviceMethod}`,
// url: `/${serviceName}/${serviceMethod}`,
// resultCode: success ? 200 : 500,
// duration,
// success,
// })
// }
})

app.on('engine-ready', async () => {
Expand Down Expand Up @@ -244,26 +249,11 @@ export const pluginTelemetry: LauncherAppPlugin = async (app) => {
}
})

process.on('uncaughtException', (e) => {
if (settings.disableTelemetry) return
if (client) {
client.trackException({
exception: e,
properties: e ? { ...e } : undefined,
})
}
})
process.on('unhandledRejection', (e) => {
if (settings.disableTelemetry) return
if (client) {
client.trackException({
exception: e as any, // the applicationinsights will convert it to error automatically
properties: e ? { ...e } : undefined,
})
}
})
app.logEmitter.on('failure', (destination, tag, e: Error) => {
if (settings.disableTelemetry) return
if (e.name === 'NodeInternalError') {

}
client.trackException({
exception: e,
properties: e ? { ...e } : undefined,
Expand Down
2 changes: 1 addition & 1 deletion xmcl-runtime/uncaughtError/decorateError.ts
Expand Up @@ -20,7 +20,7 @@ export function decorateError(e: unknown) {
}
}
}
if (e.stack?.includes('This is caused by either a bug in Node.js or incorrect usage of Node.js internals.')) {
if (e.message?.includes('This is caused by either a bug in Node.js or incorrect usage of Node.js internals')) {
e.name = 'NodeInternalError'
}
}
Expand Down

0 comments on commit bd926f3

Please sign in to comment.