chore: structured logging for services and access guards (part 2 of 2)#313
Draft
NoahFreelove wants to merge 15 commits intomasterfrom
Draft
chore: structured logging for services and access guards (part 2 of 2)#313NoahFreelove wants to merge 15 commits intomasterfrom
NoahFreelove wants to merge 15 commits intomasterfrom
Conversation
Adds apps/{api,api-gateway}/src/logger/sanitize.ts and applies it to the
web file-upload controller. Wave-2 callers (attempt.service, api.service)
will adopt the helper in a follow-up PR.
Matches the wave-1 middleware change that now routes 4xx responses to warn and 5xx to error.
…field
NestJS's Logger treats the second arg to .error/.warn as a stack-trace
string. Passing a metadata object produced "stack":[{...}] in the JSON
log output, which breaks log-ingest pipelines that expect stack to be
a string. Inline the target/duration info into the message instead.
0c1f5d9 to
36a1233
Compare
The try {} block wrapping bucket resolution had an empty body, so the
catch was unreachable and the user-controlled uploadType would have
been logged without sanitization if it ever fired. Drop the dead
block entirely.
36a1233 to
6fa0f26
Compare
…vents PrismaService used @nestjs/common's Logger, whose error(msg, stack?, context?) signature made nest-winston emit "stack":[null] on every prisma_error log line (observed in staging). Switch to the winston-provider injection pattern used elsewhere in the logging work, and convert .log() -> .info() and positional-error calls into structured metadata objects. Specs that instantiated PrismaService directly (database, health, admin) now provide a mock WINSTON_MODULE_PROVIDER; database.module.spec wraps it in a @global() MockWinstonModule so DatabaseModule's @global provider can resolve it.
Added in 72c6dd6 alongside the api-side helper but never imported anywhere in the gateway. The api gateway's logger.middleware still logs user-controlled fields unsanitized; plumbing this helper into the gateway is a follow-up, not this PR's scope.
- Extract getGithubConfig helper in report.service.ts, replacing 5 copy-paste "config or token missing" log+throw blocks. - Mirror formatStreamError helper from api-gateway into api's messaging.service.ts, removing two nested-ternary blocks. - Hoist duplicate safeMethod/safeEndpoint/requestId in api-gateway api.service.ts error handler (also drops non-null assertion by using forwardingDetails.endpoint directly, fixing a new lint warning). - Cache Date.now()-requestStart in lti-grade-sync.service.ts to avoid double-computing the duration. No behavior changes; 626 api tests + 53 api-gateway specs still pass.
Part-1 removed this file as dead code. Part-2 introduces genuine call sites for it in apps/api-gateway/src/api/api.service.ts when sanitizing method/endpoint/request-id/downstream_data before logging.
9c9fb80 to
1f5a801
Compare
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
PR Description
Stacked PR 2 of 2. Builds on #312 (
mark-logs-part-1). Do not merge before part 1.This PR applies the logging infrastructure landed in part 1 to the rest of the app: service layer, access-control guards, messaging, and the sanitize helper's wave-2 call sites.
Overview:
attempt.service,attempt-submission.service,lti-sync-scheduler,lti-grade-sync.service,question-response.service,openai-llm.service,report.service,api.service(gateway), bothmessaging.servicesassignment.access.control.guard,assignment.attempt.access.control.guard,assignment.question.access.control.guard,chat.access.control.guard,admin.guard,roles.global.guard,files/guards/auth.guardlogger/sanitize.tshelper (from part 1) toapi.serviceandattempt.servicecall sitesmessaging.service.spec(×2) andattempt.service.specType of Issue:
feat)bug)chore)doc)Change Type:
Test Coverage
yarn lint+yarn build+yarn test:stagedall green)Impact / Risk
messaging.serviceerror-callback wrapping (both apps) — now wraps user-supplied error callbacks so the service can log before delegating.Rollback
Reviewer Focus
report.service.ts) and one guard (e.g.assignment.access.control.guard.ts) — the rest follow the same pattern.apps/api-gateway/src/messaging/messaging.service.ts— error-callback wrapping is the only non-mechanical change.messaging.service.specuseexpect.any(Function)where the wrapped callback is now a new function identity.