Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve Workers example #14

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

jahands
Copy link

@jahands jahands commented Nov 1, 2023

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

Copy link
Member

@bahlo bahlo left a comment

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?

"description": "send logs form cloudflare to axiom",
"main": "src/worker.js",
"author": "Axiom, Inc.",
"name": "workers-axiom-example",
Copy link
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.?

/** Version of the Workers application */
version?: string
/** Data included in every log */
data?: any
Copy link
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 }

data?: any
/** ID unique to each invocation. Default: crypto.randomUUID() */
invocationId?: string
/** Axiom ingest API endpoint. Default: https://api.axiom.co */
Copy link
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 //?

/** Flush after this many ms. Default: 10,000 */
flushAfterMs?: number
/** Flush after this many logs. Default: 100 */
flushAfterLogs?: number
Copy link
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

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
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()}`)

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
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}`)

// Make sure the last one is done before starting a flush
await this.flushPromise

this.flushPromise = doFlush()
Copy link
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.

None yet

2 participants