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

logger: provide new flag to enable Vercel log drain for API routes #218

Closed
wants to merge 5 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -222,6 +222,14 @@ export default function ErrorPage({
}
```

## Improve API routes logging performance (Vercel)

If you have Vercel's integration enabled and don't mind the log drain cost, you
can make use of the log drain feature to send logs from API routes to Axiom.
This would tell next-axiom to use `console.log()` to send logs to Vercel's log drain instead of sending the logs directly to Axiom and waiting for the logger's http flush request to complete.

To enable this feature, set the `AXIOM_ENABLE_VERCEL_LOGDRAIN` environment variable to `true`.

## Upgrade to the App Router

next-axiom switched to support the App Router starting with version 1.0. If you are upgrading a Pages Router app with next-axiom v0.x to the App Router, you will need to make the following changes:
Expand Down
6 changes: 5 additions & 1 deletion examples/logger/app/api/edge/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,10 @@ import { AxiomRequest, withAxiom } from 'next-axiom';
export const runtime = 'edge';

export const GET = withAxiom(async (req: AxiomRequest) => {
req.log.info('fired from edge route');
req.log.info('fired from edge route', {
method: req.method,
author: 'Islam Sheata',
purpose: 'testing',
});
return new Response('Hello, Next.js!');
});
6 changes: 5 additions & 1 deletion examples/logger/app/api/lambda/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,10 @@ import { AxiomRequest, withAxiom } from 'next-axiom';
export const runtime = 'nodejs';

export const GET = withAxiom(async (req: AxiomRequest) => {
req.log.info('axiom lambda route');
req.log.info('axiom lambda route', {
method: req.method,
author: 'Islam Sheata',
purpose: 'testing',
});
return new Response('Hello, Next.js!');
});
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "next-axiom",
"description": "Send WebVitals from your Next.js project to Axiom.",
"version": "1.3.0",
"version": "1.4.0",
"author": "Axiom, Inc.",
"license": "MIT",
"contributors": [
Expand Down
2 changes: 2 additions & 0 deletions src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ declare global {
export const Version = require('../package.json').version;
// detect if Vercel integration & logdrain is enabled
export const isVercelIntegration = process.env.NEXT_PUBLIC_AXIOM_INGEST_ENDPOINT || process.env.AXIOM_INGEST_ENDPOINT;
export const enableMakeUseOfVercelLogdrain =
process.env.NEXT_PUBLIC_AXIOM_ENABLE_VERCEL_LOGDRAIN || process.env.AXIOM_ENABLE_VERCEL_LOGDRAIN;
// detect if app is running on the Vercel platform
export const isVercel = process.env.NEXT_PUBLIC_VERCEL || process.env.VERCEL;
export const isNetlify = process.env.NETLIFY == 'true';
Expand Down
8 changes: 6 additions & 2 deletions src/logger.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { NextRequest } from 'next/server';
import { config, isBrowser, isVercelIntegration, Version } from './config';
import { config, isBrowser, isVercelIntegration, enableMakeUseOfVercelLogdrain, Version } from './config';
import { NetlifyInfo } from './platform/netlify';
import { isNoPrettyPrint, throttle } from './shared';

Expand Down Expand Up @@ -211,7 +211,11 @@ export class Logger {
// if vercel integration is enabled, we can utilize the log drain
// to send logs to Axiom without HTTP.
// This saves resources and time on lambda and edge functions
if (isVercelIntegration && (this.config.source === 'edge-log' || this.config.source === 'lambda-log')) {
if (
isVercelIntegration &&
enableMakeUseOfVercelLogdrain &&
(this.config.source === 'edge-log' || this.config.source === 'lambda-log')
) {
this.logEvents.forEach((ev) => console.log(JSON.stringify(ev)));
this.logEvents = [];
return;
Expand Down
3 changes: 2 additions & 1 deletion tests/vercelConfig.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import { Logger } from '../src/logger';
vi.hoisted(() => {
process.env.NEXT_PUBLIC_AXIOM_URL = undefined;
process.env.NEXT_PUBLIC_AXIOM_INGEST_ENDPOINT = 'https://api.axiom.co/v1/integrations/vercel';
process.env.AXIOM_ENABLE_VERCEL_LOGDRAIN = 'true';
});

test('reading vercel ingest endpoint', () => {
Expand All @@ -16,7 +17,7 @@ test('reading vercel ingest endpoint', () => {
expect(url).toEqual('https://api.axiom.co/v1/integrations/vercel?type=logs');
});

test('logging to console when running on lambda', async () => {
test('logging to console when running on vercel/lambda', async () => {
vi.useFakeTimers();
const mockedConsole = vi.spyOn(console, 'log');
const time = new Date(Date.now()).toISOString();
Expand Down
Loading