Skip to content

Commit

Permalink
test passing vercel data with logevents
Browse files Browse the repository at this point in the history
  • Loading branch information
schehata committed Aug 8, 2022
1 parent 30afba8 commit 6e888ee
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 5 deletions.
16 changes: 14 additions & 2 deletions src/logger.ts
@@ -1,4 +1,4 @@
import { proxyPath, isBrowser, EndpointType, getIngestURL, isEnvVarsSet, isNoPrettyPrint } from './shared';
import { proxyPath, isBrowser, EndpointType, getIngestURL, isEnvVarsSet, isNoPrettyPrint, vercelEnv, vercelRegion } from './shared';
import { throttle } from './shared';

const url = isBrowser ? `${proxyPath}/logs` : getIngestURL(EndpointType.logs);
Expand All @@ -9,6 +9,7 @@ interface LogEvent {
fields: {};
_time: string;
request?: RequestReport;
vercel?: VercelData;
}

export interface RequestReport {
Expand All @@ -21,7 +22,12 @@ export interface RequestReport {
method: string;
scheme: string;
userAgent?: string | null;
environment?: string | null;
}

interface VercelData {
environment?: string;
region?: string;
route?: string;
}

export class Logger {
Expand Down Expand Up @@ -57,8 +63,14 @@ export class Logger {
logEvent.fields = args;
}

logEvent.vercel = {
environment: vercelEnv,
region: vercelRegion,
}

if (this.req != null) {
logEvent.request = this.req;
logEvent.vercel.route = this.req.path;
}

this.logEvents.push(logEvent);
Expand Down
4 changes: 1 addition & 3 deletions src/withAxiom.ts
Expand Up @@ -2,7 +2,7 @@ import { NextConfig, NextApiHandler, NextApiResponse, NextApiRequest } from 'nex
import { NextFetchEvent, NextMiddleware, NextRequest } from 'next/server';
import { NextMiddlewareResult } from 'next/dist/server/web/types';
import { Logger, RequestReport } from './logger';
import { proxyPath, EndpointType, getIngestURL, vercelEnv, vercelRegion } from './shared';
import { proxyPath, EndpointType, getIngestURL, vercelRegion } from './shared';

declare global {
var EdgeRuntime: string;
Expand Down Expand Up @@ -108,7 +108,6 @@ function withAxiomNextApiHandler(handler: NextApiHandler): NextApiHandler {
userAgent: getHeaderOrDefault(req, 'user-agent', ''),
scheme: 'https',
ip: getHeaderOrDefault(req, 'x-forwarded-for', ''),
environment: vercelEnv,
region: vercelRegion,
};
const logger = new Logger({}, report, false);
Expand Down Expand Up @@ -147,7 +146,6 @@ function withAxiomNextEdgeFunction(handler: NextMiddleware): NextMiddleware {
path: req.nextUrl.pathname,
scheme: req.nextUrl.protocol.replace(':', ''),
userAgent: req.headers.get('user-agent'),
environment: vercelEnv,
};

const logger = new Logger({}, report, false);
Expand Down

0 comments on commit 6e888ee

Please sign in to comment.