Skip to content

Commit

Permalink
Merge pull request #10 from axiomhq/arne/0.3.0
Browse files Browse the repository at this point in the history
Release 0.3.0
  • Loading branch information
bahlo committed Jun 27, 2023
2 parents 373a5a8 + 5bb94e7 commit 115a7b1
Showing 1 changed file with 23 additions and 7 deletions.
30 changes: 23 additions & 7 deletions src/worker.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
const axiomDataset = 'my-dataset' // Your Axiom dataset
const axiomToken = 'xapt-xxx' // Your Axiom API token

const requestHeadersToCapture = ['user-agent'];
const responseHeadersToCapture = ['cf-cache-status', 'cf-ray'];

// 8< ----------- snip ------------
const Version = '0.2.0'
const Version = '0.3.0'
const axiomEndpoint = 'https://api.axiom.co'
let workerTimestamp
let batch = []
Expand Down Expand Up @@ -31,8 +34,8 @@ const throttle = (fn, wait, maxLen) => {
setTimeout(() => {
timeoutInProgress = false
fn.apply(context, args).then(resolve).catch(resolve);
})
}, wait)
}, wait)
})
}
}
}
Expand All @@ -48,8 +51,7 @@ async function sendLogs () {
return fetch(url, {
signal: AbortSignal.timeout(30_000),
method: 'POST',
body: logs.map(JSON.stringify).join('\n'),
keepalive: true,
body: logs.map(log => JSON.stringify(log)).join('\n'),
headers: {
'Content-Type': 'application/x-ndjson',
Authorization: `Bearer ${axiomToken}`,
Expand All @@ -61,6 +63,20 @@ async function sendLogs () {
// This will send logs every 10 seconds or every 1000 logs
const throttledSendLogs = throttle(sendLogs, 10_000, 1000)

function getHeaderMap(headers, allowlist) {
if (!allowlist.length) {
return {};
}

return [...headers].reduce((acc, [headerKey, headerValue]) => {
if (allowlist.includes(headerKey)) {
acc[headerKey] = headerValue;
}

return acc;
}, {});
}

async function handleRequest (request, context) {
const start = Date.now()

Expand All @@ -81,13 +97,13 @@ async function handleRequest (request, context) {
_time: Date.now(),
request: {
url: request.url,
headers: request.headers,
headers: getHeaderMap(request.headers, requestHeadersToCapture),
method: request.method,
...cf
},
response: {
duration,
headers: response.headers,
headers: getHeaderMap(response.headers, responseHeadersToCapture),
status: response.status
},
worker: {
Expand Down

0 comments on commit 115a7b1

Please sign in to comment.