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

next-axiom transports #142

Open
dasfmi opened this issue Aug 28, 2023 · 0 comments
Open

next-axiom transports #142

dasfmi opened this issue Aug 28, 2023 · 0 comments
Assignees

Comments

@dasfmi
Copy link
Collaborator

dasfmi commented Aug 28, 2023

next-axiom advanced configuration support

This researches the different ways to improve the package's configuration, allowing the user to make use of different transports for example, in the same app.

Configuration

  • should be able to import helper functions from axiom kit
import { Logger } from 'next-axiom';
import { Axiom as AxiomTransport } from '@axiomhq/js`
import { isVercel, isBrowser, isNode, isEdge, EventsBatch, ConsoleTransport, FetchTransport, autoDetectConfig } from '@axiomhq/kit';
  • An automatic config reader can auto detect config based on environment
const log = new Logger(autoDetectConfig());

const autoDetectConfig = () => {
    
    const transport = new FetchTransport() // ConsoleTransport()

    
    if (isVercel) {
        return VercelConfig({
            token: process.env.AXIOM_TOKEN,
            dataset: process.env.AXIOM_DATASET,
            edge: {
                transport: ConsoleTransport,
            },
        })
    }

    return {
        token: process.env.AXIOM_TOKEN,
        dataset: process.env.AXIOM_DATASET,
        transport: AxiomTransport()
    }
}
  • configuration could mixed and overridden
const log = new Logger({
    token: 'XAT-123',
    dataset: 'something',
    adapter: new FetchTransport(),
});

const log2 = new Logger({
    ...autoDetectConfig(),
    transport: new ConsoleTransport({ pretty: false }),
});
  • auto configure based on platform
const log = new Logger(VercelConfig())

const VercelConfig = (override) => {
    // get url and token from integration env
    const url = process.env.NEXT_PUBLIC_AXIOM_INGEST_URL;
    const token = process.env.NEXT_PUBLIC_AXIOM_TOKEN;
    const dataset = "vercel";

    if (isBrowser) {
        return { transport: new FetchTransport({ token, dataset })}
    } else if (isEdge || isNode) {
        return { transport: new ConsoleTransport() }
    }
}

passing a logger to route handlers:

loggerConfig = {
    transport: new ConsoleTransport(),
}

const handler = (request) => { ... }

export const Post = withAxiom(handler, loggerConfig)

ConsoleTransport

The ConsoleTransport would be a great choice for apps that have vercel or netlify integration because then functions can just log every thing to the console without waiting for fetch to finish. This would save time and avoid errors on edge and lambda functions.

The downside is the console log limit per platform, for example, vercel's 4kb limit. maybe the ConsoleTransport could have a fallback method, to queue to fetch. or vice-verse, the Fetch transport would queue stuff, but if its below the limit and integration is active, then it just prints to console instead of queuing.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant