axios+telemetry cleanup

This commit is contained in:
2026-04-02 15:19:11 +03:00
parent a3cbca1e11
commit 7e1eac8002
100 changed files with 3048 additions and 4491 deletions

View File

@@ -7,7 +7,7 @@
*/
import { feature } from 'bun:bundle'
import axios from 'axios'
import { isHttpError, nativeRequest } from '../http.js'
import { createHash } from 'crypto'
import { chmod, writeFile } from 'fs/promises'
import { join } from 'path'
@@ -77,24 +77,25 @@ export async function getLatestVersionFromBinaryRepo(
authConfig?: { auth: { username: string; password: string } },
): Promise<string> {
const startTime = Date.now()
try {
const response = await axios.get(`${baseUrl}/${channel}`, {
timeout: 30000,
responseType: 'text',
...authConfig,
})
const latencyMs = Date.now() - startTime
logEvent('tengu_version_check_success', {
latency_ms: latencyMs,
})
return response.data.trim()
} catch (error) {
const latencyMs = Date.now() - startTime
const errorMessage = error instanceof Error ? error.message : String(error)
let httpStatus: number | undefined
if (axios.isAxiosError(error) && error.response) {
httpStatus = error.response.status
}
try {
const response = await nativeRequest<string>(`${baseUrl}/${channel}`, {
timeout: 30000,
responseType: 'text',
...(authConfig?.auth ? {
headers: {
Authorization: `Basic ${Buffer.from(`${authConfig.auth.username}:${authConfig.auth.password}`).toString('base64')}`,
},
} : {}),
})
const latencyMs = Date.now() - startTime
logEvent('tengu_version_check_success', {
latency_ms: latencyMs,
})
return response.data.trim()
} catch (error) {
const latencyMs = Date.now() - startTime
const errorMessage = error instanceof Error ? error.message : String(error)
const httpStatus = isHttpError(error) ? error.status : undefined
logEvent('tengu_version_check_failure', {
latency_ms: latencyMs,
@@ -318,22 +319,18 @@ async function downloadAndVerifyBinary(
// Start the stall timer before the request
resetStallTimer()
const response = await axios.get(binaryUrl, {
const response = await nativeRequest<ArrayBuffer>(binaryUrl, {
timeout: 5 * 60000, // 5 minute total timeout
responseType: 'arraybuffer',
signal: controller.signal,
onDownloadProgress: () => {
// Reset stall timer on each chunk of data received
resetStallTimer()
},
...requestConfig,
...(requestConfig?.headers ? { headers: requestConfig.headers } : {}),
})
clearStallTimer()
// Verify checksum
const hash = createHash('sha256')
hash.update(response.data)
hash.update(Buffer.from(response.data))
const actualChecksum = hash.digest('hex')
if (actualChecksum !== expectedChecksum) {
@@ -351,8 +348,8 @@ async function downloadAndVerifyBinary(
} catch (error) {
clearStallTimer()
// Check if this was a stall timeout (axios wraps abort signals in CanceledError)
const isStallTimeout = axios.isCancel(error)
// Check if this was a stall timeout (abort signal fires)
const isStallTimeout = error instanceof Error && error.name === 'AbortError'
if (isStallTimeout) {
lastError = new StallTimeoutError()
@@ -403,22 +400,23 @@ export async function downloadVersionFromBinaryRepo(
// Fetch manifest to get checksum
let manifest
try {
const manifestResponse = await axios.get(
const manifestResponse = await nativeRequest(
`${baseUrl}/${version}/manifest.json`,
{
timeout: 10000,
responseType: 'json',
...authConfig,
...(authConfig?.auth ? {
headers: {
Authorization: `Basic ${Buffer.from(`${authConfig.auth.username}:${authConfig.auth.password}`).toString('base64')}`,
},
} : {}),
},
)
manifest = manifestResponse.data
} catch (error) {
const latencyMs = Date.now() - startTime
const errorMessage = error instanceof Error ? error.message : String(error)
let httpStatus: number | undefined
if (axios.isAxiosError(error) && error.response) {
httpStatus = error.response.status
}
const httpStatus = isHttpError(error) ? error.status : undefined
logEvent('tengu_binary_manifest_fetch_failure', {
latency_ms: latencyMs,
@@ -466,10 +464,7 @@ export async function downloadVersionFromBinaryRepo(
} catch (error) {
const latencyMs = Date.now() - startTime
const errorMessage = error instanceof Error ? error.message : String(error)
let httpStatus: number | undefined
if (axios.isAxiosError(error) && error.response) {
httpStatus = error.response.status
}
const httpStatus = isHttpError(error) ? error.status : undefined
logEvent('tengu_binary_download_failure', {
latency_ms: latencyMs,