-
-
Notifications
You must be signed in to change notification settings - Fork 1.8k
fix(deno): Enable sessions for HTTP requests #23313
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
AG0708
wants to merge
6
commits into
getsentry:develop
Choose a base branch
from
AG0708:codex/22888-deno-http-sessions
base: develop
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.
+248
−11
Open
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
a47c68b
fix(deno): Enable sessions for HTTP requests
AG0708 0e80294
fixup! fix(deno): Enable sessions for HTTP requests
isaacs 614e1c7
fixup! fixup! fix(deno): Enable sessions for HTTP requests
isaacs f940bd3
fixup! fixup! fixup! fix(deno): Enable sessions for HTTP requests
isaacs ded3cdd
fixup! fixup! fixup! fixup! fix(deno): Enable sessions for HTTP requests
isaacs fded049
fixup! fixup! fixup! fixup! fixup! fix(deno): Enable sessions for HTT…
isaacs 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,64 @@ | ||
| // <reference lib="deno.ns" /> | ||
|
|
||
| /** | ||
| * Lives in its own file because `setupOnce` runs once per process | ||
| * (`installedIntegrations` guards it) and the diagnostics channel | ||
| * subscription is global. Deno gives each test file a fresh module graph, | ||
| * so this is the only way to install `denoHttpIntegration` with | ||
| * non-default options after `deno-http.test.ts` has installed it with | ||
| * the defaults. | ||
| */ | ||
|
|
||
| import * as http from 'node:http'; | ||
| import type { Envelope } from '@sentry/core'; | ||
| import { forEachEnvelopeItem, getIsolationScope, getMainCarrier } from '@sentry/core'; | ||
| import { assert } from 'https://deno.land/std@0.212.0/assert/assert.ts'; | ||
| import { assertEquals } from 'https://deno.land/std@0.212.0/assert/assert_equals.ts'; | ||
| import { denoHttpIntegration, init } from '../build/esm/index.js'; | ||
| import { makeTestTransport } from './transport.ts'; | ||
|
|
||
| Deno.test({ | ||
| name: 'denoHttpIntegration: node:http incoming request records no session when sessions: false', | ||
| async fn() { | ||
| getMainCarrier().__SENTRY__ = undefined; | ||
|
|
||
| const envelopes: Envelope[] = []; | ||
| const client = init({ | ||
| dsn: 'https://username@domain/123', | ||
| release: '1.0.0', | ||
| integrations: [denoHttpIntegration({ sessions: false })], | ||
| transport: makeTestTransport(envelope => { | ||
| envelopes.push(envelope); | ||
| }), | ||
| }); | ||
|
|
||
| // Captured inside the handler so we can tell "sessions were disabled" | ||
| // apart from "the request was never instrumented at all". | ||
| let isolatedTransactionName: string | undefined; | ||
| const server = http.createServer((_req, res) => { | ||
| isolatedTransactionName = getIsolationScope().getScopeData().transactionName; | ||
| res.end('ok'); | ||
| }); | ||
| const port: number = await new Promise(resolve => { | ||
| server.listen(0, '127.0.0.1', () => { | ||
| resolve((server.address() as { port: number }).port); | ||
| }); | ||
| }); | ||
|
|
||
| const response = await fetch(`http://127.0.0.1:${port}/health`); | ||
| assertEquals(await response.text(), 'ok'); | ||
| await new Promise<void>(resolve => server.close(() => resolve())); | ||
| await client.flush(2_000); | ||
|
|
||
| const itemTypes: string[] = []; | ||
| for (const envelope of envelopes) { | ||
| forEachEnvelopeItem(envelope, ([headers]) => { | ||
| itemTypes.push(headers.type); | ||
| }); | ||
| } | ||
|
|
||
| assertEquals(isolatedTransactionName, 'GET /health'); | ||
| assert(!itemTypes.includes('sessions'), `expected no session envelope item, got: ${itemTypes.join(', ')}`); | ||
| assert(!itemTypes.includes('session'), `expected no session envelope item, got: ${itemTypes.join(', ')}`); | ||
| }, | ||
| }); |
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,130 @@ | ||
| // <reference lib="deno.ns" /> | ||
|
|
||
| /** | ||
| * Lives in its own file because `setupOnce` runs once per process | ||
| * (`installedIntegrations` guards it) and the diagnostics channel | ||
| * subscription is global. Deno gives each test file a fresh module graph, | ||
| * so this is the only way to install `denoHttpIntegration` with | ||
| * `spans: false` after another file has installed it with the defaults. | ||
| * | ||
| * Both tests below share that single `spans: false` subscription. | ||
| */ | ||
|
|
||
| import * as http from 'node:http'; | ||
| import type { TransactionEvent } from '@sentry/core'; | ||
| import { getIsolationScope, getMainCarrier } from '@sentry/core'; | ||
| import { assert } from 'https://deno.land/std@0.212.0/assert/assert.ts'; | ||
| import { assertEquals } from 'https://deno.land/std@0.212.0/assert/assert_equals.ts'; | ||
| import type { DenoClient } from '../build/esm/index.js'; | ||
| import { denoHttpIntegration, init, startSpan } from '../build/esm/index.js'; | ||
|
|
||
| /** | ||
| * `spans: false` must win over `tracesSampleRate: 1`, so tracing is on | ||
| * everywhere except the HTTP integration. Without it the option would be | ||
| * indistinguishable from tracing being off. | ||
| */ | ||
| function initWithSpansDisabled(transactions: TransactionEvent[]): DenoClient { | ||
| getMainCarrier().__SENTRY__ = undefined; | ||
| return init({ | ||
| dsn: 'https://username@domain/123', | ||
| tracesSampleRate: 1, | ||
| traceLifecycle: 'static', | ||
| integrations: [denoHttpIntegration({ spans: false })], | ||
| beforeSendTransaction: (event: TransactionEvent) => { | ||
| transactions.push(event); | ||
| return null; | ||
| }, | ||
| }) as DenoClient; | ||
| } | ||
|
|
||
| Deno.test({ | ||
| name: 'denoHttpIntegration: node:http outgoing request creates no http.client span when spans: false', | ||
| async fn() { | ||
| const transactions: TransactionEvent[] = []; | ||
| const client = initWithSpansDisabled(transactions); | ||
|
|
||
| // Deno.serve for the target so this does not depend on the node:http | ||
| // server instrumentation. | ||
| const abortController = new AbortController(); | ||
| let onListen: ((_: unknown) => void) | undefined; | ||
| const listening = new Promise(resolve => (onListen = resolve)); | ||
| // Captured so we can tell "spans were disabled" apart from "the client | ||
| // was never instrumented at all" -- header injection survives spans: false. | ||
| let sentryTraceHeader: string | null = null; | ||
| const target = Deno.serve( | ||
| { port: 0, signal: abortController.signal, onListen, hostname: '127.0.0.1' }, | ||
| (request: Request) => { | ||
| sentryTraceHeader = request.headers.get('sentry-trace'); | ||
| return new Response('pong'); | ||
| }, | ||
| ); | ||
| await listening; | ||
|
|
||
| await startSpan({ name: 'parent', op: 'test' }, async () => { | ||
| await new Promise<void>((resolve, reject) => { | ||
| const req = http.request({ host: '127.0.0.1', port: target.addr.port, path: '/ping', method: 'GET' }, res => { | ||
| res.on('data', () => {}); | ||
| res.on('end', () => resolve()); | ||
| res.on('error', reject); | ||
| }); | ||
| req.on('error', reject); | ||
| req.end(); | ||
| }); | ||
| }); | ||
|
|
||
| abortController.abort(); | ||
| await target.finished; | ||
|
|
||
| // Event capture runs through the client's async processing queue, so | ||
| // drain it before reading the sink -- otherwise these assertions race. | ||
| await client.flush(5_000); | ||
|
|
||
| // The parent span proves tracing itself is live, so an absent | ||
| // http.client span is the option working rather than tracing being off. | ||
| assert(sentryTraceHeader, 'expected an injected sentry-trace header, so the client was instrumented'); | ||
| const parent = transactions.find(t => t.transaction === 'parent'); | ||
| assert(parent, `expected the 'parent' transaction, got: ${transactions.map(t => t.transaction).join(', ')}`); | ||
| const childOps = parent!.spans?.map(s => s.op) ?? []; | ||
| assertEquals( | ||
| childOps.includes('http.client'), | ||
| false, | ||
| `expected no http.client span, got ops: ${childOps.join(', ')}`, | ||
| ); | ||
|
cursor[bot] marked this conversation as resolved.
|
||
| }, | ||
| }); | ||
|
|
||
| Deno.test({ | ||
| name: 'denoHttpIntegration: node:http incoming request creates no http.server transaction when spans: false', | ||
| async fn() { | ||
| const transactions: TransactionEvent[] = []; | ||
| const client = initWithSpansDisabled(transactions); | ||
|
|
||
| // Captured inside the handler so we can tell "spans were disabled" apart | ||
| // from "the request was never instrumented at all". | ||
| let isolatedTransactionName: string | undefined; | ||
| const server = http.createServer((_req, res) => { | ||
| isolatedTransactionName = getIsolationScope().getScopeData().transactionName; | ||
| res.end('ok'); | ||
| }); | ||
| const port: number = await new Promise(resolve => { | ||
| server.listen(0, '127.0.0.1', () => { | ||
| resolve((server.address() as { port: number }).port); | ||
| }); | ||
| }); | ||
|
|
||
| const response = await fetch(`http://127.0.0.1:${port}/users/42`); | ||
| assertEquals(await response.text(), 'ok'); | ||
| await new Promise<void>(resolve => server.close(() => resolve())); | ||
|
|
||
| // Drain the async processing queue first. Without this, "no http.server | ||
| // transaction yet" and "no http.server transaction at all" look alike, | ||
| // so the assertion below could pass while spans were still enabled. | ||
| await client.flush(5_000); | ||
|
|
||
| // Request isolation still runs with spans off, so this proves the | ||
| // instrumentation saw the request. | ||
| assertEquals(isolatedTransactionName, 'GET /users/42'); | ||
| const ops = transactions.map(t => t.contexts?.trace?.op); | ||
| assertEquals(ops.includes('http.server'), false, `expected no http.server transaction, got ops: ${ops.join(', ')}`); | ||
| }, | ||
| }); | ||
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.