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

utilize log drain on vercel functions #185

Merged
merged 2 commits into from
Apr 3, 2024
Merged
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
9 changes: 9 additions & 0 deletions src/logger.ts
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
@@ -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');
});
Loading