1.0.5 • Published 11 months ago

worker-axiom v1.0.5

Weekly downloads
-
License
MIT
Repository
github
Last release
11 months ago

worker-axiom

Axiom wrapper for Cloudflare workers. This package provides a simple way to ingest data to Axiom from Cloudflare workers. You can then use the ingested data to view logs, create dashboards, and monitors.

Prerequisites

  • Axiom account
  • Axiom API token

Set up

Add the AXIOM_API_TOKEN environment variable to your Cloudflare worker.

Usage

Logging

Note: You must call sendLogs() to send the logs to Axiom.

import { createAxiomClient, getRequestMetadata } from 'worker-axiom';

export default {
  async fetch(request: Request, env: Env, ctx: ExecutionContext): Promise<Response> {

    const createLogger = createAxiomClient({ orgId: '<my-org-id>', datasetName: '<axiom-data-set-name>', apiToken: env.AXIOM_API_TOKEN });
    const mainLogger = createLogger(getRequestMetadata(request));

    mainLogger.log('Main worker function', {
      origin,
      referer,
      requestedFrom,
      requestOrigin,
      apiEndpoint,
      method,
    });

    // must call sendLogs() to send the logs to Axiom
    mainLogger.sendLogs();
    // ... rest of the code

Update logger data

import { createAxiomClient, getRequestMetadata } from 'worker-axiom';

export default {
  async fetch(request: Request, env: Env, ctx: ExecutionContext): Promise<Response> {

    const createLogger = createAxiomClient({ orgId: '<my-org-id>', datasetName: '<axiom-data-set-name>', apiToken: env.AXIOM_API_TOKEN });
    const mainLogger = createLogger(getRequestMetadata(request));

    mainLogger.log('Main worker function', {
      origin,
      referer,
      requestedFrom,
      requestOrigin,
      apiEndpoint,
      method,
    });

    const projectSettings = await getProjectSettingsFromDatabase(request);

    // project settings will be added to all the logs
    mainLogger.updateLoggerData({ ...projectSettings });

    // must call sendLogs() to send the logs to Axiom
    mainLogger.sendLogs();
    // ... rest of the code

Disable logging to console

import { createAxiomClient, getRequestMetadata } from 'worker-axiom';

export default {
  async fetch(request: Request, env: Env, ctx: ExecutionContext): Promise<Response> {

    const createLogger = createAxiomClient({ orgId: '<my-org-id>', alsoLogToConsole: false,
    datasetName: '<axiom-data-set-name>', apiToken: env.AXIOM_API_TOKEN });
    const mainLogger = createLogger(getRequestMetadata(request));
  // rest of the code

Sponsers

  • docsly.dev - Get actionable feedback and insights from your documentation.

License

MIT

1.0.5

11 months ago

1.0.4

11 months ago

1.0.3

11 months ago

1.0.2

11 months ago

1.0.1

12 months ago

1.0.0

12 months ago