Skip to content

Commit eebd5cc

Browse files
committed
remove tracing config runtime check, cleanup, move some files
1 parent 927cbf6 commit eebd5cc

File tree

19 files changed

+101
-199
lines changed

19 files changed

+101
-199
lines changed

dev-packages/e2e-tests/test-applications/sveltekit-2-kit-tracing/tests/errors.server.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ test.describe('server-side errors', () => {
7171

7272
expect(errorEventFrames?.[errorEventFrames?.length - 1]).toEqual(
7373
expect.objectContaining({
74-
filename: expect.stringMatching(/app:\/\/\/chunks\/_server.ts-.+.js/),
74+
filename: expect.stringMatching(/app:\/\/\/_server.ts-.+.js/),
7575
function: 'GET',
7676
in_app: true,
7777
}),

dev-packages/e2e-tests/test-applications/sveltekit-2-kit-tracing/tests/test.json

Lines changed: 0 additions & 49 deletions
This file was deleted.

packages/sveltekit/src/server-common/handle.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -170,7 +170,7 @@ async function instrumentHandle(
170170
// Update the root span emitted from SvelteKit to resemble a `http.server` span
171171
// We're doing this here instead of an event processor to ensure we update the
172172
// span name as early as possible (for dynamic sampling, et al.)
173-
// Other spans are enhanced in the `processKitSpans` function.
173+
// Other spans are enhanced in the `processKitSpans` integration.
174174
const spanJson = spanToJSON(kitRootSpan);
175175
const kitRootSpanAttributes = spanJson.data;
176176
const originalName = spanJson.description;

packages/sveltekit/src/server-common/rewriteFramesIntegration.ts renamed to packages/sveltekit/src/server-common/integrations/rewriteFramesIntegration.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,8 @@ import {
77
join,
88
rewriteFramesIntegration as originalRewriteFramesIntegration,
99
} from '@sentry/core';
10-
import { WRAPPED_MODULE_SUFFIX } from '../common/utils';
11-
import type { GlobalWithSentryValues } from '../vite/injectGlobalValues';
10+
import { WRAPPED_MODULE_SUFFIX } from '../../common/utils';
11+
import type { GlobalWithSentryValues } from '../../vite/injectGlobalValues';
1212

1313
type StackFrameIteratee = (frame: StackFrame) => StackFrame;
1414
interface RewriteFramesOptions {

packages/sveltekit/src/server-common/processKitSpans.ts renamed to packages/sveltekit/src/server-common/integrations/svelteKitSpans.ts

File renamed without changes.

packages/sveltekit/src/server-common/utils.ts

Lines changed: 0 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -53,17 +53,3 @@ export function sendErrorToSentry(e: unknown, handlerFn: 'handle' | 'load' | 'se
5353

5454
return objectifiedErr;
5555
}
56-
57-
/**
58-
* During build, we inject the SvelteKit tracing config into the global object of the server.
59-
* @returns tracing config (available since 2.31.0)
60-
*/
61-
export function getKitTracingConfig(): { instrumentation: boolean; tracing: boolean } {
62-
const globalWithSentryValues: GlobalWithSentryValues = GLOBAL_OBJ;
63-
const kitTracingConfig = globalWithSentryValues.__sentry_sveltekit_tracing_config;
64-
65-
return {
66-
instrumentation: kitTracingConfig?.instrumentation?.server ?? false,
67-
tracing: kitTracingConfig?.tracing?.server ?? false,
68-
};
69-
}

packages/sveltekit/src/server/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,6 @@ export {
5353
getTraceMetaTags,
5454
graphqlIntegration,
5555
hapiIntegration,
56-
httpIntegration,
5756
// eslint-disable-next-line deprecation/deprecation
5857
inboundFiltersIntegration,
5958
eventFiltersIntegration,
@@ -141,6 +140,7 @@ export { wrapLoadWithSentry, wrapServerLoadWithSentry } from '../server-common/l
141140
export { sentryHandle } from '../server-common/handle';
142141
export { initCloudflareSentryHandle } from './handle';
143142
export { wrapServerRouteWithSentry } from '../server-common/serverRoute';
143+
export { httpIntegration } from './integrations/http';
144144

145145
/**
146146
* Tracks the Svelte component's initialization and mounting operation as well as
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
import type { IntegrationFn } from '@sentry/core';
2+
import { httpIntegration as originalHttpIntegration } from '@sentry/node';
3+
4+
type HttpOptions = Parameters<typeof originalHttpIntegration>[0];
5+
6+
/**
7+
* The http integration instruments Node's internal http and https modules.
8+
* It creates breadcrumbs and spans for outgoing HTTP requests which will be attached to the currently active span.
9+
*
10+
* For SvelteKit, does not create spans for incoming requests but instead we use SvelteKit's own spans.
11+
* If you need to create incoming spans, set the `disableIncomingRequestSpans` option to `false`.
12+
* (You likely don't need this!)
13+
*
14+
*/
15+
export const httpIntegration = ((options: HttpOptions = {}) => {
16+
/*
17+
* This is a slightly modified version of the original httpIntegration: We avoid creating
18+
* incoming request spans because:
19+
*
20+
* - If Kit-tracing is available and enabled, we take the `sveltekit.handle.root` span
21+
* as the root span and make it the `http.server` span. This gives us a single root
22+
* span across all deployment plaftorms (while httpIntegration doesn't apply on e.g.
23+
* AWS Lambda or edge)
24+
* - If Kit-tracing is N/A or disabled and users follow the current/old docs, httpIntegration
25+
* does nothing anyway, so this isn't a concern.
26+
* - Which leaves the undocumented case that users --import an instrument.mjs file
27+
* in which they initialize the SDK. IMHO it's fine to ignore this for now since it was
28+
* well ... undocumented. Given in the future there won't be be an easy way for us
29+
* to detect where the SDK is initialized, we should simply redirect users to use
30+
* instrumentation.server.ts instead. If users want to, they can simply import and
31+
* register `httpIntegration` and explicitly enable incoming request spans.
32+
*/
33+
34+
return originalHttpIntegration({
35+
// We disable incoming request spans here, because otherwise we'd end up with duplicate spans.
36+
disableIncomingRequestSpans: true,
37+
...options,
38+
});
39+
}) satisfies IntegrationFn;

packages/sveltekit/src/server/sdk.ts

Lines changed: 11 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,35 +1,21 @@
1-
import { applySdkMetadata } from '@sentry/core';
1+
import { applySdkMetadata, hasSpansEnabled } from '@sentry/core';
22
import type { NodeClient, NodeOptions } from '@sentry/node';
3-
import {
4-
getDefaultIntegrations as getDefaultNodeIntegrations,
5-
httpIntegration,
6-
init as initNodeSdk,
7-
} from '@sentry/node';
8-
import { svelteKitSpansIntegration } from '../server-common/processKitSpans';
9-
import { rewriteFramesIntegration } from '../server-common/rewriteFramesIntegration';
10-
import { getKitTracingConfig } from '../server-common/utils';
3+
import { getDefaultIntegrations as getDefaultNodeIntegrations, init as initNodeSdk } from '@sentry/node';
4+
import { rewriteFramesIntegration } from '../server-common/integrations/rewriteFramesIntegration';
5+
import { svelteKitSpansIntegration } from '../server-common/integrations/svelteKitSpans';
6+
import { httpIntegration } from './integrations/http';
117

128
/**
139
* Initialize the Server-side Sentry SDK
1410
* @param options
1511
*/
1612
export function init(options: NodeOptions): NodeClient | undefined {
17-
const defaultIntegrations = [...getDefaultNodeIntegrations(options), rewriteFramesIntegration()];
18-
19-
const config = getKitTracingConfig();
20-
if (config.instrumentation) {
21-
// Whenever `instrumentation` is enabled, we don't need httpIntegration to emit spans
22-
// - if `tracing` is enabled, kit will emit the root span
23-
// - if `tracing` is disabled, our handler will emit the root span
24-
// In old (hooks.server.ts) based SDK setups, adding the default version of the integration does nothing
25-
// for incoming requests. We still add it in default config for the undocumented case that anyone is
26-
// using the SDK by `--import`ing the SDK setup directly (e.g. with adapter-node).
27-
defaultIntegrations.push(httpIntegration({ disableIncomingRequestSpans: true }));
28-
if (config.tracing) {
29-
// If `tracing` is enabled, we need to instrument spans for the server
30-
defaultIntegrations.push(svelteKitSpansIntegration());
31-
}
32-
}
13+
const defaultIntegrations = [
14+
...getDefaultNodeIntegrations(options).filter(integration => integration.name !== 'Http'),
15+
rewriteFramesIntegration(),
16+
httpIntegration(),
17+
svelteKitSpansIntegration(),
18+
];
3319

3420
const opts = {
3521
defaultIntegrations,

packages/sveltekit/src/vite/injectGlobalValues.ts

Lines changed: 5 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,11 @@
11
import { type InternalGlobal, escapeStringForRegex } from '@sentry/core';
22
import MagicString from 'magic-string';
33
import type { Plugin } from 'vite';
4-
import {
5-
type BackwardsForwardsCompatibleSvelteConfig,
6-
type SvelteKitTracingConfig,
7-
getAdapterOutputDir,
8-
getHooksFileName,
9-
getTracingConfig,
10-
} from './svelteConfig';
4+
import { type BackwardsForwardsCompatibleSvelteConfig, getAdapterOutputDir, getHooksFileName } from './svelteConfig';
115
import type { SentrySvelteKitPluginOptions } from './types';
126

137
export type GlobalSentryValues = {
148
__sentry_sveltekit_output_dir?: string;
15-
__sentry_sveltekit_tracing_config?: SvelteKitTracingConfig;
169
};
1710

1811
/**
@@ -54,14 +47,16 @@ export async function makeGlobalValuesInjectionPlugin(
5447

5548
const globalSentryValues: GlobalSentryValues = {
5649
__sentry_sveltekit_output_dir: adapterOutputDir,
57-
__sentry_sveltekit_tracing_config: getTracingConfig(svelteConfig),
5850
};
5951

6052
if (debug) {
6153
// eslint-disable-next-line no-console
6254
console.log('[Sentry SvelteKit] Global values:', globalSentryValues);
6355
}
6456

57+
// eslint-disable-next-line @sentry-internal/sdk/no-regexp-constructor -- not end user input + escaped anyway
58+
const hooksFileRegexp = new RegExp(`/${escapeStringForRegex(serverHooksFile)}(.(js|ts|mjs|mts))?`);
59+
6560
return {
6661
name: 'sentry-sveltekit-global-values-injection-plugin',
6762
resolveId: (id, _importer, _ref) => {
@@ -85,10 +80,7 @@ export async function makeGlobalValuesInjectionPlugin(
8580
},
8681

8782
transform: async (code, id) => {
88-
const isServerEntryFile =
89-
/instrumentation\.server\./.test(id) ||
90-
// eslint-disable-next-line @sentry-internal/sdk/no-regexp-constructor -- not end user input + escaped anyway
91-
new RegExp(`/${escapeStringForRegex(serverHooksFile)}(.(js|ts|mjs|mts))?`).test(id);
83+
const isServerEntryFile = /instrumentation\.server\./.test(id) || hooksFileRegexp.test(id);
9284

9385
if (isServerEntryFile) {
9486
if (debug) {

0 commit comments

Comments
 (0)