Skip to content

Structured logging with open telemetry#155

Open
gloskull wants to merge 2 commits into
Utility-Protocol:mainfrom
gloskull:Structured-Logging-with-OpenTelemetry
Open

Structured logging with open telemetry#155
gloskull wants to merge 2 commits into
Utility-Protocol:mainfrom
gloskull:Structured-Logging-with-OpenTelemetry

Conversation

@gloskull

Copy link
Copy Markdown
Contributor

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

@elizabetheonoja-art

Copy link
Copy Markdown
Contributor

resolve conflicts @gloskull

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Structured Logging with OpenTelemetry Semantic Conventions

2 participants