Skip to content

Commit

Permalink
Send timestamp in "dt" field to be accepted by Better Stack (#21)
Browse files Browse the repository at this point in the history
  • Loading branch information
PetrHeinz authored Nov 26, 2024
1 parent 145d4f3 commit b96e347
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 19 deletions.
4 changes: 2 additions & 2 deletions src/logger.ts
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ export class Logger {
}
const logEvent: LogEvent = { level, message, _time: new Date(Date.now()).toISOString(), fields: this.args || {} };

// check if passed args is an object, if its not an object, add it to fields.args
// check if passed args is an object, if it's not an object, add it to fields.args
if (args instanceof Error) {
logEvent.fields = { ...logEvent.fields, message: args.message, stack: args.stack, name: args.name };
} else if (typeof args === 'object' && args !== null && Object.keys(args).length > 0) {
Expand Down Expand Up @@ -139,7 +139,7 @@ export class Logger {

const method = 'POST';
const keepalive = true;
const body = JSON.stringify(this.logEvents);
const body = JSON.stringify(this.logEvents.map((event) => ({ dt: event._time, ...event })));
// clear pending logs
this.logEvents = [];
const headers = {
Expand Down
16 changes: 9 additions & 7 deletions src/platform/generic.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ export default class GenericConfig implements Provider {
shouldSendEdgeReport = false;
token = process.env.LOGTAIL_SOURCE_TOKEN;
environment: string = process.env.NODE_ENV;
logtailUrl = process.env.LOGTAIL_URL || 'https://in.logtail.com';
logtailUrl = process.env.LOGTAIL_URL || 'https://in.logs.betterstack.com';
region = process.env.REGION || undefined;

isEnvVarsSet(): boolean {
Expand Down Expand Up @@ -43,13 +43,15 @@ export default class GenericConfig implements Provider {
}

wrapWebVitalsObject(metrics: any[]): any {
const time = new Date().getTime();
return metrics.map(m => ({
webVital: m,
_time: new Date().getTime(),
platform: {
environment: this.environment,
source: 'web-vital',
},
webVital: m,
dt: time,
_time: time,
platform: {
environment: this.environment,
source: 'web-vital',
},
}))
}

Expand Down
22 changes: 12 additions & 10 deletions src/platform/netlify.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,18 +19,20 @@ export interface NetlifyInfo extends PlatformInfo {
export default class NetlifyConfig extends GenericConfig implements Provider {

wrapWebVitalsObject(metrics: any[]): any {
const time = new Date().getTime();
return metrics.map(m => ({
webVital: m,
_time: new Date().getTime(),
netlify: {
environment: this.environment,
source: 'web-vital',
siteId: netlifySiteId,
buildId: netlifyBuildId,
context: netlifyContext,
deploymentUrl: netlifyDeploymentUrl,
deploymentId: netlifyDeploymentId,
},
dt: time,
_time: time,
netlify: {
environment: this.environment,
source: 'web-vital',
siteId: netlifySiteId,
buildId: netlifyBuildId,
context: netlifyContext,
deploymentUrl: netlifyDeploymentUrl,
deploymentId: netlifyDeploymentId,
},
}))
}

Expand Down

0 comments on commit b96e347

Please sign in to comment.