-
Notifications
You must be signed in to change notification settings - Fork 61
refactor: unify server-side observability logging #293
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
ben-fornefeld
wants to merge
2
commits into
main
Choose a base branch
from
fix/server-side-observability
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,24 @@ | ||
| import { describe, expect, it } from 'vitest' | ||
| import { | ||
| getPublicRepoErrorMessage, | ||
| PUBLIC_ERROR_MESSAGE_INTERNAL, | ||
| repoErrorFromHttp, | ||
| } from '@/core/shared/errors' | ||
|
|
||
| describe('repoErrorFromHttp', () => { | ||
| it('preserves 400 messages as validation errors', () => { | ||
| const error = repoErrorFromHttp(400, 'User is already part of this team.') | ||
|
|
||
| expect(error.code).toBe('validation') | ||
| expect(getPublicRepoErrorMessage(error)).toBe( | ||
| 'User is already part of this team.' | ||
| ) | ||
| }) | ||
|
|
||
| it('still obfuscates unexpected internal errors', () => { | ||
| const error = repoErrorFromHttp(500, 'database exploded') | ||
|
|
||
| expect(error.code).toBe('unavailable') | ||
| expect(getPublicRepoErrorMessage(error)).toBe(PUBLIC_ERROR_MESSAGE_INTERNAL) | ||
| }) | ||
| }) |
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,49 @@ | ||
| import { describe, expect, it } from 'vitest' | ||
| import { | ||
| createRequestObservabilityContext, | ||
| createRequestObservabilityContextFromHeaders, | ||
| formatRequestLogMessage, | ||
| } from '@/core/shared/clients/logger/request-observability' | ||
|
|
||
| describe('request observability', () => { | ||
| it('extracts request path from an absolute URL', () => { | ||
| const requestContext = createRequestObservabilityContext({ | ||
| requestUrl: 'https://dashboard.test/dashboard/acme/sandboxes?tab=logs', | ||
| transport: 'action', | ||
| handlerName: 'addTeamMember', | ||
| }) | ||
|
|
||
| expect(requestContext.request_path).toBe('/dashboard/acme/sandboxes') | ||
| expect(requestContext.transport).toBe('action') | ||
| expect(requestContext.handler_name).toBe('addTeamMember') | ||
| }) | ||
|
|
||
| it('prefers referer when deriving browser-origin request context', () => { | ||
| const headers = new Headers({ | ||
| origin: 'https://dashboard.test', | ||
| referer: 'https://dashboard.test/dashboard/acme/sandboxes', | ||
| 'next-url': '/api/trpc', | ||
| }) | ||
|
|
||
| const requestContext = createRequestObservabilityContextFromHeaders( | ||
| headers, | ||
| { | ||
| fallbackPath: '/server/actions/addTeamMember', | ||
| preferReferer: true, | ||
| } | ||
| ) | ||
|
|
||
| expect(requestContext.request_url).toBe( | ||
| 'https://dashboard.test/dashboard/acme/sandboxes' | ||
| ) | ||
| expect(requestContext.request_path).toBe('/dashboard/acme/sandboxes') | ||
| }) | ||
|
|
||
| it('prefixes log messages with the request path', () => { | ||
| expect( | ||
| formatRequestLogMessage('action addTeamMember failed', { | ||
| request_path: '/dashboard/acme/sandboxes', | ||
| }) | ||
| ).toBe('/dashboard/acme/sandboxes: action addTeamMember failed') | ||
| }) | ||
| }) |
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
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.
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.
🔴 Removing the early-return guard from
logObfuscatedRepoErrorcauses double-logging for all expected repo errors (validation, not_found, conflict): once inlogObfuscatedRepoErroritself and again in the downstream telemetry handler (endTelemetryMiddlewarefor tRPC,handleServerErrorfor actions). The action transport produces three log entries per error. Consider keeping the guard and only adding thewas_obfuscated/observed_messagefields, or suppressing the downstream log when the transport adapter already logged.Extended reasoning...
What the bug is and how it manifests
Before this PR, logObfuscatedRepoError contained an early-return guard: if (publicMessage === error.message) { return }. For validation, not_found, and conflict errors, getPublicRepoErrorMessage returns error.message unchanged, so the condition was always true and the function returned without logging. The single log entry came exclusively from the downstream telemetry handler. This PR removes the guard so logObfuscatedRepoError now always calls l.warn() for expected errors (via the isExpectedRepoError branch), and the downstream handlers still log as before — producing duplicates.
The specific code paths that trigger it
tRPC path: throwTRPCErrorFromRepoError -> logObfuscatedRepoError('trpc', error) -> l.warn with key 'transport:trpc:repo_error' (NEW). The thrown TRPCError(BAD_REQUEST/NOT_FOUND/CONFLICT) propagates to endTelemetryMiddleware, where isExpectedTRPCError() returns true -> l.warn with key 'trpc:procedure_failure' (pre-existing). Total: 2 entries (was 1).
Action path: toActionErrorFromRepoError -> logObfuscatedRepoError('action', error) -> l.warn key 'transport:action:repo_error' (NEW). ActionError(expected: true) caught by handleServerError -> l.warn key 'action_client:expected_server_error' (pre-existing). Middleware sees result.serverError -> l.warn key 'action_client:failure' (pre-existing). Total: 3 entries (was 2).
Why existing code does not prevent it
The telemetry handlers have no awareness that logObfuscatedRepoError already logged the same event at the transport adapter layer. Before the PR, the guard in logObfuscatedRepoError implicitly coordinated with them by staying silent for non-obfuscated errors. Removing the guard without suppressing the downstream logs breaks this implicit contract.
Addressing the refutations
Two refutations argue this is intentional design: (1) unauthorized/forbidden already double-logged before this PR, and (2) the two entries carry genuinely different context (repo-layer detail vs. procedure timing/input). Both points are fair. However, they describe a pre-existing accepted pattern for obfuscated errors where the transport-layer context adds real value — not necessarily a deliberate extension to unobfuscated errors where both logs emit the same public message. The PR description says 'log unobfuscated repo errors as warnings', which is achievable while keeping the guard — by adding was_obfuscated, observed_message, and transport fields before the early return rather than removing it.
What the impact is
Every validation failure (e.g., duplicate team member, invalid input) and every 404 now emits 2-3 warning log lines instead of 1-2. In production these are among the most common expected errors, so this materially inflates log volume and can obscure signal in dashboards or alerting thresholds that monitor warning rates.
Step-by-step proof
How to fix it
The minimal fix restores the guard while still capturing the new fields:
function logObfuscatedRepoError(transport, error) {
const publicMessage = getPublicRepoErrorMessage(error)
const observedMessage = getObservedErrorMessage(error)
const wasObfuscated = publicMessage !== error.message
if (!wasObfuscated) {
return // downstream telemetry handler will log; skip double-log
}
// ... log with was_obfuscated/observed_message/transport fields
}
Alternatively, accept the double log but remove the redundant downstream log when was_obfuscated === false.