Skip to content

Improve Workers example - #14

Open
jahands wants to merge 1 commit into
axiomhq:mainfrom
jahands:jahands/update-example
Open

Improve Workers example#14
jahands wants to merge 1 commit into
axiomhq:mainfrom
jahands:jahands/update-example

Conversation

@jahands

@jahands jahands commented Nov 1, 2023

Copy link
Copy Markdown

Been using something similar to this for my Workers for a few months and have ingested ~38M logs so far from Workers, so thought I'd make a PR.

This improves this example with a few improvements:

  • Updated config files based on npm create cloudflare@latest "Hello World worker" template
  • Created new logger library with best practices
    • Avoid using global variables because the global scope is shared across requests
    • Use TypeScript (would recommend publishing as an NPM package to support plain JS, but we could publish a transpiled version here if needed.)

This logger class can be used with all handler types (fetch/queues/email/etc.) I removed some of the request-based example we had before so that it's easier for users to use with non-http handlers. In a future PR, I could move this to a monorepo so that it's easier to create additional examples

Testing

Deployed to Workers and verified logs show up in my Axiom account as expected

@islameldigwi
islameldigwi requested a review from bahlo November 1, 2023 10:58
@jahands
jahands force-pushed the jahands/update-example branch from b5c7f07 to 6e458fc Compare November 6, 2023 12:02
@jahands
jahands force-pushed the jahands/update-example branch from 6e458fc to 06e94ec Compare November 22, 2023 06:43

@bahlo bahlo left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

First of all, thank you so much for the review and sorry for taking so long to review!

This looks great in general, would be even better as an npm package (as you wrote as well), but we can do that in a follow-up.

The biggest thing is that this takes the worker from logging requests to providing a logger implementation and, while the logger is a great addition, the use-case of logging requests and being able to inspect req/res/metadata is just as valid.

Can we add that functionality back please?

Comment thread package.json
"description": "send logs form cloudflare to axiom",
"main": "src/worker.js",
"author": "Axiom, Inc.",
"name": "workers-axiom-example",

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could we keep the old metadata like name, contributors, repository, keywords, etc.?

Comment thread src/axiom.ts
/** Version of the Workers application */
version?: string
/** Data included in every log */
data?: any

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
data?: any
data?: { [key: string]: any }

Comment thread src/axiom.ts
data?: any
/** ID unique to each invocation. Default: crypto.randomUUID() */
invocationId?: string
/** Axiom ingest API endpoint. Default: https://api.axiom.co */

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we turn these into //?

Comment thread src/axiom.ts
/** Flush after this many ms. Default: 10,000 */
flushAfterMs?: number
/** Flush after this many logs. Default: 100 */
flushAfterLogs?: number

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please default to 1s/1000 logs for consistent behaviour with other Axiom tools

Comment thread src/axiom.ts
this.logs.splice(0, logsCount)
await res.arrayBuffer() // Read the body to completion
} else {
console.log(`axiom failed to ingest logs: ${res.status} ${res.statusText} ${await res.text()}`)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
console.log(`axiom failed to ingest logs: ${res.status} ${res.statusText} ${await res.text()}`)
console.warn(`Axiom failed to ingest logs: ${res.status} ${res.statusText} ${await res.text()}`)

Comment thread src/axiom.ts
console.log(`axiom failed to ingest logs: ${res.status} ${res.statusText} ${await res.text()}`)
}
} catch (err) {
console.error(`axiom failed to ingest logs: ${err}`)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
console.error(`axiom failed to ingest logs: ${err}`)
console.error(`Axiom failed to ingest logs: ${err}`)

Comment thread src/axiom.ts
// Make sure the last one is done before starting a flush
await this.flushPromise

this.flushPromise = doFlush()

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If you call flush multiple times, won't they all await this.flushPromise and, once done, race to set this.flushPromise?

@F0rce F0rce mentioned this pull request Feb 27, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants