* Flushes buffered log entries to Profound's API.
()
| 42 | * Flushes buffered log entries to Profound's API. |
| 43 | */ |
| 44 | async function flush(): Promise<void> { |
| 45 | if (buffer.length === 0) return |
| 46 | |
| 47 | const apiKey = env.PROFOUND_API_KEY |
| 48 | if (!apiKey) { |
| 49 | buffer = [] |
| 50 | return |
| 51 | } |
| 52 | |
| 53 | const endpoint = env.PROFOUND_ENDPOINT |
| 54 | if (!endpoint) { |
| 55 | buffer = [] |
| 56 | return |
| 57 | } |
| 58 | const entries = buffer.splice(0, MAX_BATCH_SIZE) |
| 59 | |
| 60 | try { |
| 61 | const response = await fetch(endpoint, { |
| 62 | method: 'POST', |
| 63 | headers: { |
| 64 | 'x-api-key': apiKey, |
| 65 | 'Content-Type': 'application/json', |
| 66 | }, |
| 67 | body: JSON.stringify(entries), |
| 68 | }) |
| 69 | |
| 70 | if (!response.ok) { |
| 71 | logger.error(`Profound API returned ${response.status}`) |
| 72 | } |
| 73 | } catch (error) { |
| 74 | logger.error('Failed to flush logs to Profound', error) |
| 75 | } |
| 76 | } |
| 77 | |
| 78 | function ensureFlushTimer(): void { |
| 79 | if (flushTimer) return |
no test coverage detected