Skip to content

Commit

Permalink
Merge pull request #185 from axiomhq/console-transport-on-vercel
Browse files Browse the repository at this point in the history
utilize log drain on vercel functions
  • Loading branch information
schehata committed Apr 3, 2024
2 parents cb9b681 + 289b2b8 commit 7490994
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 0 deletions.
9 changes: 9 additions & 0 deletions src/logger.ts
Expand Up @@ -156,6 +156,15 @@ export class Logger {
return;
}

// 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 (isVercel && (this.config.source === 'edge' || this.config.source === 'lambda')) {
this.logEvents.forEach((ev) => console.log(JSON.stringify(ev)));
this.logEvents = [];
return;
}

const method = 'POST';
const keepalive = true;
const body = JSON.stringify(this.logEvents);
Expand Down
22 changes: 22 additions & 0 deletions tests/vercelConfig.test.ts
@@ -1,6 +1,7 @@
import { test, expect, vi } from 'vitest';
import { config } from '../src/config';
import { EndpointType } from '../src/shared';
import { Logger } from '../src/logger';

vi.hoisted(() => {
process.env.NEXT_PUBLIC_AXIOM_URL = undefined;
Expand All @@ -14,3 +15,24 @@ test('reading vercel ingest endpoint', () => {
url = config.getIngestURL(EndpointType.logs);
expect(url).toEqual('https://api.axiom.co/v1/integrations/vercel?type=logs');
});

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

const logger = new Logger({
source: 'lambda',
});

logger.info('hello, world!');

await logger.flush();
expect(mockedConsole).toHaveBeenCalledTimes(1);

const calledWithPayload = JSON.parse(mockedConsole.mock.calls[0][0]);
expect(calledWithPayload.message).toEqual('hello, world!');
expect(calledWithPayload.level).toEqual('info');
expect(calledWithPayload._time).toEqual(time);
expect(calledWithPayload.vercel.source).toEqual('lambda');
});

0 comments on commit 7490994

Please sign in to comment.