-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
15 changed files
with
227 additions
and
72 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Binary file not shown.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
import { getLogger, isSerializable } from '@app/logger'; | ||
import fastifyPlugin from 'fastify-plugin'; | ||
|
||
const log = getLogger('frontend-error-reporter'); | ||
|
||
export const ERROR_REPORT_PLUGIN_ID = 'error-report'; | ||
|
||
export const frontendLogPlugin = fastifyPlugin( | ||
(app, _, pluginDone) => { | ||
app.post('/error-report', (req, reply) => { | ||
if (!isSerializable(req.body)) { | ||
reply.status(400).send('Invalid request body'); | ||
|
||
return; | ||
} | ||
|
||
log.warn({ msg: 'Error report', data: req.body }); | ||
|
||
reply.status(200).send(); | ||
}); | ||
|
||
pluginDone(); | ||
}, | ||
{ fastify: '4', name: ERROR_REPORT_PLUGIN_ID }, | ||
); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
import { getLogger } from '@app/logger'; | ||
import { FrontendEventTypes, Level } from '@app/plugins/frontend-log/types'; | ||
import { Type, type TypeBoxTypeProvider } from '@fastify/type-provider-typebox'; | ||
import fastifyPlugin from 'fastify-plugin'; | ||
|
||
const log = getLogger('frontend-log'); | ||
|
||
export const FRONTEND_LOG_PLUGIN_ID = 'frontend-log'; | ||
|
||
export const frontendLogPlugin = fastifyPlugin( | ||
(app, _, pluginDone) => { | ||
app.withTypeProvider<TypeBoxTypeProvider>().post( | ||
'/frontend-log', | ||
{ | ||
schema: { | ||
body: Type.Composite([ | ||
Type.Object({ | ||
session_id: Type.String(), | ||
level: Type.Enum(Level), | ||
client_timestamp: Type.Number({ description: 'Current client Unix timestamp in milliseconds.' }), | ||
token_expires: Type.Optional(Type.Number({ description: 'Milliseconds until the token expires.' })), | ||
is_logged_in: Type.Boolean(), | ||
client_version: Type.String(), | ||
session_time: Type.Number({ description: 'Milliseconds since start of session.' }), | ||
session_time_formatted: Type.String({ | ||
description: 'Formatted time since start of session.', | ||
format: 'time', | ||
examples: ['1:35:45.987'], | ||
}), | ||
route: Type.String({ | ||
description: 'Current path.', | ||
examples: ['/nb/sak/uuid-uuid-uuid-uuid/begrunnelse'], | ||
}), | ||
message: Type.String(), | ||
}), | ||
Type.Union([ | ||
// Navigation event | ||
Type.Object({ | ||
type: Type.Literal(FrontendEventTypes.NAVIGATION), | ||
}), | ||
// App event | ||
Type.Object({ | ||
type: Type.Literal(FrontendEventTypes.APP), | ||
action: Type.String(), | ||
}), | ||
// Error event | ||
Type.Object( | ||
{ | ||
type: Type.Literal(FrontendEventTypes.ERROR), | ||
message: Type.String(), | ||
stack: Type.Optional(Type.String({ description: 'Stack trace of the error.' })), | ||
}, | ||
{ description: 'Error event.' }, | ||
), | ||
// API event | ||
Type.Object( | ||
{ | ||
type: Type.Literal(FrontendEventTypes.API), | ||
request: Type.String(), | ||
response_time: Type.Number({ description: 'API response time in milliseconds.' }), | ||
status: Type.Union([Type.Number(), Type.String()]), | ||
}, | ||
{ description: 'API event.' }, | ||
), | ||
]), | ||
]), | ||
}, | ||
}, | ||
(req, reply) => { | ||
const { level, message, ...data } = req.body; | ||
|
||
log[level]({ msg: message, data }); | ||
|
||
reply.status(200).send(); | ||
}, | ||
); | ||
|
||
pluginDone(); | ||
}, | ||
{ fastify: '4', name: FRONTEND_LOG_PLUGIN_ID }, | ||
); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
export enum Level { | ||
DEBUG = 'debug', | ||
INFO = 'info', | ||
WARN = 'warn', | ||
ERROR = 'error', | ||
} | ||
|
||
export enum FrontendEventTypes { | ||
NAVIGATION = 'navigation', | ||
APP = 'app', | ||
ERROR = 'error', | ||
API = 'api', | ||
SESSION = 'session', | ||
} | ||
|
||
interface BaseEventData { | ||
session_id: string; | ||
level: Level; | ||
/** Current client Unix timestamp in milliseconds. */ | ||
client_timestamp: number; | ||
/** Milliseconds until the token expires. */ | ||
token_expires?: number; | ||
/** If the user is logged in. */ | ||
is_logged_in: boolean; | ||
/** Current client version. */ | ||
client_version: string; | ||
/** Milliseconds since start of session. */ | ||
session_time: number; | ||
/** Formatted time since start of session. | ||
* @example `1:35:45.987` | ||
*/ | ||
session_time_formatted: string; | ||
/** Current path. | ||
* @example `/nb/klage/1234/begrunnelse` | ||
* */ | ||
route: string; | ||
message: string; | ||
} | ||
|
||
interface NavigationEvent extends BaseEventData { | ||
type: FrontendEventTypes.NAVIGATION; | ||
} | ||
|
||
interface AppEvent extends BaseEventData { | ||
type: FrontendEventTypes.APP; | ||
action: string; | ||
} | ||
|
||
interface ErrorEvent extends BaseEventData { | ||
type: FrontendEventTypes.ERROR; | ||
message: string; | ||
stack?: string; | ||
} | ||
|
||
interface ApiEvent extends BaseEventData { | ||
type: FrontendEventTypes.API; | ||
request: string; | ||
response_time: number; | ||
status: number | string; | ||
} | ||
|
||
enum SessionAction { | ||
/** Load session case */ | ||
LOAD = 'load', | ||
/** Create session case */ | ||
CREATE = 'create', | ||
/** Load or create session case */ | ||
LOAD_OR_CREATE = 'load-create', | ||
/** Delete session case */ | ||
DELETE = 'delete', | ||
/** Set session case */ | ||
SET = 'set', | ||
/** Update session case */ | ||
UPDATE = 'update', | ||
} | ||
|
||
interface SessionEvent extends BaseEventData { | ||
type: FrontendEventTypes.SESSION; | ||
message: string; | ||
action: SessionAction; | ||
} | ||
|
||
export type FrontendLogEvent = NavigationEvent | AppEvent | ErrorEvent | ApiEvent | SessionEvent; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.