Structured logging with open telemetry#155
Open
gloskull wants to merge 2 commits into
Open
Conversation
…h-opentelemetry Add OpenTelemetry structured logging
Contributor
|
resolve conflicts @gloskull |
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.
Motivation
Implement a system-wide structured logging facility following OpenTelemetry semantic conventions to provide consistent JSON logs, trace propagation, and sensitive-attribute redaction across the frontend.
Ensure server startup instrumentation and request-error handling are recorded in a structured way for observability and incident triage.
Emit HTTP request completion logs from the shared API client to capture latency, status, and error metadata for monitoring and alerting.
Description
Add src/utils/telemetry/structuredLogger.ts, a configurable structured logger that maps severity text to OpenTelemetry severity numbers, redacts sensitive attributes, parses W3C traceparent, and supports pluggable sinks.
Add src/instrumentation.ts to register the structured logger on Next.js server startup and implement onRequestError to log errors with normalized exception fields, request/route context, and parsed trace context.
Instrument the shared API client in src/services/api.ts to call logApiRequest on completion or error and record http.request.method, http.response.status_code, url.path, duration.ms, and error details.
Add unit tests in tests/unit/structuredLogger.test.ts covering severity/resource attributes, attribute redaction, W3C trace propagation, and malformed traceparent handling.
Testing
Ran unit tests with npm test -- tests/unit/structuredLogger.test.ts, which passed (4 tests, 4 passed).
Ran npx eslint against the modified files (src/utils/telemetry/structuredLogger.ts, src/instrumentation.ts, src/services/api.ts), which completed for the changed files (no new errors introduced in those files).
Ran type checks with npx tsc --noEmit, which passed after fixes for unknown error handling and header typing.
Attempted npm run build, which failed due to environment/network inability to fetch Google Fonts used by the app (unrelated to the logging changes).
Note: running full-repo npm run lint still reports pre-existing unrelated lint errors in other files and was not addressed by this change.
Closes #118