-
Notifications
You must be signed in to change notification settings - Fork 19
chore: create a log for http requests #124
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
ruocco-l
wants to merge
2
commits into
master
Choose a base branch
from
chore/http-request-database
base: master
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.
+76
−0
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,66 @@ | ||
| import { Actor, ApifyClient } from 'apify'; | ||
|
|
||
| import { Routes } from './const.js'; | ||
| import { getMiniActor } from './mini-actors.js'; | ||
|
|
||
| let dataset: ReturnType<ApifyClient['dataset']> | null = null; | ||
| let userId: string | null = null; | ||
| let enabled = false; | ||
|
|
||
| // For these routes, the single allow-listed key alone doesn't warrant logging (see logRequest). | ||
| const REQUIRED_QUERY_PARAM: Record<string, string> = { | ||
| [Routes.SEARCH]: 'query', | ||
| [Routes.FETCH]: 'url', | ||
| }; | ||
|
|
||
| /** | ||
| * Initialize request logging (no-op if REQUEST_LOG_DATASET_ID is not set). Call once at startup. | ||
| */ | ||
| export function initRequestLogger(): void { | ||
| const datasetId = process.env.REQUEST_LOG_DATASET_ID; | ||
| if (!datasetId) return; | ||
|
|
||
| try { | ||
| userId = Actor.getEnv().userId; | ||
| const client = new ApifyClient({ token: process.env.REQUEST_LOG_DATASET_TOKEN }); | ||
| dataset = client.dataset(datasetId); | ||
| enabled = true; | ||
| } catch { | ||
| // Silently disable logging on any failure (invalid dataset id, etc.) | ||
| dataset = null; | ||
| enabled = false; | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * Fire-and-forget logging of an incoming request to the request-log dataset. Never throws, never blocks. | ||
| * @param pathname the request path (e.g. req.path), also used to look up the route's required query param | ||
| * @param query the raw req.query object | ||
| */ | ||
| export function logRequest(pathname: string, query: Record<string, unknown>): void { | ||
| if (!enabled || !dataset) return; | ||
|
|
||
| try { | ||
| const requiredParamKey = REQUIRED_QUERY_PARAM[pathname]; | ||
| let loggedQuery: Record<string, string> = {}; | ||
|
|
||
| delete loggedQuery.token; | ||
|
|
||
| if (requiredParamKey !== undefined) { | ||
| const keys = Object.keys(query).filter((key) => key !== requiredParamKey); | ||
| if (keys.length === 0) return; | ||
| loggedQuery = { ...(query as Record<string, string>) }; | ||
| delete loggedQuery[requiredParamKey]; | ||
| } | ||
|
|
||
| const item = { | ||
| miniActor: getMiniActor().name, | ||
| pathname, | ||
| timestamp: new Date().toISOString(), | ||
| userId, | ||
| query: loggedQuery, | ||
| }; | ||
|
|
||
| dataset.pushItems(item).catch(() => { /**/ }); | ||
| } catch { /**/ } | ||
| } | ||
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.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
and move
delete loggedQuery.token;right underdelete loggedQuery[requiredParamKey];so it's not after declaration 😄