Skip to content

Commit

Permalink
Merge pull request #15 from KrustyHack/patch-1
Browse files Browse the repository at this point in the history
Add filter logs by status code
  • Loading branch information
dasfmi authored Mar 5, 2024
2 parents 115a7b1 + 5cfb281 commit ad6823c
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 25 deletions.
5 changes: 5 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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`.
Expand Down
51 changes: 26 additions & 25 deletions src/worker.js
Original file line number Diff line number Diff line change
@@ -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'];
Expand All @@ -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) {
Expand All @@ -40,7 +41,7 @@ const throttle = (fn, wait, maxLen) => {
}
}

async function sendLogs () {
async function sendLogs() {
if (batch.length === 0) {
return
}
Expand Down Expand Up @@ -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)
Expand All @@ -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) {
Expand Down

0 comments on commit ad6823c

Please sign in to comment.