Skip to content

Commit

Permalink
fix: telemetry network error
Browse files Browse the repository at this point in the history
was causing an uncaught promise rejection error
  • Loading branch information
Roy Razon committed Jul 24, 2023
1 parent cb6dc28 commit 0d21bab
Showing 1 changed file with 19 additions and 16 deletions.
35 changes: 19 additions & 16 deletions packages/core/src/telemetry/emitter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import stringify from 'fast-safe-stringify'
import fetch from 'node-fetch'
import { debounce } from 'lodash'
import pLimit from 'p-limit'
import { inspect } from 'util'
import { memoizedMachineId } from './machine-id'
import { TelemetryEvent, TelemetryProperties, serializableEvent } from './events'
import { detectCiProvider } from '../ci-providers'
Expand Down Expand Up @@ -31,24 +32,26 @@ export const telemetryEmitter = async ({ dataDir, version, debug }: {
const runId = newRunId()
let debounceDisabled = false
const flushLimit = pLimit(1)
const flush = async () =>
await flushLimit(async () => {
if (!pendingEvents.length) {
return
}
const body = stringify({ batch: pendingEvents.map(serializableEvent) })
pendingEvents.length = 0
const req = fetch(TELEMETRY_URL, {
headers: { 'Content-Type': 'application/json' },
method: 'POST',
redirect: 'follow',
body,
})
const response = await req
if (!response.ok && debug) {
process.stderr.write(`Error sending telemetry: ${response.status} ${response.statusText} ${response.url}${os.EOL}`)
const flush = async () => await flushLimit(async () => {
if (!pendingEvents.length) {
return
}
const body = stringify({ batch: pendingEvents.map(serializableEvent) })
pendingEvents.length = 0
const response = await fetch(TELEMETRY_URL, {
headers: { 'Content-Type': 'application/json' },
method: 'POST',
redirect: 'follow',
body,
}).catch(err => {
if (debug) {
process.stderr.write(`Error sending telemetry: ${inspect(err)}${os.EOL}`)
}
})
if (response && !response.ok && debug) {
process.stderr.write(`Error response from telemetry: ${response.status} ${response.statusText} ${response.url}${os.EOL}`)
}
})

const debouncedFlush = debounce(flush, 3000, { maxWait: 8000 })

Expand Down

0 comments on commit 0d21bab

Please sign in to comment.