Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit 5e441a7

Browse files
committedMar 24, 2025·
ref: Refactor some more dropUndefinedKeys
1 parent 34b69b3 commit 5e441a7

File tree

10 files changed

+36
-40
lines changed

10 files changed

+36
-40
lines changed
 

‎packages/core/src/tracing/dynamicSamplingContext.ts

+12-3
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import {
88
baggageHeaderToDynamicSamplingContext,
99
dynamicSamplingContextToSentryBaggageHeader,
1010
} from '../utils-hoist/baggage';
11-
import { addNonEnumerableProperty, dropUndefinedKeys } from '../utils-hoist/object';
11+
import { addNonEnumerableProperty } from '../utils-hoist/object';
1212
import { hasSpansEnabled } from '../utils/hasSpansEnabled';
1313
import { getRootSpan, spanIsSampled, spanToJSON } from '../utils/spanUtils';
1414
import { getCapturedScopesOnSpan } from './utils';
@@ -41,12 +41,21 @@ export function getDynamicSamplingContextFromClient(trace_id: string, client: Cl
4141

4242
const { publicKey: public_key } = client.getDsn() || {};
4343

44-
const dsc = dropUndefinedKeys({
44+
// Instead of conditionally adding non-undefined values, we add them and then remove them if needed
45+
// otherwise, the order of baggage entries changes, which "breaks" a bunch of tests etc.
46+
const dsc: DynamicSamplingContext = {
4547
environment: options.environment || DEFAULT_ENVIRONMENT,
4648
release: options.release,
4749
public_key,
4850
trace_id,
49-
}) satisfies DynamicSamplingContext;
51+
};
52+
53+
if (!dsc.release) {
54+
delete dsc.release;
55+
}
56+
if (!dsc.public_key) {
57+
delete dsc.public_key;
58+
}
5059

5160
client.emit('createDsc', dsc);
5261

‎packages/core/src/types-hoist/envelope.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ import type { SpanJSON } from './span';
1717
// Based on https://github.com/getsentry/relay/blob/b23b8d3b2360a54aaa4d19ecae0231201f31df5e/relay-sampling/src/lib.rs#L685-L707
1818
export type DynamicSamplingContext = {
1919
trace_id: string;
20-
public_key: DsnComponents['publicKey'];
20+
public_key?: DsnComponents['publicKey'];
2121
sample_rate?: string;
2222
release?: string;
2323
environment?: string;

‎packages/core/src/utils-hoist/anr.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -81,12 +81,12 @@ export function callFrameToStackFrame(
8181
const colno = frame.location.columnNumber ? frame.location.columnNumber + 1 : undefined;
8282
const lineno = frame.location.lineNumber ? frame.location.lineNumber + 1 : undefined;
8383

84-
return dropUndefinedKeys({
84+
return {
8585
filename,
8686
module: getModuleFromFilename(filename),
8787
function: frame.functionName || UNKNOWN_FUNCTION,
8888
colno,
8989
lineno,
9090
in_app: filename ? filenameIsInApp(filename) : undefined,
91-
});
91+
};
9292
}

‎packages/core/src/utils-hoist/envelope.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -196,13 +196,13 @@ export function createAttachmentEnvelopeItem(attachment: Attachment): Attachment
196196
const buffer = typeof attachment.data === 'string' ? encodeUTF8(attachment.data) : attachment.data;
197197

198198
return [
199-
dropUndefinedKeys({
199+
{
200200
type: 'attachment',
201201
length: buffer.length,
202202
filename: attachment.filename,
203203
content_type: attachment.contentType,
204204
attachment_type: attachment.attachmentType,
205-
}),
205+
},
206206
buffer,
207207
];
208208
}

‎packages/core/src/utils/request.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -91,14 +91,14 @@ export function httpRequestToRequestData(request: {
9191
// This is non-standard, but may be set on e.g. Next.js or Express requests
9292
const cookies = (request as PolymorphicRequest).cookies;
9393

94-
return dropUndefinedKeys({
94+
return {
9595
url: absoluteUrl,
9696
method: request.method,
9797
query_string: extractQueryParamsFromUrl(url),
9898
headers: headersToDict(headers),
9999
cookies,
100100
data,
101-
});
101+
};
102102
}
103103

104104
function getAbsoluteUrl({

‎packages/core/src/utils/spanUtils.ts

+4-4
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ export function spanToTransactionTraceContext(span: Span): TraceContext {
4242
const { spanId: span_id, traceId: trace_id } = span.spanContext();
4343
const { data, op, parent_span_id, status, origin, links } = spanToJSON(span);
4444

45-
return dropUndefinedKeys({
45+
return {
4646
parent_span_id,
4747
span_id,
4848
trace_id,
@@ -51,7 +51,7 @@ export function spanToTransactionTraceContext(span: Span): TraceContext {
5151
status,
5252
origin,
5353
links,
54-
});
54+
};
5555
}
5656

5757
/**
@@ -67,11 +67,11 @@ export function spanToTraceContext(span: Span): TraceContext {
6767

6868
const span_id = isRemote ? scope?.getPropagationContext().propagationSpanId || generateSpanId() : spanId;
6969

70-
return dropUndefinedKeys({
70+
return {
7171
parent_span_id,
7272
span_id,
7373
trace_id,
74-
});
74+
};
7575
}
7676

7777
/**

‎packages/core/src/utils/transactionEvent.ts

+3-6
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,13 @@
11
import { SEMANTIC_ATTRIBUTE_EXCLUSIVE_TIME, SEMANTIC_ATTRIBUTE_PROFILE_ID } from '../semanticAttributes';
22
import type { SpanJSON, TransactionEvent } from '../types-hoist';
3-
import { dropUndefinedKeys } from '../utils-hoist';
43

54
/**
65
* Converts a transaction event to a span JSON object.
76
*/
87
export function convertTransactionEventToSpanJson(event: TransactionEvent): SpanJSON {
98
const { trace_id, parent_span_id, span_id, status, origin, data, op } = event.contexts?.trace ?? {};
109

11-
return dropUndefinedKeys({
10+
return {
1211
data: data ?? {},
1312
description: event.transaction,
1413
op,
@@ -23,14 +22,14 @@ export function convertTransactionEventToSpanJson(event: TransactionEvent): Span
2322
exclusive_time: data?.[SEMANTIC_ATTRIBUTE_EXCLUSIVE_TIME] as number | undefined,
2423
measurements: event.measurements,
2524
is_segment: true,
26-
});
25+
};
2726
}
2827

2928
/**
3029
* Converts a span JSON object to a transaction event.
3130
*/
3231
export function convertSpanJsonToTransactionEvent(span: SpanJSON): TransactionEvent {
33-
const event: TransactionEvent = {
32+
return {
3433
type: 'transaction',
3534
timestamp: span.timestamp,
3635
start_timestamp: span.start_timestamp,
@@ -52,6 +51,4 @@ export function convertSpanJsonToTransactionEvent(span: SpanJSON): TransactionEv
5251
},
5352
measurements: span.measurements,
5453
};
55-
56-
return dropUndefinedKeys(event);
5754
}

‎packages/nuxt/src/runtime/utils.ts

+8-17
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import type { ClientOptions, Context } from '@sentry/core';
2-
import { captureException, dropUndefinedKeys, getClient, getTraceMetaTags } from '@sentry/core';
2+
import { captureException, getClient, getTraceMetaTags } from '@sentry/core';
33
import type { VueOptions } from '@sentry/vue/src/types';
44
import type { CapturedErrorContext } from 'nitropack';
55
import type { NuxtRenderHTMLContext } from 'nuxt/app';
@@ -10,24 +10,15 @@ import type { ComponentPublicInstance } from 'vue';
1010
* and created a structured context object.
1111
*/
1212
export function extractErrorContext(errorContext: CapturedErrorContext): Context {
13-
const structuredContext: Context = {
14-
method: undefined,
15-
path: undefined,
16-
tags: undefined,
17-
};
18-
19-
if (errorContext) {
20-
if (errorContext.event) {
21-
structuredContext.method = errorContext.event._method || undefined;
22-
structuredContext.path = errorContext.event._path || undefined;
23-
}
24-
25-
if (Array.isArray(errorContext.tags)) {
26-
structuredContext.tags = errorContext.tags || undefined;
27-
}
13+
if (!errorContext.event) {
14+
return {};
2815
}
2916

30-
return dropUndefinedKeys(structuredContext);
17+
return {
18+
method: errorContext.event._method,
19+
path: errorContext.event._path,
20+
tags: Array.isArray(errorContext.tags) ? errorContext.tags : undefined,
21+
};
3122
}
3223

3324
/**

‎packages/replay-internal/src/integration.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -356,7 +356,7 @@ function loadReplayOptionsFromClient(initialOptions: InitialReplayPluginOptions,
356356
const finalOptions: ReplayPluginOptions = {
357357
sessionSampleRate: 0,
358358
errorSampleRate: 0,
359-
...dropUndefinedKeys(initialOptions),
359+
...initialOptions,
360360
};
361361

362362
const replaysSessionSampleRate = parseSampleRate(opt.replaysSessionSampleRate);

‎packages/sveltekit/src/vite/sentryVitePlugins.ts

+1-2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
import { dropUndefinedKeys } from '@sentry/core';
21
import type { Plugin } from 'vite';
32
import type { AutoInstrumentSelection } from './autoInstrument';
43
import { makeAutoInstrumentationPlugin } from './autoInstrument';
@@ -104,5 +103,5 @@ export function generateVitePluginOptions(
104103
}
105104
}
106105

107-
return dropUndefinedKeys(sentryVitePluginsOptions);
106+
return sentryVitePluginsOptions;
108107
}

0 commit comments

Comments
 (0)
Please sign in to comment.