-
Notifications
You must be signed in to change notification settings - Fork 2
Update logs #299
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
Open
7eliassen
wants to merge
10
commits into
main
Choose a base branch
from
chore/logs-and-optimization
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Update logs #299
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
62e038d
feat: add durationMs to db queries
7eliassen 38d2ba2
chore: middlewares and policies logs
7eliassen 4b62083
feat: add reqId for policies and middlewares
7eliassen 6db34f8
feat: add reqId for db queries
7eliassen fe1b1ba
refactor: use context provider for logs
7eliassen 55cc43c
chore: update config
7eliassen 5d9b45a
chore: lint fixes
7eliassen 5b471ce
chore: change log levels
7eliassen 75ffe66
chore: grammar
7eliassen 8596447
chore: add caching for getLogger
7eliassen File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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,24 @@ | ||
| import { AsyncLocalStorage } from 'node:async_hooks'; | ||
|
|
||
| /** | ||
| * Context for storing reqId within asynchronous operations | ||
| */ | ||
| interface RequestContext { | ||
| reqId?: string; | ||
| } | ||
|
|
||
| /** | ||
| * AsyncLocalStorage for storing request context | ||
| * Allows tying database logs to the corresponding request | ||
| */ | ||
| export const requestContextStorage = new AsyncLocalStorage<RequestContext>(); | ||
|
|
||
| /** | ||
| * Gets the current reqId from the execution context | ||
| * @returns reqId or undefined if context is not set | ||
| */ | ||
| export function getCurrentReqId(): string | undefined { | ||
| const context = requestContextStorage.getStore(); | ||
|
|
||
| return context?.reqId; | ||
| } |
This file contains hidden or 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 hidden or 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,18 @@ | ||
| import type { FastifyInstance } from 'fastify'; | ||
| import { requestContextStorage } from '@infrastructure/logging/reqId.context.js'; | ||
|
|
||
| /** | ||
| * Adds middleware to set reqId context at the beginning of each request | ||
| * @param server - fastify instance | ||
| */ | ||
| export default function addReqIdContext(server: FastifyInstance): void { | ||
| /** | ||
| * Sets reqId context for all asynchronous operations within the request | ||
| * Uses 'onRequest' hook so context is available in all subsequent hooks and handlers | ||
| */ | ||
| server.addHook('onRequest', (request, _reply, done) => { | ||
| requestContextStorage.run({ reqId: request.id }, () => { | ||
| done(); | ||
| }); | ||
| }); | ||
| } |
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 |
|---|---|---|
| @@ -1,15 +1,21 @@ | ||
| import type { PolicyContext } from '@presentation/http/types/PolicyContext.js'; | ||
| import { getRequestLogger } from '@infrastructure/logging/index.js'; | ||
|
|
||
| /** | ||
| * Policy to enforce user to be logged in | ||
| * @param context - Context object, containing Fatify request, Fastify reply and domain services | ||
| */ | ||
| export default async function authRequired(context: PolicyContext): Promise<void> { | ||
| const { request, reply } = context; | ||
| const logger = getRequestLogger('policies'); | ||
|
|
||
| const { userId } = request; | ||
|
|
||
| if (userId === null) { | ||
| logger.warn('User is not authenticated'); | ||
|
|
||
| return await reply.unauthorized(); | ||
| } | ||
|
|
||
| logger.debug('User authenticated'); | ||
| } |
This file contains hidden or 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 hidden or 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
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.