From 44f5bbbfc696318fb6fd13cc869c4e5887615af1 Mon Sep 17 00:00:00 2001 From: Krystof Woldrich Date: Mon, 20 Jan 2025 15:08:56 +0100 Subject: [PATCH 1/4] chore(browser): Export ipAddress helpers for use in other SDKs --- packages/browser/src/client.ts | 32 +++-------------------- packages/browser/src/exports.ts | 1 + packages/browser/src/utils/ipAddress.ts | 34 +++++++++++++++++++++++++ 3 files changed, 38 insertions(+), 29 deletions(-) create mode 100644 packages/browser/src/utils/ipAddress.ts diff --git a/packages/browser/src/client.ts b/packages/browser/src/client.ts index 801a1bffbd2e..4a8acba5995d 100644 --- a/packages/browser/src/client.ts +++ b/packages/browser/src/client.ts @@ -8,12 +8,12 @@ import type { ParameterizedString, Scope, SeverityLevel, - User, } from '@sentry/core'; import { Client, applySdkMetadata, getSDKSource } from '@sentry/core'; import { eventFromException, eventFromMessage } from './eventbuilder'; import { WINDOW } from './helpers'; import type { BrowserTransportOptions } from './transports/types'; +import { addAutoIpAddressToSession, addAutoIpAddressToUser } from './utils/ipAddress'; /** * Configuration options for the Sentry Browser SDK. @@ -84,22 +84,8 @@ export class BrowserClient extends Client { }); } - this.on('postprocessEvent', event => { - addAutoIpAddressToUser(event); - }); - - this.on('beforeSendSession', session => { - if ('aggregates' in session) { - if (session.attrs?.['ip_address'] === undefined) { - session.attrs = { - ...session.attrs, - ip_address: '{{auto}}', - }; - } - } else { - addAutoIpAddressToUser(session); - } - }); + this.on('postprocessEvent', addAutoIpAddressToUser); + this.on('beforeSendSession', addAutoIpAddressToSession); } /** @@ -134,15 +120,3 @@ export class BrowserClient extends Client { return super._prepareEvent(event, hint, currentScope, isolationScope); } } - -// By default, we want to infer the IP address, unless this is explicitly set to `null` -// We do this after all other processing is done -// If `ip_address` is explicitly set to `null` or a value, we leave it as is -function addAutoIpAddressToUser(objWithMaybeUser: { user?: User | null }): void { - if (objWithMaybeUser.user?.ip_address === undefined) { - objWithMaybeUser.user = { - ...objWithMaybeUser.user, - ip_address: '{{auto}}', - }; - } -} diff --git a/packages/browser/src/exports.ts b/packages/browser/src/exports.ts index 289643a6c6c0..b12b438d14da 100644 --- a/packages/browser/src/exports.ts +++ b/packages/browser/src/exports.ts @@ -99,3 +99,4 @@ export { linkedErrorsIntegration } from './integrations/linkederrors'; export { browserApiErrorsIntegration } from './integrations/browserapierrors'; export { lazyLoadIntegration } from './utils/lazyLoadIntegration'; +export { addAutoIpAddressToSession, addAutoIpAddressToUser } from './utils/ipAddress'; diff --git a/packages/browser/src/utils/ipAddress.ts b/packages/browser/src/utils/ipAddress.ts new file mode 100644 index 000000000000..0f48d8de82dd --- /dev/null +++ b/packages/browser/src/utils/ipAddress.ts @@ -0,0 +1,34 @@ +import type { Session, SessionAggregates, User } from '@sentry/core'; + +/** + * @internal + * By default, we want to infer the IP address, unless this is explicitly set to `null` + * We do this after all other processing is done + * If `ip_address` is explicitly set to `null` or a value, we leave it as is + */ +export function addAutoIpAddressToUser(objWithMaybeUser: { user?: User | null }): void { + if (objWithMaybeUser.user?.ip_address === undefined) { + objWithMaybeUser.user = { + ...objWithMaybeUser.user, + ip_address: '{{auto}}', + }; + } +} + +/** + * @internal + * Adds the `ip_address` attribute to the session if it is not explicitly set to `null` or a value. + * @param session The session to add the `ip_address` attribute to. + */ +export function addAutoIpAddressToSession(session: Session | SessionAggregates): void { + if ('aggregates' in session) { + if (session.attrs?.['ip_address'] === undefined) { + session.attrs = { + ...session.attrs, + ip_address: '{{auto}}', + }; + } + } else { + addAutoIpAddressToUser(session); + } +} From 3115cc42eaa01aac1fd153880858cb5fb31a7c7a Mon Sep 17 00:00:00 2001 From: Krystof Woldrich Date: Tue, 21 Jan 2025 15:52:48 +0100 Subject: [PATCH 2/4] change jsdoc --- packages/browser/src/utils/ipAddress.ts | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/packages/browser/src/utils/ipAddress.ts b/packages/browser/src/utils/ipAddress.ts index 0f48d8de82dd..25dc929c2cc4 100644 --- a/packages/browser/src/utils/ipAddress.ts +++ b/packages/browser/src/utils/ipAddress.ts @@ -1,10 +1,11 @@ import type { Session, SessionAggregates, User } from '@sentry/core'; +// By default, we want to infer the IP address, unless this is explicitly set to `null` +// We do this after all other processing is done +// If `ip_address` is explicitly set to `null` or a value, we leave it as is + /** * @internal - * By default, we want to infer the IP address, unless this is explicitly set to `null` - * We do this after all other processing is done - * If `ip_address` is explicitly set to `null` or a value, we leave it as is */ export function addAutoIpAddressToUser(objWithMaybeUser: { user?: User | null }): void { if (objWithMaybeUser.user?.ip_address === undefined) { @@ -17,8 +18,6 @@ export function addAutoIpAddressToUser(objWithMaybeUser: { user?: User | null }) /** * @internal - * Adds the `ip_address` attribute to the session if it is not explicitly set to `null` or a value. - * @param session The session to add the `ip_address` attribute to. */ export function addAutoIpAddressToSession(session: Session | SessionAggregates): void { if ('aggregates' in session) { From a3e44f222caef518ea1f006dc75c1a0c98b2617e Mon Sep 17 00:00:00 2001 From: Krystof Woldrich Date: Tue, 21 Jan 2025 16:02:54 +0100 Subject: [PATCH 3/4] move helpers to core --- packages/browser/src/client.ts | 9 +++++++-- packages/browser/src/exports.ts | 1 - packages/core/src/index.ts | 1 + packages/{browser => core}/src/utils/ipAddress.ts | 2 +- 4 files changed, 9 insertions(+), 4 deletions(-) rename packages/{browser => core}/src/utils/ipAddress.ts (92%) diff --git a/packages/browser/src/client.ts b/packages/browser/src/client.ts index 4a8acba5995d..077282ebd915 100644 --- a/packages/browser/src/client.ts +++ b/packages/browser/src/client.ts @@ -9,11 +9,16 @@ import type { Scope, SeverityLevel, } from '@sentry/core'; -import { Client, applySdkMetadata, getSDKSource } from '@sentry/core'; +import { + Client, + addAutoIpAddressToSession, + addAutoIpAddressToUser, + applySdkMetadata, + getSDKSource, +} from '@sentry/core'; import { eventFromException, eventFromMessage } from './eventbuilder'; import { WINDOW } from './helpers'; import type { BrowserTransportOptions } from './transports/types'; -import { addAutoIpAddressToSession, addAutoIpAddressToUser } from './utils/ipAddress'; /** * Configuration options for the Sentry Browser SDK. diff --git a/packages/browser/src/exports.ts b/packages/browser/src/exports.ts index b12b438d14da..289643a6c6c0 100644 --- a/packages/browser/src/exports.ts +++ b/packages/browser/src/exports.ts @@ -99,4 +99,3 @@ export { linkedErrorsIntegration } from './integrations/linkederrors'; export { browserApiErrorsIntegration } from './integrations/browserapierrors'; export { lazyLoadIntegration } from './utils/lazyLoadIntegration'; -export { addAutoIpAddressToSession, addAutoIpAddressToUser } from './utils/ipAddress'; diff --git a/packages/core/src/index.ts b/packages/core/src/index.ts index 6349e342ff1d..35c08c147975 100644 --- a/packages/core/src/index.ts +++ b/packages/core/src/index.ts @@ -72,6 +72,7 @@ export { hasTracingEnabled } from './utils/hasTracingEnabled'; export { isSentryRequestUrl } from './utils/isSentryRequestUrl'; export { handleCallbackErrors } from './utils/handleCallbackErrors'; export { parameterize } from './utils/parameterize'; +export { addAutoIpAddressToSession, addAutoIpAddressToUser } from './utils/ipAddress'; export { spanToTraceHeader, spanToJSON, diff --git a/packages/browser/src/utils/ipAddress.ts b/packages/core/src/utils/ipAddress.ts similarity index 92% rename from packages/browser/src/utils/ipAddress.ts rename to packages/core/src/utils/ipAddress.ts index 25dc929c2cc4..23f8adbb6746 100644 --- a/packages/browser/src/utils/ipAddress.ts +++ b/packages/core/src/utils/ipAddress.ts @@ -1,4 +1,4 @@ -import type { Session, SessionAggregates, User } from '@sentry/core'; +import type { Session, SessionAggregates, User } from '../types-hoist'; // By default, we want to infer the IP address, unless this is explicitly set to `null` // We do this after all other processing is done From 01c465c1f9c14ab6908fdfd119fb3f1337bc5506 Mon Sep 17 00:00:00 2001 From: Krystof Woldrich <31292499+krystofwoldrich@users.noreply.github.com> Date: Mon, 3 Feb 2025 18:54:00 +0100 Subject: [PATCH 4/4] Update ipAddress.ts --- packages/core/src/utils/ipAddress.ts | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/packages/core/src/utils/ipAddress.ts b/packages/core/src/utils/ipAddress.ts index 23f8adbb6746..8ca389dc68a1 100644 --- a/packages/core/src/utils/ipAddress.ts +++ b/packages/core/src/utils/ipAddress.ts @@ -28,6 +28,8 @@ export function addAutoIpAddressToSession(session: Session | SessionAggregates): }; } } else { - addAutoIpAddressToUser(session); + if (session.ipAddress === undefined) { + session.ipAddress = '{{auto}}'; + } } }