-
Notifications
You must be signed in to change notification settings - Fork 4
Add simple nextjs request logger plus fix OTEL resource attributes #3247
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
Merged
Merged
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
b9f17a6
Add OTEL resource overrides and request span logs
ChristopherChudzicki fed8799
add a comment
ChristopherChudzicki ed5fe7d
support env.OTEL_EXPORTER_OTLP_TRACES_ENDPOINT
ChristopherChudzicki 6d2a2c7
Use OTEL envDetector for resource attributes
ChristopherChudzicki b218b2e
Include app version in structured request logs
ChristopherChudzicki ec3b1bc
Set NEXT_PUBLIC_VERSION in jest shared setup
ChristopherChudzicki e5eb18e
Decouple request logging from OTEL via diagnostics_channel
ChristopherChudzicki 9c5667a
Guard request-logger subscription against double-evaluation
ChristopherChudzicki 89a35f2
reword comment
ChristopherChudzicki 14ab0aa
Filter Next-internal paths from request logs and split out query
ChristopherChudzicki 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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,186 @@ | ||
| import type { IncomingMessage, ServerResponse } from "node:http" | ||
| import type { ReadableSpan } from "@opentelemetry/sdk-trace-base" | ||
| import { | ||
| applyResourceOverrides, | ||
| createRequestLogEntry, | ||
| detectResourceOverrides, | ||
| hasOtlpEndpointConfig, | ||
| } from "./otel-utils" | ||
|
|
||
| function makeResource( | ||
| attributes: ReadableSpan["resource"]["attributes"] = {}, | ||
| ): ReadableSpan["resource"] { | ||
| const resource: ReadableSpan["resource"] = { | ||
| attributes, | ||
| merge(other) { | ||
| return other ?? resource | ||
| }, | ||
| getRawAttributes() { | ||
| return Object.entries(resource.attributes) | ||
| }, | ||
| } | ||
| return resource | ||
| } | ||
|
|
||
| const makeReadableSpan = ( | ||
| overrides: Partial<ReadableSpan> = {}, | ||
| ): ReadableSpan => ({ | ||
| name: "test span", | ||
| kind: 0, | ||
| spanContext: () => ({ | ||
| traceId: "trace-id", | ||
| spanId: "span-id", | ||
| traceFlags: 1, | ||
| }), | ||
| startTime: [0, 0], | ||
| endTime: [0, 0], | ||
| status: { code: 0 }, | ||
| attributes: {}, | ||
| links: [], | ||
| events: [], | ||
| duration: [0, 0], | ||
| ended: true, | ||
| resource: makeResource(), | ||
| instrumentationScope: { name: "test-scope", version: "1.0.0" }, | ||
| droppedAttributesCount: 0, | ||
| droppedEventsCount: 0, | ||
| droppedLinksCount: 0, | ||
| ...overrides, | ||
| }) | ||
|
|
||
| describe("createRequestLogEntry", () => { | ||
| it("builds a log entry from request and response", () => { | ||
| const request = { method: "GET", url: "/courses" } as IncomingMessage | ||
| const response = { statusCode: 200 } as ServerResponse | ||
|
|
||
| expect( | ||
| createRequestLogEntry({ request, response, durationMs: 1250 }), | ||
| ).toEqual({ | ||
| message: "next_request", | ||
| method: "GET", | ||
| route: "/courses", | ||
| query: null, | ||
| statusCode: 200, | ||
| durationMs: 1250, | ||
| traceId: null, | ||
| spanId: null, | ||
| version: "test-version", | ||
| }) | ||
| }) | ||
|
|
||
| it("splits route and query when the URL has a query string", () => { | ||
| const request = { | ||
| method: "POST", | ||
| url: "/api/foo?bar=baz&qux=1", | ||
| } as IncomingMessage | ||
| const response = { statusCode: 201 } as ServerResponse | ||
|
|
||
| const entry = createRequestLogEntry({ request, response, durationMs: 5 }) | ||
| expect(entry.route).toBe("/api/foo") | ||
| expect(entry.query).toBe("bar=baz&qux=1") | ||
| }) | ||
|
|
||
| it("falls back to UNKNOWN when method is missing", () => { | ||
| const request = { url: "/" } as IncomingMessage | ||
| const response = { statusCode: 500 } as ServerResponse | ||
|
|
||
| expect( | ||
| createRequestLogEntry({ request, response, durationMs: 1 }).method, | ||
| ).toBe("UNKNOWN") | ||
| }) | ||
| }) | ||
|
|
||
| describe("applyResourceOverrides", () => { | ||
| it("copies overrides onto the span resource", () => { | ||
| const span = makeReadableSpan({ | ||
| resource: makeResource({ | ||
| "service.name": "node", | ||
| "service.namespace": "sentry", | ||
| }), | ||
| }) | ||
|
|
||
| applyResourceOverrides(span, { | ||
| "service.name": "learn-nextjs", | ||
| "deployment.environment.name": "prod", | ||
| }) | ||
|
|
||
| expect(span.resource.attributes["service.name"]).toBe("learn-nextjs") | ||
| expect(span.resource.attributes["service.namespace"]).toBe("sentry") | ||
| expect(span.resource.attributes["deployment.environment.name"]).toBe("prod") | ||
| }) | ||
|
|
||
| it("leaves the resource unchanged when overrides is empty", () => { | ||
| const span = makeReadableSpan({ | ||
| resource: makeResource({ "service.name": "node" }), | ||
| }) | ||
|
|
||
| applyResourceOverrides(span, {}) | ||
|
|
||
| expect(span.resource.attributes["service.name"]).toBe("node") | ||
| }) | ||
|
|
||
| it("skips non-string values defensively", () => { | ||
| const span = makeReadableSpan({ | ||
| resource: makeResource({ "service.name": "node" }), | ||
| }) | ||
|
|
||
| applyResourceOverrides(span, { | ||
| "service.name": "learn-nextjs", | ||
| "broken.promise": Promise.resolve("ignored"), | ||
| "broken.array": ["a", "b"], | ||
| }) | ||
|
|
||
| expect(span.resource.attributes["service.name"]).toBe("learn-nextjs") | ||
| expect(span.resource.attributes["broken.promise"]).toBeUndefined() | ||
| expect(span.resource.attributes["broken.array"]).toBeUndefined() | ||
| }) | ||
| }) | ||
|
|
||
| describe("detectResourceOverrides", () => { | ||
| const originalEnv = { ...process.env } | ||
|
|
||
| afterEach(() => { | ||
| process.env = { ...originalEnv } | ||
| }) | ||
|
|
||
| it("parses OTEL_SERVICE_NAME and OTEL_RESOURCE_ATTRIBUTES via the SDK", () => { | ||
| process.env.OTEL_SERVICE_NAME = "app-name" | ||
| process.env.OTEL_RESOURCE_ATTRIBUTES = | ||
| "service.namespace=learn,service.version=1.2.3" | ||
|
|
||
| expect(detectResourceOverrides()).toEqual({ | ||
| "service.name": "app-name", | ||
| "service.namespace": "learn", | ||
| "service.version": "1.2.3", | ||
| }) | ||
| }) | ||
|
|
||
| it("percent-decodes values per the OTEL spec", () => { | ||
| process.env.OTEL_RESOURCE_ATTRIBUTES = | ||
| "deployment.environment.name=us%2Ceast,service.version=1%3D2" | ||
| delete process.env.OTEL_SERVICE_NAME | ||
|
|
||
| expect(detectResourceOverrides()).toEqual({ | ||
| "deployment.environment.name": "us,east", | ||
| "service.version": "1=2", | ||
| }) | ||
| }) | ||
|
|
||
| it("returns an empty object when no env vars are set", () => { | ||
| delete process.env.OTEL_SERVICE_NAME | ||
| delete process.env.OTEL_RESOURCE_ATTRIBUTES | ||
|
|
||
| expect(detectResourceOverrides()).toEqual({}) | ||
| }) | ||
| }) | ||
|
|
||
| describe("hasOtlpEndpointConfig", () => { | ||
| it("returns true when only OTEL_EXPORTER_OTLP_TRACES_ENDPOINT is set", () => { | ||
| expect( | ||
| hasOtlpEndpointConfig({ | ||
| OTEL_EXPORTER_OTLP_TRACES_ENDPOINT: | ||
| "http://alloy.monitoring:4318/v1/traces", | ||
| }), | ||
| ).toBe(true) | ||
| }) | ||
| }) |
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.
This seems likely to be fixed soon, see
though i'd still like to have the data before their fix is merged/released.