Skip to content

Commit

Permalink
clear separation of platforms
Browse files Browse the repository at this point in the history
  • Loading branch information
schehata committed Sep 19, 2022
1 parent 96ebb0c commit 48cc545
Show file tree
Hide file tree
Showing 5 changed files with 95 additions and 54 deletions.
10 changes: 5 additions & 5 deletions src/logger.ts
Expand Up @@ -78,10 +78,10 @@ export class Logger {

// logEvent.vercel = {
logEvent.platform = {
environment: config.environment,
region: config.region,
environment: config.platform.getEnvironment(),
region: config.platform.getRegion(),
source: this.source,
provider: config.provider,
provider: config.platform.provider,
};

if (this.req != null) {
Expand Down Expand Up @@ -109,7 +109,7 @@ export class Logger {
return;
}

if (!config.isEnvVarsSet) {
if (!config.platform.isEnvVarsSet) {
// if AXIOM ingesting url is not set, fallback to printing to console
// to avoid network errors in development environments
this.logEvents.forEach((ev) => prettyPrint(ev));
Expand All @@ -121,7 +121,7 @@ export class Logger {
const keepalive = true;
const body = JSON.stringify(this.logEvents);
const headers = {
Authorization: `Bearer ${config.token}`,
Authorization: `Bearer ${config.platform.getAuthToken()}`,
'content-type': 'application/json',
};
// clear pending logs
Expand Down
64 changes: 64 additions & 0 deletions src/platform.ts
@@ -0,0 +1,64 @@
export interface PlatformManager {
provider: string;
shoudSendEdgeReport: boolean;

isEnvVarsSet(): boolean;
getURL(): string | undefined;
getEnvironment(): string | undefined;
getRegion(): string | undefined;
getAuthToken(): string | undefined;
}

export class Generic implements PlatformManager {
provider = 'self-hosted';
shoudSendEdgeReport = false;
private token = process.env.AXIOM_TOKEN;
private url = process.env.AXOIOM_URL;
private env = process.env.NODE_ENV;
private dataset = process.env.AXIOM_DATASET;

isEnvVarsSet() {
return this.url != undefined && this.dataset != undefined && this.token != undefined;
}

getURL() {
return this.url;
}

getEnvironment() {
return this.env;
}

getRegion() {
return undefined;
}

getAuthToken() {
return this.token;
}
}

export class Vercel implements PlatformManager {
provider = 'vercel';
shoudSendEdgeReport = true;

isEnvVarsSet() {
return process.env.AXIOM_INGEST_ENDPOINT != undefined || process.env.NEXT_PUBLIC_AXIOM_INGEST_ENDPOINT != undefined;
}

getURL() {
return process.env.NEXT_PUBLIC_AXIOM_INGEST_ENDPOINT || process.env.AXIOM_INGEST_ENDPOINT;
}

getEnvironment() {
return process.env.VERCEL_ENV;
}

getRegion() {
return process.env.VERCEL_REGION;
}

getAuthToken() {
return undefined;
}
}
39 changes: 9 additions & 30 deletions src/shared.ts
@@ -1,38 +1,17 @@
export const proxyPath = '/_axiom';
const TOKEN = process.env.AXIOM_TOKEN;
import { type PlatformManager, Generic, Vercel } from './platform';

function detectEnvironmentConfiguration() {
const isVercel = process.env.NEXT_PUBLIC_VERCEL_ENV ? true : false;
const nodeEnv = process.env.NODE_ENV;
class Config {
isVercel = process.env.NEXT_PUBLIC_VERCEL_ENV ? true : false;
isNoPrettyPrint = process.env.AXIOM_NO_PRETTY_PRINT == 'true' ? true : false;
isBrowser = typeof window !== 'undefined';
platform: PlatformManager;

const baseConfig = {
isBrowser: typeof window !== 'undefined',
// isEnvVarsSet: process.env.AXIOM_INGEST_ENDPOINT || process.env.NEXT_PUBLIC_AXIOM_INGEST_ENDPOINT,
isEnvVarsSet: true,
isNoPrettyPrint: process.env.AXIOM_NO_PRETTY_PRINT == 'true' ? true : false,
isVercel,
token: TOKEN,
};

if (isVercel) {
return {
...baseConfig,
region: process.env.VERCEL_REGION || process.env.NEXT_PUBLIC_VERCEL_REGION,
environment: process.env.VERCEL_ENV || process.env.NEXT_PUBLIC_VERCEL_ENV,
dataset: 'vercel',
provider: 'vercel',
};
constructor() {
this.platform = this.isVercel ? new Vercel() : new Generic();
}

return {
...baseConfig,
region: '',
environment: nodeEnv || 'dev',
provider: 'self-hosted',
};
}

export const config = detectEnvironmentConfiguration();
export const config = new Config();

export enum EndpointType {
webVitals = 'web-vitals',
Expand Down
34 changes: 16 additions & 18 deletions src/webVitals.ts
Expand Up @@ -12,7 +12,7 @@ export function reportWebVitals(metric: NextWebVitalsMetric) {
collectedMetrics.push({ route: window.__NEXT_DATA__?.page, ...metric });
// if Axiom env vars are not set, do nothing,
// otherwise devs will get errors on dev environments
if (!config.isEnvVarsSet) {
if (!config.platform.isEnvVarsSet) {
return;
}
throttledSendMetrics();
Expand All @@ -25,8 +25,8 @@ function sendMetrics() {
webVitals: collectedMetrics,
_time: new Date().getTime(),
platform: {
provider: config.provider,
environment: config.environment,
provider: config.platform.provider,
environment: config.platform.getEnvironment(),
source: 'reportWebVitals',
},
},
Expand All @@ -39,26 +39,24 @@ function sendMetrics() {
method: 'POST',
keepalive: true,
headers: {
Authorization: `Bearer ${config.token}`,
Authorization: `Bearer ${config.platform.getAuthToken()}`,
'Content-Type': 'application/json',
},
}).catch(console.error);
}

// if (config.isBrowser && navigator.sendBeacon) {
// try {
// // See https://github.com/vercel/next.js/pull/26601
// // Navigator has to be bound to ensure it does not error in some browsers
// // https://xgwang.me/posts/you-may-not-know-beacon/#it-may-throw-error%2C-be-sure-to-catch
// const b
// navigator.sendBeacon.bind(navigator)(url, body);
// } catch (err) {
// sendFallback();
// }
// } else {
// sendFallback();
// }
sendFallback();
if (config.isBrowser && navigator.sendBeacon) {
try {
// See https://github.com/vercel/next.js/pull/26601
// Navigator has to be bound to ensure it does not error in some browsers
// https://xgwang.me/posts/you-may-not-know-beacon/#it-may-throw-error%2C-be-sure-to-catch
navigator.sendBeacon.bind(navigator)(url, body);
} catch (err) {
sendFallback();
}
} else {
sendFallback();
}

collectedMetrics = [];
}
2 changes: 1 addition & 1 deletion src/withAxiom.ts
Expand Up @@ -174,7 +174,7 @@ function withAxiomNextEdgeFunction(handler: NextMiddleware): NextMiddleware {
}

function logEdgeReport(report: any) {
if (config.isVercel) {
if (config.platform.shoudSendEdgeReport) {
console.log(`AXIOM_EDGE_REPORT::${JSON.stringify(report)}`);
}
}
Expand Down

0 comments on commit 48cc545

Please sign in to comment.