diff --git a/README.md b/README.md index ee10584..294795d 100644 --- a/README.md +++ b/README.md @@ -32,6 +32,11 @@ const axiomDataset = "my-dataset" // Your Axiom dataset const axiomToken = "xapt-xxx" // Your Axiom API token ``` +Update the log filter variable to only send logs with status code above or equal (default to HTTP code 100): +```ts +const logsHttpMinStatusCode = 100 // Filter logs and send only logs with status code above or equal +``` + Add triggers for the worker, e.g a route trigger: - Navigate to the worker and click on the `Triggers` tab. - Scroll down to Routes and click `Add Route`. diff --git a/src/worker.js b/src/worker.js index 8df1d9a..f488df7 100644 --- a/src/worker.js +++ b/src/worker.js @@ -1,5 +1,6 @@ const axiomDataset = 'my-dataset' // Your Axiom dataset const axiomToken = 'xapt-xxx' // Your Axiom API token +const logsHttpMinStatusCode = 100 // Filter logs and send only logs with status code above or equal const requestHeadersToCapture = ['user-agent']; const responseHeadersToCapture = ['cf-cache-status', 'cf-ray']; @@ -23,7 +24,7 @@ const WORKER_ID = generateId(6) const throttle = (fn, wait, maxLen) => { let timeoutInProgress = false - return async function actual (...args) { + return async function actual(...args) { const context = this if (batch.length >= maxLen) { @@ -40,7 +41,7 @@ const throttle = (fn, wait, maxLen) => { } } -async function sendLogs () { +async function sendLogs() { if (batch.length === 0) { return } @@ -77,7 +78,7 @@ function getHeaderMap(headers, allowlist) { }, {}); } -async function handleRequest (request, context) { +async function handleRequest(request, context) { const start = Date.now() const response = await fetch(request) @@ -92,34 +93,34 @@ async function handleRequest (request, context) { } }) } - - batch.push({ - _time: Date.now(), - request: { - url: request.url, - headers: getHeaderMap(request.headers, requestHeadersToCapture), - method: request.method, - ...cf - }, - response: { - duration, - headers: getHeaderMap(response.headers, responseHeadersToCapture), - status: response.status - }, - worker: { - version: Version, - id: WORKER_ID, - started: workerTimestamp - } - }) - + if (response.status >= logsHttpMinStatusCode) { + batch.push({ + _time: Date.now(), + request: { + url: request.url, + headers: getHeaderMap(request.headers, requestHeadersToCapture), + method: request.method, + ...cf + }, + response: { + duration, + headers: getHeaderMap(response.headers, responseHeadersToCapture), + status: response.status + }, + worker: { + version: Version, + id: WORKER_ID, + started: workerTimestamp + } + }) + } context.waitUntil(throttledSendLogs()) return response } export default { - async fetch (req, _, context) { + async fetch(req, _, context) { context.passThroughOnException() if (!workerTimestamp) {