• Stars
    star
    353
  • Rank 120,322 (Top 3 %)
  • Language
    TypeScript
  • License
    MIT License
  • Created over 2 years ago
  • Updated 4 months ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

The official Next.js library for Axiom.

next-axiom: The official Next.js library for Axiom next-axiom: The official Next.js library for Axiom

build Latest release License

Axiom unlocks observability at any scale.

  • Ingest with ease, store without limits: Axiom’s next-generation datastore enables ingesting petabytes of data with ultimate efficiency. Ship logs from Kubernetes, AWS, Azure, Google Cloud, DigitalOcean, Nomad, and others.
  • Query everything, all the time: Whether DevOps, SecOps, or EverythingOps, query all your data no matter its age. No provisioning, no moving data from cold/archive to β€œhot”, and no worrying about slow queries. All your data, all. the. time.
  • Powerful dashboards, for continuous observability: Build dashboards to collect related queries and present information that’s quick and easy to digest for you and your team. Dashboards can be kept private or shared with others, and are the perfect way to bring together data from different sources.

For more information, check out the official documentation.

Introduction

This library allows you to send Web Vitals as well as structured logs from your Next.js application to Axiom.

Installation

Note
Using Next.js 12? Use version 0.*, which will continue to get security patches. Here's the README for 0.18.0. If you're upgrading to Next.js 13, check out the next-axiom upgrade guide.

In your Next.js project, install next-axiom:

npm install --save next-axiom

In the next.config.ts file, wrap your Next.js config in withAxiom as follows:

const { withAxiom } = require('next-axiom');

module.exports = withAxiom({
  // ... your existing config
});

If you are using the Vercel integration, no further configuration is required.

Otherwise create a dataset and an API token in Axiom settings, then export them as environment variables NEXT_PUBLIC_AXIOM_DATASET and NEXT_PUBLIC_AXIOM_TOKEN.

Usage

Web Vitals

Go to app/layout.tsx and add the Web Vitals component:

import { AxiomWebVitals } from 'next-axiom';

export default function RootLayout() {
  return (
    <html>
      ...
      <AxiomWebVitals />
      <div>...</div>
    </html>
  );
}

Note: WebVitals are only sent from production deployments.

Logs

Send logs to Axiom from different parts of your application. Each log function call takes a message and an optional fields object.

log.debug('Login attempt', { user: 'j_doe', status: 'success' }); // results in {"message": "Login attempt", "fields": {"user": "j_doe", "status": "success"}}
log.info('Payment completed', { userID: '123', amount: '25USD' });
log.warn('API rate limit exceeded', { endpoint: '/users/1', rateLimitRemaining: 0 });
log.error('System Error', { code: '500', message: 'Internal server error' });

Route Handlers

Wrapping your Route Handlers in withAxiom will add a logger to your request and automatically log exceptions:

import { withAxiom, AxiomRequest } from 'next-axiom';

export const GET = withAxiom((req: AxiomRequest) => {
  req.log.info('Login function called');

  // You can create intermediate loggers
  const log = req.log.with({ scope: 'user' });
  log.info('User logged in', { userId: 42 });

  return NextResponse.json({ hello: 'world' });
});

Client Components

For Client Components, you can add a logger to your component with useLogger:

'use client';
import { useLogger } from 'next-axiom';

export default function ClientComponent() {
  const log = useLogger();
  log.debug('User logged in', { userId: 42 });
  return <h1>Logged in</h1>;
}

Server Components

For Server Components, create a logger and make sure to call flush before returning:

import { Logger } from 'next-axiom';

export default async function ServerComponent() {
  const log = new Logger();
  log.info('User logged in', { userId: 42 });

  // ...

  await log.flush();
  return <h1>Logged in</h1>;
}

Log Levels

The log level defines the lowest level of logs sent to Axiom. The default is debug, resulting in all logs being sent. Available levels are (from lowest to highest): debug, info, warn, error

For example, if you don't want debug logs to be sent to Axiom:

export NEXT_PUBLIC_AXIOM_LOG_LEVEL=info

You can also disable logging completely by setting the log level to off.

Upgrade to Next.js 13

next-axiom switched to support Next.js 13 with app directory support starting version 0.19.0. If you are upgrading from Next.js 12, you will need to make the following changes:

  • Upgrade next-axiom to version 1.0.0 or higher
  • Make sure that exported variables has NEXT_PUBLIC_ prefix, e.g: NEXT_PUBLIC_AXIOM_TOKEN
  • Use useLogger hook in client components instead of log prop
  • For server side components, you will need to create an instance of Logger and flush the logs before component returns.
  • For web-vitals, remove reportWebVitals() and instead add the AxiomWebVitals component to your layout.

FAQ

How can I send logs from Vercel preview deployments?

The Axiom Vercel integration sets up an environment variable called NEXT_PUBLIC_AXIOM_INGEST_ENDPOINT, which by default is only enabled for the production environment. To send logs from preview deployments, go to your site settings in Vercel and enable preview deployments for that environment variable.

How can I extend the logger?

You can use log.with to create an intermediate logger, for example:

const logger = userLogger().with({ userId: 42 });
logger.info('Hi'); // will ingest { ..., "message": "Hi", "fields" { "userId": 42 }}

License

Distributed under the MIT License.

More Repositories

1

hyperloglog

HyperLogLog with lots of sugar (Sparse, LogLog-Beta bias correction and TailCut space reduction) brought to you by Axiom
Go
920
star
2

hyperminhash

HyperMinHash: Bringing intersections to HyperLogLog
Go
302
star
3

rust-cuckoofilter

Cuckoo Filter: Practically Better Than Bloom (In Rust)
Rust
270
star
4

quantiles

Optimal Quantile Approximation in Streams
Go
162
star
5

axiom-js

Official language bindings and library extensions for Axiom
TypeScript
85
star
6

zig-hyperloglog

Zig library for HyperLogLog estimation
Zig
82
star
7

axiom-node

[DEPRECATED] Use axiomhq/axiom-js instead.
TypeScript
78
star
8

axiom-go

Official Go bindings for the Axiom API
Go
47
star
9

cli

The power of Axiom on the command line.
Go
41
star
10

flipcounter

A counter data structure that knows when to start estimating to save space
Go
36
star
11

axiom-rs

Official Rust bindings for the Axiom API
Rust
31
star
12

axiom-py

The official Python bindings for the Axiom API
Python
23
star
13

axiom-cloudflare-workers

Send logs from Cloudflare Workers to Axiom
JavaScript
22
star
14

tracing-axiom

The official Rust tracing layer for Axiom
Rust
18
star
15

puppeteer-request-intercepter

Intercept API Requests and return Mocked Data
TypeScript
16
star
16

prisma-axiom

Axiom observability for Prisma
TypeScript
16
star
17

variance

Go implementation of variance's method for one-pass variance computation with D. H. D. West improved methods which features merging of several multiple sets of statistics and adding weighted values.
Go
16
star
18

hypertwobits

HyperTwoBits implementation
Rust
12
star
19

awesome-axiom

A curated list of awesome Axiom Platform, libraries, open source repos, guides, blogs, Documentation and other resources.
12
star
20

axiom-demo

Take a look at Axiom on your local machine.
Shell
11
star
21

ngbuild

Dream builders πŸ˜΄πŸ’­
Go
9
star
22

axiom-lambda-extension

Ingest logs and platform events from your lambda functions
Go
9
star
23

axiom-syslog-proxy

A syslog push interface to Axiom.
Go
8
star
24

axiom-honeycomb-proxy

A log forwarder/multiplexer for Axiom and Honeycomb.
Go
7
star
25

deno-client

Minimal deno library for sending events and logs to Axiom
TypeScript
6
star
26

axiom-ai

The official package to send events from AI libraries to Axiom
TypeScript
6
star
27

axiom-grafana

The official Axiom datasource plugin for Grafana.
TypeScript
5
star
28

splitblockbloom

Go
5
star
29

pkg

Commonly used Go packages for Axiom projects.
Go
4
star
30

axiom-loki-multiplexer

A push interface to Axiom via Loki endpoint.
Go
4
star
31

axiom-elements

TypeScript
4
star
32

topkapi

Topkapi: Parallel and Fast Sketches for Finding Top-K Frequent Elements
Go
3
star
33

axiom-cloudwatch-forwarder

Forward CloudWatch Logs to Axiom.
Python
3
star
34

randhn

Random new or top 500 HN story
TypeScript
3
star
35

setup-axiom

Set up a local Axiom stack for testing your integration.
JavaScript
2
star
36

annotation-action

This action allows you to create an annotation in Axiom.
TypeScript
2
star
37

terraform-provider-axiom

Axiom Terraform Provider
Go
2
star
38

homebrew-tap

Collection of Homebrew formulas for Axiom, Inc. open source projects.
Ruby
1
star
39

golang-sync-bench

Benchmarking Synchronization Primitives in Go
Go
1
star
40

axiom-nomad

Axiom 🀝 HashiCorp
HCL
1
star
41

axiom-helm-charts

Axiom Helm Charts
Smarty
1
star
42

logmanager

Yet another Go logging library.
Go
1
star
43

terraform-aws-axiom

Setup required infrastructure and install Axiom Enterprise on AWS using Terraform.
1
star
44

monaco-kusto

JavaScript
1
star
45

terraform-aws-axiom-cloudwatch-forwarder

Official Terraform modules for Axiom Cloudwatch Forwarder
HCL
1
star