-
Notifications
You must be signed in to change notification settings - Fork 28
feat(instrumentation): Add deployment metadata to telemetry #578
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
+237
−10
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
91b6f39
feat(instrumentation): Add deployment metadata to spans
dcramer fff8788
feat(instrumentation): Add deployment metadata to logs
dcramer c4012ac
fix(instrumentation): Harden deployment telemetry attributes
dcramer d82a794
fix(instrumentation): Align release deployment metadata
dcramer 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,78 @@ | ||
| import { afterEach, describe, expect, it, vi } from "vitest"; | ||
|
|
||
| const ORIGINAL_SENTRY_DSN = process.env.SENTRY_DSN; | ||
| const ORIGINAL_SENTRY_RELEASE = process.env.SENTRY_RELEASE; | ||
| const ORIGINAL_VERCEL_DEPLOYMENT_ID = process.env.VERCEL_DEPLOYMENT_ID; | ||
| const ORIGINAL_VERCEL_GIT_COMMIT_SHA = process.env.VERCEL_GIT_COMMIT_SHA; | ||
|
|
||
| function resetEnv(): void { | ||
| if (ORIGINAL_SENTRY_DSN === undefined) { | ||
| delete process.env.SENTRY_DSN; | ||
| } else { | ||
| process.env.SENTRY_DSN = ORIGINAL_SENTRY_DSN; | ||
| } | ||
| if (ORIGINAL_SENTRY_RELEASE === undefined) { | ||
| delete process.env.SENTRY_RELEASE; | ||
| } else { | ||
| process.env.SENTRY_RELEASE = ORIGINAL_SENTRY_RELEASE; | ||
| } | ||
| if (ORIGINAL_VERCEL_DEPLOYMENT_ID === undefined) { | ||
| delete process.env.VERCEL_DEPLOYMENT_ID; | ||
| } else { | ||
| process.env.VERCEL_DEPLOYMENT_ID = ORIGINAL_VERCEL_DEPLOYMENT_ID; | ||
| } | ||
| if (ORIGINAL_VERCEL_GIT_COMMIT_SHA === undefined) { | ||
| delete process.env.VERCEL_GIT_COMMIT_SHA; | ||
| } else { | ||
| process.env.VERCEL_GIT_COMMIT_SHA = ORIGINAL_VERCEL_GIT_COMMIT_SHA; | ||
| } | ||
| } | ||
|
|
||
| async function loadInstrumentationModule() { | ||
| vi.resetModules(); | ||
| const init = vi.fn(); | ||
| vi.doMock("@/chat/sentry", () => ({ | ||
| getClient: () => undefined, | ||
| init, | ||
| vercelAIIntegration: () => ({ name: "vercel-ai" }), | ||
| })); | ||
| const instrumentation = await import("@/instrumentation"); | ||
| return { init, instrumentation }; | ||
| } | ||
|
|
||
| afterEach(() => { | ||
| vi.restoreAllMocks(); | ||
| vi.resetModules(); | ||
| vi.doUnmock("@/chat/sentry"); | ||
| resetEnv(); | ||
| }); | ||
|
|
||
| describe("initSentry", () => { | ||
| it("adds deployment metadata to outgoing spans", async () => { | ||
| process.env.SENTRY_DSN = "https://public@example.com/1"; | ||
| process.env.SENTRY_RELEASE = " "; | ||
| process.env.VERCEL_DEPLOYMENT_ID = "dpl_123"; | ||
| process.env.VERCEL_GIT_COMMIT_SHA = "git-sha"; | ||
|
|
||
| const { init, instrumentation } = await loadInstrumentationModule(); | ||
| instrumentation.initSentry(); | ||
|
|
||
| expect(init).toHaveBeenCalledTimes(1); | ||
| const options = init.mock.calls[0]?.[0]; | ||
| expect(options?.release).toBe("git-sha"); | ||
| expect(options?.beforeSendSpan).toBeTypeOf("function"); | ||
|
|
||
| const span = { | ||
| data: { | ||
| "deployment.id": "span-deployment", | ||
| "service.version": "span-version", | ||
| }, | ||
| }; | ||
|
|
||
| expect(options.beforeSendSpan(span)).toBe(span); | ||
| expect(span.data).toMatchObject({ | ||
| "deployment.id": "dpl_123", | ||
| "service.version": "git-sha", | ||
| }); | ||
| }); | ||
| }); |
79 changes: 79 additions & 0 deletions
79
packages/junior/tests/unit/logging/deployment-attributes.test.ts
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,79 @@ | ||
| import { afterEach, describe, expect, it, vi } from "vitest"; | ||
| import type { EmittedLogRecord } from "@/chat/logging"; | ||
|
|
||
| const ORIGINAL_SENTRY_RELEASE = process.env.SENTRY_RELEASE; | ||
| const ORIGINAL_VERCEL_DEPLOYMENT_ID = process.env.VERCEL_DEPLOYMENT_ID; | ||
| const ORIGINAL_VERCEL_GIT_COMMIT_SHA = process.env.VERCEL_GIT_COMMIT_SHA; | ||
|
|
||
| async function loadLoggingModule() { | ||
| vi.resetModules(); | ||
| vi.doMock("@/chat/sentry", () => ({ | ||
| captureException: undefined, | ||
| captureMessage: undefined, | ||
| getActiveSpan: () => undefined, | ||
| logger: {}, | ||
| setTag: undefined, | ||
| setUser: undefined, | ||
| spanToJSON: () => ({}), | ||
| withScope: undefined, | ||
| })); | ||
| return await import("@/chat/logging"); | ||
| } | ||
|
|
||
| function resetEnv(): void { | ||
| if (ORIGINAL_SENTRY_RELEASE === undefined) { | ||
| delete process.env.SENTRY_RELEASE; | ||
| } else { | ||
| process.env.SENTRY_RELEASE = ORIGINAL_SENTRY_RELEASE; | ||
| } | ||
| if (ORIGINAL_VERCEL_DEPLOYMENT_ID === undefined) { | ||
| delete process.env.VERCEL_DEPLOYMENT_ID; | ||
| } else { | ||
| process.env.VERCEL_DEPLOYMENT_ID = ORIGINAL_VERCEL_DEPLOYMENT_ID; | ||
| } | ||
| if (ORIGINAL_VERCEL_GIT_COMMIT_SHA === undefined) { | ||
| delete process.env.VERCEL_GIT_COMMIT_SHA; | ||
| } else { | ||
| process.env.VERCEL_GIT_COMMIT_SHA = ORIGINAL_VERCEL_GIT_COMMIT_SHA; | ||
| } | ||
| } | ||
|
|
||
| afterEach(() => { | ||
| vi.restoreAllMocks(); | ||
| vi.resetModules(); | ||
| vi.doUnmock("@/chat/sentry"); | ||
| resetEnv(); | ||
| }); | ||
|
|
||
| describe("deployment log attributes", () => { | ||
| it("adds deployment metadata to emitted log records", async () => { | ||
| process.env.SENTRY_RELEASE = " "; | ||
| process.env.VERCEL_DEPLOYMENT_ID = "dpl_123"; | ||
| process.env.VERCEL_GIT_COMMIT_SHA = "git-sha"; | ||
|
|
||
| const { log, registerLogRecordSink } = await loadLoggingModule(); | ||
| const records: EmittedLogRecord[] = []; | ||
| const unregister = registerLogRecordSink((record) => { | ||
| records.push(record); | ||
| }); | ||
|
|
||
| try { | ||
| log.warn( | ||
| "deployment_context_test", | ||
| { | ||
| "deployment.id": "caller-deployment", | ||
| "service.version": "caller-version", | ||
| }, | ||
| "Deployment context test", | ||
| ); | ||
| } finally { | ||
| unregister(); | ||
| } | ||
|
|
||
| expect(records).toHaveLength(1); | ||
| expect(records[0]?.attributes).toMatchObject({ | ||
| "deployment.id": "dpl_123", | ||
| "service.version": "git-sha", | ||
| }); | ||
| }); | ||
| }); |
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.
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.
Uh oh!
There was an error while loading. Please reload this page.